イメージデータ用のTFRecordファイルを作成し、読み込んでネットワークをトレーニングすることができました。TFRecordデータを使用したトレーニングとテスト
height = 28
width = 28
tfrecords_train_filename = '../train-00000-of-00001'
tfrecords_test_filename = '../test-00000-of-00001'
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image/class/label': tf.FixedLenFeature([], tf.int64),
'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='')
})
image_buffer = features['image/encoded']
image_label = tf.cast(features['image/class/label'], tf.int32)
with tf.name_scope('decode_jpeg', [image_buffer], None):
image = tf.image.decode_jpeg(image_buffer, channels=3)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.image.rgb_to_grayscale(image)
image_shape = tf.stack([height, width, 1])
image = tf.reshape(image, image_shape)
return image, image_label
def inputs(filename, batch_size, num_epochs):
if not num_epochs: num_epochs = None
with tf.name_scope('input'):
filename_queue = tf.train.string_input_producer([filename], num_epochs=None)
image, label = read_and_decode(filename_queue)
images, sparse_labels = tf.train.shuffle_batch(
[image, label], batch_size=batch_size, num_threads=2,
capacity=1000 + 3 * batch_size,
min_after_dequeue=1000)
return images, sparse_labels
image, label = inputs(filename=tfrecords_train_filename, batch_size=200, num_epochs=None)
image = tf.reshape(image, [-1, 784])
label = tf.one_hot(label - 1, 10)
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
for i in range(1000):
img, lbl = sess.run([image, label])
sess.run(train_step, feed_dict={x: img, y_: lbl})
img, lbl = sess.run([image, label])
print(sess.run(accuracy, feed_dict={x: img, y_: lbl}))
coord.request_stop()
coord.join(threads)
最初の機能は、基本的にTFRecordファイルを読み込んでデータをイメージデータに変換することです。次にinputs
で、データはバッチにシャッフルされます。
トレーニング中にネットワーク上で定期的にトレーニングデータを評価したいと思っています。このため私はtest_image, test_label = inputs(filename=tfrecords_test_filename, batch_size=20, num_epochs=None)
に類似したものを持っています。しかし、それは私の以前に定義されたキューを上書きし、OutOfRangeErrorをスローするようです。 私は共有変数を使ってこれを行う可能性について読んでいましたが、私はこれをどのように暗示するのか分かりません。行かなくてもいいのですか?定期的にネットワークを評価するにはどうすればよいですか?
トレーニングプロセスをキューで実行し、テストデータをロードして実行することがありますか?テストデータのためだけに別のキューを作成できますか? – Engineero
'test_image、test_label = inputs(filename = tfrecords_test_filename、batch_size = 20、num_epochs = None) 'で述べたようにこれを試みましたが、これは他のイメージとラベルを上書きしてOutOfRangeExceptionをスローします。私はこれは変数がおそらく上書きされることと関係があると思いますか?しかし、私は何が欠けているのか分からない。 – SparkierFlunky