質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1702閲覧

TensorFlowのPythonAPIで画像分析する際に抽出できる情報について

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/10/07 13:03

前提・実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FLAGS.num_top_predictions

FLAGSはパーサからの取得なので、コマンドライン引数を与えれば上位5位以降にも変えられるはずです。

https://qiita.com/piyo_papa/items/276f6dad35acb998492e
https://blog.logicky.com/2017/01/20/tensorflow-tf-app-flags-flags(ファイル実行時にパラメタを付与でき/

投稿2018/10/07 14:41

mkgrei

総合スコア8560

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問