python - Train and Test with TFRecord Data -
i created tfrecord file image data , able load , train network it.
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) 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 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 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) tf.session() 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)) 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)
the first function loading tfrecord file , converting data image data. in inputs
, data gets shuffled batches.
i want have training data being evaluated regularly on network while training. have similar test_image, test_label = inputs(filename=tfrecords_test_filename, batch_size=20, num_epochs=none)
. however, seems overwrite defined queue , throw outofrangeerror. reading possibility using shared variables, don't how implament though. right way go? how can evaluate network periodically?
check out section on feedable iterators here. think might looking for. using dataset
api, think parallels tfrecord
api. not positive that.
the gist, taken largely documentation linked previously:
# define training , test datasets same structure. training_data = tf.contrib.data.dataset.(whatever) test_data = tf.contrib.data.dataset.(something_else) # feedable iterators use handle placeholder. handle = tf.placeholder(tf.string, shape=[]) iterator = tf.contrib.data.iterator.from_string_handle( handle, training_data.output_types, training_data.output_shapes) next_element = iterator.get_next() # need iterators each dataset feed feedable iterator. # gets little wonky. training_iterator = training_data.make_one_shot_iterator() test_iterator = test_data.make_initiailizable_iterator() # use `iterator.string_handle()` value `handle` # placeholder. training_handle = sess.run(training_iterator.string_handle()) test_handle = sess.run(test_iterator.string_handle()) # run training/testing. want train 100 # steps, test 50 iterations, repeat 10 times. , # want reset test iterator every outer loop. _ in range(10): _ in range(100): sess.run(next_element, feed_dict={handle: training_handle}) sess.run(test_iterator.initializier) _ in range(50): sess.run(next_element, feed_dict={handle: test_handle})
looking @ bit more not sure you. leave until hear feedback either way.
Comments
Post a Comment