Shun7_natural
Shun7_natural

Reputation: 133

How to understand input images for convolutional neural network in this code

I am not sure my answer is correct (#note is my answer)

def process_image(data):
  r""" [0,255] -> [-1, 1]"""  

Is it means normalize the image data? but I have not seen this syntax

img = data['image'] 

classification

lab = data['label']
  img = (tf.cast(img, tf.float32) / 255.0 - 0.5) * 2.0 

images-> float32 format(0,255)->normalization to (-1,1) return img, lab

def create_image_dataset(ds, batch_size=256, training=None):
  if training:
    ds = ds.shuffle(1000)   

I know it means shuffle dataset randomly,but what is 1000 means?

ds = ds.batch(batch_size).map(process_image).prefetch(
      tf.data.experimental.AUTOTUNE)    

about the part prefetch, is it means The program can automatically select the optimal number of threads in parallel?

  return ds  

train = create_image_dataset(mnist['train'], batch_size=256, training=True)

image_shape = tf.data.experimental.get_structure(train)[0].shape[1:] 

print("Image shape:", image_shape)

what did get_structure means? is it same as reshape func?

Thanks a lot

Upvotes: 1

Views: 62

Answers (1)

Balraj Ashwath
Balraj Ashwath

Reputation: 1427

In ds = ds.shuffle(1000), 1000 refers to the buffer_size. From tensorflow docs, this represents the number of elements from the dataset from which the new dataset will sample.

Regarding prefetch, tensorflow docs refers to prefetching as input pipeline reading data for step s+1 while the model is executing step s overlapping the preprocessing and model execution stages of a training step. It uses a background thread and an internal buffer to prefetch elements from the input dataset ahead of the time they are requested. The number of elements to prefetch could either be manually tuned or set it to tf.data.experimental.AUTOTUNE which will prompt the tf.data runtime to tune the value dynamically at runtime.

From tensorflow docs, get_structure returns the type specification of an element of a Dataset or Iterator, which in your case, is being used to obtain the image shape.

Upvotes: 1

Related Questions