huyquoctrinh / MetaPolyp-CBMS2023

Meta-Polyp: a baseline for efficient Polyp segmentation (CBMS 2023)
MIT License
68 stars 9 forks source link

Incompatible shape of CAFormerS18 output #8

Closed andrewcheng2016 closed 4 months ago

andrewcheng2016 commented 4 months ago

In def build model() from model.py, I found the shapes of the CAFormerS18 may be incompatible. `def build_model(img_size = 256, num_classes = 1): backbone = caformer.CAFormerS18(input_shape=(256, 256, 3), pretrained="imagenet", num_classes = 0)

layer_names = ['tf.reshape_51', 'stack3_downsample_conv', 'stack2_block3_mlp_Dense_1', 'stack1_block3_mlp_Dense_1']
"""
Shape of these four layers:
Original: 'stack4_block3_mlp_Dense_1': (None, 64, 512); Replace: 'tf.reshape_51': (None, 8, 8, 512)
Original: 'stack3_block9_mlp_Dense_1': (None, 256, 320); Replace: 'stack3_downsample_conv': (None, 16, 16, 320)
'stack2_block3_mlp_Dense_1': (None, 32, 32, 128)
'stack1_block3_mlp_Dense_1': (None, 64, 64, 64)    
"""

` Hence, I changed 'stack4_block3_mlp_Dense_1' to 'tf.reshape_51' and 'stack3_block9_mlp_Dense_1' to 'stack3_downsample_conv' and the problem works now. However, I tried to train the model for kvasir-seg dataset and the training stopped in Epoch 2.

Epoch 2: val_loss improved from 0.58084 to 0.57210, saving model to best_model.h5 95/95 [==============================] - 2s 11ms/step - loss: 0.3841 - dice_coeff: 0.6159 - bce_dice_loss: 0.8204 - IoU: 0.4140 - zero_IoU: 0.6248 - val_loss: 0.5721 - val_dice_coeff: 0.4233 - val_bce_dice_loss: 4.1260 - val_IoU: 0.2524 - val_zero_IoU: 0.5116 - lr: 1.0000e-04 17/17 [==============================] - 1s 42ms/step - loss: 0.5650 - dice_coeff: 0.4350 - bce_dice_loss: 3.9963 - IoU: 0.2532 - zero_IoU: 0.5206

I just changed the first two layers' names, and the rest remain unchanged. May you give me some guide? Thank you.

huyquoctrinh commented 4 months ago

Hi, thank you so much for this issue. This is the updated version of CAFormerS18 from keras_cv_attention_models, but if you reshape the feature output, just remind to consider about the feature shape that still contains the meaning.

andrewcheng2016 commented 4 months ago

Thank you for your reply. I mistakenly installed the latest version of keras_cv_attention_models, which caused the problem. It is solved by reinstalling keras_cv_attention_models==1.3.9.