Mask_RCNN icon indicating copy to clipboard operation
Mask_RCNN copied to clipboard

edit conv1 layer weights in order to work with grayscale images

Open avnerst1 opened this issue 3 years ago • 0 comments

hey! :) I have updated my code to work with grayscale images using the coco pre-trained weights, and it works fine when I exclude the conv1 layer, but it leads to very bad results. I read that a good way to use this layer is to sum the weights of all 3 channels into 1 channel, but I have no idea how to do it (have tried some stuff, and nothing worked). does anybody have any idea how to do it? I'm adding a code example I saw for doing this with pytorch from https://stackoverflow.com/questions/51995977/how-can-i-use-a-pre-trained-neural-network-with-grayscale-images: `def _load_pretrained(model, url, inchans=3): state_dict = model_zoo.load_url(url) if inchans == 1: conv1_weight = state_dict['conv1.weight'] state_dict['conv1.weight'] = conv1_weight.sum(dim=1, keepdim=True) elif inchans != 3: assert False, "Invalid number of inchans for pretrained weights" model.load_state_dict(state_dict)

def resnet50(pretrained=False, inchans=3): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(Bottleneck, [3, 4, 6, 3], inchans=inchans) if pretrained: _load_pretrained(model, model_urls['resnet50'], inchans=inchans) return model`

thanks!!!

avnerst1 avatar Aug 13 '22 03:08 avnerst1