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

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

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

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

Q&A

解決済

1回答

359閲覧

オリジナルデータを用いて顔識別をしたいのですが、学習を開始すると動作が停止してしまいます。

Potack

総合スコア7

Python 3.x

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

0グッド

0クリップ

投稿2017/12/12 07:41

###前提・実現したいこと
ここに質問したいことを詳細に書いてください
画像認識を勉強しています。
tensorflowにより、オリジナルデータを用いて、顔識別を行おうとしていますが、エラーを出さずにプログラムの動作が停止してしまいます。
原因の調査をよろしくお願いします。

###発生している問題・エラーメッセージ

_, loss_value, accuracy_value = sess.run([train_op, loss, accuracy], feed_dict={keep_prob: 0.5}) 動作停止

###該当のソースコード

python

1# -*- coding: utf-8 -*- 2""" 3Created on Tue Nov 14 14:16:48 2017 4 5@author: Owner 6""" 7 8import tensorflow as tf 9 10import os 11from enum import Enum 12 13 14class Channel(Enum): 15 os.chdir(r"D:\WinPython-64bit-3.5.3.1Qt5\face\data") 16 KOHNO = 0 17 FUKAMI = 1 18 NAKAHIRA = 2 19 20LOG_DIR = 'log' 21 22def load_data(csvpath, batch_size, image_size, class_count, shuffle=False, min_after_dequeue=1000): 23 24 queue = tf.train.string_input_producer([csvpath], shuffle=shuffle) 25 reader = tf.TextLineReader() 26 key, value = reader.read(queue) 27 imagepath, label = tf.decode_csv(value, [['imagepath'], [0]]) 28 29 jpeg = tf.read_file(imagepath) 30 image = tf.image.decode_jpeg(jpeg, channels=3) 31 image = tf.image.resize_images(image, [image_size, image_size]) 32 # 平均が 0 になるようにスケーリングする。 33 image = tf.image.per_image_standardization(image) 34 35 # ラベルの値を one-hot 表現に変換する。 36 label = tf.one_hot(label, depth=class_count, dtype=tf.float32) 37 38 capacity = min_after_dequeue + batch_size * 3 39 40 if shuffle: 41 images, labels = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=3, capacity=capacity, min_after_dequeue=min_after_dequeue) 42 else: 43 images, labels = tf.train.batch([image, label], batch_size=batch_size, capacity=capacity) 44 45 return images, labels 46 47class CNN: 48 def __init__(self, image_size=48, class_count=2, color_channel_count=3): 49 self.image_size = image_size 50 self.class_count = class_count 51 self.color_channel_count = color_channel_count 52 53 # 推論のための関数。 54 def inference(self, x, keep_prob, softmax=False): 55 # 重みを格納するための tf.Variable を生成する。 56 def weight_variable(shape): 57 initial = tf.truncated_normal(shape, stddev=0.1) 58 59 return tf.Variable(initial) 60 61 # バイアスを格納するための tf.Variable を生成する。 62 def bias_variable(shape): 63 initial = tf.constant(0.1, shape=shape) 64 65 return tf.Variable(initial) 66 67 # 畳み込みを行う。 68 def conv2d(x, W): 69 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 70 71 # [2x2] の大きさ、移動量 2 でプーリングを行う。 72 def max_pool_2x2(x): 73 return tf.nn.max_pool(x, 74 ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], 75 padding='SAME') 76 77 x_image = tf.reshape( 78 x, 79 [-1, self.image_size, self.image_size, self.color_channel_count]) 80 81 with tf.name_scope('conv1'): 82 W_conv1 = weight_variable([5, 5, self.color_channel_count, 32]) 83 b_conv1 = bias_variable([32]) 84 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 85 86 with tf.name_scope('pool1'): 87 h_pool1 = max_pool_2x2(h_conv1) 88 89 with tf.name_scope('conv2'): 90 W_conv2 = weight_variable([5, 5, 32, 64]) 91 b_conv2 = bias_variable([64]) 92 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 93 94 with tf.name_scope('pool2'): 95 h_pool2 = max_pool_2x2(h_conv2) 96 97 with tf.name_scope('fc1'): 98 W_fc1 = weight_variable( 99 [int(self.image_size / 4) * int(self.image_size / 4) * 64, 1024]) 100 b_fc1 = bias_variable([1024]) 101 h_pool2_flat = tf.reshape( 102 h_pool2, 103 [-1, int(self.image_size / 4) * int(self.image_size / 4) * 64]) 104 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 105 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 106 107 with tf.name_scope('fc2'): 108 W_fc2 = weight_variable([1024, self.class_count]) 109 b_fc2 = bias_variable([self.class_count]) 110 y = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 111 112 if softmax: 113 with tf.name_scope('softmax'): 114 y = tf.nn.softmax(y) 115 116 return y 117 118 # 推論結果と正解の誤差を計算するための損失関数。 119 def loss(self, y, labels): 120 # 交差エントロピーを計算する。 121 # tf.nn.softmax_cross_entropy_with_logits の引数 logits には 122 # ソフトマックス関数を適用した変数を与えないこと。 123 cross_entropy = tf.reduce_mean( 124 tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=labels)) 125 tf.summary.scalar('cross_entropy', cross_entropy) 126 127 return cross_entropy 128 129 # 学習のための関数 130 def training(self, cross_entropy, learning_rate=1e-4): 131 train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) 132 133 return train_step 134 135 # 正答率 (accuracy) を求める。 136 def accuracy(self, y, labels): 137 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(labels, 1)) 138 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 139 tf.summary.scalar('accuracy', accuracy) 140 141 return accuracy 142 143 144os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 145 146flags = tf.app.flags 147FLAGS = flags.FLAGS 148flags.DEFINE_integer('image_size', 48, 'Image size.') 149flags.DEFINE_integer('step_count', 1000, 'Number of steps.') 150flags.DEFINE_integer('batch_size', 50, 'Batch size.') 151flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.') 152 153def main(): 154 with tf.Graph().as_default(): 155 cnn = CNN(image_size=FLAGS.image_size, class_count=len(Channel)) 156 images, labels = load_data( 157 r"D:\WinPython-64bit-3.5.3.1Qt5\face\data\train\data.csv", 158 batch_size=FLAGS.batch_size, 159 image_size=FLAGS.image_size, 160 class_count=len(Channel), 161 shuffle=True) 162 keep_prob = tf.placeholder(tf.float32) 163 logits = cnn.inference(images, keep_prob) 164 loss = cnn.loss(logits, labels) 165 train_op = cnn.training(loss, FLAGS.learning_rate) 166 accuracy = cnn.accuracy(logits, labels) 167 168 saver = tf.train.Saver() 169 init_op = tf.global_variables_initializer() 170 #summary_op = tf.summary.merge_all() 171 #summary_writer = tf.summary.FileWriter("./"+LOG_DIR, graph=sess.graph) 172 count = 1 173 sess = tf.Session() 174 sess.run(init_op) 175 for step in range(1, FLAGS.step_count + 1): 176 print("a") 177 _, loss_value, accuracy_value = sess.run([train_op, loss, accuracy], feed_dict={keep_prob: 0.5}) 178 print(count) 179 count *= 1 180 181 if step % 10 == 0: 182 print("step {step}: training accuracy {accuracy_value}".format(**locals())) 183 #summary = sess.run(summary_op, feed_dict={keep_prob: 1.0}) 184 #summary_writer.add_summary(summary, step) 185 186 187 save_path = saver.save(sess, os.path.join(LOG_DIR, 'model.ckpt')) 188if __name__ == '__main__': 189 main()

###試したこと
tf.reshapeのパラメータ変更, 指定ディレクトリのパス変更等

###補足情報(言語/FW/ツール等のバージョンなど)
環境 Windows10, Spyder(Python3.5)
モジュール tensorflow ver1.0.1

参考ソースコードURL
https://qiita.com/QUANON/items/b35003d347edb1cd9ec3#_reference-e18e3376dd2be6ba67d0

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

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

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

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

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

guest

回答1

0

ベストアンサー

feed_dict={keep_prob: 0.5}
データを与えていないからではないでしょうか。


TFRecordsなら
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
などが足りてないのでしょうか。
http://tensorflow.classcat.com/2016/02/13/tensorflow-how-tos-reading-data/

投稿2017/12/12 07:43

編集2017/12/12 07:52
mkgrei

総合スコア8560

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問