Accessing the last convolutional layer in the model name

11 Views Asked by At

class PneumoniaCnnModel(BinaryClassificationBase): def init(self): super().init()

    self.base_model = models.resnet50(pretrained=True)
    modules = list(self.base_model.children())[:-2]
    self.base_model = nn.Sequential(*modules)
    # Freeze the lower layers
    set_trainable = False
    for param in self.base_model.parameters():
        if set_trainable:
            param.requires_grad = True
        else:
            param.requires_grad = False

        if param.name == "conv5_block3_2_conv":
            set_trainable = True

   
    for name, module in self.base_model.named_modules():
        #print(name)
        if name == last_conv_layer_name:
            last_conv_layer = module
            break

    if last_conv_layer is None:
        raise ValueError(f"Could not find layer with name '{last_conv_layer_name}'")
   

def forward(self, x):
    x = self.base_model(x)
    x = self.custom_layers(x)
    return x
def get_last_conv_layer(self):
    return self.last_conv_layer

want a way to acess the last layer for cam and GRADCAM the calsculati teh last_conv_layer is giving none. unable to access the last layer of the models.

0

There are 0 best solutions below