前提:ubuntu+tensorflow-gpu+python3.6
各種環境提前配好
1.下載工程源碼
網址:https://github.com/tensorflow/models
下載時會遇到速度過慢或中間因為網絡錯誤停止,可以換移動網絡或者用迅雷下載。
2.測試環境
先添加slim路徑,每次打開terminal都要加載路徑
# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
運行測試1
# From tensorflow/models/research/
python deeplab/model_test.py
測試2
# From tensorflow/models/research/deeplab
sh local_test.sh
3.處理數據標簽
處理標簽為單通道,運行下面代碼即可
import numpy as np from PIL import Image from keras.preprocessing.image import load_img, img_to_array import os classes = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'dining table', 'dog', 'horse', 'motorbike', 'person', 'potted plant', 'sheep', 'sofa', 'train', 'tv/monitor'] colormap = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128], [128, 0, 128], [0, 128, 128], [128, 128, 128], [64, 0, 0], [192, 0, 0], [64, 128, 0], [192, 128, 0], [64, 0, 128], [192, 0, 128], [64, 128, 128], [192, 128, 128], [0, 64, 0], [128, 64, 0], [0, 192, 0], [128, 192, 0], [0, 64, 128]] # 利用下面的代碼,將標注的圖片轉換為單通道的label圖像 cm2lbl = np.zeros(256**3) for i, cm in enumerate(colormap): cm2lbl[(cm[0]*256+cm[1])*256+cm[2]] = i def image2label(im): # 輸入為標記圖像的矩陣,輸出為單通道映射的label圖像 data = im.astype('int32') idx = (data[:, :, 0]*256+data[:, :, 1])*256+data[:, :, 2] return np.array(cm2lbl[idx]) def change_label(label_url, label_name): label_img = load_img(label_url) label_img = img_to_array(label_img) label_img = image2label(label_img) # 將圖片映射為單通道數據 print(np.max(label_img)) label_single = Image.fromarray(label_img) label_single = label_single.convert('L') save_path = './datasets/VOC2012/Label' save_path = os.path.join(save_path, label_name) # 確定保存路徑及名稱 label_single.save(save_path) val_file_path = './datasets/VOC2012/ImageSets/trainval.txt' # 文件名存放路徑 label_file_path = './datasets/VOC2012/SegmentationClass' # 原label存放路徑 with open(val_file_path, 'r') as f: file_names = f.readlines() count = 0 for name in file_names: count += 1 name = name.strip('\n') # 去掉換行符 label_name = name + '.png' # label文件名 label_url = os.path.join(label_file_path, label_name) print('這是第 %s 張' % count) print(label_url) change_label(label_url, label_name)
4.運行build_voc2012_data.py 生成 .tfrecord數據
