打造自己的图像识别模型(1):将准备好的图片转化为tfrecord格式
打造自己的图像识别模型(2):TensorFlow Slim
打造自己的图像识别模型(3):训练数据导出模型,开始使用
上回说的是《打造自己的图像识别模型(2):TensorFlow Slim》,眼瞅着就要开打……开始训练了。
训练数据,使用的文件是slim下的train_image_classifier.py
运行命令如下:
python train_image_classifier.py \ --train_dir=satellite/train_dir \ --dataset_name=satellite \ --dataset_split_name=train \ --dataset_dir=satellite/data \ --model_name=inception_v3 \ --checkpoint_path=satellite/pretrained/inception_v3.ckpt \ --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ --max_number_of_steps=100000 \ --batch_size=32 \ --learning_rate=0.001 \ --learning_rate_decay_type=fixed \ --save_interval_secs=300 \ --save_summaries_secs=2 \ --log_every_n_steps=10 \ --optimizer=rmsprop \ --weight_decay=0.00004
说道说道这些参数的意义
python train_image_classifier.py \ --train_dir=satellite/train_dir \ 保存日志和训练结果 --dataset_name=satellite \ 数据集名称 --dataset_split_name=train \ 数据集分割标志 --dataset_dir=satellite/data \ 数据集放哪儿了 --model_name=inception_v3 \ 使用的模型名称 --checkpoint_path=satellite/pretrained/inception_v3.ckpt \ 就是零件合格那条线,基础半成品,在此基础上继续训练 --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ 中断恢复训练时,不恢复这两层 --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ 微调哪些部分,这里指定V3末端层,不设置的话就是所有 --max_number_of_steps=100000 \ 最大执行步数 --batch_size=32 \ 每步使用的batch数量 --learning_rate=0.001 \ 学习率 --learning_rate_decay_type=fixed \ 学习率是否自动下降 --save_interval_secs=300 \ 每隔300s把模型保存一次,保存的位置就是-train_dir那个地址 --save_summaries_secs=2 \ 每隔2s写入日志,写入位置就是-train_dir那个地址 --log_every_n_steps=10 \ 每隔10步打印训练信息 --optimizer=rmsprop \ 选定的优化器 --weight_decay=0.00004 衰减权重
有点乱,看不明白了,也没有耐心了。
只能记住,然后再去理解和融会贯通。
想想刚学英文单词的时候,beautiful就是漂亮的。
不需要理解,为什么beautiful 9个字母,就代表漂亮的,记住就可以,这就是规定。学习必须有一个先接受的过程。
上面这么多参数,还引发很多概念,倒是可以详细说说。
InceptionV3简单来说就是一个基础模型。有兴趣可以戳 https://blog.csdn.net/whz1861/article/details/78289379
我们的微调的训练,主要是在ImageNet的VGG16基础上进行微调训练,这个InceptionV3就是其模型。
--checkpoint_path=satellite/pretrained/inception_v3.ckpt \ 就是零件合格那条线,基础半成品,在此基础上继续训练 --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ 中断恢复训练时,不恢复这两层 --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ 微调哪些部分,这里指定V3末端层,不设置的话就是所有
参数当中,涉及到了InceptionV3相关值。
第一个已经说过了。说一下第二个和第三个。
VGG16层,其中前面16层学习图片,后面1层输出分类,ImageNet是输出1000个分类,我们是输出6个。
如果我们只微调输出的话,那么就微调InceptionV3的末端层Logits和AuxLogits 。
同理,如果训练过程中关机了,再重新恢复,不能从模型里面恢复这两层,我们有我们微调的数据。
batch_size一批数据的数量。训练的样本是很大的我们这次是4800张图片。
上一篇文章,我们说过,程序比较笨。随便画一条线,假设是正确的。然后拿数据去验证。
验证一下,发现猜对了,就开心。猜错了,就调整一下参数。调整完了,再拿数据验证。
那么,肯定得一批一批的验证,有了结果再调整,再验证下一批。
一批数据多少个,就是这个控制的。
learning_rate学习率。
说学习率之前,先说损失函数。
损失函数是用来计算程序猜测的结果和样本标记的正确答案之间的差距。
程序看到一张图,图+参数得出计算结果。
那么程序计算的结果和标准答案越接近,也就是说损失越接近0,也说明快修炼……训练成功了。
上面这张图,假设θ是参数,J(θ)是损失函数的值。
那么,程序就会往损失小的方向移动。
就像猜价格,小明买了手机,你猜3000,小明说高了,你说2500,小明说高了。
那么这个学习率,就是移动的步幅距离。
学习率太小,你说3000,小明说高了,你说2999.99,小明说你先猜着,我去上个大学。
学习率太大,你说3000,小明说高了,你说1毛,小明说低了。你说2999,小明说我也得去上个大学。
weight_decay衰减权重。
还是猜小明手机价格那个例子。
你说3000,小明说高了。
你降1000,说2000,小明说高了。
你降500, 说1500,小明说低了。
你升200,说1700,小明说低了。
你升50,说1750,小明说差不多。
每次的增幅或者降幅的距离不是固定的,随着慢慢接近真相是衰减的。
运行程序之后,显示如下结果。
根据上面参数的设置,10步一打印信息,300s一保存模型。
我采用的CPU方式训练,训练过程比较慢,一步60s,1000步就是16个小时。
这2000多步就是32个小时。如果是GPU的话,会快不少,因为GPU是并行的。
我们有4800张图片用于训练,还有1200张用于验证。
我们训练完毕之后,在商用之前,要先看看训练的效果。
如何验证,准备一定量的图片,不要使用训练图片,训练图片程序都记下来了。
找程序没有见过的图片,进行验证。
验证的程序位于slim下的eval_image_classifier.py
python eval_image_classifier.py \ --checkpoint_path=satellite/train_dir \ --eval_dir=satellite/eval_dir \ --dataset_name=satellite \ --dataset_split_name=validation \ --dataset_dir=satellite/data \ --model_name=inception_v3
参数说明
--checkpoint_path=satellite/train_dir \ 训练目录,里面保存了训练的参数结果 --eval_dir=satellite/eval_dir \ 执行日志结果 --dataset_name=satellite \ 数据集名称 --dataset_split_name=validation \ 数据集的分割符 --dataset_dir=satellite/data \ 数据集保存的路径 --model_name=inception_v3 模型
运行之后,会输出准确率,如果还可以,那么可以导出使用。
如果准确率太低,还需要继续优化。
运行之后,
在slim文件夹下运行:
python export_inference_graph.py \ --alsologtostderr \ --model_name=inception_v3 \ --output_file=satellite/inception_v3_inf_graph.pb \ --dataset_name satellite
参数都比较易懂,运行之后会在satellite下生成inception_v3_inf_graph.pb文件,是一个自己的v3模型。
然后运行自己编写的freeze_graph.py(提供下载)导出模型。
python freeze_graph.py \ --input_graph slim/satellite/inception_v3_inf_graph.pb \ --input_checkpoint slim/satellite/train_dir/model.ckpt-5271 \ --input_binary true \ --output_node_names InceptionV3/Predictions/Reshape_1 \ --output_graph slim/satellite/frozen_graph.pb
参数说明
python freeze_graph.py \ --input_graph slim/satellite/inception_v3_inf_graph.pb \ 上一步生成的数据 --input_checkpoint slim/satellite/train_dir/model.ckpt-2000 \ 训练的model数据,2000需要查看自己训练到多少步 --input_binary true \ --output_node_names InceptionV3/Predictions/Reshape_1 \ --output_graph slim/satellite/frozen_graph.pb 导出pb数据
运行之后,会在slim/satellite生成frozen_graph.pb。
至此,我们的参数就生成完毕了,这两个文件,就是最终训练后的价值观,药丸子,精华。
我们训练的是卫星图像,6个分类。
训练之后最终生成了frozen_graph.pb文件。
我从地图上,截取了大明湖的图片。
让程序识别这个图片属于什么。
run classify_image_inception_v3.py \ --model_path slim/satellite/frozen_graph.pb \ --label_path data_prepare/pic/label.txt \ --image_file test_dmh.jpg
执行结果。
至此,通过slim工具,对VGG16进行微调训练数据,并得出模型,然后对新图片进行识别,整个过程完成了。
源码下载-打造自己的图像识别模型-全套源码.zip
运行环境:tensorflow 1.9.0, python 3.6