tensorflowで犬猫判別機を作って、ラベルをつけたtrainデータを使って学習させました。そこで、ラベルのついていないtestデータに対して分類を行い、その分類があっているのかどうかを可視化したいと思っています。
ここでの可視化とは、示された犬か猫の画像に対して分類機が犬と猫、どちらだと示しているのかというのをわかるようにさせたいです。つまり、画像の中のここの領域に犬または猫がいるというのを領域で示し、その上部に犬か猫かどちらなのかを文字で示すようにさせたいです。
OpenCVを使えば実装できると思ったのですが、どのように実装すれば良いのかわかりません。また、学習させたモデルを最後に保存するようにしているのですが、そのモデルを再度使う方法がわかりません。
以下に犬猫画像を学習させた判別機のコードを示します。
python
1# -*- coding: utf-8 -*- 2import sys 3import pickle 4import os 5import numpy as np 6import tensorflow as tf 7import tensorflow.python.platform 8 9NUM_CLASSES = 2 10IMAGE_SIZE = 56 11IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3 12 13flags = tf.app.flags 14FLAGS = flags.FLAGS 15flags.DEFINE_integer('max_steps', 10, 'Number of steps to run trainer.') 16flags.DEFINE_integer('batch_size', 10, 'Batch size' 17 'Must divide evenly into the dataset sizes.') 18flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.') 19 20def inference(images_placeholder, keep_prob): 21 22 def weight_variable(shape): 23 initial = tf.truncated_normal(shape, stddev=0.1) 24 25 return tf.Variable(initial) 26 27 def bias_variable(shape): 28 initial = tf.constant(0.1, shape=shape) 29 30 return tf.Variable(initial) 31 32 # 畳み込み層の作成 33 def conv2d(x, W): 34 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 35 36 # プーリング層の作成 37 def max_pool_2x2(x): 38 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 39 strides=[1, 2, 2, 1], padding='SAME') 40 41 x_image = tf.reshape(images_placeholder, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]) 42 print(x_image.shape) 43 44 # 畳み込み層1の作成 45 with tf.name_scope('conv1') as scope: 46 W_conv1 = weight_variable([3, 3, 3, 32]) 47 b_conv1 = bias_variable([32]) 48 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 49 50 # プーリング層1の作成 51 with tf.name_scope('pool1') as scope: 52 h_pool1 = max_pool_2x2(h_conv1) 53 print("h_pool1のshape={}".format(h_pool1.shape)) 54 55 # 畳み込み層2の作成 56 with tf.name_scope('conv2') as scope: 57 W_conv2 = weight_variable([3, 3, 32, 64]) 58 b_conv2 = bias_variable([64]) 59 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 60 61 # プーリング層2の作成 62 with tf.name_scope('pool2') as scope: 63 h_pool2 = max_pool_2x2(h_conv2) 64 print("h_pool2のshape={}".format(h_pool2.shape)) 65 66 # 畳み込み層3の作成 67 with tf.name_scope('conv3') as scope: 68 W_conv3 = weight_variable([3, 3, 64, 128]) 69 b_conv3 = bias_variable([128]) 70 h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) 71 72 # プーリング層3の作成 73 with tf.name_scope('pool3') as scope: 74 h_pool3 = max_pool_2x2(h_conv3) 75 print("h_pool3のshape={}".format(h_pool3.shape)) 76 77 # 全結合層1の作成 78 with tf.name_scope('fc1') as scope: 79 W_fc1 = weight_variable([7*7*128, 1024]) 80 b_fc1 = bias_variable([1024]) 81 h_pool3_flat = tf.reshape(h_pool3, [-1, 7*7*128] 82 h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1) 83 # dropoutの設定 84 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 85 86 # 全結合層2の作成 87 with tf.name_scope('fc2') as scope: 88 W_fc2 = weight_variable([1024, NUM_CLASSES]) 89 b_fc2 = bias_variable([NUM_CLASSES]) 90 91 # ソフトマックス関数による正規化 92 with tf.name_scope('softmax') as scope: 93 y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 94 95 return y_conv 96 97def loss(logits, labels): 98 99 cross_entropy = -tf.reduce_sum(labels*tf.log(logits)) 100 tf.summary.scalar("cross_entropy", cross_entropy) 101 return cross_entropy 102 103def training(loss, learning_rate): 104 105 train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss) 106 107 return train_step 108 109def accuracy(logits, labels): 110 111 correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) 112 113 accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 114 115 tf.summary.scalar("accuracy", accuracy) 116 117 return accuracy 118 119if __name__ == '__main__': 120 with open('x_train.pickle_rgb', 'rb') as f1: 121 train_image = pickle.load(f1) 122 print(train_image.shape) 123 124 with open('t_train.pickle_rgb', 'rb') as f2: 125 train_label = pickle.load(f2) 126 127 with open('x_test.pickle_rgb', 'rb') as f3: 128 test_image = pickle.load(f3) 129 130 with open('t_test.pickle_rgb', 'rb') as f4: 131 test_label = pickle.load(f4) 132 133 with tf.Graph().as_default(): 134 135 # 画像を入れる仮のTensor 136 images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) 137 # ラベルを入れる仮のTensor 138 labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) 139 # dropout率を入れる仮のTensor 140 keep_prob = tf.placeholder("float") 141 142 # inference()を呼び出してモデルを作る 143 logits = inference(images_placeholder, keep_prob) 144 # loss()を呼び出して損失を計算 145 loss_value = loss(logits, labels_placeholder) 146 # training()を呼び出して訓練 147 train_op = training(loss_value, FLAGS.learning_rate) 148 # 精度の計算 149 acc = accuracy(logits, labels_placeholder) 150 151 # 保存の準備 152 saver = tf.train.Saver() 153 # Sessionの作成 154 sess = tf.Session() 155 # 変数の初期化 156 sess.run(tf.global_variables_initializer()) 157 # TensorBoardで表示する値の設定 158 summary_op = tf.summary.merge_all() 159 summary_writer = tf.summary.FileWriter('./logs', sess.graph) 160 161 # 訓練の実行 162 for step in range(FLAGS.max_steps): 163 for i in range(int(len(train_image)/FLAGS.batch_size)): 164 165 batch = FLAGS.batch_size * i 166 sess.run(train_op, feed_dict={ 167 images_placeholder: train_image[batch:batch+FLAGS.batch_size], 168 labels_placeholder: train_label[batch:batch+FLAGS.batch_size], 169 keep_prob: 0.5}) 170 171 # 1 step終わるたびに精度を計算する 172 train_accuracy = sess.run(acc, feed_dict={ 173 images_placeholder: train_image, 174 labels_placeholder: train_label, 175 keep_prob: 1.0}) 176 print ("step {}, training accuracy {}".format(step, train_accuracy)) 177 178 # 1 step終わるたびにTensorBoardに表示する値を追加する 179 summary_str = sess.run(summary_op, feed_dict={ 180 images_placeholder: train_image, 181 labels_placeholder: train_label, 182 keep_prob: 1.0}) 183 summary_writer.add_summary(summary_str, step) 184 185 # 訓練が終了したらテストデータに対する精度を表示 186 print ("test accuracy {}".format(sess.run(acc, feed_dict={ 187 images_placeholder: test_image, 188 labels_placeholder: test_label, 189 keep_prob: 1.0}))) 190 191 # 1 step終わるたびにTensorBoardに表示する値を追加する 192 summary_str = sess.run(summary_op, feed_dict={ 193 images_placeholder: test_image, 194 labels_placeholder: test_label, 195 keep_prob: 1.0}) 196 summary_writer.add_summary(summary_str, step) 197 198 # 最終的なモデルを保存 199 cwd = os.getcwd() 200 save_path = saver.save(sess, cwd + "//model.ckpt") 201
このコードの途中にあるファイルの呼び出し部分では、kaggle(https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data)からダウンロードした犬猫画像にラベルづけなどの処理を施したものを呼び出しています。
最初の学習の部分ではkaggleからダウンロードしたファイル内にあったtrainデータを8:2の20000:5000枚に分けて、ラベル付けを行なって学習させました。なので、実際に分類できるかどうかはkaggleからダウンロードした中にあったテストデータで行おうと思っています。
回答2件
あなたの回答
tips
プレビュー