Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load an image and resize it for segmentation.
TensorFlow
import tensorflow as tf image = tf.io.read_file('image.jpg') image = tf.image.decode_jpeg(image, channels=3) image = tf.image.resize(image, [1]) print(image.shape)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong tuple format like [256, 256] instead of (256, 256).
Not resizing the image before feeding it to the model.
✗ Incorrect
We resize the image to (256, 256) pixels, a common size for segmentation models.
2fill in blank
mediumComplete the code to normalize the image pixels between 0 and 1.
TensorFlow
image = tf.cast(image, tf.float32) image = image [1] 255.0
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying by 255 instead of dividing.
Subtracting 255 which leads to negative values.
✗ Incorrect
Dividing by 255.0 scales pixel values from 0-255 to 0-1.
3fill in blank
hardFix the error in the model output layer for binary segmentation.
TensorFlow
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', padding='same'),
tf.keras.layers.Conv2D(1, 1, activation=[1])
])Attempts:
3 left
💡 Hint
Common Mistakes
Using softmax which is for multi-class problems.
Using relu which does not limit output range.
✗ Incorrect
For binary segmentation, the output layer uses sigmoid activation to predict probabilities between 0 and 1.
4fill in blank
hardFill both blanks to compile the model with appropriate loss and metric for segmentation.
TensorFlow
model.compile(optimizer='adam', loss=[1], metrics=[[2]])
Attempts:
3 left
💡 Hint
Common Mistakes
Using categorical_crossentropy for binary segmentation.
Using mean_squared_error which is for regression.
✗ Incorrect
Binary crossentropy is the right loss for binary segmentation; accuracy is a common metric to track.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps image filenames to their resized tensors if width {{BLANK_1}} 256 and height {{BLANK_2}} 256, using tf.image.resize with size {{BLANK_3}}.
TensorFlow
images = {filename: tf.image.resize(tf.io.decode_jpeg(tf.io.read_file(filename)), [3]) for filename in file_list if tf.shape(tf.io.decode_jpeg(tf.io.read_file(filename)))[1] [1] 256 and tf.shape(tf.io.decode_jpeg(tf.io.read_file(filename)))[0] [2] 256}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' in conditions.
Resizing to a different size than the condition.
✗ Incorrect
We check if width and height equal 256, then resize to (256, 256).
