2018年5月14日星期一

SSD VOC评估与训练自己的数据集


在验证VOC2007测试集时
1. 首先将数据集转换为tfrecord格式:
DATASET_DIR=./VOC2007/test/
OUTPUT_DIR=./tfrecords
python tf_convert_data.py \
    --dataset_name=pascalvoc \
    --dataset_dir=${DATASET_DIR} \
    --output_name=voc_2007_test \
    --output_dir=${OUTPUT_DIR}

调用tf_convert_data.py将test set转化成tfrecoeds:(注意:这里直接运行会碰到无法读取图片,UTF-8无法decode的Erro,解决办法是打开SSD工程—>datasets—>pascalvoc_to_tfrecords.py 。。。然后更改文件的83行读取方式为’rb’)

注意将voc_2007_train改为voc_2007_test。

2. 进行模型评估:
DATASET_DIR=./tfrecords
EVAL_DIR=./logs/
CHECKPOINT_PATH=./checkpoints/ssd_300_vgg.ckpt
python eval_ssd_network.py \
    --eval_dir=${EVAL_DIR} \
    --dataset_dir=${DATASET_DIR} \
    --dataset_name=pascalvoc_2007 \
    --dataset_split_name=test \
    --model_name=ssd_300_vgg \
    --checkpoint_path=${CHECKPOINT_PATH} \
    --batch_size=
运行以上代码报错:
TypeError: Can not convert a tuple into a Tensor or Operation.

解决方法:
打开eval_ssd_network.py文件,然后加入以下代码:
  1. def flatten(x):  
  2.     result = []  
  3.     for el in x:  
  4.          if isinstance(el, tuple):  
  5.                result.extend(flatten(el))  
  6.          else:  
  7.                result.append(el)  
  8.     return result 
下面两处地方调用:
  1. # Standard evaluation loop.  
  2.             start = time.time()  
  3.             slim.evaluation.evaluate_once(  
  4.                 master=FLAGS.master,  
  5.                 checkpoint_path=checkpoint_path,  
  6.                 logdir=FLAGS.eval_dir,  
  7.                 num_evals=num_batches,  
  8.                 eval_op=flatten(list(names_to_updates.values())), #这里也调用flatten  
  9.                 variables_to_restore=variables_to_restore,  
  10.                 session_config=config)
  11.   
  1. # Waiting loop.  
  2.             slim.evaluation.evaluation_loop(  
  3.                 master=FLAGS.master,  
  4.                 checkpoint_dir=checkpoint_path,  
  5.                 logdir=FLAGS.eval_dir,  
  6.                 num_evals=num_batches,  
  7.                 eval_op=flatten(list(names_to_updates.values())), #这里调用flatten  
  8.                 variables_to_restore=variables_to_restore,  
  9.                 eval_interval_secs=60,  
  10.                 max_number_of_evaluations=np.inf,  
  11.                 session_config=config,  
  12.                 timeout=None
2 训练自己的数据集:
voc格式的数据集制作好以后,转换成tfrecords。需要修改一下源码,
datasets\pascalvoc_common.py:
#VOC_LABELS = {
#    'none': (0, 'Background'),
#    'aeroplane': (1, 'Vehicle'),
#    'bicycle': (2, 'Vehicle'),
#    'bird': (3, 'Animal'),
#    'boat': (4, 'Vehicle'),
#    'bottle': (5, 'Indoor'),
#    'bus': (6, 'Vehicle'),
#    'car': (7, 'Vehicle'),
#    'cat': (8, 'Animal'),
#    'chair': (9, 'Indoor'),
#    'cow': (10, 'Animal'),
#    'diningtable': (11, 'Indoor'),
#    'dog': (12, 'Animal'),
#    'horse': (13, 'Animal'),
#    'motorbike': (14, 'Vehicle'),
#    'person': (15, 'Person'),
#    'pottedplant': (16, 'Indoor'),
#    'sheep': (17, 'Animal'),
#    'sofa': (18, 'Indoor'),
#    'train': (19, 'Vehicle'),
#    'tvmonitor': (20, 'Indoor'),
#}

VOC_LABELS = {
    'none': (0, 'Background'),
    'crazing': (1, 'crazing'),
    'inclusion': (2, 'inclusion'),
    'patches': (3, 'patches'),
    'pitted_surface': (4, 'pitted_surface'),
    'rolled-in_scale': (5, 'rolled-in_scale'),
    'scratches': (6, 'scratches'),
}
接着跳转到SSD-tensorflow目录下,进行tfrecords操作,我的运行命令如下:
  1. DATASET_DIR=VOCtest2018/  
  2. OUTPUT_DIR=tfrecords/  
  3. python3 tf_convert_data.py \  
  4.     --dataset_name=pascalvoc \  
  5.     --dataset_dir=${DATASET_DIR} \  
  6.     --output_name=voc_2007_train \  
  7.     --output_dir=${OUTPUT_DIR}
这样就可以进行训练了,运行的命令为:
  1. DATASET_DIR=tfrecords  
  2. TRAIN_DIR=logs/  
  3. CHECKPOINT_PATH=./checkpoints/ssd_300_vgg.ckpt  
  4. python3 train_ssd_network.py \  
  5.     --train_dir=${TRAIN_DIR} \  
  6.     --dataset_dir=${DATASET_DIR} \  
  7.     --dataset_name=pascalvoc_2007 \  
  8.     --dataset_split_name=train \  
  9.     --model_name=ssd_300_vgg \  
  10.     --checkpoint_path=${CHECKPOINT_PATH} \  
  11.     --save_summaries_secs=60 \  
  12.     --save_interval_secs=600 \  
  13.     --weight_decay=0.0005 \  
  14.     --optimizer=adam \  
  15.     --learning_rate=0.001 \  
  16.     --batch_size=16 
  17.     --device=cpu(如果在cpu机器上跑)
  18.     --data_format=NHWC(针对cpu,或者在train_ssd_network中第27行代码改)



1 条评论:

  1. 博主您好,请问类别不同时是否需要修改ckpt文件,现在直接加载ssd_vgg_300.ckpt会提示"Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint." 不知博主是否遇到这样的错误提示?期待您的回复!非常感谢!

    回复删除

Failed to find TIFF library

ImportError: Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|.. 解决方法: ...