按照下面這個博客修改faster-rcnn源碼,訓練自己的數據:
http://blog.csdn.net/sinat_30071459/article/details/51332084
訓練自己的數據的時候如果提示assert (boxes[:, 2] >= boxes[:, 0]).all()這樣的AssertionError,有下面幾種解決方法:
1、為了防止和之前的模型搞混,訓練前把output文件夾刪除(或改個其他名),還要把py-faster-rcnn/data/cache中的文件和
py-faster-rcnn/data/VOCdevkit2007/annotations_cache中的文件刪除(如果有的話)。
2、因為VOC2007 的矩形標注坐標是以1為基准的(1-based),而我們在處理圖像坐標都是0起始的(0-based)。在py-faster-rcnn/lib/datasets/pascal_voc.py (line 207)文件中會對Xmin,Ymin,Xmax,Ymax進行減1操作。
# Make pixel indexes 0-based
x1 = float(bbox.find('xmin').text) - 1 y1 = float(bbox.find('ymin').text) - 1 x2 = float(bbox.find('xmax').text) - 1 y2 = float(bbox.find('ymax').text) - 1 cls = self._class_to_ind[obj.find('name').text.lower().strip()] boxes[ix, :] = [x1, y1, x2, y2] gt_classes[ix] = cls overlaps[ix, cls] = 1.0 seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)
xmin,ymin,xmax,ymax的含義:
xmin,ymin為矩形的左上角坐標(x,y),xmax=x+w-1,ymax=y+h-1
錯誤來自於py-faster-rcnn/lib/datasets/imdb.py文件中append_flipped_images(self)函數中:
def append_flipped_images(self): num_images = self.num_images widths = [PIL.Image.open(self.image_path_at(i)).size[0] for i in xrange(num_images)] for i in xrange(num_images): boxes = self.roidb[i]['boxes'].copy() oldx1 = boxes[:, 0].copy() oldx2 = boxes[:, 2].copy() boxes[:, 0] = widths[i] - oldx2 - 1 print boxes[:, 0] boxes[:, 2] = widths[i] - oldx1 - 1 print boxes[:, 0] assert (boxes[:, 2] >= boxes[:, 0]).all() entry = {'boxes' : boxes, 'gt_overlaps' : self.roidb[i]['gt_overlaps'], 'gt_classes' : self.roidb[i]['gt_classes'], 'flipped' : True} self.roidb.append(entry) self._image_index = self._image_index * 2
所以在生成XML文件的時候就一定要以1為基准(1-based)來生成。
3、如果已經將你的標注xml中的坐標統一為1-based坐標了,但在執行訓練的時候還是會有上面的Assert錯誤,那么就要好好檢查你的標注數據中,是不是有超出圖像范圍的矩形。如果有,一定要修正。
