py-faster-rcnn报错解决+用别人训练好的模型测试自己的图片

     Pro1: TypeError: 'numpy.float64' object cannot be interpreted as an index

      这个问题是因为我用的numpy版本太高了, 最简单的方法是直接改版本 sudo python2.7 /usr/local/bin/pip install -U numpy==1.11.0;但是我不是管理员,没有办法更改,修改如下几个地方的code:
  1) /home/xxx/py-faster-rcnn/lib/roi_data_layer/minibatch.py

  将第26行:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
  改为:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)


  2) /home/xxx/py-faster-rcnn/lib/datasets/ds_utils.py
  将第12行:hashes = np.round(boxes * scale).dot(v)
  改为:hashes = np.round(boxes * scale).dot(v).astype(np.int)


  3) /home/xxx/py-faster-rcnn/lib/fast_rcnn/test.py
  将第129行: hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v)
  改为: hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v).astype(np.int)


  4) /home/xxx/py-faster-rcnn/lib/rpn/proposal_target_layer.py

  将第60行:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)

  改为:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)

  Pro2:TypeError: slice indices must be integers or None or have an __index__ method

  还是numpy的版本问题:

        修改 /home/lzx/py-faster-rcnn/lib/rpn/proposal_target_layer.py,转到123行:

for ind in inds:
cls = clss[ind]
start = 4 * cls
end = start + 4
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
return bbox_targets, bbox_inside_weights

这里的ind,start,end都是 numpy.int 类型,这种类型的数据不能作为索引,所以必须对其进行强制类型转换,转化结果如下:
for ind in inds:
ind = int(ind)
cls = clss[ind]
start = int(4 * cls)
end = int(start + 4)
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
return bbox_targets, bbox_inside_weights

  File "/py-faster-rcnn/tools/../lib/datasets/imdb.py", line 112, in append_flipped_images
  assert (boxes[:, 2] >= boxes[:, 0]).all()
  AssertionError

  解决方法:这些问题的根源都是faster-rcnn系列在处理生成pascal voc数据集时,为了使像素以0为起点,每个bbox的左上右下坐标都减1,如果你的数据里有坐标为0,一般是x1或y1,这时x1 = 0-1 = 65535。比如bbox为[1,1,8,8,](x,y,w,h非xmin,ymin,xmax,ymax)在py-faster-rcnn处理时实际坐标处理为[0,0,8,8],因为贴合图像边缘也会报错。但是处理边缘信息时少一个像素可能会影响效果,8*8的图片一个像素的差距就会少12.5%的信息量,小目标检测上会吃亏

也可以打开$faster-rcnn-root/lib/datasets/imdb.py 修改

测试代码:time ./tools/test_net.py --gpu 0 --def test.prototxt --net  VGG16_faster_rcnn_final.caffemodel --imdb voc_2007_test --cfg oldfaster_rcnn_end2end.yml

测试结果获得detections.pkl和pedestrian_pr.pkl用下列代码读取并画图。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import os.path
import numpy as np
import xml.etree.ElementTree as xmlET
from PIL import Image, ImageDraw
import cPickle as pickle

f = open('C:\Users\Admin\Downloads\pedestrians_pr.pkl')
ship_pr = pickle.load(f)

test_file = 'C:/Users/Admin/Desktop/xml/mytest.txt'
file_path_img = 'F:\VOC2007\JPEGImages'
save_file_path = 'F:/results'

with open(test_file) as f:
    image_index = [x.strip() for x in f.readlines()]

fd = open('C:\Users\Admin\Downloads\detections.pkl','rb')
info = pickle.load(fd)


dets = info[1]

# print dets

num = 0
for idx in xrange(len(dets)):
    if len(dets[idx]) == 0:
        continue
    img = Image.open(os.path.join(file_path_img, image_index[idx] + '.jpg'))
    draw = ImageDraw.Draw(img)
    for i in xrange(len(dets[idx])):
        box = dets[idx][i]

        draw.rectangle([int(np.round(float(box[0]))), int(np.round(float(box[1]))),
                    int(np.round(float(box[2]))), int(np.round(float(box[3])))], outline=(255, 0, 0))
        
    img.save(os.path.join(save_file_path, image_index[idx] + '.jpg'))

 

 

你可能感兴趣的:(深度学习)