前提・実現したいこと
TensorFlowのInception-v3で画像を分類してみた(Python API編)にしたがって、
TensorFlowで画像分析をしようとしています。
実行環境はGPUではなくCPUです。
発生している問題・エラーメッセージ
TensorFlowの公式サイトに書かれているように、
PythonAPIをGitHubからcloneして実行すると
サンプルのパンダの画像では以下の結果が得られるのですが、上位5件だけでなく、
画像の色など他の属性(パンダの場合は白と黒など)も抽出することが可能かのか、
そして可能であればどのようにすればいいのか知りたいです。
bash
1classify_image.py
結果
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.88493) indri, indris, Indri indri, Indri brevicaudatus (score = 0.00878) lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00317) custard apple (score = 0.00149) earthstar (score = 0.00127)
試したこと
classify_image.pyの中身をcat
で確認しました。
node_lookup = NodeLookup() top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1] 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)) .... FLAGS, unparsed = parser.parse_known_args() tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
この辺りが関係ありそうだと感じたのですが、APIなのでどこまで手を加えられるのか分かっていない状態です・
python
1from __future__ import absolute_import 2from __future__ import division 3from __future__ import print_function 4 5import argparse 6import os.path 7import re 8import sys 9import tarfile 10 11import numpy as np 12from six.moves import urllib 13import tensorflow as tf 14 15FLAGS = None 16 17# pylint: disable=line-too-long 18DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz' 19# pylint: enable=line-too-long 20 21 22class NodeLookup(object): 23 """Converts integer node ID's to human readable labels.""" 24 25 def __init__(self, 26 label_lookup_path=None, 27 uid_lookup_path=None): 28 if not label_lookup_path: 29 label_lookup_path = os.path.join( 30 FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt') 31 if not uid_lookup_path: 32 uid_lookup_path = os.path.join( 33 FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt') 34 self.node_lookup = self.load(label_lookup_path, uid_lookup_path) 35 36 def load(self, label_lookup_path, uid_lookup_path): 37 """Loads a human readable English name for each softmax node. 38 39 Args: 40 label_lookup_path: string UID to integer node ID. 41 uid_lookup_path: string UID to human-readable string. 42 43 Returns: 44 dict from integer node ID to human-readable string. 45 """ 46 if not tf.gfile.Exists(uid_lookup_path): 47 tf.logging.fatal('File does not exist %s', uid_lookup_path) 48 if not tf.gfile.Exists(label_lookup_path): 49 tf.logging.fatal('File does not exist %s', label_lookup_path) 50 51 # Loads mapping from string UID to human-readable string 52 proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines() 53 uid_to_human = {} 54 p = re.compile(r'[n\d]*[ \S,]*') 55 for line in proto_as_ascii_lines: 56 parsed_items = p.findall(line) 57 uid = parsed_items[0] 58 human_string = parsed_items[2] 59 uid_to_human[uid] = human_string 60 61 # Loads mapping from string UID to integer node ID. 62 node_id_to_uid = {} 63 proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines() 64 for line in proto_as_ascii: 65 if line.startswith(' target_class:'): 66 target_class = int(line.split(': ')[1]) 67 if line.startswith(' target_class_string:'): 68 target_class_string = line.split(': ')[1] 69 node_id_to_uid[target_class] = target_class_string[1:-2] 70 71 # Loads the final mapping of integer node ID to human-readable string 72 node_id_to_name = {} 73 for key, val in node_id_to_uid.items(): 74 if val not in uid_to_human: 75 tf.logging.fatal('Failed to locate: %s', val) 76 name = uid_to_human[val] 77 node_id_to_name[key] = name 78 79 return node_id_to_name 80 81 def id_to_string(self, node_id): 82 if node_id not in self.node_lookup: 83 return '' 84 return self.node_lookup[node_id] 85 86 87def create_graph(): 88 """Creates a graph from saved GraphDef file and returns a saver.""" 89 # Creates graph from saved graph_def.pb. 90 with tf.gfile.FastGFile(os.path.join( 91 FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f: 92 graph_def = tf.GraphDef() 93 graph_def.ParseFromString(f.read()) 94 _ = tf.import_graph_def(graph_def, name='') 95 96 97def run_inference_on_image(image): 98 """Runs inference on an image. 99 100 Args: 101 image: Image file name. 102 103 Returns: 104 Nothing 105 """ 106 if not tf.gfile.Exists(image): 107 tf.logging.fatal('File does not exist %s', image) 108 image_data = tf.gfile.FastGFile(image, 'rb').read() 109 110 # Creates graph from saved GraphDef. 111 create_graph() 112 113 with tf.Session() as sess: 114 # Some useful tensors: 115 # 'softmax:0': A tensor containing the normalized prediction across 116 # 1000 labels. 117 # 'pool_3:0': A tensor containing the next-to-last layer containing 2048 118 # float description of the image. 119 # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG 120 # encoding of the image. 121 # Runs the softmax tensor by feeding the image_data as input to the graph. 122 softmax_tensor = sess.graph.get_tensor_by_name('softmax:0') 123 predictions = sess.run(softmax_tensor, 124 {'DecodeJpeg/contents:0': image_data}) 125 predictions = np.squeeze(predictions) 126 127 # Creates node ID --> English string lookup. 128 node_lookup = NodeLookup() 129 130 top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1] 131 for node_id in top_k: 132 human_string = node_lookup.id_to_string(node_id) 133 score = predictions[node_id] 134 print('%s (score = %.5f)' % (human_string, score)) 135 136 137def maybe_download_and_extract(): 138 """Download and extract model tar file.""" 139 dest_directory = FLAGS.model_dir 140 if not os.path.exists(dest_directory): 141 os.makedirs(dest_directory) 142 filename = DATA_URL.split('/')[-1] 143 filepath = os.path.join(dest_directory, filename) 144 if not os.path.exists(filepath): 145 def _progress(count, block_size, total_size): 146 sys.stdout.write('\r>> Downloading %s %.1f%%' % ( 147 filename, float(count * block_size) / float(total_size) * 100.0)) 148 sys.stdout.flush() 149 filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress) 150 print() 151 statinfo = os.stat(filepath) 152 print('Successfully downloaded', filename, statinfo.st_size, 'bytes.') 153 tarfile.open(filepath, 'r:gz').extractall(dest_directory) 154 155 156def main(_): 157 maybe_download_and_extract() 158 image = (FLAGS.image_file if FLAGS.image_file else 159 os.path.join(FLAGS.model_dir, 'cropped_panda.jpg')) 160 run_inference_on_image(image) 161 162 163if __name__ == '__main__': 164 parser = argparse.ArgumentParser() 165 # classify_image_graph_def.pb: 166 # Binary representation of the GraphDef protocol buffer. 167 # imagenet_synset_to_human_label_map.txt: 168 # Map from synset ID to a human readable string. 169 # imagenet_2012_challenge_label_map_proto.pbtxt: 170 # Text representation of a protocol buffer mapping a label to synset ID. 171 parser.add_argument( 172 '--model_dir', 173 type=str, 174 default='/tmp/imagenet', 175 help="""\ 176 Path to classify_image_graph_def.pb, 177 imagenet_synset_to_human_label_map.txt, and 178 imagenet_2012_challenge_label_map_proto.pbtxt.\ 179 """ 180 ) 181 parser.add_argument( 182 '--image_file', 183 type=str, 184 default='', 185 help='Absolute path to image file.' 186 ) 187 parser.add_argument( 188 '--num_top_predictions', 189 type=int, 190 default=5, 191 help='Display this many predictions.' 192 ) 193 FLAGS, unparsed = parser.parse_known_args() 194 tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
補足情報(FW/ツールのバージョンなど)
tensorflow 1.11.0
Ubuntu 16.04.4 LTS
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。