1.deeplab 源码地址 https://github.com/tensorflow/models 在reserach/deeplab
(1)下载源码
git clone https://github.com/tensorflow/models.git
(2)在/research/路径下,加入slim路径
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
source ~/.bashrc
(3)测试
python deeplab/model_test.py
如果出现
表示配置成功
2.制作数据:参考(https://blog.csdn.net/malvas/article/details/90776327)
(1)labelme
下载地址:https://github.com/wkentaro/labelme
(2)标注需要分割的物体保存为json格式文件
(3)把json转换为voc格式
找到路径:\labelme-master\examples\semantic_segmentation
把自己的数据放在data_annotated文件中,把里面原来的数据删除。然后把labels.txe改成自己的类别
然后执行
python labelme2voc.py data_annotated data_dataset_voc --labels labels.txt
会生成data_dataset_voc文件夹,其包含了
Convert to 灰度图
在下载后的文件中,找到models/research/deeplab/datasets
# from models/research/deeplab/datasets
python remove_gt_colormap.py \
--original_gt_folder="/path/SegmentationClassPNG" \
--output_dir="/path/SegmentationClassRaw"
Convert to tfrecord
# from /root/models/research/deeplab/datasets/
python ./build_voc2012_data.py \
--image_folder="/root/data/image" \
--semantic_segmentation_folder="/root/data/mask" \
--list_folder="/root/data/index" \
--image_format="jpg" \
--output_dir="/root/data/tfrecord"
3.训练
(1)修改训练文件
在models-master\research\deeplab\deprecated\segmentation_dataset.py中大概110 添加一段代码
_MYDATA_INFORMATION = DatasetDescriptor(
splits_to_sizes={
'train': 1500, # 训练集数量
'val': 300, # 测试集数量
},
num_classes=4,
ignore_label=255,
)
然后在113行把自己的数据集添加进去
_DATASETS_INFORMATION = {
'cityscapes': _CITYSCAPES_INFORMATION,
'pascal_voc_seg': _PASCAL_VOC_SEG_INFORMATION,
'ade20k': _ADE20K_INFORMATION,
'mydata':_MYDATA_INFORMATION # 添加自己的数据集
}
在\models-master\research\deeplab\utils\train_utils.py中210行修改
# Variables that will not be restored.
exclude_list = ['global_step','logits']
if not initialize_last_layer:
exclude_list.extend(last_layers)
在\models-master\research\deeplab\trian.py中大概157行修改
flags.DEFINE_boolean('initialize_last_layer', False,
'Initialize the last layer.')
flags.DEFINE_boolean('last_layers_contain_logits_only', True,
'Only consider logits as last layers or not.')
训练指令
# from /root/models/research/
python deeplab/train.py \
--logtostderr \
--num_clones=1 \
--training_number_of_steps=100000 \
--train_split="train" \
--model_variant="xception_71" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--train_crop_size=513,513
--train_batch_size=3\
--dataset="mydata" \
--fine_tune_batch_norm=True \
--tf_initial_checkpoint='./deeplab/backbone/xception_71/model.ckpt'
--train_logdir='./deeplab/train/'
--dataset_dir='./deeplab/data/tfrecord/'
其中model_variant 是你使用预训练的模型,下载地址为:https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/model_zoo.md
...
INFO:tensorflow:global step 98250: loss = 1.9128 (0.731 sec/step)
INFO:tensorflow:global step 98260: loss = 3.2374 (0.740 sec/step)
INFO:tensorflow:global step 98270: loss = 1.3137 (0.736 sec/step)
INFO:tensorflow:global step 98280: loss = 3.3541 (0.732 sec/step)
INFO:tensorflow:global step 98290: loss = 1.1512 (0.740 sec/step)
INFO:tensorflow:global step 98300: loss = 1.8416 (0.735 sec/step)
INFO:tensorflow:global step 98310: loss = 1.5447 (0.753 sec/step)
...
4.测试
(1)指令
python deeplab/vis.py \
--logtostderr \
--vis_split="val" \
--model_variant="xception_71" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--vis_crop_size=512,640
--dataset="mydata" \
--colormap_type="pascal" \
--checkpoint_dir='./deeplab/train/' \
--vis_logdir='./deeplab/vis/' \
--dataset_dir='/deeplab/data/tfrecord/'
注意填写真实图片的大小,宽度在前,高度在后
数据集自提:
5.模型保存成pb格式(参考:http://www.pythonheidong.com/blog/article/10596/#deeplab_pdfrozen_to_pd)
python deeplab/export_model.py \
--logtostderr \
--checkpoint_path="./deeplab/train/model.ckpt-30000" \
--export_path="./model.pb" \
--model_variant="xception_71" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--num_classes=3 \
--crop_size=512,640
--inference_scales=1.0
图片大小为,真是预测图片大小
import tensorflow as tf
import numpy as np
import cv2 as cv
import os
from keras.preprocessing.image import load_img, img_to_array
from matplotlib import pyplot as plt
img_path = "..." # 原图存放文件夹路径
graph_path = ".../.../.pb" # .pd模型路径
pre_path = "..." # 预测出的图片存放位置
graph = tf.Graph()
INPUT_TENSOR_NAME = 'ImageTensor:0'
OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
graph_def = None
with tf.gfile.FastGFile(graph_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
if graph_def is None:
raise RuntimeError('Cannot find inference graph in tar archive.')
with graph.as_default():
tf.import_graph_def(graph_def, name='')
sess = tf.Session(graph=graph)
for filename in os.listdir(img_path):
prename = filename[0:-4] + ".png" #预测输出保存为 .png格式
file_path = ori_path + "/" + filename
save_path = pre_path + '/' + prename
img = load_img(file_path)
img = img_to_array(img)
img = np.expand_dims(img, axis=0).astype(np.uint8)
result = sess.run(
OUTPUT_TENSOR_NAME,
feed_dict={INPUT_TENSOR_NAME: img})
cv.imwrite(save_path, result.transpose((1, 2, 0)))
预测的图片是全黑色,数值乘以100可以显示
# -*- coding: utf-8 -*-
# @Time : 2019/11/20 14:22
# @Author : Don
# @File : read.py
# @Software: PyCharm
import cv2
img=cv2.imread("2.png")
img=img*100
cv2.namedWindow('contours', 0)
cv2.imshow("contours", img)
cv2.waitKey(0)
cv2.destroyAllWindows()