在本博客中,我们首先介绍相关环境的建立,利用VOC数据训练Faster-Rcnn,和自己的数据训练faster-RCNN
1.环境和数据准备
环境:python2.7,tensorflow,cuda8(其他版本的Python,cuda9等也是可以的,但是需要做更多的修改)。
download codes:https://github.com/smallcorgi/Faster-RCNN_TF.git
git clone --recursive https://github.com/smallcorgi/Faster-RCNN_TF.git
download data
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
编译Cython模块
首先,编译Cython前,应对make.sh进行修改。
sudo gedit ./lib/make.sh
将make.sh文件修改为
TF_INC=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())')
TF_LIB=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_lib())')
CUDA_PATH=/usr/local/cuda/
CXXFLAGS=''
if [[ "$OSTYPE" =~ ^darwin ]]; then
CXXFLAGS+='-undefined dynamic_lookup'
fi
cd roi_pooling_layer
if [ -d "$CUDA_PATH" ]; then
nvcc -std=c++11 -c -o roi_pooling_op.cu.o roi_pooling_op_gpu.cu.cc \
-I $TF_INC -D GOOGLE_CUDA=1 -x cu -Xcompiler -fPIC $CXXFLAGS \
-arch=sm_37
g++ -std=c++11 -shared -o roi_pooling.so roi_pooling_op.cc -D_GLIBCXX_USE_CXX11_ABI=0 \
roi_pooling_op.cu.o -I $TF_INC -L $TF_LIB -ltensorflow_framework -D GOOGLE_CUDA=1 \
-fPIC $CXXFLAGS -lcudart -L $CUDA_PATH/lib64
else
g++ -std=c++11 -shared -o roi_pooling.so roi_pooling_op.cc \
-I $TF_INC -fPIC $CXXFLAGS
fi
cd ..
#cd feature_extrapolating_layer
#nvcc -std=c++11 -c -o feature_extrapolating_op.cu.o feature_extrapolating_op_gpu.cu.cc \
# -I $TF_INC -D GOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -arch=sm_50
#g++ -std=c++11 -shared -o feature_extrapolating.so feature_extrapolating_op.cc \
# feature_extrapolating_op.cu.o -I $TF_INC -fPIC -lcudart -L $CUDA_PATH/lib64
#cd ..
然后,编译
cd $FRCN_ROOT/lib
make
准备预训练文件和已训练好的权重文件,并放到。./data/pretrain_model/目录下
预训练权重文件(VGG_imagenet.npy):https://drive.google.com/open?id=0ByuDEGFYmWsbNVF5eExySUtMZmM
已训练好的权重文件(70000):https://drive.google.com/open?id=0ByuDEGFYmWsbZ0EzeUlHcGFIVWM
准备解压VOC数据到./data/下
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
建立软连接
cd ./data
ln -s $VOCdevkit VOCdevkit2007
准备自己的数据
2.自带demo测试和VOC训练
用demo.py测试
python ./tools/demo.py --model model_path
例如
python ./tools/demo.py --model ./data/pretrain_model/VGGnet_fast_rcnn_iter_70000.ckpt
采用VOC数据进行训练
./experiments/scripts/faster_rcnn_end2end.sh $DEVICE $DEVICE_ID VGG16 pascal_voc
例如
./experiments/scripts/faster_rcnn_end2end.sh gpu 0 VGG16 pascal_voc
3.自己的数据训练
首先,将自己的数据转换为VOC格式,voc格式为
---Annotations
---ImageSets
---JPEGImages
然后将Annotations、ImageSets、JPEGImages文件替换。。data/VOCdevkit/VOC2007中的Annotations、ImageSets、JPEGImages文件。
然后,修改./lib/datasets/pascal_voc.py中的类别
self._classes = ('__background__', # always index 0
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
对于我的实验,被修改为:
self._classes = ('__background__', # always index 0
'plane', 'ground', 'ship', 'vehicle',
'harbor', 'basketball', 'bridge', 'tennis',
'baseball')
再修改文件./libnetworks/VGGnet_test.py,./libnetworks/VGGnet_train.py类别数n_classes为“类别数”+1,对于我的实验,被修改为 :
n_classes = 10 #9+1 “1” is background
最后,删除./data/cache文件夹(因为如果该文件夹下数据存在,不会重新生成数据),然后重新训练模型。
./experiments/scripts/faster_rcnn_end2end.sh $DEVICE $DEVICE_ID VGG16 pascal_voc
常见错误:用自己的数据时,常出现的一个错误是
imdb.py
arrert (boxes[:, 2]>=boxes[:, 0]).all AssertionError
这个错误一般是因为自己的数据标注超出了图片的边界,可以通过语句print self.image_path_at(i),找出有问题的标注文件,修改后,重新开始训练(记住,开始训练前一定要删除cache文件)。如下所示。
for i in xrange(num_images):
print self.image_path_at(i)
boxes = self.roidb[i]['boxes'].copy()
oldx1 = boxes[:, 0].copy()
oldx2 = boxes[:, 2].copy()
boxes[:, 0] = widths[i] - oldx2 - 1
boxes[:, 2] = widths[i] - oldx1 - 1
assert (boxes[:, 2] >= boxes[:, 0]).all()
/usr/local/lib/python2.7/dist-packages/tensorflow/include/tensorflow/core/platform/default/mutex.h:25:22: fatal error: nsync_cv.h: No such file or directory
(之前TFFRCNN-master改用python2.7版本时好像也出了这个问题)
解决办法:
sudo gedit /usr/local/lib/python2.7/dist-packages/tensorflow/include/tensorflow/core/platform/default/mutex.h
添加路径:
#include "/usr/local/lib/python2.7/dist-packages/tensorflow/include/external/nsync/public/nsync_cv.h"
#include "/usr/local/lib/python2.7/dist-packages/tensorflow/include/external/nsync/public/nsync_mu.h"
--------------------- 本文来自 zhongxinleishi 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zhongxinleishi/article/details/78972944?utm_source=copy