2
$\begingroup$

I am using the following code to import a bunch of .png images and decode them using TensorFlow:

from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
import os

tf.enable_eager_execution()

NUM_TRAINING_SAMPLES = 333
NUM_CLASSES = 3
BATCH_SIZE = 5
NUM_EPOCHS = 6
INPUT_SIZE = (256, 256, 3)

random_indices = np.random.choice(range(13000), NUM_TRAINING_SAMPLES)
directory = "/home/local/CYCLOMEDIA001/ebos/Downloads/SYNTHIA_RAND_CVPR16"
directory_images = "/home/Downloads/SYNTHIA_RAND_CVPR16/RGB"
directory_labels = "/home/Downloads/SYNTHIA_RAND_CVPR16/GT"
train_images = np.array(os.listdir(directory_images))
train_labels = np.array(os.listdir(directory_images))
train_images = train_images[random_indices]
train_labels = train_labels[random_indices]
train_images = [tf.read_file(os.path.join(directory_images, img)) for img in train_images]
train_labels = [tf.read_file(os.path.join(directory_labels, img)) for img in train_labels]
train_images = [tf.io.decode_image(img, channels=3) for img in train_images]
train_labels = [tf.io.decode_image(img, channels=3) for img in train_labels]
train_images = tf.image.resize_images(train_images, INPUT_SIZE[:2])
train_labels = tf.image.resize_images(train_labels, INPUT_SIZE[:2])

train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels))
train_dataset = train_dataset.batch(3)
print(train_dataset.output_types)

This returns:

(tf.float32, tf.float32)

However, according to the documentation it should return a tensor of uint8's or uint16's. Why and where does the conversion take place?

I checked all intermediate steps with print statements, which doesn't tell me much as most intermediate lists are of class 'tensorflow.python.framework.ops.EagerTensor'.

$\endgroup$
0
$\begingroup$

Types are changing to float due to tf.image.resize_images.

Convert them back to uint as follows:

train_images = tf.cast(train_images, dtype=tf.uint8)
train_labels = tf.cast(train_labels, dtype=tf.uint8)

Output:

(tf.uint8, tf.uint8)

Versions of my code:

tensorflow version: 1.14.1-dev20190330
numpy version: 1.16.2
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Ah, that makes sense! I was also wondering why the floats actually had decimal values, but that must be because of the averaging, I assume. Thanks! $\endgroup$ – EmielBoss Mar 30 '19 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.