全程参考:https://www.jianshu.com/p/1a07990705ee
环境-实验室电脑-D:\models-master\research\deeplab,tensorflow-GPU,tensoflow1.13.1,pycharm,python3.6.6,win10,
教程是针对老版本的tensorflow model,官网现下是最新的,所以跑的时候遇到了很多问题。最终还是有一些未解决,直接换了老版本的models,最终顺利跑通。
新版本(针对tensorflow2):官网下载https://github.com/tensorflow/models
老版本(针对教程是第二个):https://blog.csdn.net/qq_27882063/article/details/85127756
在跑最新版时遇到几个问题,记录一下。下次有时间跑一下最新版本。
(这似乎是一个常见问题,跑老版时可能也受益于这个问题的解决)
参考博客:
https://blog.csdn.net/hdu2013/article/details/81177563
SET PYTHONPATH=%cd%;%cd%\slim
//有效,但每次都需要设置
参考:https://bbs.aianaconda.com/thread-790-1-1.html
slim版本更新,用老的slim模块替换
//用了这个,出现了问题③,换方法三
File “D:\models-master\research\slim\nets\mobilenet\mobilenet.py”,
line 399, in def global_pool(input_tensor, pool_op=tf.compat.v2.nn.avg_pool2d): AttributeError: module ‘tensorflow.compat’ has no attribute ‘v2’
将【tf.compat.v2.nn.avg_pool2d】改成【tensorflow.contrib.layers.avg_pool2d()】
//用了这个可行,但又出现问题④。。。
参考:https://blog.csdn.net/yeler082/article/details/82933178
//偶然看到的一个方法,未尝试
//上面的问题的衍生,未解决,直接跳过
File “D:\models-master\research\deeplab\datasets\data_generator.py”,
line 290, in _preprocess_image
crop_width=self.crop_size[1],IndexError: list index out of range
版本问题,教程分开传值,新版本合并传值。将X-Train.bat中的这部分修改为【-–train_crop_size=513,513】
参考:https://blog.csdn.net/xjtdw/article/details/92848032
//X-Train.bat训练成功了
只找到对应代码块【evaluate_repeatedly】,并尝试修改,未成功。此时才意识到tensormodle版本问题。。。新手伤不起。
//鉴于后续可能还会有很多问题,决定把模型换成老版本
最终都顺利运行了。
待解决:训练自己的数据集。
①在cmd命令行敲入:tensorboard --logdir=log地址
这里是:
tensorboard --logdir=D:\111learning\deeplabv3\models-master\research\deeplab
②打开浏览器输入端口号访问,这里是:
http://localhost:6006
X-remove_gt_colormap.bat 在D:\models-master\research\deeplab\datasets
echo "Removing the color map in ground truth annotations..."
python remove_gt_colormap.py ^
--original_gt_folder="./pascal_voc_seg/VOCdevkit/VOC2012/SegmentationClass" ^
--output_dir="./pascal_voc_seg/VOCdevkit/VOC2012/SegmentationClassRaw"
PAUSE
X-build_voc2012_data.bat在D:\models-master\research\deeplab\datasets
echo "Converting PASCAL VOC 2012 dataset..."
python build_voc2012_data.py ^
--image_folder=".\pascal_voc_seg\VOCdevkit\VOC2012\JPEGImages" ^
--semantic_segmentation_folder="./pascal_voc_seg/VOCdevkit/VOC2012/SegmentationClassRaw" ^
--list_folder="./pascal_voc_seg/VOCdevkit/VOC2012/ImageSets/Segmentation" ^
--image_format="jpg" ^
--output_dir="./pascal_voc_seg/tfrecord"
PAUSE
X-Train.bat 在D:\models-master\research\deeplab
python train.py ^
--logtostderr ^
--training_number_of_steps=30000 ^
--train_split="train" ^
--model_variant="xception_65" ^
--atrous_rates=6 ^
--atrous_rates=12 ^
--atrous_rates=18 ^
--output_stride=16 ^
--decoder_output_stride=4 ^
--train_crop_size=513 ^
--train_crop_size=513 ^
--train_batch_size=1 ^
--fine_tune_batch_norm=False ^
--dataset="pascal_voc_seg" ^
--tf_initial_checkpoint=.\model.ckpt ^
--train_logdir=../deeplab ^
--dataset_dir=./datasets/pascal_voc_seg/tfrecord
X-Eval.bat 在D:\models-master\research\deeplab
python eval.py ^
--logtostderr ^
--eval_split="val" ^
--model_variant="xception_65" ^
--atrous_rates=6 ^
--atrous_rates=12 ^
--atrous_rates=18 ^
--output_stride=16 ^
--decoder_output_stride=4 ^
--train_crop_size=513 ^
--train_crop_size=513 ^
--dataset="pascal_voc_seg" ^
--checkpoint_dir=.\model.ckpt-9000 ^
--eval_logdir=../deeplab ^
--dataset_dir=./datasets/pascal_voc_seg/tfrecord
修改eval.py
slim.evaluation.evaluate_once(
master=FLAGS.master,
checkpoint_path=FLAGS.checkpoint_dir,
logdir=FLAGS.eval_logdir,
num_evals=num_batches,
)
修改vis.py
# last_checkpoint = slim.evaluation.wait_for_new_checkpoint(
# FLAGS.checkpoint_dir, last_checkpoint)
#修改后的代码
last_checkpoint = FLAGS.checkpoint_dir
X-Vis.bat 在D:\models-master\research\deeplab
python vis.py ^
--logtostderr ^
--vis_split="val" ^
--model_variant="xception_65" ^
--atrous_rates=6 ^
--atrous_rates=12 ^
--atrous_rates=18 ^
--output_stride=16 ^
--decoder_output_stride=4 ^
--train_crop_size=513 ^
--train_crop_size=513 ^
--dataset="pascal_voc_seg" ^
--checkpoint_dir=.\model.ckpt-9000 ^
--vis_logdir=../deeplab ^
--dataset_dir=./datasets/pascal_voc_seg/tfrecord
PAUSE
X-export_model.bat 在D:\models-master\research\deeplab
python export_model.py ^
--checkpoint_path=.\model.ckpt-9000 ^
--export_path=.\XOut\frozen_inference_graph.pb ^
--model_variant="xception_65" ^
--atrous_rates=6 ^
--atrous_rates=12 ^
--atrous_rates=18 ^
--output_stride=16 ^
--decoder_output_stride=4 ^
PAUSE
X_visualization.py 在D:\models-master\research\deeplab
# -*- coding: utf-8 -*-
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
import tensorflow as tf
#这个地方指定输出的模型路径
TEST_PB_PATH = 'XOut/frozen_inference_graph.pb'
#这个地方指定需要测试的图片
TEST_IMAGE_PATH = "D:/test.png"
class DeepLabModel(object):
"""Class to load deeplab model and run inference."""
INPUT_TENSOR_NAME = 'ImageTensor:0'
OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
INPUT_SIZE = 513
FROZEN_GRAPH_NAME = 'frozen_inference_graph'
def __init__(self):
"""Creates and loads pretrained deeplab model."""
self.graph = tf.Graph()
graph_def = None
with open(TEST_PB_PATH, 'rb') as fhandle:
graph_def = tf.GraphDef.FromString(fhandle.read())
if graph_def is None:
raise RuntimeError('Cannot find inference graph in tar archive.')
with self.graph.as_default():
tf.import_graph_def(graph_def, name='')
# config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
# config.gpu_options.allow_growth = True
# config.gpu_options.per_process_gpu_memory_fraction = 0.3
# self.sess = tf.Session(graph=self.graph, config=config)
self.sess = tf.Session(graph=self.graph)
def run(self, image):
""" Runs inference on a single image.
Args:
image: A PIL.Image object, raw input image.
Returns:
resized_image: RGB image resized from original input image.
seg_map: Segmentation map of `resized_image`.
"""
width, height = image.size
resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
target_size = (int(resize_ratio * width), int(resize_ratio * height))
resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
batch_seg_map = self.sess.run(
self.OUTPUT_TENSOR_NAME,
feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
seg_map = batch_seg_map[0]
return resized_image, seg_map
# MODEL = DeepLabModel(INPUT_PATH)
# resized_im, seg_map = MODEL.run(original_im)
# mask_save = change_to_3_channels(seg_map)
# seg_map_show = Image.fromarray(mask_save.astype(np.uint8))
def create_pascal_label_colormap():
"""Creates a label colormap used in PASCAL VOC segmentation benchmark.
Returns:
A Colormap for visualizing segmentation results.
"""
colormap = np.zeros((256, 3), dtype=int)
ind = np.arange(256, dtype=int)
for shift in reversed(range(8)):
for channel in range(3):
colormap[:, channel] |= ((ind >> channel) & 1) << shift
ind >>= 3
return colormap
def label_to_color_image(label):
"""
Adds color defined by the dataset colormap to the label.
Args:
label: A 2D array with integer type, storing the segmentation label.
Returns:
result: A 2D array with floating type. The element of the array
is the color indexed by the corresponding element in the input label
to the PASCAL color map.
Raises:
ValueError: If label is not of rank 2 or its value is larger than color
map maximum entry.
"""
if label.ndim != 2:
raise ValueError('Expect 2-D input label')
colormap = create_pascal_label_colormap()
if np.max(label) >= len(colormap):
raise ValueError('label value too large.')
return colormap[label]
def vis_segmentation(image, seg_map):
"""Visualizes input image, segmentation map and overlay view."""
plt.figure(figsize=(15, 5))
grid_spec = gridspec.GridSpec(1, 4, width_ratios=[6, 6, 6, 1])
plt.subplot(grid_spec[0])
plt.imshow(image)
plt.axis('off')
plt.title('input image')
plt.subplot(grid_spec[1])
seg_image = label_to_color_image(seg_map).astype(np.uint8)
# seg_image = label_to_color_image(seg_map)
# seg_image.save('/str(ss)+imagefile')
plt.imshow(seg_image)
# plt.savefig('./' + imagefile + '.png')
plt.axis('off')
plt.title('segmentation map')
plt.subplot(grid_spec[2])
plt.imshow(image)
plt.imshow(seg_image, alpha=0.7)
plt.savefig('./myresult.png')
plt.axis('off')
plt.title('segmentation overlay') # 添加标题
"""
"""
unique_labels = np.unique(seg_map)
ax = plt.subplot(grid_spec[3])
plt.imshow(
FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation='nearest')
ax.yaxis.tick_right()
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
plt.xticks([], [])
ax.tick_params(width=0.0)
plt.grid('off')
plt.show()
LABEL_NAMES = np.asarray([
'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv'
])
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
MODEL = DeepLabModel()
print('model loaded successfully!')
#------------------------
def run_visualization(path):
oringnal_im = Image.open(path)
print('running deeplab on image %s...' % path)
resized_im, seg_map = MODEL.run(oringnal_im)
vis_segmentation(resized_im, seg_map)
run_visualization(TEST_IMAGE_PATH)
接下来待解决待看
1.博客deeplabV3+运行及训练自己的数据集总结(jupyter) https://blog.csdn.net/gloria971111/article/details/95596301(https://www.jianshu.com/p/1a07990705ee)
https://blog.csdn.net/dou3516/article/details/89418725
2.deeplabV3+源码分解学习 https://www.jianshu.com/p/d0cc35b3f100
3.在tensorflow上用其他数据集训练DeepLabV3+ https://www.jianshu.com/p/dcca31142b993.训练自己的数据集 》》》https://blog.csdn.net/heiheiya/article/details/88535576 https://zhuanlan.zhihu.com/p/42756363
【其他方法https://www.jianshu.com/p/dcca31142b99(在tensorflow上用其他数据集训练DeepLabV3+)】
———————关于其他————————
语义分割之图片和 mask 的可视化 https://www.aiuai.cn/aifarm276.html