3
dataset = tf.data.Dataset.from_tensor_slices((images,boxes)) function_to_map = lambda x,y: func3(x,y) fast_benchmark(dataset.map(function_to_map).batch(1).prefetch(tf.data.experimental.AUTOTUNE)) 

Now I here is the func3

def fast_benchmark(dataset, num_epochs=2): start_time = time.perf_counter() print('dataset->',dataset) for _ in tf.data.Dataset.range(num_epochs): for _,__ in dataset: print(_,__) break pass 

the ooutput of print is

tf.Tensor([b'/media/jake/mark-4tb3/input/datasets/pascal/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages/2008_000008.jpg'], shape=(1,), dtype=string) <tf.RaggedTensor [[[52, 86, 470, 419], [157, 43, 288, 166]]]> 

what I want to do in func3()
want to change image directory to the real image and run the batch

1 Answer 1

6

You need to extract string form the tensor and use the appropriate image reading function. Below are the steps to be implemented in the code to achieve this.

  1. You have to decorate the map function with tf.py_function(get_path, [x], [tf.float32]). You can find more about tf.py_function here. In tf.py_function, first argument is the name of map function, second argument is the element to be passed to map function and final argument is the return type.
  2. You can get your string part by using bytes.decode(file_path.numpy()) in map function.
  3. Use appropriate function to load your image. We are using load_img.

In the below simple program, we are using tf.data.Dataset.list_files to read path of the image. Next in the map function we are reading the image using load_img and later doing the tf.image.central_crop function to crop central part of the image.

Code -

%tensorflow_version 2.x import tensorflow as tf from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array, array_to_img from matplotlib import pyplot as plt import numpy as np def load_file_and_process(path): image = load_img(bytes.decode(path.numpy()), target_size=(224, 224)) image = img_to_array(image) image = tf.image.central_crop(image, np.random.uniform(0.50, 1.00)) return image train_dataset = tf.data.Dataset.list_files('/content/bird.jpg') train_dataset = train_dataset.map(lambda x: tf.py_function(load_file_and_process, [x], [tf.float32])) for f in train_dataset: for l in f: image = np.array(array_to_img(l)) plt.imshow(image) 

Output -

enter image description here

Hope this answers your question. Happy Learning.

Sign up to request clarification or add additional context in comments.

1 Comment

@slowmonk - Hope we have answered your question. Can you please accept and upvote the answer if you are satisfied with the answer.