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

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

新規登録して質問してみよう
ただいま回答率
85.49%
深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Python 3.x

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

Q&A

1回答

346閲覧

Tensorflowでラベルを返す際任意の文字で返す方法

TyoNgc

総合スコア14

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Python 3.x

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

0グッド

1クリップ

投稿2018/01/16 09:16

Tensorflowを用いて画像分類を行っているものです。学習した後に実際に分類ができているか見分けるプログラムは多くのサイトにあるように下記のようなプログラムで実行できると思いますが、ラベルを返す際に任意の文字で返したいと思っています。(1:りんご、2:みかん)のようにしたいと思っています。どのように実行するのが一番効率的かアイデアをください。Python組み込みのWebアプリを作るのは難しいでしょうか?

python

1import sys 2import numpy as np 3import tensorflow as tf 4import cv2 5 6NUM_CLASSES = 7 7IMAGE_SIZE = 28 8IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3 9 10def inference(images_placeholder, keep_prob): 11 12 def weight_variable(shape): 13 initial = tf.truncated_normal(shape, stddev=0.1) 14 return tf.Variable(initial) 15 16 def bias_variable(shape): 17 initial = tf.constant(0.1, shape=shape) 18 return tf.Variable(initial) 19 20 def conv2d(x, W): 21 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 22 23 def max_pool_2x2(x): 24 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 25 strides=[1, 2, 2, 1], padding='SAME') 26 27 x_image = tf.reshape(images_placeholder, [-1, 28, 28, 3]) 28 29 with tf.name_scope('conv1') as scope: 30 W_conv1 = weight_variable([5, 5, 3, 32]) 31 b_conv1 = bias_variable([32]) 32 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 33 34 with tf.name_scope('pool1') as scope: 35 h_pool1 = max_pool_2x2(h_conv1) 36 37 with tf.name_scope('conv2') as scope: 38 W_conv2 = weight_variable([5, 5, 32, 64]) 39 b_conv2 = bias_variable([64]) 40 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 41 42 with tf.name_scope('pool2') as scope: 43 h_pool2 = max_pool_2x2(h_conv2) 44 45 with tf.name_scope('fc1') as scope: 46 W_fc1 = weight_variable([7*7*64, 1024]) 47 b_fc1 = bias_variable([1024]) 48 h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 49 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 50 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 51 52 with tf.name_scope('fc2') as scope: 53 W_fc2 = weight_variable([1024, NUM_CLASSES]) 54 b_fc2 = bias_variable([NUM_CLASSES]) 55 56 with tf.name_scope('softmax') as scope: 57 y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 58 59 return y_conv 60 61if __name__ == '__main__': 62 test_image = [] 63 for i in range(1, len(sys.argv)): 64 img = cv2.imread(sys.argv[i]) 65 img = cv2.resize(img, (28, 28)) 66 test_image.append(img.flatten().astype(np.float32)/255.0) 67 test_image = np.asarray(test_image) 68 69 images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) 70 labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) 71 keep_prob = tf.placeholder("float") 72 73 logits = inference(images_placeholder, keep_prob) 74 sess = tf.InteractiveSession() 75 saver = tf.train.Saver() 76 sess.run(tf.initialize_all_variables()) 77 saver.restore(sess, "./model.ckpt") 78 79 for i in range(len(test_image)): 80 accr = logits.eval(feed_dict={ 81 images_placeholder: [test_image[i]], 82 keep_prob: 1.0 })[0] 83 pred = np.argmax(logits.eval(feed_dict={ 84 images_placeholder: [test_image[i]], 85 keep_prob: 1.0 })[0]) 86 print (pred,accr)

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

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

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

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

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

wakame

2018/01/16 11:12

タイトルの「Tensorflowでラベルを返す際任意の文字で返す方法」と質問文の「Python組み込みのWebアプリを作るのは難しいでしょうか?」は何か関係があるのですか。
guest

回答1

0

辞書で実装するのが一番素直ですが、numpy.arrayで実装すると一番行数少なく済みます。
内包表記使えばいずれも1行ですみますが。

投稿2018/01/16 13:53

mkgrei

総合スコア8560

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問