分享

TensorFlow 第八课 使用inception-v3

 木俊 2018-08-15
/usr/bin/python3.5 /home/mj/test/1.py
Traceback (most recent call last):
File "/home/mj/test/1.py", line 42, in <module>
graph_def.ParseFromString(f.read())
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/lib/io/file_io.py", line 127, in read
pywrap_tensorflow.ReadFromStream(self._read_buf, length, status))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/lib/io/file_io.py", line 95, in _prepare_value
return compat.as_str_any(val)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/compat.py", line 113, in as_str_any
return as_str(value)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/compat.py", line 86, in as_text
return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 1: invalid start byte

Process finished with exit code 1

# -*- coding: utf-8 -*-
import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt
class NodeLookup(object):
def __init__(self):
label_lookup_path='imagenet_2012_challenge_label_map_proto.pbtxt'
uid_lookup_path='imagenet_synset_to_human_label_map.txt'
self.node_lookup=self.load(label_lookup_path,uid_lookup_path)
def load(self,label_lookup_path,uid_lookup_path):
#
proto_as_ascii_lines=tf.gfile.GFile(uid_lookup_path).readlines()
uid_to_human={}
for line in proto_as_ascii_lines:
line=line.strip('\n')
parsed_items=line.split('\t')
uid=parsed_items[0]
uid_to_human[uid]=human_string
proto_as_ascii=tf.gfile.GFile(label_lookup_path).readlines()
node_id_to_uid={}
for line in proto_as_ascii:
if line.startswith(' target_class:'):
target_class=int(line.split(': ')[1])
if line.startswith(' target_class_string: '):
target_class_string=line.split(':')[1]
node_id_to_uid[ target_class ]=target_class_string[1:-2]
node_id_to_name={}
for key,val,in node_id_to_uid.items():
name=uid_to_human[val]
node_id_to_name[key]=name
return node_id_to_name
def id_to_string(self,node_id):
if node_id not in self.node_lookup:
return ' '
return self.node_lookup[node_id]
#创建一个图来放已经训练好的模型
with tf.gfile.FastGFile('classify_image_graph_def.pb') as f:
graph_def=tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def,name='')
with tf.Session() as sess:
softmax_tensor=sess.graph.get_tensor_by_name('softmax:0')
#遍历目录
for root,dirs,files in os.walk('/home/mj/pictureSet/'):
for file in files:
#载入图片
image_data=tf.gfile.FastGFile(os.path.join(root,file),'rb').read()
predictions=sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})
predictions=np.squeeze(predictions)#把结果转换为一维数据
#打印图片路径及其名称
image_path=os.path.join(root,file)
print(image_path)
#显示图片
img=Image.open(image_path)
plt.imshow(img)
plt.axis('off')
plt.show()
#排序
top_k=predictions.argsort()[-5:][::-1]
node_lookup=NodeLookup()
for node_id in top_k:
#获取分类名称
human_string=node_lookup.id_to_string(node_id)
score=predictions[node_id]
print('%s(score=%.5f)'%(human_string,score))
print()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多