0
$\begingroup$

My input shape is of (168,18). I create batches of size 256 and create my dataset using timeseries_from_Array_dataset. I am visualizing this 2D snapshot of a multivariate timeseries (batch size- 256, timesteps - 168, variables- 18) as an image and building a image_segmentation themed model. I have defined a model as follows:

# Reshape input to (168, 18, 1) to use 2D convolutions                                 
input_shape = (sequence_length, n_features, 1)                                         
                                                                                       
# Define the FCN model with 2D convolutions and Conv2DTranspose                        
model = Sequential()                                                                   
                                                                                       
# Encoder                                                                              
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same',input_shape=
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))                              
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))           
#model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))                             
                                                                                       
model.add(MaxPooling2D(pool_size=(3, 3), padding='same'))                              
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'))          
#model.add(MaxPooling2D(pool_size=(3, 3), padding='same'))                             
model.add(UpSampling2D(size=(2, 2)))                                                   
# Decoder                                                                              
model.add(Conv2DTranspose(128, kernel_size=(3, 3), activation='relu', padding='same')) 
model.add(UpSampling2D(size=(3, 3)))                                                   
model.add(Conv2DTranspose(64, kernel_size=(3, 3), activation='relu', padding='same'))  
                                                                                       
# Output layer with n_classes channels                                                 
model.add(Conv2DTranspose(n_classes, kernel_size=(3, 3), activation='softmax', padding=
                                                                                       
# Compile the model                                                                    
model.compile(                                                                         
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),                       
loss=tf.keras.losses.SparseCategoricalCrossentropy(),                                  
metrics=['accuracy',keras.metrics.MeanIoU(num_classes=3,target_class_ids=[0,1,2])])

In model.compile, when I try to use IoU or MeanIoU I get the following error:

Detected at node confusion_matrix/stack_1 defined at (most recent call last): File "/home/ec2-user/sample.py", line 149, in

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 1807, in fit

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 1401, in train_function

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 1384, in step_function

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 1373, in run_step

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 1155, in train_step

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 1249, in compute_metrics

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/engine/compile_utils.py", line 620, in update_state

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/utils/metrics_utils.py", line 77, in decorated

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/metrics/base_metric.py", line 140, in update_state_fn

File "/opt/tensorflow/lib/python3.10/site-packages/keras/src/metrics/iou_metrics.py", line 141, in update_state

Shapes of all inputs must match: values[0].shape = [774144] != values[1].shape = [2322432] [[{{node confusion_matrix/stack_1}}]] [Op:__inference_train_function_2150]

Any help on how to contruct the segmentation model and setting up the metrics would be greatly appreciated. Thanks and regards

$\endgroup$
2
  • $\begingroup$ Based on the error, it seems as though the IoU class expects an array with a single channel with the class id for each pixel instead of an array with n_classes channels. $\endgroup$
    – Oxbowerce
    Feb 24 at 12:26
  • $\begingroup$ @Oxbowerce, I read that in latest version to build an segmentation model the last layer needs to have n_classes channels . Is there a way to do an argmax anywhere so the IoU gets a single channel as input ? or do I need to change the final layer of the model ? The accuracy parameter seems to work though. Also storing the truth and prediction labels and then computing the IoU seprartely I htink would be unnecessary complexcity because my data is huge and in effect I am running a model.fit in an iteration over each of my data files. $\endgroup$
    – Vjs
    Feb 24 at 19:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.