hi ,thanks for reply
i use jupyter
I moved the save features out,encoder was already a class attribute.
here is complete code i paste in
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()
class Resnet4Channel(nn.Module):
def init(self, encoder_depth=34, 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(3, 64, kernel_size=7, stride=2, padding=3,
                           bias=False)
   
    
    
    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(512,256,256) #if comment x_unet how i append it to resnet
   
     
    
    self.fc = nn.Linear(512 * (1 if encoder_depth==34 else 4), num_classes)
    


def forward(self, x):
    self.new_classifier = nn.Sequential(*list(encoder.children())[:-1])
    
    self.sfs = [SaveFeatures(self.new_classifier[4])]
    
  
    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, self.sfs[0].features) # features and x input get concatenated to produce 512
    x = x.view(x.size(0), -1)
    x = self.fc(x)

    return x