I'm running into trouble setting the shape of my image data when trying to work with TFRecords format. I've been going over the how-to for reading data and have taken the code from the MNIST example for converting the image data to a TFRecords and reading the data from the TFRecords. However, this example code originally expects the image to be used in a format where all the pixel data is in one long vector.
I've been trying to alter this code to work with NumPy arrays that still are in the original image shape. So in my code below, images is a NumPy array with shape [number_of_images, height, width, channels]. I'm not sure if my problem is on the side of how I'm writing the data to the TFRecords or how I'm reading it back out. However, when I try to set the shape of the decoded image, I get the error ValueError: Shapes (?,) and (464, 624, 3) must have the same rank (note: 464 x 624 x 3 are the image dimensions). Any suggestions as to what I may be doing wrong?
Relavent code (slightly altered from example code):
def convert_to_tfrecord(images, labels, name, data_directory):
number_of_examples = labels.shape[0]
rows = images.shape[1] # images is the 4D ndarray with the images in their original shape.
cols = images.shape[2]
depth = images.shape[3]
...
for index in range(number_of_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'channels': _int64_feature(depth),
'image': _bytes_feature(image_raw),
...
}))
writer.write(example.SerializeToString())
...
def read_and_decode(filename_queue):
...
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
...
})
...
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape([464, 624, 3]) # This is where the error occurs.
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
...