新手学习,参考了http://www.cnblogs.com/CarryPotMan/p/5390336.html
只是在这篇博客中并没有说明自己训练集文件夹放置的位置以及修改中对应的文件信息。刚刚调通,记下来,以备以后自己少走些弯路。
配置py-faster-rcnn没什么好记的,文档也很多。
先说下数据集,需要准备三个文件夹:Annotations Images Imagelist
Images文件夹 :存放所有的 train 和 val 的图片
Annotation文件夹 :存放对应图片的xml文件(因为我没找到专门的xml格式标注的行人检测数据集,所以就自己用labelimg手动标注了200张图片)
Imagelist文件夹 :存放用于训练的图片名称.txt
==>此三个文件夹均放在 /home/cc/py-faster-rcnn/lib/datasets/xingren 文件夹下 || 也可根据个人爱好,只需注意要修改读取文件中的路径与之对应。
工程目录介绍
只说接下来会用到的内容
- lib
用来存放一些python接口文件,如其下的datasets主要负责数据库读取,config负责cnn一些训练的配置选项。
接下来的一系列修改也是在这个目录的datasets下进行的。
$faster_ROOT/lib/datasets 下需要修改的有 pascal_voc.py :图片及对于label的读取,
和 factory.py:工厂类,用类生成imdb类并返回数据库供网络训练和测试使用
一.下面补充说明pascal_voc.py的修改
==> 记住改完后要将该文件改名成 ‘ xingren.py ’ !!!!!!!不然会出现from datasets.xingren import xingren 出错
1. __init__()修改
class xingren(imdb): #以下全是在构建这个 xingren 类 def __init__(self, image_set, devkit_path=None): # modified imdb.__init__(self, image_set) self._image_set = image_set #图片总文件夹存储在datasets目录下的位置 eg. xingren self._devkit_path = devkit_path #根路径 /home/cc/py-faster-rcnn/lib/datasets self._data_path = os.path.join(self._devkit_path,image_set)
#上句操作等同==>self._data_path='/home/cc/py-faster-rcnn/lib/datasets/xingren' self._classes = ('__background__', # always index 0 'person') #two classes self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes))) # form the dict{'__background__':'0','person':'1'} self._image_ext = '.jpg' #根据自己图片的格式,我用的.png也没问题 self._image_index = self._load_image_set_index('ImageList.txt') #加载样本的list.txt文件,需要注意路径是从 'lib/datasets/'下开始的。
#******(该函数其余部分不做修改)
2.image_path_from_index()修改
def image_path_from_index(self, index): #modified """ Construct an image path from the image's "index" identifier. """ image_path = os.path.join(self._data_path,'Images',index +'.jpg') #index就是从list.txt中读取的图片名 assert os.path.exists(image_path), 'Path does not exist: {}'.format(image_path) return image_path
3._load_image_set_index()修改 -- 在__init__()中被调用
def _load_image_set_index(self,imagelist): # modified """ Load the indexes listed in this dataset's image set file. """ # Example path to image set file: # self._devkit_path + /VOCdevkit2007/VOC2007/ImageSets/Main/val.txt image_set_file = os.path.join(self._data_path, 'Imagelist',imagelist) assert os.path.exists(image_set_file), 'Path does not exist: {}'.format(image_set_file) with open(image_set_file) as f: image_index = [x.strip() for x in f.readlines()] return image_index
4._load_pascal_annotation()修改 只是修改了xml文件读取的路径
def _load_pascal_annotation(self, index): #modified """ Load image and bounding boxes info from XML file in the PASCAL VOC format. """ filename = os.path.join(self._data_path, 'Annotations', index + '.xml') tree = ET.parse(filename) objs = tree.findall('object')
5.最后在main函数下也修改相应的路径
from datasets.xingren import xingren d = xingren('xingren', '/home/cc/py-faster-rcnn/lib/datasets/') res = d.roidb from IPython import embed; embed()
二.factory.py的修改
# -------------------------------------------------------- # Fast R-CNN # Copyright (c) 2015 Microsoft # Licensed under The MIT License [see LICENSE for details] # Written by Ross Girshick # -------------------------------------------------------- """Factory method for easily getting imdbs by name.""" __sets = {} from datasets.xingren import xingren import numpy as np # # Set up voc_<year>_<split> using selective search "fast" mode # for year in ['2007', '2012']: # for split in ['train', 'val', 'trainval', 'test']: # name = 'voc_{}_{}'.format(year, split) # __sets[name] = (lambda split=split, year=year: pascal_voc(split, year)) # # # Set up coco_2014_<split> # for year in ['2014']: # for split in ['train', 'val', 'minival', 'valminusminival']: # name = 'coco_{}_{}'.format(year, split) # __sets[name] = (lambda split=split, year=year: coco(split, year)) # # # Set up coco_2015_<split> # for year in ['2015']: # for split in ['test', 'test-dev']: # name = 'coco_{}_{}'.format(year, split) # __sets[name] = (lambda split=split, year=year: coco(split, year)) name = 'xingren' devkit = '/home/cc/py-faster-rcnn/lib/datasets/' __sets['xingren'] = (lambda name = name,devkit = devkit: xingren(name,devkit)) def get_imdb(name): """Get an imdb (image database) by name.""" if not __sets.has_key(name): raise KeyError('Unknown dataset: {}'.format(name)) return __sets[name]() def list_imdbs(): """List all registered imdbs.""" return __sets.keys()
大功告成!!!!
还有一些模型训练参数的调整,我后面再接着看。
路径总结:
main函数中的参数传入:d = xingren('xingren', '/home/cc/py-faster-rcnn/lib/datasets') ==>__init__(self,image_set,devkit_path)
self._devkit_path = devkit_path #根路径 /home/cc/py-faster-rcnn/lib/datasets
self._data_path = os.path.join(self._devkit_path,image_set) #路径 /home/cc/py-faster-rcnn/lib/datasets/xingren
image_path = os.path.join(self._data_path,'Images',index+ '.jpg') #路径 /home/cc/py-faster-rcnn/lib/datasets/xingren/Images/xxxxx.jpg
__init__函数中参数传入: self._image_index = self._load_image_set_index('Trainval.txt')
image_set_file = os.path.join(self._data_path, 'Imagelist',imagelist) #路径 /home/cc/py-faster-rcnn/lib/datasets/xingren/Imagelist/Trainval.txt
filename = os.path.join(self._data_path, 'Annotations', index + '.xml') #路径 /home/cc/py-faster-rcnn/lib/datasets/xingren/Annotations/xxxxxx.xml