Hi,
I have trouble understanding how this code snippet from the image segmentation with U-Net programming exercise (2.2 - Preprocess Your Data) works. I understand what it does, but I don’t understand the code. (My problem is basically a Python issue, sorry if that is not appropriate here - but Googling didn’t help.)
def process_path(image_path, mask_path):
img = tf.io.read_file(image_path)
img = tf.image.decode_png(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
mask = tf.io.read_file(mask_path)
mask = tf.image.decode_png(mask, channels=3)
mask = tf.math.reduce_max(mask, axis=-1, keepdims=True)
return img, mask
def preprocess(image, mask):
input_image = tf.image.resize(image, (96, 128), method='nearest')
input_mask = tf.image.resize(mask, (96, 128), method='nearest')
return input_image, input_mask
image_ds = dataset.map(process_path)
processed_image_ds = image_ds.map(preprocess)
It’s the last two lines. Here, map()
applies the functions process_path()
and preprocess()
to each element of dataset
and image_ds
, respectively - but I don’t understand why those functions do not take their positional arguments here. My guesses:
-
image_path
andmask_path
have been defined earlier in the notebook, so that’s whyprocess_path()
knows these global variables, and they don’t have to be provided usingmap()
. -
image
andmask
, however, have not been defined.preprocess()
knows what they are because, withmap()
,preprocess()
is applied to each item ofimage_ds
(which consists of an image and its mask), so it assumes thatimage
is the first element of this item, andmask
the second - and these variables also do not have to be provided usingmap()
.
Is that about right?