基于tensorflow1.9+gpu可分类图片
import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt
lines=tf.gfile.GFile("/path/classify_mission/retrain/").readlines()
uid_to_human={}#一行一行读取数据,就字典的形式存储那些文件夹的名称(也就是下面分类的结果)
for uid,line in enumerate(lines):#去掉换行符line=line.strip('n')uid_to_human[uid]=linedef id_to_string(node_id):if node_id not in uid_to_human:return ''return uid_to_human[node_id]#创建一个图来保存google训练好的模型,经过自己处理得到的pb
with tf.gfile.FastGFile("/path/classify_mission/retrain/output_graph.pb","rb") as f:graph_def=tf.GraphDef()graph_def.ad())tf.import_graph_def(graph_def,name='')with tf.Session() as sess:softmax_tensor_tensor_by_name('final_result:0')#查tensorboard可去E盘的tmp查#更8.4一样for root,dirs,files in os.walk("/path/classify_mission/retrain/images"):for file in files:image_data=tf.gfile.FastGFile(os.path.join(root,file),'rb').read()#把图片传进去我们创好的图进行计算,计算得到的结果是个二维数组要把它转成一维,它就是inception的softmax得到的概率而已predictions=sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})#格式为JPg#把结果转为1维的predictions=np.squeeze(predictions)image_path=os.path.join(root,file)print(image_path)a = plt.imread(image_path)plt.imshow(a)#plt.axis('off')plt.show()#-1就从概率从高到低排序,返回下标top_k=predictions.argsort()[::-1]print(top_k)for node_id in top_k:human_string=id_to_string(node_id)score=predictions[node_id]print("%s (score=%.5f)" % (human_string,score))print()
本文发布于:2024-01-30 16:52:07,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170660473021464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |