资源简介
调用deeplabv3生成的.pb文件,对图片进行批量预测并生成彩色图像。需要将图片尺寸修改为自己图片的大小,将类别与颜色对应图修改为自己的类型。
代码片段和文件信息
#时间:2018.12.10
#作用:调用pb文件批量生成deeplabv3预测图
#输入:图像文件夹,pb模型文件
#输出:文件预测图
#存在问题:矩阵操作繁琐
#软件版本:win10python3.5tensorflow1.6
import tensorflow as tf
from tensorflow.python.platform import gfile
import numpy as np
import os
from PIL import Image
from keras.preprocessing.image import load_img img_to_arrayarray_to_img
Image.MAX_IMAGE_PIXELS = 400000000
imagedir = ‘E:/1204/1211test/cut/‘
savepath = ‘E:/1204/1211test/prediction/‘
imagelist = os.listdir(imagedir)
rgbim = np.zeros((5005003) ‘uint8‘)
R = rgbim[::0]
G = rgbim[::1]
B = rgbim[::2]
img = tf.placeholder(tf.uint8 [1500 500 3] name=“img“)
with open(“E:/models-master/research/deeplab/output/model.pb“ “rb“) as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
output = tf.import_graph_def(graph_def input_map={“ImageTensor:0“: img}
return_elements=[“SemanticPredictions:0“])
sess = tf.Session()
for i in range(0len(imagelist)):
path = os.path.join(imagedirimagelist[i])
filename = os.path.splitext(imagelist[i])[0]
filetype = os.path.splitext(imagelist[i])[1]
print(‘正在处理‘+filename)
image = Image.open(path)
image = np.asarray(image)
image = np.expand_dims(image axis=0).astype(np.uint8)
result = sess.run(output feed_dict = {img:image})
grayim = np.squeeze(result[0])
#将灰度图像转换为rgb图像根据自己的类别数目修改相应的颜色
for k in range(0500):
for j in range(0500):
if grayim[k][j] == 1:
R[kj] = 128
B[kj] = 0
G[kj] = 0
elif grayim[k][j] == 2:
R[kj] = 0
B[kj] = 128
G[kj] = 0
elif grayim[k][j] == 3:
R[kj] = 128
B[kj] = 128
G[kj] = 0
elif grayim[k][j] == 4:
R[kj] = 0
B[kj] = 0
G[kj] = 128
elif grayim[k][j] == 0:
R[kj] = 0
B[kj] = 0
G[kj] = 0
rgbim = Image.fromarray(np.uint8(rgbim))
savedir = os.path.join(savepathfilename + ‘.png‘)
rgbim.save(savedir)
rgbim = np.zeros((5005003) ‘uint8‘)
R = rgbim[::0]
G = rgbim[::1]
B = rgbim[::2]
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2199 2018-12-12 10:55 pre.py
文件 124 2018-12-12 10:58 使用说明.txt
评论
共有 条评论