# Week 3, Programming Assignment 2, section 2.2

**URL:** <https://community.deeplearning.ai/t/week-3-programming-assignment-2-section-2-2/454834>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [September 25, 2023, 2:55pm UTC](https://community.deeplearning.ai/t/week-3-programming-assignment-2-section-2-2/454834 "2023-09-25T14:55:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommaso\_Villani](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@Tommaso\_Villani](https://community.deeplearning.ai/u/Tommaso_Villani)\
**Post date:** [September 25, 2023, 2:55pm UTC](https://community.deeplearning.ai/t/week-3-programming-assignment-2-section-2-2/454834/1 "2023-09-25T14:55:43Z")

</div>

Dear community,

I am currently completing the Programming assignment of Week 3. In section 2.2 I see a the following piece of code:

> 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)

I understand everything about this code, the only thing I don’t get is why are we applying the reduce\_max function on the last axis to the mask… (line in bold \ within the \*\*). Am I missing something?

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [September 25, 2023, 6:00pm UTC](https://community.deeplearning.ai/t/week-3-programming-assignment-2-section-2-2/454834/2 "2023-09-25T18:00:02Z")

</div>

It’s sufficient to represent classes as the 0th channel of the mask to show up properly on the screen when using `plt.imshow`.

Since there are 23 classes in total for the assignment, the 1st channel can take on values in range [0, 22]. When you reduce\_max along the last axis, the image mask goes from shape `(480, 640, 3)` (which can be used for display purpose) to shape `(480, 640)` which can be be used as the class labels when using sparse categorical crossentropy as the loss function in training the unet model.

---

<div class="post-metadata">

**Author:** ![Tommaso\_Villani](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@Tommaso\_Villani](https://community.deeplearning.ai/u/Tommaso_Villani)\
**Post date:** [September 26, 2023, 7:19am UTC](https://community.deeplearning.ai/t/week-3-programming-assignment-2-section-2-2/454834/3 "2023-09-26T07:19:09Z")

</div>

Got it, thank you!
