|
这一节我利用训练出的.pb网络测试下自己的图片
新建一个python文件“6deeplab_predictions.py”,输入代码如下
- # 时间:2020.09.14
- # 作用:调用pb文件批量生成deeplabv3预测图
- # 输入:图像文件夹,pb模型文件
- # 输出:文件预测图
- # 注意:原始图片里不要有其他文件,包括.DS_Store、文件夹等。
- import tensorflow as tf
- import numpy as np
- import os
- from PIL import Image
- Image.MAX_IMAGE_PIXELS = 400000000
- image_wide = 400
- image_high = 320
- imagedir = '/Users/zhengyuchun/Desktop/Anaconda/pre_images/'
- pbpath = "./exp/mydata_train/export/frozen_inference_graph.pb"
- imagelist = os.listdir(imagedir)
- rgbim = np.zeros((image_high, image_wide, 3), 'uint8')
- R = rgbim[:, :, 0]
- G = rgbim[:, :, 1]
- B = rgbim[:, :, 2]
- img = tf.placeholder(tf.uint8, [1, image_high, image_wide, 3], name="img")
- with open(pbpath, "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(0, len(imagelist)):
- path = os.path.join(imagedir, imagelist[i]) # 带文件名的路径
- filename = os.path.splitext(imagelist[i])[0] # 文件名
- filetype = os.path.splitext(imagelist[i])[1] # 后缀".jpg"
- print('正在处理 '+path)
- 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(0, image_high):
- for j in range(0, image_wide):
- if grayim[k][j] == 1:
- R[k, j] = 128
- B[k, j] = 0
- G[k, j] = 0
- elif grayim[k][j] == 0:
- R[k, j] = 0
- B[k, j] = 0
- G[k, j] = 0
- rgbim = Image.fromarray(np.uint8(rgbim))
- savedir = os.path.join(imagedir, filename + '.png')
- rgbim.save(savedir)
- rgbim = np.zeros((image_high,image_wide, 3), 'uint8')
- R = rgbim[:, :, 0]
- G = rgbim[:, :, 1]
- B = rgbim[:, :, 2]
复制代码
运行就可以了,切记源图像不能是8bit的图片,而且源文件夹不能有其他文件和文件夹(文件夹下ls -la查看是否有其他文件,比如:包括.DS_Store、文件夹等)。
运行结果如下
可以看出,预测还是很准确的。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|