0
votes

I'm new to tensorflow. I intended to flatten a tensor of shape (28,28,1) to a 1-D tensor by using tf.reshape, but didn't get the expected result. Here is the piece of the code I used.

def input_pipeline(self, batch_size, num_epochs=None):
    images_tensor = tf.convert_to_tensor(self.image_names, dtype=tf.string)
    labels_tensor = tf.convert_to_tensor(self.labels, dtype=tf.int64)
    input_queue = tf.train.slice_input_producer([images_tensor, labels_tensor], num_epochs=num_epochs)

    labels = input_queue[1]
    images_content = tf.read_file(input_queue[0])
    images = tf.image.convert_image_dtype(tf.image.decode_png(images_content, channels=N_CHANNELS), tf.float32)
    new_size = tf.constant([IMAGE_SIZE_CHN, IMAGE_SIZE_CHN], dtype=tf.int32)
    images = tf.image.resize_images(images, new_size)
    reshape_vec = tf.constant([-1], dtype=tf.int32)
    print(images.get_shape())
    tf.reshape(images, reshape_vec)
    print(images.get_shape())
    image_batch, label_batch = tf.train.shuffle_batch([images, labels], batch_size=batch_size, capacity=50000,
                                                      min_after_dequeue=10000)
    return image_batch, label_batch

The result of the two print functions are both (28,28,1). Can anyone help me on this? Thanks!

1

1 Answers

0
votes

reshape returns a new tensor, which is the result of reshaping the old tensor. It does not change the original tensor. To fix, simply change the reshape line to

images = tf.reshape(images, reshape_vec)

Note it's probably nicer/cleaner to just use

images = tf.reshape(images, (-1,))

rather than define the new shape in a separate constant tensor, though that's more a personal preference thing.