shel coop
shel coop

Reputation: 45

Getting dimension mismatch error after adding a custom layer in densenet201

I want to create a modified densenet201 ,model by adding a deformable convolution layer after conv1/conv,while keeping the rest of layers unchanged. The modified code is given below;

import tensorflow as tf
from tensorflow.keras.applications import DenseNet201
from tensorflow.keras.layers import Layer, Input
from tensorflow.keras.models import Model

class DeformConv2D(Layer):
    # Placeholder for the actual implementation
    def __init__(self, out_channels, kernel_size, stride=1, padding='same', **kwargs):
        super(DeformConv2D, self).__init__(**kwargs)
        # Initialize your DeformConv2D layer here

    def call(self, inputs):
        # Implement the forward pass of your DeformConv2D layer here
        return inputs  # Placeholder return

class ModifiedDenseNet201(Model):
    def __init__(self, **kwargs):
        super(ModifiedDenseNet201, self).__init__(**kwargs)
        self.base_model = DenseNet201(include_top=False, input_shape=(224, 224, 3))
        self.deform_conv2d = DeformConv2D(out_channels=64, kernel_size=(3, 3), stride=1, padding='same')
        
        # Freeze the layers of the base model
        for layer in self.base_model.layers:
            layer.trainable = False

    def call(self, inputs):
        x = inputs
        for layer in self.base_model.layers:
            if layer.name == 'conv1/conv':
                x = layer(x)
                x = self.deform_conv2d(x)  # Insert the DeformConv2D layer after 'conv1/conv'
            else:
                x = layer(x)
        return x

# Create an instance of the modified model
input_tensor = Input(shape=(224, 224, 3))
modified_densenet201 = ModifiedDenseNet201()
output = modified_densenet201(input_tensor)

# Create the modified model
model = Model(inputs=input_tensor, outputs=output)

# Summary to verify the model structure
model.summary()

However i am getting error as;

ValueError: Exception encountered when calling layer 'conv2_block1_concat' (type Concatenate).
    
    A merge layer should be called on a list of inputs. Received: inputs=Tensor("modified_dense_net201_1/conv2_block1_2_conv/Conv2D:0", shape=(None, 56, 56, 32), dtype=float32) (not a list of tensors)
    
    Call arguments received by layer 'conv2_block1_concat' (type Concatenate):
      • inputs=tf.Tensor(shape=(None, 56, 56, 32), dtype=float32)


Call arguments received by layer "modified_dense_net201_1" (type ModifiedDenseNet201):
  • inputs=tf.Tensor(shape=(None, 224, 224, 3), dtype=float32)

In the concat layer ,dimension mismatch is occuring. How to remove this error to get this modified architecture. Where to change in the code to get this. please help.

I created ModifiedDenseNet201 class. I expected a deformable Conv layer to be added after Conv1/conv layer of existing DenseNet201 keeping the rest of the layers as it is. while executing I am getting errors at the concat layer. It seems like a dimension mismatch.

Upvotes: 1

Views: 49

Answers (0)

Related Questions