tensorflow ssd mobilenet模型训练

2019/1/7

很久没搞tensorflow,看评论有人说没有train.py了,就查了一下

Tensorflow object detection api没有train.py

我也没有验证,能不能用也不知道,这篇文章流程还通不通也不知道,毕竟tensorflow一个版本一个样,就这样吧

正文

经过了65个小时左右的训练,终于训练出android能跑的模型了

首先下载models

再下载数据集VOC2012

先配置环境,说实话哪些有用哪些没用都没搞清楚大哭

$ pip install pillow

$ pip install jupyter

$ pip install matplotlib

最后一个lxml要重点说以下,版本安装不对会找不到etree

$ python   //这里是自己tensorflow对应到python

>>import pip; print(pip.pep425tags.get_supported())

再去官网下载对应的版本

可能是环境没配置好或我这个方法不是本来的方法,运行时总是报找不到object_detection.xx路径,所以就把models-master/research/object_detection下,将所有.py文件都修改了,包括子文件夹内的.py文件。将from object_detection.xx import yy改成from xx import yy这样就找得到了,如果没和我报一样到错误可以跳过这一步

$ cd models-master/research/

$ mkdir object_detection/ssd_model

把数据集解压到ssd_model文件夹内

$ python object_detection/create_pascal_tf_record.py --label_map_path=object_detection/data/pascal_label_map.pbtxt --data_dir=object_detection/ssd_model/VOCdevkit --year=VOC2012 --set=train --output_path=object_detection/ssd_model/pascal_train.record

再执行

$ python object_detection/create_pascal_tf_record.py --label_map_path=object_detection/data/pascal_label_map.pbtxt --data_dir=object_detection/ssd_model/VOCdevkit --year=VOC2012 --set=val --output_path=object_detection/ssd_model/pascal_val.record

这样生成pascal_train.record和pascal_val.record文件

复制object_detection/data/pascal_label_map.pbtxt到ssd_model文件夹

复制object_detection/samples/configs/ssd_mobilenet_v1_pets.config到ssd_model文件夹,修改内容

因为VOC2012只有20个类 num_classes : 20

num_steps是训练次数,默认200000次根据需求改,我改成了30000次 num_steps: 30000

设置上边生成和复制过来的文件路径

train_input_reader: {
  tf_record_input_reader {
    input_path: "/home/xxx/models-master/research/object_detection/ssd_model/pascal_train.record"
  }
  label_map_path: "/home/xxx/models-master/research/object_detection/ssd_model/pascal_label_map.pbtxt"
}
eval_input_reader: {
  tf_record_input_reader {
    input_path: "/home/xxx/models-master/research/object_detection/ssd_model/pascal_val.record"
  }
  label_map_path: "/home/xxx/models-master/research/object_detection/ssd_model/pascal_label_map.pbtxt"
  shuffle: false
  num_readers: 1
}

下载ssd_mobilenet

解压到object_detection/ssd_model文件夹,继续修改ssd_mobilenet_v1_pets.config内容

fine_tune_checkpoint: "/home/xxx/models-master/ssd_model/ssd_mobilenet/model.ckpt"

再执行

$ python object_detection/train.py --train_dir object_detection/train --pipeline_config_path object_detection/ssd_model/ssd_mobilenet_v1_pets.config

如果报找不到nets,就把research文件夹内的slim复制到object_detection文件夹,在from nets改成from slim.nets

执行成功,就等训练完成吧

完成后,可以看到object_detection/train下生成了模型

在ssd_model文件夹下创建model文件夹

$python object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path object_detection/ssd_model/ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix object_detection/train/model.ckpt-30000 --output_directory object_detection/ssd_model/model/

这样就生成了.pb文件

.txt文件要自己创建,把pascal_label_map.pbtxt里的name复制到自己创建的.txt文件,第一行随便加几个字符,第二行开始添加name,因为取分类name时是从1开始,而不是0

,也可以在java取分类时-1

把生成的frozen_inference_graph.pb文件和自己创建的.txt文件复制到项目

修改DetectorActivity的TF_OD_API_MODEL_FILE和TF_OD_API_LABELS_FILE为自己训练好的模型

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值