What is an example of how to use a TensorFlow TFRecord with a Keras Model and tf.session.run() while keeping the dataset in tensors w/ queue runners?
Below is a snippet that works but it needs the following improvements:
- Use the Model API
- specify an Input()
- Load a dataset from a TFRecord
- Run through a dataset in parallel (such as with a queuerunner)
Here is the snippet, there are several TODO lines indicating what is needed:
from keras.models import Model
import tensorflow as tf
from keras import backend as K
from keras.layers import Dense, Input
from keras.objectives import categorical_crossentropy
from tensorflow.examples.tutorials.mnist import input_data
sess = tf.Session()
K.set_session(sess)
# Can this be done more efficiently than placeholders w/ TFRecords?
img = tf.placeholder(tf.float32, shape=(None, 784))
labels = tf.placeholder(tf.float32, shape=(None, 10))
# TODO: Use Input()
x = Dense(128, activation='relu')(img)
x = Dense(128, activation='relu')(x)
preds = Dense(10, activation='softmax')(x)
# TODO: Construct model = Model(input=inputs, output=preds)
loss = tf.reduce_mean(categorical_crossentropy(labels, preds))
# TODO: handle TFRecord data, is it the same?
mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
sess.run(tf.global_variables_initializer())
# TODO remove default, add queuerunner
with sess.as_default():
for i in range(1000):
batch = mnist_data.train.next_batch(50)
train_step.run(feed_dict={img: batch[0],
labels: batch[1]})
print(loss.eval(feed_dict={img: mnist_data.test.images,
labels: mnist_data.test.labels}))
Why is this question relevant?
- For high performance training without going back to python
- no TFRecord to numpy to tensor conversions
- Keras will soon be part of tensorflow
- Demonstrate how Keras Model() classes can accept tensors for input data correctly.
Here is some starter information for a semantic segmentation problem example:
- example unet Keras model unet.py, happens to be for semantic segmentation.
- Keras + Tensorflow Blog Post
- An attempt at running the unet model a tf session with TFRecords and a Keras model (not working)
- Code to create the TFRecords: tf_records.py
- An attempt at running the unet model a tf session with TFRecords and a Keras model is in densenet_fcn.py (not working)
Update 2018-08-29 this is now directly supported in keras, see the following example:
https://github.com/keras-team/keras/blob/master/examples/mnist_tfrecord.py
Original Answer:
TFRecords are supported by using an external loss. Here are the key lines constructing an external loss:
Here is an example for Keras 2. It works after applying the small patch #7060:
I've also been working to improve the support for TFRecords in the following issue and pull request:
Finally, it is possible to use
tf.contrib.learn.Experiment
to train Keras models in TensorFlow.I don't use tfrecord dataset format so won't argue on the pros and cons, but I got interested in extending Keras to support the same.
github.com/indraforyou/keras_tfrecord is the repository. Will briefly explain the main changes.
data_to_tfrecord
andread_and_decode
here takes care of creating tfrecord dataset and loading the same. Special care must be to implement theread_and_decode
otherwise you will face cryptic errors during training.Now both
tf.train.shuffle_batch
and KerasInput
layer returns tensor. But the one returned bytf.train.shuffle_batch
don't have metadata needed by Keras internally. As it turns out, any tensor can be easily turned into a tensor with keras metadata by callingInput
layer withtensor
param.So this takes care of initialization:
Now with
x_train_inp
any keras model can be developed.Lets say
train_out
is the output tensor of your keras model. You can easily write a custom training loop on the lines of:One of the features of keras that makes it so lucrative is its generalized training mechanism with the callback functions.
But to support tfrecords type training there are several changes that are need in the
fit
functionfeed_dict
But all this can be easily supported by another flag parameter. What makes things messing are the keras features
sample_weight
andclass_weight
they are used to weigh each sample and weigh each class. For this incompile()
keras creates placeholders (here) and placeholders are also implicitly created for the targets (here) which is not needed in our case the labels are already fed in by tfrecord readers. These placeholders needs to be fed in during session run which is unnecessary in our cae.So taking into account these changes,
compile_tfrecord
(here) andfit_tfrecord
(here) are the extension ofcompile
andfit
and shares say 95% of the code.They can be used in the following way:
You are welcome to improve on the code and pull requests.