在github上clone下来的代码,可以看到根目录下有以下几个文件夹,其中output为训练完之后才会有的文件夹。
cd py-faster-rcnn
./experiments/scripts/faster_rcnn_alt_opt.sh 0 VGG16 pascal_voc
# 第一块GPU(0) 模型是VGG16 数据集时pascal_voc
cd $FRCN_ROOT
./experiments/scripts/faster_rcnn_end2end.sh [GPU_ID] [NET] [--set ...]
python ./tools/train_net.py --gpu 1 --solver models/pascal_voc/VGG_CNN_M_1024/faster_rcnn_end2end/solver.prototxt --weights data/imagenet_models/VGG_CNN_M_1024.v2.caffemodel --imdb voc_2012_trainval --iters 70000 --cfg experiments/cfgs/faster_rcnn_end2end.yml
问题1:如何在同一张图像中画出不同种类对应颜色的目标框?
修改demo.py中的代码,代码如下:
`# Visualize detections for each class
CONF_THRESH = 0.7
NMS_THRESH = 0.3
for cls_ind, cls in enumerate(CLASSES[1:]):
cls_ind += 1 # because we skipped background
cls_boxes = boxes[:, 4_cls_ind:4_(cls_ind + 1)]
cls_scores = scores[:, cls_ind]
dets = np.hstack((cls_boxes,
cls_scores[:, np.newaxis])).astype(np.float32)
keep = nms(dets, NMS_THRESH)
dets = dets[keep, :]
#draw
#vis_detections(im, cls, dets, thresh=CONF_THRESH)
font = cv2.FONT_HERSHEY_SIMPLEX
color = (0,0,0)
if cls_ind == 1: #motorbike
color = (0, 0, 255)
elif cls_ind == 2: #car
color = (0, 255, 0)
elif cls_ind == 3: #bus
color = (255, 0, 0)
else: #truck
color = (255, 255, 255)
inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
if len(inds) > 0:
for i in inds:
bbox = dets[i, :4]
score = dets[i, -1]
cv2.rectangle(im,(bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
cv2.putText(im,'{:s} {:.3f}'.format(cls, score),(bbox[0], (int)((bbox[1]- 2))), font, 0.5, (0,255,0), 1)
# Display the resulting frame
cv2.imshow('{:s}'.format(image_name),im)`
问题1:如果想检测小的物体,应该怎么办? 解答:改变anchor_target_layer 和proposal_layer层的参数,[链接在此]
scales: decrease these values to account for smaller boxes
ratios: adjust them depending on the shape of your grount-truth boxes
feat_stride : supposedly this can be modified to improve accuracy of the generated anchors
问题2:如何实时的进行视频的检测?(#578) 解答: 需要修改原代码demo.py,代码如下
while True:
demo_video(net,cv2.VideoCapture(videoFilePath))
def demo_video(net, videoFile):
global frameRate
# Load the demo image
ret, im = videoFile.read()
# Detect all object classes and regress object bounds
timer = Timer()
timer.tic()
scores, boxes = im_detect(net, im)
timer.toc()
print ('Detection took {:.3f}s for '
'{:d} object proposals').format(timer.total_time, boxes.shape[0])
frameRate = 1.0/timer.total_time
print "fps: " + str(frameRate)
# Visualize detections for each class
CONF_THRESH = 0.65
NMS_THRESH = 0.2
for cls_ind, cls in enumerate(CLASSES[1:]):
cls_ind += 1 # because we skipped background
cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
cls_scores = scores[:, cls_ind]
dets = np.hstack((cls_boxes,
cls_scores[:, np.newaxis])).astype(np.float32)
keep = nms(dets, NMS_THRESH)
dets = dets[keep, :]
im=vis_detections_video(im, cls, dets, thresh=CONF_THRESH)
cv2.putText(im,'{:s} {:.2f}'.format("FPS:", frameRate(1750,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255))
cv2.imshow(videoFilePath.split('/')[len(videoFilePath.split('/'))-1],im)
cv2.waitKey(20)
问题3:如何针对小的目标检测?(#443)
针对一个大图像中的小目标进行检测,需要修改anchor的参数,具体的文件:generate_anchors.py from this: def generate_anchors(base_size=16, ratios=[0.5, 1, 2], scales=2**np.arange(3, 6)): To this: def generate_anchors(base_size=16, ratios=[0.3, 0.75, 1], scales=2**np.arange(3, 6)):
问题1:训练完成的模型,但是使用原图却检测不到任何结果?
原因:很有可能标注的时候的label超出了图像的边界。推荐两个验证标注的方式:[check the boxes] 和最新版本的LabelImg。
问题2:如何去训练一个RPN模型(#364)
首先需要知道alt_opt是如何工作的:
然后,只需做1-2步即可生成proposals. 可视化这些proposals可以将 lib/rpn/generate.py中的visualisation置为1。
问题3:faster-rcnn如何使用多GPU进行训练
首先答案是否定的,python不支持多GPU训练。但也有相关的解决方案: 1. https://github.com/315386775/py-R-FCN-multiGPU 这个分支支持多GPU 2. mxnet可以支持多GPU训练
0526更新 问题4:训练时出现bbox_loss为0的问题
问题对应的链接如下:[loss为0的问题]
在$FRCNN_ROOT的experiments/script中有脚本可以查看:faster_rcnn_end2end.sh
LOG="experiments/logs/faster_rcnn_end2end_${NET}_${EXTRA_ARGS_SLUG}.txt.`date +'%Y-%m-%d_%H-%M-%S'`"