完整⼯程,deeplabv3+(tensorflow)代码全理解及其运⾏过程,长期更新前提:ubuntu+tensorflow-gpu+python3.6
各种环境提前配好
1.下载⼯程源码
⽹址:/tensorflow/models
下载时会遇到速度过慢或中间因为⽹络错误停⽌,可以换移动⽹络或者⽤迅雷下载。
2.测试环境
北宋皇帝列表先添加slim路径,每次打开terminal都要加载路径
# From tensorflow/models/rearch/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
运⾏测试1
# From tensorflow/models/rearch/
python deeplab/model_test.py
测试2
# From tensorflow/models/rearch/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
class = ['background', 'aeroplane', 'bicycle', 'bird', 'boat',
品质控制
'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'dining table',
'dog', 'hor', '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 = vert('L')
德国西门子save_path = './datats/VOC2012/Label'
save_path = os.path.join(save_path, label_name) # 确定保存路径及名称
label_single.save(save_path)
val_file_path = './datats/VOC2012/' # ⽂件名存放路径
label_file_path = './datats/VOC2012/SegmentationClass' # 原label存放路径
with open(val_file_path, 'r') as f:
file_names = f.readlines()
count = 0
for name in file_names:
esp是什么
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数据
5. 运⾏train.py训练模型