Building a convolutional neural network for image classification has been my first foray into both Python and Tensorflow. I have a large database of images to train on, and my application is rapid, real-time classification of single images. Currently, in order to perform inference of single images, I have to make the training batch size equal to 1. If I don't (and the training batch size is 16) I get the following error:
ValueError: Cannot feed value of shape (1, 132, 128, 1) for Tensor
'tf_reshape1:0', which has shape '(16, 132, 128, 1)'
I'd really like to have the flexibility to train on larger batch sizes, whilst still being able to classify single images.
My question is very similar to other asked on here e.g. Training in batches but testing individual data item in Tensorflow? and Tensorflow: Layer size dependent on batch size? but my lack of experience with tf means that I'm unable to implement the suggested solutions into my code.
I've written my training and inference codes for image classification by basing them around the example given on the tensorflow website for classification of the MNIST database https://www.tensorflow.org/tutorials/estimators/cnn#train_eval_mnist. My codes use tf.estimators https://www.tensorflow.org/guide/estimators a high level tensorflow API. The solutions to the similar question asked and answered above suggest modifying the tf.placeholders which I am not (knowingly) using. I've copied my code for the input function and the model function that I use below. I'm sure I'll need to post more but this is also the first SO question I've asked so apologies if I've forgotten many things.
Training:
bead_classifier = tf.estimator.Estimator(
model_fn=bead_model_fn,
model_dir=r'/tmp/trained_model')
# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": training_imgs},
y=training_labels,
batch_size=16,
num_epochs=None,
shuffle=True)
bead_classifier.train(
input_fn=train_input_fn,
steps = 13000)
hooks=[logging_hook])
Model function (first few layers only):
def bead_model_fn(features, labels, mode):
"""Model function for CNN."""
# Input Layer
# Reshape X to 4-D tensor: [batch_size, width, height, channels]
input_layer = tf.reshape(features["x"], [-1, 132, 128, 1], name =
'tf_reshape1')
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=4,
kernel_size=[2, 2],
padding="same",
activation=tf.nn.relu)
# Pooling Layer #1
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
# Convolutional Layer #2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=8,
kernel_size=[2, 2],
padding="same",
activation=tf.nn.relu)