CNNに関する質問です。モデルの読み込みでNotFoundErrorエラーが出ます。
CNNを使って、数字画像を認識するプログラムを作成しています。
保存したモデルをrestoreしたいのですが、NotFoundErrorエラーが出ます。
発生している問題・エラーメッセージ
NotFoundError (see above for traceback): Key Variable_10 not found in checkpoint [[Node: save_1/RestoreV2_2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_1/Const_0_0, save_1/RestoreV2_2/tensor_names, save_1/RestoreV2_2/shape_and_slices)]]
該当のソースコード
モデルを保存するためのファイル sample.py from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf import time mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) def dl(x): x_img = tf.reshape(x,[-1,28,28,1]) W_conv1 = tf.Variable(tf.truncated_normal([5,5,1,32], stddev=0.1)) conv1 = tf.nn.conv2d(x_img, W_conv1, strides=[1,1,1,1], padding='SAME') b_conv1 = tf.Variable(tf.constant(0.1, shape=[32])) h_conv1 = tf.nn.relu(conv1+b_conv1) h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') W_conv2 = tf.Variable(tf.truncated_normal([5,5,32,64], stddev=0.1)) conv2 = tf.nn.conv2d(h_pool1, W_conv2, strides=[1,1,1,1], padding='SAME') b_conv2 = tf.Variable(tf.constant(0.1, shape=[64])) h_conv2 = tf.nn.relu(conv2+b_conv2) h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') keep_prob = tf.placeholder(tf.float32) out_drop = tf.nn.dropout(h_pool2, keep_prob) h_pool2_flat = tf.reshape(out_drop, [-1, 7*7*64]) w_fc1 = tf.Variable(tf.truncated_normal([7*7*64, 1024], stddev=0.1)) b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024])) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1) W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1)) b_fc2 = tf.Variable(tf.constant(0.1, shape=[10])) out = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2) return out, keep_prob x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) y,keep_prob = dl(x) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y + 1e-5), axis=[1])) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) ac = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) for _ in range(6000): bs, b_y = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: bs, y_:b_y, keep_prob: 0.5}) if _ % 200 == 0: accuracy = sess.run(ac, feed_dict={x: bs, y_:b_y, keep_prob: 0.5}) print("ステップ{0:5d}, 正解率{1:6.2f}%" .format(_, accuracy*100)) saver = tf.train.Saver() saver.save(sess, "./model/cn_model") accuracy = sess.run(ac, feed_dict={x: mnist.test.images, y_:mnist.test.labels, keep_prob: 1.0}) print("正解率", accuracy*100 , "%") モデルを読み込みたいファイル sample2.py import tensorflow as tf import time from tensorflow.examples.tutorials.mnist import input_data import sample mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) y,keep_prob = sample.dl(x) correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) ac = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, "./model/cn_model") ここでエラーが発生しているようです! accuracy = sess.run(ac, feed_dict={x: mnist.test.images, y_:mnist.test.labels, keep_prob: 1.0}) print("正解率", accuracy*100 , "%")
試したこと
定義する変数に不足があるエラーなのかなと思い、コードの順番を変えてみたり、sample.pyに定義していた変数をsample2.pyに追加したりしてみたのですが、解決できませんでした。皆様のお力を貸していただけると幸いです。よろしくお願いいたします。
sample.pyを実行すると、問題なく実行され、モデルも保存されているように見えます。しかし、sample2.pyを実行すると、保存がうまくされていないのか、まずsample.pyの方のコードの実行内容が出力され、そのあとsample2.pyを実行している状態です(その途中で今回のエラー)。
これまでは、モデルを再利用する際に、前のファイルの実行が出力されることがなかったので、その辺り、そもそも保存できていないことや、読み込み方が間違っていることが原因かとも思ってます。
補足情報(FW/ツールのバージョンなど)
tensorflow (1.4.0)
python (3.6.0)
あなたの回答
tips
プレビュー