程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python deep learning tensorflow trained model for image classification

編輯:Python

Catalog

Text

Find a random picture

Read pictures for classification and recognition

The final output

Text

Google is in the big image database ImageNet I trained one last week Inception-v3 Model , This model can be directly used for image classification .

Download link : https://pan.baidu.com/s/1XGfwYer5pIEDkpM3nM6o2A

Extraction code : hu66

After downloading and decompressing , Get a few files :

among

classify_image_graph_def.pb Documents are trained Inception-v3 Model .

imagenet_synset_to_human_label_map.txt Is a category file .

Find a random picture

Identify this picture , See what kind it belongs to ?

The code is as follows : First create a class NodeLookup to softmax The probability value is mapped to the label .

Then create a function create_graph() To read the model .

Read pictures for classification and recognition # -*- coding: utf-8 -*-import tensorflow as tfimport numpy as npimport reimport osmodel_dir='D:/tf/model/'image='d:/cat.jpg'# Put the categories ID Convert to human readable labels class NodeLookup(object): def __init__(self, label_lookup_path=None, uid_lookup_path=None): if not label_lookup_path: label_lookup_path = os.path.join( model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt') if not uid_lookup_path: uid_lookup_path = os.path.join( model_dir, '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): if not tf.gfile.Exists(uid_lookup_path): tf.logging.fatal('File does not exist %s', uid_lookup_path) if not tf.gfile.Exists(label_lookup_path): tf.logging.fatal('File does not exist %s', label_lookup_path) # Loads mapping from string UID to human-readable string proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines() uid_to_human = {} p = re.compile(r'[n\d]*[ \S,]*') for line in proto_as_ascii_lines: parsed_items = p.findall(line) uid = parsed_items[0] human_string = parsed_items[2] uid_to_human[uid] = human_string # Loads mapping from string UID to integer node ID. node_id_to_uid = {} proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines() 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] # Loads the final mapping of integer node ID to human-readable string node_id_to_name = {} for key, val in node_id_to_uid.items(): if val not in uid_to_human: tf.logging.fatal('Failed to locate: %s', val) 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]# Read the trained Inception-v3 Model to create graphdef create_graph(): with tf.gfile.FastGFile(os.path.join( model_dir, 'classify_image_graph_def.pb'), 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='')# Read the picture image_data = tf.gfile.FastGFile(image, 'rb').read()# establish graphcreate_graph()sess=tf.Session()#Inception-v3 The last layer of the model softmax Output softmax_tensor= sess.graph.get_tensor_by_name('softmax:0')# Input image data , obtain softmax Probability value ( One shape=(1,1008) Vector )predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})#(1,1008)->(1008,)predictions = np.squeeze(predictions)# ID --> English string label.node_lookup = NodeLookup()# Before removal 5 The value with the highest probability (top-5)top_5 = predictions.argsort()[-5:][::-1]for node_id in top_5: human_string = node_lookup.id_to_string(node_id) score = predictions[node_id] print('%s (score = %.5f)' % (human_string, score))sess.close() The final output

tiger cat (score = 0.40316)
Egyptian cat (score = 0.21686)
tabby, tabby cat (score = 0.21348)
lynx, catamount (score = 0.01403)
Persian cat (score = 0.00394)

That's all python Deep learning tensorflow The details of image classification with the trained model , More about tensorflow For information on training model image classification, please pay attention to other relevant articles on the software development network !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved