我的AI之路(21)--用Tensorflow object_detection跑PASCAL VOC 2012数据集

     PASCAL VOC(Visual Object Classes)http://host.robots.ox.ac.uk/pascal/VOC/竞赛项目提供了用于目标分类识别的图片数据集以及development kit(用于访问数据和标签的MATLAB代码),分四种竞赛:

对象分类/识别竞赛(Classification/Detection Competitions)

 

目标轮廓划分竞赛(Segmentation Competition)

  

人的动作预测竞赛(Action Classification Competition)

   

人体各部分识别竞赛(Person Layout Taster Competition)

   

     注:上面四张图片截取自PASCAL VOC网站,我觉得这些图片非常直观的解释了上面的四种任务,懒得用大段的文字说明来解释,所以截图过来在此用作示例。

   

    把VOC2012数据集VOCtrainval_11-May-2012.tar下载下来后展开,可以看到ImageSets目录下对应有四个目录Main、Segmentation、Action、Layout,即分别用于上面四种竞赛:

   

执行下面命令把PASCAL VOC数据转换成tfrecord格式数据:

cd D:\AI\dataset\VOC2012

python D:\AI\tensorflow\models\research\object_detection\dataset_tools\create_pascal_tf_record.py ^
   --label_map_path=D:\AI\tensorflow\models\research\object_detection\data\pascal_label_map.pbtxt ^
   --data_dir=D:\AI\dataset ^
   --year=VOC2012 ^
   --set=train ^
   --output_path=pascal_train.record

python D:\AI\tensorflow\models\research\object_detection\dataset_tools\create_pascal_tf_record.py ^
   --label_map_path=D:\AI\tensorflow\models\research\object_detection\data\pascal_label_map.pbtxt ^
   --data_dir=D:\AI\dataset ^
   --year=VOC2012 ^
   --set=val ^
   --output_path=pascal_val.record

可以看到生成了两个tfrecord数据文件:

  把D:\AI\tensorflow\models\research\object_detection\samples\configs\ssd_mobilenet_v1_pets.config拷贝到D:\AI\dataset\VOC2012下并改名为ssd_mobilenet_v1_voc2012.config,修改其内容如下:

# SSD with Mobilenet v1 configuration for MSCOCO Dataset.
# Users should configure the fine_tune_checkpoint field in the train config as
# well as the label_map_path and input_path fields in the train_input_reader and
# eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that
# should be configured.


model {
  ssd {
    num_classes: 20
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
    }
    matcher {
      argmax_matcher {
        matched_threshold: 0.5
        unmatched_threshold: 0.5
        ignore_thresholds: false
        negatives_lower_than_unmatched: true
        force_match_for_each_row: true
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    anchor_generator {
      ssd_anchor_generator {
        num_layers: 6
        min_scale: 0.2
        max_scale: 0.95
        aspect_ratios: 1.0
        aspect_ratios: 2.0
        aspect_ratios: 0.5
        aspect_ratios: 3.0
        aspect_ratios: 0.3333
      }
    }
    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
    box_predictor {
      convolutional_box_predictor {
        min_depth: 0
        max_depth: 0
        num_layers_before_predictor: 0
        use_dropout: false
        dropout_keep_probability: 0.8
        kernel_size: 1
        box_code_size: 4
        apply_sigmoid_to_scores: false
        conv_hyperparams {
          activation: RELU_6,
          regularizer {
            l2_regularizer {
              weight: 0.00004
            }
          }
          initializer {
            truncated_normal_initializer {
              stddev: 0.03
              mean: 0.0
            }
          }
          batch_norm {
            train: true,
            scale: true,
            center: true,
            decay: 0.9997,
            epsilon: 0.001,
          }
        }
      }
    }
    feature_extractor {
      type: 'ssd_mobilenet_v1'
      min_depth: 16
      depth_multiplier: 1.0
      conv_hyperparams {
        activation: RELU_6,
        regularizer {
          l2_regularizer {
            weight: 0.00004
          }
        }
        initializer {
          truncated_normal_initializer {
            stddev: 0.03
            mean: 0.0
          }
        }
        batch_norm {
          train: true,
          scale: true,
          center: true,
          decay: 0.9997,
          epsilon: 0.001,
        }
      }
    }
    loss {
      classification_loss {
        weighted_sigmoid {
        }
      }
      localization_loss {
        weighted_smooth_l1 {
        }
      }
      hard_example_miner {
        num_hard_examples: 3000
        iou_threshold: 0.99
        loss_type: CLASSIFICATION
        max_negatives_per_positive: 3
        min_negatives_per_image: 0
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
    normalize_loss_by_num_matches: true
    post_processing {
      batch_non_max_suppression {
        score_threshold: 1e-8
        iou_threshold: 0.6
        max_detections_per_class: 100
        max_total_detections: 100
      }
      score_converter: SIGMOID
    }
  }
}

train_config: {
  batch_size: 24
  optimizer {
    rms_prop_optimizer: {
      learning_rate: {
        exponential_decay_learning_rate {
          initial_learning_rate: 0.004
          decay_steps: 800720
          decay_factor: 0.95
        }
      }
      momentum_optimizer_value: 0.9
      decay: 0.9
      epsilon: 1.0
    }
  }
fine_tune_checkpoint: "D:\\AI\\tensorflow\\models\\research\\object_detection\\ssd_mobilenet_v1_coco_2017_11_17\\model.ckpt"
  from_detection_checkpoint: true
  # Note: The below line limits the training process to 200K steps, which we
  # empirically found to be sufficient enough to train the pets dataset. This
  # effectively bypasses the learning rate schedule (the learning rate will
  # never decay). Remove the below line to train indefinitely.
  num_steps: 50000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
}


train_input_reader: {
  tf_record_input_reader {
    input_path: "D:\\AI\\dataset\\voc2012\\pascal_train.record"
  }
  label_map_path: "D:\\AI\\tensorflow\\models\\research\\object_detection\\data\\pascal_label_map.pbtxt"
}


eval_config: {
  num_examples: 5717
  # Note: The below line limits the evaluation process to 10 evaluations.
  # Remove the below line to evaluate indefinitely.
  max_evals: 10
}


eval_input_reader: {
  tf_record_input_reader {
    input_path: "D:\\AI\\dataset\\voc2012\\pascal_val.record"
  }
  label_map_path: "D:\\AI\\tensorflow\\models\\research\\object_detection\\data\\pascal_label_map.pbtxt"
  shuffle: false
  num_readers: 1
}

     

     根据实际情况需要修改D:\AI\tensorflow\models\research\object_detection\model_main.py,如果你需要打开日志并且需要限制GPU的内存占用的话:

from tensorflow.core.protobuf import config_pb2
tf.logging.set_verbosity(tf.logging.INFO)

...

def main(unused_argv):
  flags.mark_flag_as_required('model_dir')
  flags.mark_flag_as_required('pipeline_config_path')
  gpu_options= config_pb2.GPUOptions(per_process_gpu_memory_fraction=0.5)
  session_config = config_pb2.ConfigProto(log_device_placement=True,gpu_options=gpu_options)

  config = tf.estimator.RunConfig(model_dir=FLAGS.model_dir,session_config=session_config)

在D:\AI\dataset\VOC2012下面创建train_result和eval_result目录,然后执行下面的命令开始训练或测试/验证:

cd D:\AI\tensorflow\models\research\object_detection

python model_main.py ^
   --logtostderr ^
   --model_dir=D:\AI\dataset\voc2012\train_result ^
   --pipeline_config_path=D:\AI\dataset\voc2012\ssd_mobilenet_v1_voc2012.config ^
   --num_train_steps=50000

python model_main.py ^
   --logtostderr ^
   --model_dir=D:\AI\dataset\voc2012\eval_result ^
   --checkpoint_dir=D:\AI\dataset\voc2012\train_result ^
   --pipeline_config_path=D:\AI\dataset\voc2012\ssd_mobilenet_v1_voc2012.config ^
   --num_eval_steps=5000

我的AI之路(1)--前言

我的AI之路(2)--安装Fedora 28

我的AI之路(3)--安装Anaconda3 和Caffe

我的AI之路(4)--在Anaconda3 下安装Tensorflow 1.8

我的AI之路(5)--如何选择和正确安装跟Tensorflow版本对应的CUDA和cuDNN版本

我的AI之路(6)--在Anaconda3 下安装PyTorch

我的AI之路(7)--安装OpenCV3_Python 3.4.1 + Contrib以及PyCharm

我的AI之路(8)--体验用OpenCV 3的ANN进行手写数字识别及解决遇到的问题

我的AI之路(9)--使用scikit-learn

我的AI之路(10)--如何在Linux下安装CUDA和CUDNN

我的AI之路(11)--如何解决在Linux下编译OpenCV3时出现的多个错误

我的AI之路(12)--如何配置Caffe使用GPU计算并解决编译中出现的若干错误

我的AI之路(13)--解决编译gcc/g++源码过程中出现的错误

我的AI之路(14)--Caffe example:使用MNIST数据集训练和测试LeNet-5模型

我的AI之路(15)--Linux下编译OpenCV3的最新版OpenCV3.4.1及错误解决

我的AI之路(16)--云服务器上安装和调试基于Tensorflow 1.10.1的训练环境

我的AI之路(17)--Tensorflow和Caffe的API及Guide

我的AI之路(18)--Tensorflow的模型安装之object_detection

我的AI之路(19)--如何在Windows下安装pycocotools PythonAPI

我的AI之路(20)--用Tensorflow object_detection跑raccoon数据集

我的AI之路(21)--用Tensorflow object_detection跑PASCAL VOC 2012数据集

我的AI之路(22)--使用Object_Detection_Tensorflow_API

猜你喜欢

转载自blog.csdn.net/XCCCCZ/article/details/82746384
今日推荐