1. 程式人生 > >tf版faster rcnn執行

tf版faster rcnn執行

資料集製作

資料集格式:

與pascal voc格式一樣, 分為Annotations和JPEGImages xml檔案有這些內容就可以了

<annotation>
  <folder>VOC2007</folder>
  <filename>001.jpg</filename>
  <size>
    <width>958</width>
    <height>808</height>
    <depth>3</depth>
  </size>
  <object>
    <name>airplane</name>
    <pose>unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndbox>
      <xmin>563</xmin>
      <ymin>478</ymin>
      <xmax>630</xmax>
      <ymax>573</ymax>
    </bndbox>
  </object>
</annotation>

跑自己的資料集修改的內容:

1 上傳資料至Faster-RCNN_Tensorflow/data/io/data,data資料夾是我自己新建的 2 修改convert_data_to_tfrecord.py檔案,把路徑修改至你自己的資料路徑就可以了。tf.app.flags.DEFINE_string(‘dataset’, ‘nwpu’, ‘dataset’)這裡選擇要使用的資料集。 3.進入Faster-RCNN_Tensorflow/libs/label_name_dict開啟label_dict.py檔案,新增自己的資料集,'back_ground':0是預設的 4.修改Faster-RCNN_Tensorflow/libs/configs/cfgs.py,DATASET_NAME = 'nwpu'

資料集選擇自己的資料集,CLASS_NUM = 10改成自己資料集的類數

5.修改Faster-RCNN_Tensorflow/data/io/read_tfrecord.py,next_batch函式中新增自己的資料集

if dataset_name not in ['ship', 'spacenet', 'pascal', 'coco']:
        raise ValueError('dataSet name must be in pascal, coco spacenet and ship')

6 convert_data_to_tfrecord.py檔案的def convert_pascal_to_tfrecord():函式中'img_name': _bytes_feature(img_name)

修改為'img_name': _bytes_feature(img_name.encode()),如果沒有報錯就不用改了。 至此就算修改完了,執行convert_data_to_tfrecord.py檔案,生成訓練和測試的tfrecord檔案 如果執行時出現類似這種錯誤,是因為資料集中出現了沒有的類名,好好檢查下資料集。

Traceback (most recent call last):
  File "convert_data_to_tfrecord.py", line 122, in <module>
    convert_pascal_to_tfrecord()
  File "convert_data_to_tfrecord.py", line 93, in convert_pascal_to_tfrecord
    img_height, img_width, gtbox_label = read_xml_gtbox_and_label(xml)
  File "convert_data_to_tfrecord.py", line 59, in read_xml_gtbox_and_label
    label = NAME_LABEL_MAP[child_item.text]
KeyError: '10'

編譯:

進入/libs/box_utils/cython_utils刪除裡邊的bbox.c和nms.c檔案以及.so檔案

python setup.py build_ext --inplace

編譯通過後應該會有這些檔案

bbox.c                                       __init__.py   nms.pyx
bbox.pyx                                     __init__.pyc  __pycache__
cython_bbox.cpython-35m-x86_64-linux-gnu.so  Makefile      setup.py
cython_nms.cpython-35m-x86_64-linux-gnu.so   nms.c

訓練:

1.上傳預訓練的模型至Faster-RCNN_Tensorflow/data/pretrained_weights,預設的是resnet_v1_101 2.報錯:

Traceback (most recent call last):
  File "train.py", line 14, in <module>
    from libs.networks import build_whole_network
  File "../libs/networks/build_whole_network.py", line 20, in <module>
    from libs.detection_oprations.anchor_target_layer_without_boxweight import anchor_target_layer
  File "../libs/detection_oprations/anchor_target_layer_without_boxweight.py", line 15, in <module>
    from libs.box_utils.cython_utils.cython_bbox import bbox_overlaps
ImportError: ../libs/box_utils/cython_utils/cython_bbox.so: undefined symbol: _Py_ZeroStruct

原因是python版本的問題,我電腦裡裝了python2和python3,但是tensorflow是python3的,我用python2編譯就會出問題。 3.進入到tools,執行python train.py就可以開始訓練了

期間遇到的問題:

PaddingFIFOQueue '_2_get_batch/batch/padding_fifo_queue' is closed and has insufficient elements (requested 1, current size 0)

我是因為tfrecord放的位置不對,程式沒有找到,預設的位置是Faster-RCNN_Tensorflow/data/tfrecord下

Traceback (most recent call last):
 File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/script_ops.py", line 206, in __call__
    ret = func(*args)
  File "../libs/box_utils/draw_box_in_img.py", line 124, in draw_boxes_with_label_and_scores
    draw_label_with_scores(draw_obj, box, a_label, a_score, color='White')
  File "../libs/box_utils/draw_box_in_img.py", line 91, in draw_label_with_scores
    print(LABEL_NAME_MAP[label])
KeyError: 15

我因為開始忘了修改類的數目,所以總是生成一些超過類目數的label,這個問題困了我好久,果然是馬虎不得。