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