使用py-faster-rcnn進行目標檢測(object detect)
背景
目標檢測(object detect)是深度學習在圖像領域的初級應用,更高階的應用如圖像分割、場景分析等。目標檢測領域現在主流的框架有Faster R-CNN、SSD、Yolo等,本文Gemfield將介紹Faster R-CNN的工程實施。Faster R-CNN幾乎可以做到實時的目標檢測。
官方的Faster R-CNN 代碼是使用matlab實現的,不利於工程部署;Gemfield將選擇一個流行的Faster R-CNN的python實現:rbgirshick/py-faster-rcnn。python實現比matlab實現在性能上大約慢了10%。
禮物
1,Gemfield將py-faster-rcnn用python3重新實現了一遍:CivilNet/py-faster-rcnn
2,Gemfield製作了python3版本的py-faster-rcnn的docker image,你可以使用下面的命令來直接使用:
nvidia-docker run -it --rm -v YOUR_VOC_DATA:/opt/gemfield/py-faster-rcnn/data/VOCdevkit2007 -v YOUR_IMAGENET_MODEL:/opt/gemfield/py-faster-rcnn/data/imagenet_models gemfield/py-faster-rcnn bash
準備好py-faster-rcnn
1,克隆Faster R-CNN 倉庫:
gemfield@ThinkPad-X1C:~/github/new$ git clone --recursive https://github.com/rbgirshick/py-faster-rcnn正克隆到 py-faster-rcnn...remote: Counting objects: 1541, done.remote: Total 1541 (delta 0), reused 0 (delta 0), pack-reused 1541接收對象中: 100% (1541/1541), 1.36 MiB | 499.00 KiB/s, 完成.處理 delta 中: 100% (901/901), 完成.子模組 caffe-fast-rcnn(https://github.com/rbgirshick/caffe-fast-rcnn.git)未對路徑 caffe-fast-rcnn 註冊正克隆到 /home/gemfield/github/new/py-faster-rcnn/caffe-fast-rcnn...remote: Counting objects: 23976, done. remote: Compressing objects: 100% (13/13), done. remote: Total 23976 (delta 29), reused 25 (delta 25), pack-reused 23938 接收對象中: 100% (23976/23976), 31.51 MiB | 135.00 KiB/s, 完成.處理 delta 中: 100% (15779/15779), 完成.子模組路徑 caffe-fast-rcnn:檢出 0dcd397b29507b8314e252e850518c5695efbb83
2,更新caffe版本
此倉庫的代碼使用的 Caffe 版本比較舊,不支持 cudnn的新版本等,比如編譯的時候會報如下介面錯誤:
./include/caffe/util/cudnn.hpp:108:70: error: too few arguments to function cudnnStatus_t cudnnSetConvolution2dDescriptor(cudnnConvolutionDescriptor_t, int, int, int, int, int, int, cudnnConvolutionMode_t, cudnnDataType_t) pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION));
所以需要update一下caffe的版本。切換到caffe-fast-rcnn子目錄,設置remote url,fetch 然後使用theirs策略進行merge(也就是遇到merge衝突的時候全部使用上游caffe的代碼):
注意下列命令執行時所在的目錄!!!
gemfield@ThinkPad-X1C:~/github/new$ cd py-faster-rcnn/caffe-fast-rcnn/gemfield@ThinkPad-X1C:~/github/new/py-faster-rcnn/caffe-fast-rcnn$ git remote add caffe https://github.com/BVLC/caffe.git gemfield@ThinkPad-X1C:~/github/new/py-faster-rcnn/caffe-fast-rcnn$ git config --listuser.email=gemfield@civilnet.cnuser.name=Gemfieldcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=truecore.worktree=../../../caffe-fast-rcnnremote.origin.url=https://github.com/rbgirshick/caffe-fast-rcnn.gitremote.origin.fetch=+refs/heads/*:refs/remotes/origin/*branch.master.remote=originbranch.master.merge=refs/heads/masterremote.caffe.url=https://github.com/BVLC/caffe.gitremote.caffe.fetch=+refs/heads/*:refs/remotes/caffe/*
fetch:
gemfield@ThinkPad-X1C:~/github/new/py-faster-rcnn/caffe-fast-rcnn$ git fetch cafferemote: Counting objects: 28928, done.remote: Compressing objects: 100% (3/3), done.remote: Total 28928 (delta 6190), reused 6189 (delta 6189), pack-reused 22736接收對象中: 100% (28928/28928), 29.77 MiB | 1.02 MiB/s, 完成.處理 delta 中: 100% (19892/19892), 完成 747 個本地對象.來自 https://github.com/BVLC/caffe * [新分支] gh-pages -> caffe/gh-pages * [新分支] intel -> caffe/intel * [新分支] master -> caffe/master * [新分支] opencl -> caffe/opencl * [新分支] readme_list_branches -> caffe/readme_list_branches * [新分支] tutorial -> caffe/tutorial * [新分支] windows -> caffe/windows * [新標籤] acm-mm-oss -> acm-mm-oss * [新標籤] bsd -> bsd * [新標籤] rc -> rc * [新標籤] rc2 -> rc2 * [新標籤] rc3 -> rc3 * [新標籤] rcnn-release -> rcnn-release * [新標籤] v0.1 -> v0.1 * [新標籤] v0.9 -> v0.9 * [新標籤] v0.99 -> v0.99 * [新標籤] v0.999 -> v0.999 * [新標籤] v0.9999 -> v0.9999 * [新標籤] 1.0 -> 1.0 * [新標籤] rc4 -> rc4 * [新標籤] rc5 -> rc5
merge,注意使用的是theirs合併策略(衝突的時候扔掉自己的,使用caffe上游的):
gemfield@ThinkPad-X1C:~/github/new/py-faster-rcnn/caffe-fast-rcnn$ git merge -X theirs caffe/master自動合併 src/caffe/proto/caffe.proto自動合併 src/caffe/layers/dropout_layer.cpp刪除 src/caffe/data_reader.cpp刪除 scripts/travis/travis_setup_makefile_config.sh刪除 scripts/travis/travis_install.sh刪除 scripts/travis/travis_build_and_test.sh自動合併 python/caffe/_caffe.cpp自動合併 python/caffe/__init__.py自動合併 include/caffe/layers/python_layer.hpp自動合併 include/caffe/layer.hpp刪除 include/caffe/data_reader.hpp刪除 examples/hdf5_classification/solver.prototxt刪除 examples/hdf5_classification/nonlinear_solver.prototxt刪除 examples/03-fine-tuning.ipynb刪除 examples/02-brewing-logreg.ipynb刪除 docs/performance_hardware.md自動合併 LICENSEMerge made by the recursive strategy. .github/ISSUE_TEMPLATE.md | 19 + .gitignore | 4 + .travis.yml | 58 +- CMakeLists.txt | 44 +-......
Fix錯誤
1,修改文件include/caffe/layers/python_layer.hpp,注釋掉文件中的
//self_.attr(「phase」) = static_cast(this->phase_)
2,AttributeError: module google.protobuf has no attribute text_format,在
py-faster-rcnn/lib/fast_rcnn/train.py文件上面加上下面這句:
import google.protobuf.text_format
下面假定項目所在的路徑為 $FRCN_ROOT:
重新實現為Python3(可選)
1,替換python2中的xrange為python3中的range:
find . -name "*.py" -exec sed -i s/xrange/range/g {} +#注意,下面這條命令會多替換掉six模塊的items調用find . -name "*.py" -exec sed -i s/iteritems/items/g {} +#修復上面的命令多替換的six模塊的items調用find . -name "*.py" -exec sed -i "s/six.items/six.iteritems/g" {} +
2,替換python2中的print為python3的print():
#手工再看/tmp/a.py的格式是否正確,部分行需要手工改正,因為有的print是跨行的,有的print最右邊有注釋sed -e /print / s/$/)/g -e s/print /print(/g -i ./tools/train_faster_rcnn_alt_opt.py
3,替換python2中的has_key為python3中的in操作符(AttributeError: EasyDict object has no attribute has_key,Removed dict.has_key() – use the in operator instead):
root@gemfieldlib:/bigdata/gemfield/gemfieldlib/py-faster-rcnn# find . -name "*.py" -exec grep has_key -n {} +./tools/compress_net.py:79: if net_svd.params.has_key(fc6_L):./tools/compress_net.py:101: if net_svd.params.has_key(fc7_L):./lib/datasets/imdb.py:143: assert areas.has_key(area), unknown area range: {}.format(area)./lib/datasets/coco.py:79: if self._view_map.has_key(coco_name)./lib/datasets/factory.py:36: if not __sets.has_key(name):./lib/fast_rcnn/train.py:63: net.params.has_key(bbox_pred))./lib/fast_rcnn/config.py:234: if not b.has_key(k):./lib/fast_rcnn/config.py:273: assert d.has_key(subkey)./lib/fast_rcnn/config.py:276: assert d.has_key(subkey)
4,使用dict.items() 替換ict.iteritems():
find . -name "*.py" -exec sed -i s/iteritems/items/g {} +
5,更改python2中的cPickle為python3中的pickle:
find . -name "*.py" -exec sed -i s/cPickle/pickle/g {} +
6,設置全局python3環境:
rm -f /usr/bin/pythonln -s /usr/bin/python3 /usr/bin/python
安裝依賴
安裝依賴 python setuptools
apt install python3-setuptools
安裝依賴cython3
apt-get install cython3
安裝skimage(ImportError: No module named skimage):
pip3 install scikit-image
安裝google protobuf (ImportError: No module named google):
pip3 install protobuf
安裝easydict(ImportError: No module named easydict):
pip3 install easydict
安裝yaml ( ImportError: No module named yaml):
apt install python3-yaml
修改文件支持numpy版本 >= 1.12
解決numpy version為1.14(Gemfield所用版本,但是預計凡是版本大於1.11都會有這個兼容性問題)時 API變動帶來的兼容性的問題。
1,TypeError: float object cannot be interpreted as an integer(numpy1.12 版本以後,不再支持 1.0,2.0 這樣的浮點數作為索引):
#vi py-faster-rcnn/lib/rpn/proposal_target_layer.py +177bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_this_image), replace=False)
2,TypeError: slice indices must be integers or None or have an __index__ method:
#vi /opt/gemfield/py-faster-rcnn/lib/rpn/proposal_target_layer.py for ind in inds: ind = int(ind) cls = clss[ind] start = 4 * cls end = start + 4 bbox_targets[ind, int(start):int(end)] = bbox_target_data[ind, 1:] bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS return bbox_targets, bbox_inside_weights
3,TypeError: numpy.float64 object cannot be interpreted as an integer:
$ vi lib/roi_data_layer/minibatch.py +26fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION *rois_per_image).astype(np.int)$ vi lib/datasets/ds_utils.py +12hashes = np.round(boxes * scale).dot(v).astype(np.int)$ vi lib/fast_rcnn/test.py line +129hashes = np.round(blobs[rois] * cfg.DEDUP_BOXES).dot(v).astype(np.int)$ vi lib/rpn/proposal_target_layer.py +60fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)
使用RPN
sed -i "s/HAS_RPN = True/HAS_RPN = False/g" $FRCN_ROOT/lib/fast_rcnn/config.py
編譯
編譯$FRCN_ROOT/lib
root@gemfieldlib:/bigdata/gemfield/gemfieldlib/py-faster-rcnn/lib# makepython setup.py build_ext --inplacerunning build_extcythoning utils/bbox.pyx to utils/bbox.cbuilding utils.cython_bbox extensioncreating buildcreating build/temp.linux-x86_64-3.5creating build/temp.linux-x86_64-3.5/utilsx86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/lib/python3/dist-packages/numpy/core/include -I/usr/include/python3.5m -c utils/bbox.c -o build/temp.linux-x86_64-3.5/utils/bbox.o -Wno-cpp -Wno-unused-functionx86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/utils/bbox.o -o /bigdata/gemfield/gemfieldlib/py-faster-rcnn/lib/utils/cython_bbox.cpython-35m-x86_64-linux-gnu.socythoning nms/cpu_nms.pyx to nms/cpu_nms.cbuilding nms.cpu_nms extensioncreating build/temp.linux-x86_64-3.5/nmsx86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/lib/python3/dist-packages/numpy/core/include -I/usr/include/python3.5m -c nms/cpu_nms.c -o build/temp.linux-x86_64-3.5/nms/cpu_nms.o -Wno-cpp -Wno-unused-functionx86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/nms/cpu_nms.o -o /bigdata/gemfield/gemfieldlib/py-faster-rcnn/lib/nms/cpu_nms.cpython-35m-x86_64-linux-gnu.socythoning nms/gpu_nms.pyx to nms/gpu_nms.cppbuilding nms.gpu_nms extension/usr/local/cuda/bin/nvcc -I/usr/lib/python3/dist-packages/numpy/core/include -I/usr/local/cuda/include -I/usr/include/python3.5m -c nms/nms_kernel.cu -o build/temp.linux-x86_64-3.5/nms/nms_kernel.o -arch=sm_35 --ptxas-options=-v -c --compiler-options -fPICptxas info : 0 bytes gmemptxas info : Compiling entry function _Z10nms_kernelifPKfPy for sm_35ptxas info : Function properties for _Z10nms_kernelifPKfPy 0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loadsptxas info : Used 25 registers, 1280 bytes smem, 344 bytes cmem[0], 8 bytes cmem[2]x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/lib/python3/dist-packages/numpy/core/include -I/usr/local/cuda/include -I/usr/include/python3.5m -c nms/gpu_nms.cpp -o build/temp.linux-x86_64-3.5/nms/gpu_nms.o -Wno-unused-functioncc1plus: warning: command line option -Wstrict-prototypes is valid for C/ObjC but not for C++In file included from /usr/lib/python3/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1777:0, from /usr/lib/python3/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18, from /usr/lib/python3/dist-packages/numpy/core/include/numpy/arrayobject.h:4, from nms/gpu_nms.cpp:539:/usr/lib/python3/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp] #warning "Using deprecated NumPy API, disable it by " ^x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/nms/nms_kernel.o build/temp.linux-x86_64-3.5/nms/gpu_nms.o -L/usr/local/cuda/lib64 -Wl,--enable-new-dtags,-R/usr/local/cuda/lib64 -lcudart -o /bigdata/gemfield/gemfieldlib/py-faster-rcnn/lib/nms/gpu_nms.cpython-35m-x86_64-linux-gnu.socythoning pycocotools/_mask.pyx to pycocotools/_mask.cbuilding pycocotools._mask extensioncreating build/temp.linux-x86_64-3.5/pycocotoolsx86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/lib/python3/dist-packages/numpy/core/include -Ipycocotools -I/usr/include/python3.5m -c pycocotools/maskApi.c -o build/temp.linux-x86_64-3.5/pycocotools/maskApi.o -Wno-cpp -Wno-unused-function -std=c99x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/lib/python3/dist-packages/numpy/core/include -Ipycocotools -I/usr/include/python3.5m -c pycocotools/_mask.c -o build/temp.linux-x86_64-3.5/pycocotools/_mask.o -Wno-cpp -Wno-unused-function -std=c99x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/pycocotools/maskApi.o build/temp.linux-x86_64-3.5/pycocotools/_mask.o -o /bigdata/gemfield/gemfieldlib/py-faster-rcnn/lib/pycocotools/_mask.cpython-35m-x86_64-linux-gnu.sorm -rf build
修改Caffe的Makefile.config
cd $FRCN_ROOT/caffe-fast-rcnncp Makefile.config.example Makefile.config
修改Makefile.config
# cuDNN acceleration switch (uncomment to build with cuDNN).USE_CUDNN := 1# Uncomment if youre using OpenCV 3OPENCV_VERSION := 3#把python2.7注釋掉# We need to be able to find Python.h and numpy/arrayobject.h.#PYTHON_INCLUDE := /usr/include/python2.7 /usr/lib/python2.7/dist-packages/numpy/core/include#把python3的注釋去掉# Uncomment to use Python 3 (default is Python 2)PYTHON_LIBRARIES := boost_python3 python3.5mPYTHON_INCLUDE := /usr/include/python3.5m /usr/lib/python3.5/dist-packages/numpy/core/include#編譯python介面# Uncomment to support layers written in Python (will link against Python libs)WITH_PYTHON_LAYER := 1#添加hdf5,否則編譯報錯:./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: No such file or directory# Whatever else you find you need goes here.INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /bigdata/gemfield/gemfieldlib/cv2/include/LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial /bigdata/gemfield/gemfieldlib/cv2/lib/
添加如下鏈接文件來解決/usr/bin/ld: cannot find -lboost_python3的問題:
ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
編譯Caffe
root@gemfieldlib:/bigdata/gemfield/gemfieldlib/py-faster-rcnn/caffe-fast-rcnn# makePROTOC src/caffe/proto/caffe.protoCXX .build_release/src/caffe/proto/caffe.pb.ccCXX src/caffe/util/math_functions.cppCXX src/caffe/util/upgrade_proto.cppCXX src/caffe/util/io.cppCXX src/caffe/util/im2col.cppCXX src/caffe/util/hdf5.cpp......
編譯caffe的python介面:
root@gemfieldlib:/bigdata/gemfield/gemfieldlib/py-faster-rcnn/caffe-fast-rcnn# make pycaffeCXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpptouch python/caffe/proto/__init__.pyPROTOC (python) src/caffe/proto/caffe.proto
下載數據集
mkdir -p $FRCN_ROOT/data/gemfieldcd $FRCN_ROOT/data/gemfieldwget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tarwget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tarwget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tarfor f in $(ls *.tar);do tar xvf $f;donecd ../ln -s gemfield/VOCdevkit VOCdevkit2007
開始訓練
cd $FRCN_ROOTexperiments/scripts/faster_rcnn_end2end.sh 1 VGG16 pascal_voc
訓練自己的項目
製作數據集
請參考PASCAL VOC數據集的標註格式
自定義py-faster-rcnn
在py-faster-rcnn的基礎上修改一些代碼。
1,$FRCN_ROOT/lib/datasets/pascal_voc.py
將其中的self._classes 的值改為自己的分類。
self._classes = (__background__, # always index 0#後面將VOC2007的20個分類改為自己數據集的分類。
注意:第一個分類總是background,這個不要變。
注意2:self._classes後面的初始化的值是支持中文的,在代碼文件的前面添加 #-*- coding:utf-8 -*- 即可。
2,$FRCN_ROOT/models/pascal_voc/VGG16/faster_rcnn_end2end/train.prototxt
需要改動分類的數量:
root@gemfield:/opt/gemfield/py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_end2end# diff -u train.prototxt.bk train.prototxt--- train.prototxt.bk 2018-02-11 09:59:33.803151087 +0000+++ train.prototxt 2018-02-11 10:02:27.943159142 +0000@@ -8,7 +8,7 @@ python_param { module: roi_data_layer.layer layer: RoIDataLayer- param_str: "num_classes: 21"+ param_str: "num_classes: 225" } } @@ -527,7 +527,7 @@ python_param { module: rpn.proposal_target_layer layer: ProposalTargetLayer- param_str: "num_classes: 21"+ param_str: "num_classes: 225" } } @@ -617,7 +617,7 @@ lr_mult: 2 } inner_product_param {- num_output: 21+ num_output: 225 weight_filler { type: "gaussian" std: 0.01@@ -640,7 +640,7 @@ lr_mult: 2 } inner_product_param {- num_output: 84+ num_output: 900 weight_filler { type: "gaussian" std: 0.001
3,$FRCN_ROOT/models/pascal_voc/VGG16/faster_rcnn_end2end/test.prototxt
改動的就是分類的數量:
root@gemfield:/opt/gemfield/py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_end2end# diff -u test.prototxt.bk test.prototxt--- test.prototxt.bk 2018-02-11 09:59:38.179151289 +0000+++ test.prototxt 2018-02-11 10:03:55.339163185 +0000@@ -564,7 +564,7 @@ decay_mult: 0 } inner_product_param {- num_output: 21+ num_output: 225 weight_filler { type: "gaussian" std: 0.01@@ -589,7 +589,7 @@ decay_mult: 0 } inner_product_param {- num_output: 84+ num_output: 900 weight_filler { type: "gaussian" std: 0.001
4,$FRCN_ROOT/models/pascal_voc/VGG16/faster_rcnn_end2end/solver.prototxt
更改了下model的prefix
root@gemfield:/opt/gemfield/py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_end2end# diff -u solver.prototxt.bk solver.prototxt--- solver.prototxt.bk 2018-02-11 09:59:42.403151485 +0000+++ solver.prototxt 2018-02-11 10:11:57.275185479 +0000@@ -12,5 +12,5 @@ # function snapshot: 0 # We still use the snapshot prefix, though-snapshot_prefix: "vgg16_faster_rcnn"+snapshot_prefix: "/home/lhr/lhr/skin/model/vgg16_faster_rcnn_225cls_softmax/gemfield_py-faster-rcnn_225cls" iter_size: 2
5,建立鏈接文件指向自己的數據集
否則訓練一開始就會報錯說找不到trainval.txt: "AssertionError: Path does not exist: /opt/gemfield/py-faster-rcnn/data/VOCdevkit2007/VOC2007/ImageSets/Main/trainval.txt"
root@gemfield:/opt/gemfield/py-faster-rcnn/data# mkdir VOCdevkit2007root@gemfield:/opt/gemfield/py-faster-rcnn/data# cd VOCdevkit2007root@gemfield:/opt/gemfield/py-faster-rcnn/data/VOCdevkit2007# ln -s /bigdata/VOC2018/ VOC2007
6,更改最大迭代次數
root@gemfield:/opt/gemfield/py-faster-rcnn/experiments/scripts# diff -u faster_rcnn_end2end.sh.bk faster_rcnn_end2end.sh--- faster_rcnn_end2end.sh.bk 2018-02-12 02:05:36.097832318 +0000+++ faster_rcnn_end2end.sh 2018-02-12 02:05:58.661833362 +0000@@ -27,7 +27,7 @@ TRAIN_IMDB="voc_2007_trainval" TEST_IMDB="voc_2007_test" PT_DIR="pascal_voc"- ITERS=70000+ ITERS=1000000 ;; coco) # This is a very long and slow training schedule
分析視頻
1,初始化模型
def init(): global net net_files = [faster_rcnn_model, faster_rcnn_prototxt] for f in net_files: if not os.path.isfile(f): raise IOError(({:s} not found.
).format(f)) caffe.set_mode_gpu() caffe.set_device(GPU) cfg.GPU_ID = GPU cfg.TEST.HAS_RPN = True # Use RPN for proposals net = caffe.Net(faster_rcnn_prototxt, faster_rcnn_model, caffe.TEST)
2,讀視頻抓幀
def forward(video_path, step_size): video_file = video_path video_path = VIDEO_DIR + video_path if not os.path.isfile(video_path): raise IOError(({} not found.
).format(video_path)) video_cap = cv2.VideoCapture(video_path) frame_cnt = int(video_cap.get(cv2.CAP_PROP_FRAME_COUNT)) error_frame = max(20, frame_cnt / 50) for i, frame_index in enumerate(range(0, frame_cnt, int(step_size))): video_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index) if error_frame == 0: video_cap.release() print(Bad video file, too much error frames) break status, frame = video_cap.read() if not status: error_frame -= 1 print([warning] Fetch frame {} failed..format(frame_index)) continue processFrame(frame, frame_index)
3,使用py-faster-rcnn的API
參考tools/demo.py。
一些錯誤
1,如果experiments/scripts/faster_rcnn_end2end.sh 1 VGG16 pascal_voc 報錯:
KeyError: max_overlaps
Traceback (most recent call last): File "./tools/train_net.py", line 112, in <module> max_iters=args.max_iters) File "/opt/gemfield/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 155, in train_net roidb = filter_roidb(roidb) File "/opt/gemfield/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 145, in filter_roidb filtered_roidb = [entry for entry in roidb if is_valid(entry)] File "/opt/gemfield/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 145, in <listcomp> filtered_roidb = [entry for entry in roidb if is_valid(entry)] File "/opt/gemfield/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 134, in is_valid overlaps = entry[max_overlaps]KeyError: max_overlaps
那麼刪掉cache即可,重新運行腳本即可:
root@gemfield:/opt/gemfield/py-faster-rcnn/data# rm -f cache/*
推薦閱讀:
※【Neural Networks and Deep Learning】4.神經網路可以計算任何函數的可視化證明(1)
※Inception-v2/v3結構解析(原創)
※【Neural Networks and Deep Learning】3.改進神經網路的學習方法(過擬合)
TAG:深度學習DeepLearning | Caffe深度學習框架 | 神經網路 |