I am trying to parse data using the TF-SLIM dataset classes.
I am combining two images into a 640x480x6 numpy array (because I combine the RGB channels of both the images) and serializing them to save them to a .tfrecords file. Here's the code to do so.
img_pair = combine_images(images[i][0],images[i][1])
img_flo = read_flo_file(labels[i][0])
height = img_pair.shape[0]
width = img_pair.shape[1]
img = img_pair.tostring()
flo = img_flo.tostring()
example = image_to_tfexample(
img, height, width, flo)
tfrecord_writer.write(example.SerializeToString())
def image_to_tfexample(image_data, height, width, flo):
return tf.train.Example(features=tf.train.Features(feature={
'image/img_pair': bytes_feature(image_data),
'image/flo': bytes_feature(flo),
'image/height': int64_feature(height),
'image/width': int64_feature(width),
}))
def combine_images(img1,img2):
img1 = np.array(Image.open(img1))
img2 = np.array(Image.open(img2))
return np.concatenate((img1,img2),axis=-1)
Where img_pair is a 640x480x6 numpy array and flo is a 640x480x2 numpy array.
Now I want to read these examples. Here's what I have so far from the tf-slim flower.py (updated to suit me) example.
def get_split(split_name, dataset_dir, file_pattern=None, reader=None):
if split_name not in SPLITS_TO_SIZES:
raise ValueError('split name %s was not recognized.' % split_name)
if not file_pattern:
file_pattern = _FILE_PATTERN
file_pattern = os.path.join(dataset_dir, file_pattern % split_name)
# Allowing None in the signature so that dataset_factory can use the default.
if reader is None:
reader = tf.TFRecordReader
keys_to_features = {
'image/width': tf.FixedLenFeature([], tf.int64),
'image/height': tf.FixedLenFeature([], tf.int64),
'image/img_pair': tf.FixedLenFeature([], tf.string),
'image/flo': tf.FixedLenFeature([], tf.string)
}
items_to_handlers = {
'image': slim.tfexample_decoder.Tensor('image/img_pair'),
'label': slim.tfexample_decoder.Tensor('image/flo'),
}
decoder = slim.tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers)
return slim.dataset.Dataset(
data_sources=file_pattern,
reader=reader,
decoder=decoder,
num_samples=SPLITS_TO_SIZES[split_name],
items_to_descriptions=_ITEMS_TO_DESCRIPTIONS)
The problem here is that image/img_pair and image/flo are binary strings. They first needed to be converted to Tensors in order to feed them as item_handlers, as far as I understood.
Like this.
items_to_handlers = {
'image': slim.tfexample_decoder.Tensor('image/img_pair'),
'label': slim.tfexample_decoder.Tensor('image/flo'),
}
I don't know how to parse it back into the tensor with the same shape i.e. 640x480x6 for img_pair and 640x480x2 for flo.
I get an error.
Will save model to /tmp/tfslim_model/
Traceback (most recent call last):
File "main.py", line 16, in <module>
images, _, labels = helpers.load_batch(dataset)
File "/home/muazzam/mywork/python/thesis/SceneflowTensorflow/new_stuff/helpers.py", line 36, in load_batch
common_queue_min=8)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/slim/python/slim/data/dataset_data_provider.py", line 97, in __init__
tensors = dataset.decoder.decode(data, items)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/slim/python/slim/data/tfexample_decoder.py", line 424, in decode
outputs.append(handler.tensors_to_item(keys_to_tensors))
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/slim/python/slim/data/tfexample_decoder.py", line 321, in tensors_to_item
return self._decode(image_buffer, image_format)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/slim/python/slim/data/tfexample_decoder.py", line 350, in _decode
pred_fn_pairs, default=decode_image, exclusive=True)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3169, in case
case_seq = _build_case()
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3151, in _build_case
strict=strict, name="If_%d" % i)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 296, in new_func
return func(*args, **kwargs)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1819, in cond
orig_res_t, res_t = context_t.BuildCondBranch(true_fn)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1694, in BuildCondBranch
original_result = fn()
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/slim/python/slim/data/tfexample_decoder.py", line 338, in decode_image
return image_ops.decode_image(image_buffer, self._channels)
File "/home/muazzam/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/image_ops_impl.py", line 1346, in decode_image
raise ValueError('channels must be in (None, 0, 1, 3, 4)')
ValueError: channels must be in (None, 0, 1, 3, 4)