How can I change pretrained resnet model to accept single channel image?

I’m trying to use per-trained ResNet-18 model for binary classification with modification in input channel and kernel size of 1st Conv layer. What is the best way to do this? Also does this approach uses pretrained weight for conv1 layer?Currently I’m trying like this

model=models.resnet18(pretrained=True)
model.conv1=nn.Conv2d(1,64,kernel_size=(3,3),stride=(2,2),padding=(3,3),bias=False)
num_ftrs=model.fc.in_features
model.fc=nn.Linear(num_ftrs,2)

someone told me this, but I have not tried it yet

1 Like
import torch
from torchvision.models import resnet18
net = resnet18(pretrained=False) # You should put 'True' here 
net.conv1 = torch.nn.Conv1d(1, 64, (7, 7), (2, 2), (3, 3), bias=False)
batch = torch.rand(4, 1, 224, 224) # in the NCHW format
net(batch).size()