因爲如今的教程大都是linux環境下deeplabv3+的實現,而且不少都是使用的voc數據集,所以本人在windows中使用cityscapes數據集訓練deeplabv3+的過程當中遇到了不少問題,查閱了不少前輩和大佬的博文才可以實現,在此我對整個訓練過程當中遇到的問題進行了整理。因爲問題較多,沒有分先問題出現的前後問題。linux
在最開始下載deeplab源碼時,選擇不一樣branch可能會致使不一樣問題,參見Issue #6567。本人最開始選擇了master
branch,最後出現 eval.py 和 vis.py
不出結果,及tensorflow:Waiting for new checkpoint at ...問題,切換r1.12.0
branch可用。ios
在此首先提醒各位讀者,在實現deeplab以前必定要先閱讀官方提供的文檔以及文件內容,從官網下載下來的源代碼中默認的大都是基於voc數據集的,而且不能像在linux中使用命令直接設置參數,所以不少參數須要咱們手動去修改,不然咱們會走不少彎路,遇到各式各樣的問題。git
官方提供的cityscapes數據集訓練教程:https://github.com/tensorflow/models/blob/r1.12.0/research/deeplab/g3doc/cityscapes.mdgithub
參數設置:windows
1.train.pysession
其中 model_variant 在common文件中,將其修改成 xception_65 的同時將 decoder_output_stride 設置爲4。ide
在訓練時,batch_size和 crop_size 要根據本身的電腦顯存而定,因爲本人機子較爲落後,2g的獨顯,所以將 train_batch_size 設置爲1,fine_tune_batch_norm 設置爲false,train_crop_size設置爲[321,321],其中train_crop_size最小爲321。若是僅爲測試,training_number_of_steps 能夠設置小一點,好比1000,不然會訓練很長時間。測試
tf_initial_checkpoint 爲預訓練模型路徑,可在 https://github.com/tensorflow/models/blob/r1.12.0/research/deeplab/g3doc/model_zoo.md 中下載,大小爲439兆。
ui
train_logdir 爲檢查點保存路徑,使用官方提供的目錄結構可保存在 cityscapes/exp/train_on_train_set/train 目錄中。spa
使用 xception_65 將 output_stride 設置爲16,atrous_rates 設置爲 [6, 12, 18]。
dataset 改成 cityscapes。dataset_dir 爲讀取數據集的路徑,及tfrecord保存路徑。
2.eval.py 和 vis.py
這兩個文件中的大部分參數和train.py保持一致,個別參數在下方做出說明:
checkpoint_dir 爲檢查點的路徑,及train.py中的 cityscapes/exp/train_on_train_set/train 目錄。
eval_logdir 和 vis_logdir 爲寫入評估事件的目錄,分別保存在 cityscapes/exp/train_on_train_set/eval 和 cityscapes/exp/train_on_train_set/vis 中。
eval_crop_size 和 vis_crop_size 設置爲讀入圖片的大小,cityscapes數據集爲[1025,2049]。
其餘問題及解決方法:
問題:ModuleNotFoundError: No module named 'nets' 和 No module named 'deployment'
在運行model_test和train時會現,這兩個文件在models/research文件夾下,將其添加到環境中便可。或者直接將其中用到的文件複製到外部庫中。
問題:InvalidArgumentError (see above for traceback): padded_shape[0]=49 is not divisible by block_shape[0]=2
官方默認給的crop_size爲[1025,2049]爲測試的原圖片的大小,若是將其更改可能會出現此問題。
問題:data split name train not recognized
此問題出如今master分支中,出現的緣由爲代碼中已經沒有「train」這個變量,而是train_fine,後面的eval和vis同理。此時須要把生成的tfrecord文件名修改一下
改成
若是使用r1.12.0分支則沒有此問題。
問題:OOM when allocating tensor with shape ... and type ...
出現緣由:顯卡內存不夠,可將batch_size或crop_size調小
問題:lhs shape= [1,1,512,256] rhs shape= [1,1,1280,256]
出現緣由多是因爲export_model中atrous_rates參數沒有設置
問題:tensorflow:Waiting for new checkpoint at ...
master分支下運行eval和vis出現的問題,具體緣由不清楚,可以使用r1.12.0分支下的源代碼
使用導出的模型進行測試:
其中官方給出了deeplab_demo.ipynb,你們能夠將其轉換爲 py 文件,或從網上直接查詢其 py 源代碼,將其中圖片路徑和模型路徑修改成本身本地的存儲目錄。並將其中類別和顏色修改成cityscapes數據集的。具體可參考 http://www.javashuo.com/article/p-czuurysk-hm.html
修改後的文件:
1 # -*- coding: utf-8 -*- 2 import os 3 4 from matplotlib import gridspec 5 from matplotlib import pyplot as plt 6 import numpy as np 7 from PIL import Image 8 9 import tensorflow as tf 10 from tensorflow import ConfigProto 11 from tensorflow import InteractiveSession 12 13 config = ConfigProto() 14 config.gpu_options.allow_growth = True 15 session = InteractiveSession(config=config) 16 17 18 #這個地方指定輸出的模型路徑 19 TEST_PB_PATH = './output_model/frozen_inference_graph.pb' 20 21 #這個地方指定須要測試的圖片 22 TEST_IMAGE_PATH = "./image/1.jpg" 23 24 25 class DeepLabModel(object): 26 INPUT_TENSOR_NAME = 'ImageTensor:0' 27 OUTPUT_TENSOR_NAME = 'SemanticPredictions:0' 28 INPUT_SIZE = 513 29 FROZEN_GRAPH_NAME = 'frozen_inference_graph' 30 31 def __init__(self): 32 self.graph = tf.Graph() 33 34 graph_def = None 35 36 with open(TEST_PB_PATH, 'rb') as fhandle: 37 graph_def = tf.GraphDef.FromString(fhandle.read()) 38 39 if graph_def is None: 40 raise RuntimeError('Cannot find inference graph in tar archive.') 41 42 with self.graph.as_default(): 43 tf.import_graph_def(graph_def, name='') 44 45 self.sess = tf.Session(graph=self.graph) 46 47 def run(self, image): 48 width, height = image.size 49 resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height) 50 target_size = (int(resize_ratio * width), int(resize_ratio * height)) 51 resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS) 52 batch_seg_map = self.sess.run( 53 self.OUTPUT_TENSOR_NAME, 54 feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]}) 55 seg_map = batch_seg_map[0] 56 return resized_image, seg_map 57 58 59 def create_pascal_label_colormap(): 60 return np.asarray([ 61 [128, 64, 128], 62 [244, 35, 232], 63 [70, 70, 70], 64 [102, 102, 156], 65 [190, 153, 153], 66 [153, 153, 153], 67 [250, 170, 30], 68 [220, 220, 0], 69 [107, 142, 35], 70 [152, 251, 152], 71 [70, 130, 180], 72 [220, 20, 60], 73 [255, 0, 0], 74 [0, 0, 142], 75 [0, 0, 70], 76 [0, 60, 100], 77 [0, 80, 100], 78 [0, 0, 230], 79 [119, 11, 32], 80 ]) 81 82 83 def label_to_color_image(label): 84 if label.ndim != 2: 85 raise ValueError('Expect 2-D input label') 86 87 colormap = create_pascal_label_colormap() 88 89 if np.max(label) >= len(colormap): 90 raise ValueError('label value too large.') 91 92 return colormap[label] 93 94 95 def vis_segmentation(image, seg_map): 96 plt.figure(figsize=(15, 5)) 97 grid_spec = gridspec.GridSpec(1, 4, width_ratios=[6, 6, 6, 1]) 98 99 plt.subplot(grid_spec[0]) 100 plt.imshow(image) 101 plt.axis('off') 102 plt.title('input image') 103 104 plt.subplot(grid_spec[1]) 105 seg_image = label_to_color_image(seg_map).astype(np.uint8) 106 plt.imshow(seg_image) 107 plt.axis('off') 108 plt.title('segmentation map') 109 110 plt.subplot(grid_spec[2]) 111 plt.imshow(image) 112 plt.imshow(seg_image, alpha=0.7) 113 plt.axis('off') 114 plt.title('segmentation overlay') 115 116 unique_labels = np.unique(seg_map) 117 ax = plt.subplot(grid_spec[3]) 118 plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation='nearest') 119 ax.yaxis.tick_right() 120 plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels]) 121 plt.xticks([], []) 122 ax.tick_params(width=0.0) 123 plt.grid('off') 124 plt.show() 125 126 127 LABEL_NAMES = np.asarray([ 128 'road', 'sidewalk', 'building', 'wall', 'fence', 'pole', 'traffic light', 129 'traffic sign', 'vegetation', 'terrain', 'sky', 'person', 'rider', 'car', 'truck', 130 'bus', 'train', 'motorcycle', 'bicycle' 131 ]) 132 133 FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1) 134 FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP) 135 136 137 MODEL = DeepLabModel() 138 print('model loaded successfully!') 139 140 141 def run_visualization(path): 142 oringnal_im = Image.open(path) 143 print('running deeplab on image %s...' % path) 144 resized_im, seg_map = MODEL.run(oringnal_im) 145 vis_segmentation(resized_im, seg_map) 146 147 run_visualization(TEST_IMAGE_PATH)