python
1import os 2import re 3import tensorflow as tf 4 5base_dir='/tmp/imagenet' 6 7def main(argv=None): 8 node_lookup=node_dict() 9 with tf.gfile.FastGFile(os.path.join(base_dir,'classify_image_graph_def.pb'),'rb') as f: 10 graph_def=tf.GraphDef() 11 graph_def.ParseFromString(f.read()) 12 tf.import_graph_def(graph_def,name='') 13 image_data=tf.gfile.FastGFile(os.path.join(base_dir,'cropped_panda.jpg'),'rb').read() 14 with tf.Session() as sess: 15 softmax_tensor=sess.graph.get_tensor_by_name('softmax:0') 16 predictions=sess.run(tf.squeeze(softmax_tensor),feed_dict={ 17 'DecodeJpeg/contents:0':image_data 18 }) 19 top_k=predictions.argsort()[-3:][::-1] 20 for none_id in top_k: 21 human_string=node_lookup[node_id] 22 score=predictions[node_id] 23 print('%s(score=%.5f)'%(human_string,score)) 24 25def node_dict(): 26 label_lookup_path=os.path.join(base_dir,'imagenet_2012_challenge_label_map_proto.pbtxt') 27 uid_lookup_path=os.path.join(base_dir,'imagenet_synset_to_human_label_map.txt') 28 29 uid_to_human={} 30 p=re.compile(r'[n¥d]*[¥S,]*') 31 for line in tf.gfile.GFile(uid_lookup_path).readline(): 32 parsed_items=p.findall(line) 33 uid=parsed_items[0] 34 human_string=parsed_items[2] 35 uid_to_human[uid]=human_string 36 node_id_to_uid={} 37 for lie in tf.gfile.GFile(label_lookup_path).readline(): 38 if line.startswith('target_class:'): 39 target_class=int(line.split(':')[1]) 40 if line.startswith('target_class_string:'): 41 target_class_string=line.split(':')[1] 42 node_id_to_uid[target_class]=target_class_string[1:-2] 43 node_id_to_name={} 44 for key,val in node_id_to_uid.items(): 45 name=uid_to_human[val] 46 node_id_to_name[key]=name 47 return node_id_to_name 48 49if __name__=='__main__': 50 tf.app.run() 51
画像認識アプリを作ろうとしていましたが、以下のようなエラーが発生してしまいました。
File "inception.py", line 8, in main
node_lookup=node_dict()
File "inception.py", line 34, in node_dict
human_string=parsed_items[2]
IndexError: list index out of range
こんな感じでエラーが出てしまいます。
どうしたら良いですか?