Faster-RCNN訓練自己的數據集——備忘


新手學習,參考了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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM