1.训练文件的配置

将生成的csv和record文件都放在新建的mydata文件夹下,并打开object_detection文件夹下的data文件夹,复制一个后缀为.pbtxt的文件到mtdata文件夹下,并重命名为gaoyue.pbtxt

用记事本打开该文件,因为我只分了一类,所以将其他内容删除,只剩下这一个类别,并将name改为gaoyue。

这时我们拥有的所有文件如下图所示。

我们在object_detection文件夹下新建一个training文件夹,在里面新建一个记事本文件并命名为 ssd_mobilenet_v1_coco.config

打开,输入以下代码,按右边注释进行修改

  1. # SSD with Mobilenet v1 configuration for MSCOCO Dataset.
  2. # Users should configure the fine_tune_checkpoint field in the train config as
  3. # well as the label_map_path and input_path fields in the train_input_reader and
  4. # eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that
  5. # should be configured.
  6.  
  7. model {
  8. ssd {
  9. num_classes: 1 # 你类别的数量,我这里只分了一类
  10. box_coder {
  11. faster_rcnn_box_coder {
  12. y_scale: 10.0
  13. x_scale: 10.0
  14. height_scale: 5.0
  15. width_scale: 5.0
  16. }
  17. }
  18. matcher {
  19. argmax_matcher {
  20. matched_threshold: 0.5
  21. unmatched_threshold: 0.5
  22. ignore_thresholds: false
  23. negatives_lower_than_unmatched: true
  24. force_match_for_each_row: true
  25. }
  26. }
  27. similarity_calculator {
  28. iou_similarity {
  29. }
  30. }
  31. anchor_generator {
  32. ssd_anchor_generator {
  33. num_layers: 6
  34. min_scale: 0.2
  35. max_scale: 0.95
  36. aspect_ratios: 1.0
  37. aspect_ratios: 2.0
  38. aspect_ratios: 0.5
  39. aspect_ratios: 3.0
  40. aspect_ratios: 0.3333
  41. }
  42. }
  43. image_resizer {
  44. fixed_shape_resizer {
  45. height: 300
  46. width: 300
  47. }
  48. }
  49. box_predictor {
  50. convolutional_box_predictor {
  51. min_depth: 0
  52. max_depth: 0
  53. num_layers_before_predictor: 0
  54. use_dropout: false
  55. dropout_keep_probability: 0.8
  56. kernel_size: 1
  57. box_code_size: 4
  58. apply_sigmoid_to_scores: false
  59. conv_hyperparams {
  60. activation: RELU_6,
  61. regularizer {
  62. l2_regularizer {
  63. weight: 0.00004
  64. }
  65. }
  66. initializer {
  67. truncated_normal_initializer {
  68. stddev: 0.03
  69. mean: 0.0
  70. }
  71. }
  72. batch_norm {
  73. train: true,
  74. scale: true,
  75. center: true,
  76. decay: 0.9997,
  77. epsilon: 0.001,
  78. }
  79. }
  80. }
  81. }
  82. feature_extractor {
  83. type: 'ssd_mobilenet_v1'
  84. min_depth: 16
  85. depth_multiplier: 1.0
  86. conv_hyperparams {
  87. activation: RELU_6,
  88. regularizer {
  89. l2_regularizer {
  90. weight: 0.00004
  91. }
  92. }
  93. initializer {
  94. truncated_normal_initializer {
  95. stddev: 0.03
  96. mean: 0.0
  97. }
  98. }
  99. batch_norm {
  100. train: true,
  101. scale: true,
  102. center: true,
  103. decay: 0.9997,
  104. epsilon: 0.001,
  105. }
  106. }
  107. }
  108. loss {
  109. classification_loss {
  110. weighted_sigmoid {
  111. }
  112. }
  113. localization_loss {
  114. weighted_smooth_l1 {
  115. }
  116. }
  117. hard_example_miner {
  118. num_hard_examples: 3000
  119. iou_threshold: 0.99
  120. loss_type: CLASSIFICATION
  121. max_negatives_per_positive: 3
  122. min_negatives_per_image: 0
  123. }
  124. classification_weight: 1.0
  125. localization_weight: 1.0
  126. }
  127. normalize_loss_by_num_matches: true
  128. post_processing {
  129. batch_non_max_suppression {
  130. score_threshold: 1e-8
  131. iou_threshold: 0.6
  132. max_detections_per_class: 100
  133. max_total_detections: 100
  134. }
  135. score_converter: SIGMOID
  136. }
  137. }
  138. }
  139.  
  140. train_config: {
  141. batch_size: 16 # 电脑好的话可以调高点,我电脑比较渣就调成16了
  142. optimizer {
  143. rms_prop_optimizer: {
  144. learning_rate: {
  145. exponential_decay_learning_rate {
  146. initial_learning_rate: 0.004
  147. decay_steps: 800720
  148. decay_factor: 0.95
  149. }
  150. }
  151. momentum_optimizer_value: 0.9
  152. decay: 0.9
  153. epsilon: 1.0
  154. }
  155. }
  156.  
  157. # Note: The below line limits the training process to 200K steps, which we
  158. # empirically found to be sufficient enough to train the pets dataset. This
  159. # effectively bypasses the learning rate schedule (the learning rate will
  160. # never decay). Remove the below line to train indefinitely.
  161. num_steps: 200000 # 训练的steps
  162. data_augmentation_options {
  163. random_horizontal_flip {
  164. }
  165. }
  166. data_augmentation_options {
  167. ssd_random_crop {
  168. }
  169. }
  170. }
  171.  
  172. train_input_reader: {
  173. tf_record_input_reader {
  174. input_path: "mydata/gaoyue_train.record" # 训练的tfrrecord文件路径
  175. }
  176. label_map_path: "mydata/gaoyue.pbtxt"
  177. }
  178.  
  179. eval_config: {
  180. num_examples: 8000 # 验证集的数量
  181. # Note: The below line limits the evaluation process to 10 evaluations.
  182. # Remove the below line to evaluate indefinitely.
  183. max_evals: 10
  184. }
  185.  
  186. eval_input_reader: {
  187. tf_record_input_reader {
  188. input_path: "mydata/gaoyue_test.record" # 验证的tfrrecord文件路径
  189. }
  190. label_map_path: "mydata/gaoyue.pbtxt"
  191. shuffle: false
  192. num_readers: 1
  193. }

新建后的文件显示如下。

这时,我们训练的准备工作就做好了。

2.训练模型

在object_detection文件夹下打开Anaconda Prompt,输入命令

python model_main.py --pipeline_config_path=training/ssd_mobilenet_v1_coco.config --model_dir=training  --alsologtostderr

在训练过程中如果出现no model named pycocotools的问题的话,请参考这个网址(http://www.mamicode.com/info-detail-2660241.html)解决。亲测有效

即:

(1)从https://github.com/pdollar/coco.git 下载源码,解压至全英文路径下。

(2)使用cmd进入解压后的cocoapi-master/PythonAPI路径下,输入python setup.py build_ext --inplace。如果这一步有报错,请打开set_up.py文件,将其中这两个参数删除。

即:

(3)上一步执行没问题之后,继续在cmd窗口运行命令:python setup.py build_ext install

训练完成后,training文件夹下是这样的情况

(如果想观察训练过程中参数的变化以及网络的话,可以打开新的一个Anaconda Prompt cd到object_detection文件夹下

输入命令:tensorboard --logdir=training),复制出现的网址即可。如图所示\

如果显示不出来的话,新建网页在地址栏输入http://localhost:6006/(后面的6006是我的端口号,根据你自己的输入)

3.生成模型

定位到object_detection目录下,打开Anaconda Promp输入命令

python export_inference_graph.py \ --input_type image_tensor \ --pipeline_config_path training/ssd_mobilenet_v1_coco.config \ --trained_checkpoint_prefix training/model.ckpt-500 \ --output_directory gaoyue_detection

(注意这两处标红的地方,1.    model.ckpt-500是指你训练的轮数的文件,这里因为我只训练了500轮,所以改成了500(如下图中的500)

2.   output_directory是输出模型的路径,最好是新建一个文件夹来存放模型,我新建了一个名为gaoyue_detection的模型)

命令执行完成后,打开gaoyue_detection文件夹,里面的内容如图所示

表示执行成功,这样,我们用自己数据集训练的目标检测模型就做好了

下一节会详细说我们自己模型的验证

tensorflow目标检测API之训练自己的数据集的更多相关文章

  1. tensorflow目标检测API之建立自己的数据集

    1 收集数据 为了方便,我找了11张月儿的照片做数据集,如图1,当然这在实际应用过程中是远远不够的 2 labelImg软件的安装 使用labelImg软件(下载地址:https://github.c ...

  2. tensorflow目标检测API安装及测试

    1.环境安装配置 1.1 安装tensorflow 安装tensorflow不再仔细说明,但是版本一定要是1.9 1.2 下载Tensorflow object detection API  下载地址 ...

  3. tensorflow2.4与目标检测API在3060显卡上的配置安装

    目前,由于3060显卡驱动版本默认>11.0,因此,其不能使用tensorflow1版本的任何接口,所以学习在tf2版本下的目标检测驱动是很有必要的,此配置过程同样适用于任何30系显卡配置tf2 ...

  4. TensorFlow目标检测(object_detection)api使用

    https://github.com/tensorflow/models/tree/master/research/object_detection 深度学习目标检测模型全面综述:Faster R-C ...

  5. OpenCV:OpenCV目标检测Boost方法训练

    在古老的CNN方法出现以后,并不能适用于图像中目标检测.20世纪60年代,Hubel和Wiesel( 百度百科 )在研究猫脑皮层中用于局部敏感和方向选择的神经元时发现其独特的网络结构可以有效地降低反馈 ...

  6. 【Tensorflow系列】使用Inception_resnet_v2训练自己的数据集并用Tensorboard监控

    [写在前面] 用Tensorflow(TF)已实现好的卷积神经网络(CNN)模型来训练自己的数据集,验证目前较成熟模型在不同数据集上的准确度,如Inception_V3, VGG16,Inceptio ...

  7. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

  8. 目标检测:keras-yolo3之制作VOC数据集训练指南

    制作VOC数据集指南 Github:https://github.com/hyhouyong/keras-yolo3 LabelImg标注工具(windows环境下):https://github.c ...

  9. tensorflow+ssd_mobilenet实现目标检测的训练

    本文在Ubuntu下使用tensorflow的object detection API来训练自己的数据集.所用模型为ssd_mobilenet,也可以使用其他的模型.当然也可以在windows下训练, ...

随机推荐

  1. Condition应用和源码分析

    1.Condition实现一个队列public class BoundedQueue<T> { public List<T> q; //这个列表用来存队列的元素 private ...

  2. layui实现下拉分类多级

    Layui tree 下拉菜单树   1.效果: 2.html  代码: <!DOCTYPE html> <html> <head> <meta charse ...

  3. Count the string (KMP+DP)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...

  4. 解决IE6 IE7绝对定位弹层被后面的元素遮住

    解决IE6 IE7绝对定位弹层被后面的元素遮住? 弹层边框一直被后面的元素边框遮住,试了n种方法,只有这个比较好用. <div class="tuijian-table"&g ...

  5. Codeforces 1168A(二分check)

    关键是check.要注意到其实有了mid以后每个位置都是独立的,它能从哪走到哪是固定了的,只要从左到右尽量贪心压着最小值即可. #include <cstdio> const int ma ...

  6. HATEOAS REST Service

    用户通过点击页面的href的链接地址,而跳转到其他网页,实现浏览网页的过程了. -> 让调用REST的api就可以实现,类似于用户浏览网页的从一个页面跳转到另外一个页面的过程了 -> 而这 ...

  7. css3动画:animation

    例: -webkit-animation: myfirst 5s linear 2s infinite alternate; animation: myfirst 5s linear 2s infin ...

  8. springboot使用过滤器和拦截器

    1.Filter过滤器 @Componentpublic class AuthFilter implements Filter { private static final Log log = Log ...

  9. File类--System.out.print(Object obj)的理解

    一.File 类(java.io) 概述:Java中使用File类来表示文件或者文件夹对象!     抽象路径名:描述文件或文件夹时,使用的路径符号,就是一个对象的字符串表示形式,如"c:\ ...

  10. getuser

    Help on function getuser in module getpass: getuser()    Get the username from the environment or pa ...