faster-rcnn訓練和測試自己的資料(VGG/ResNet)以及遇到的問題
http://www.cnblogs.com/caffeaoto/p/6536482.html
主要參照這個教程改的
需要準備的檔案:Annotation檔案,圖片,用來訓練的圖片名稱list.txt
訓練:
需要改的檔案:
lib/datasets/下的pascal_voc.py,factory.py
data/faster_rcnn_models/下存放預訓練好的模型,需要什麼樣的網路就到caffe model zoo下載相應的模型,我用了VGG_CNN_M_1024和resnet101
models/pascal_voc/下存放相應的模型配置檔案,開啟相應的train.prototxt
1、data層的num_classes: n (自己要訓練的類別+1,1為背景)
2、roi_data層的num_classes:n
3、cls_score層的num_output:n
4、bbox_preda層的num_output : 4*n
啟動網路:
在faster根目錄下建立一個train_xx.sh指令碼,輸入以下內容,
python ./tools/train_net.py --gpu 1 --solver models/hs/faster_rcnn_end2end/solver.prototxt --weights data/imagenet_models/VGG_CNN_M_1024.v2.caffemodel --imdb hs --iters 80000 --cfg experiments/cfgs/faster_rcnn_end2end.yml
指定引數:
train_xx.sh、faster_rcnn_end2end.yml、train_net.py、solver.prototxt都可以指定相關的引數,如果沒有特別要求,後三個檔案可以不用改
train_xx.sh:
train_net.py是網路的訓練檔案,之後的引數都是附帶的輸入引數
--gpu 代表機器上的GPU編號,如果是nvidia系列的tesla顯示卡,可以在終端中輸入nvidia-smi來檢視當前的顯示卡負荷,選擇合適的顯示卡
--solver 代表模型的配置檔案,train.prototxt的檔案路徑已經包含在這個檔案之中
--weights 代表初始化的權重檔案,這裡用的是Imagenet上預訓練好的模型,中型的網路我們選擇用VGG_CNN_M_1024.v2.caffemodel
--imdb 這裡給出的訓練的資料庫名字需要在factory.py的_sets中,我在檔案裡面有_sets[‘hs’],train_net.py這個檔案會呼叫factory.py再生成hs這個類,來讀取資料
faster_rcnn_end2end.yml
可以根據自己的需要在這個檔案中新增配置,例如在訓練時進行模型快照的迭代次數,是否翻轉等,相應的引數可以參考py-faster-rcnn / lib / fast_rcnn / config.py。
不需要更改config.py,在faster_rcnn_end2end.yml中新增語句即可
solver.prototxt
進一步修改網路訓練中的學習速率,步長,gamma值,以及輸出模型的名字,也可以訓練到一定步數先儲存一次
接著在根目錄下執行train_xx.sh就可以訓練了
訓練中遇到的問題
1、
TypeError: coercing to Unicode: need string or buffer, NoneType foundTypeError: must be encoded string without NULL bytes, not strAssertionError: Path does not exist: /home/py-faster-rcnn/lib/datasts/Images/101.jpg明明圖片是存在的,但是就是顯示找不到檔案這是編碼問題造成的,list.txt要用預設的ANSI編碼,如果特意改容易出錯
2、
File "/py-faster-rcnn/tools/../lib/datasets/imdb.py", line 108, in append_flipped_images
assert (boxes[:, 2] >= boxes[:, 0]).all()
AssertionError
出現這個問題說明圖片標註的時候存在錯誤
修改lib/datasets/imdb.py,append_flipped_images()函式
資料整理,在一行程式碼為 boxes[:, 2] = widths[i] - oldx1 - 1下加入程式碼:
for b in range(len(boxes)):
if boxes[b][2]< boxes[b][0]:
boxes[b][0] = 0
3、
AttributeError: 'module' object has no attribute ‘text_format'解決方法:在/home/xxx/py-faster-rcnn/lib/fast_rcnn/train.py的標頭檔案匯入部分加上 :import google.protobuf.text_format4、
TypeError: slice indices must be integers or None or have an __index__ method
numpy v1.12.0不支援浮點數
lib/proposal_target_layer.py
將以下行新增到第126 行之後,
start=int(start)
end=int(end)
在166行之後加上,
fg_rois_per_this_image=int(fg_rois_per_this_image)
5、
TypeError: 'numpy.float64' object cannot be interpreted as an index還是numpy版本的問題,直接換一個版本好了sudo pip install -U numpy==1.11.06、
File "/home/data1/caffe/py-faster-rcnn/tools/../lib/roi_data_layer/minibatch.py", line 139, in _get_image_blobim = im[:, ::-1, :]TypeError: 'NoneType' object has no attribute '__getitem__'File "/home/data1/caffe/py-faster-rcnn/tools/../lib/utils/blob.py", line 33, in prep_im_for_blobim = im.astype(np.float32, copy=False)AttributeError: 'NoneType' object has no attribute 'astype'其實這兩個都是圖片讀取有問題,第一個可以通過修改minibatch.py, line 139if roidb[i]['flipped']:if im is None:im = []else:im = im[:, ::-1, :]
但這不能根本解決問題在保證自己opencv沒問題的情況下,將讀取的圖片資訊print一下,找出出問題的圖片,刪掉就好了測試:將訓練好的網路複製到data/faster_rcnn_models修改demo
CLASSES = ('__background__',
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
這部分修改為自己訓練的類
NETS = {'vgg16': ('VGG16',
'VGG16_faster_rcnn_final.caffemodel'),
'zf': ('ZF',
'ZF_faster_rcnn_final.caffemodel')}
如果用的不是預設的網路,需要新增上網路的相關資訊
'vgg16'是引數名,'VGG16',對應models/pascal_voc下相應的資料夾名,'VGG16_faster_rcnn_final.caffemodel'對應data/faster_rcnn_models下訓練好的模型
parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',
choices=NETS.keys(), default='vgg16')
可以將自己用的網路設定為預設
prototxt = os.path.join(cfg.MODELS_DIR, NETS[args.demo_net][0],
prototxt #'rfcn_alt_opt_5step_ohem', 'rfcn_test.pt')
caffemodel = os.path.join(cfg.DATA_DIR, 'faster_rcnn_models',
NETS[args.demo_net][1])
改為自己用的caffemodel和prototxt
im_names就設定為自己的圖片
執行demo.py
報錯:
F0628 21:11:11.389936 9218 net.cpp:829] Cannot copy param 0 weights from layer 'cls_score'; shape mismatch. Source param shape is 2 2048 (4096); target param shape is 21 2048 (43008). To learn this layer's parameters from scratch rather than copying from a saved net, rename the layer.
將prototxt對應檔案裡,21改為自己訓練的類別+1,84改為(自己訓練的類別+1)*4