hi ,
I was able to resolve dimension related issues with this code.Howerver now filling running fit
i get load dict state error missing keys :
Missing key(s) in state_dict: “conv1.weight”, “bn1.weight”, “bn1.bias”, “bn1.running_mean”, “bn1.running_var”, “layer1.0.conv1.weight”, “layer1.0.bn1.weight”, …
…at the end of error ,in between all layers are printed…
Unexpected key(s) in state_dict: “0.0.weight”, “0.1.weight”, “0.1.bias”, “0.1.running_mean”, “0.1.running_var”
class SaveFeatures():
features=None
  
def __init__(self, m): 
    self.hook = m.register_forward_hook(self.hook_fn)
  
def hook_fn(self, module, input, output): 
    self.features = output
  
def remove(self): self.hook.remove()

def get_base(m):
#layers = cut_model(f(True), cut)
return nn.Sequential(*list(m.children()) )
class UnetBlock(nn.Module):
def init(self, up_in, x_in, n_out,m):
super().init()
#up_out = x_out = n_out//2
x_out = 1024
#n_out//2
up_out=1024
self.x_conv  = nn.Conv2d(1024,  x_out,  1)
self.tr_conv = nn.ConvTranspose2d(up_in, up_out, 2, stride=2)
self.bn = nn.BatchNorm2d(x_out+up_out)
self.sfs = [SaveFeatures(get_base(m)[5]) ]
def forward(self, up_p  ):
up_p = self.tr_conv(up_p)
x_p = self.x_conv(self.sfs[0].features)
cat_p = torch.cat([up_p,x_p], dim=1)
return self.bn(F.relu(cat_p))
RESNET_ENCODERS = {
34: torchvision.models.resnet34,
50: torchvision.models.resnet50,
101: torchvision.models.resnet101,
152: torchvision.models.resnet152,
}
class Resnet4Channel1(nn.Module):
def init(self, encoder_depth=50, pretrained=True, num_classes=28):
super().init()
    encoder = RESNET_ENCODERS[encoder_depth](pretrained=pretrained)
    
    # we initialize this conv to take in 4 channels instead of 3
    # we keeping corresponding weights and initializing new weights with zeros
    # this trick taken from https://www.kaggle.com/iafoss/pretrained-resnet34-with-rgby-0-460-public-lb
    w = encoder.conv1.weight
    self.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3,
                           bias=False)
    self.conv1.weight  = torch.nn.Parameter(torch.cat((w,w[:,:1,:,:]),dim=1))
    #nn.Parameter(torch.cat((w,torch.zeros(64,1,7,7)),dim=1))
    
    
    self.bn1 = encoder.bn1
    self.relu = nn.ReLU(inplace=True) 
    self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

    self.layer1 = encoder.layer1
    self.layer2 = encoder.layer2
    self.layer3 = encoder.layer3
    self.layer4 = encoder.layer4
    
    
    self.avgpool = encoder.avgpool
    self.x_unet  =  UnetBlock(2048,256,256,self.layer3)
    #self.x_unet  =  UnetBlock(2048,256,256)
     
    
    self.fc = nn.Linear(512 * (1 if encoder_depth==34 else 4), num_classes)
    
    #self.sfs = [SaveFeatures(get_base()[4]) ]
    #self.sfs=get_base(self.layer3)[5]
    
    
def forward(self, x):
    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu(x)
    x = self.maxpool(x)

    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    

    x = self.avgpool(x)
    x = self.x_unet(x )
    
    x = x.view(x.size(0), -1)
   
    x = self.fc(x)

    return x
def close(self):
    for sf in self.sfs:
        
        
        sf.remove()
        gc.collect()