I made a 2 step model with a u net and a gan as 2 consecutive steps.
the output that i get from the u net , i apply thresholding to get a mask , and pass the output and mask to the gan for inpainting.
i want to make the threshold also learnable .
i kept the threshold as nn.Parameter() , and also set required_grad = True , but then when I checkked while training the model , the parameter value is not getting updated at all.
The same init value of 0.5 is only coming.
Code
class Combined_Model(nn.Module):
def __init__(self , options):
super(Combined_Model, self).__init__()
self.pretrained_state_dict = torch.load(os.path.join(options.pretrained, 'G0000000.pt'), map_location=torch.device('cuda'))
self.unet = UNet().to(options.device)
if options.with_prompts:
self.inpainter = Prompted_InpaintGenerator(options)
self.org_gan = InpaintGenerator(options)
#self.inpainter.load_state_dict(load_pretrained_weights(self.org_gan, self.pretrained_state_dict), strict=False)
self.inpainter.load_state_dict(load_pretrained_weights(self.org_gan , self.inpainter) , strict=True)
else:
self.inpainter = InpaintGenerator(options)
self.inpainter.load_state_dict(torch.load(os.path.join(options.pretrained, 'G0000000.pt'), map_location=options.device), strict=False)
self.models = [self.unet, self.inpainter]
self.learnable_threshold = nn.Parameter(torch.tensor(0.5), requires_grad=True)
def forward(self , x):
unet_output = self.unet(x)
unet_output_gray = tensor_to_cv2_gray(unet_output)
flary_img_gray = tensor_to_cv2_gray(x)
print(self.learnable_threshold)
difference = (torch.from_numpy(flary_img_gray) - torch.from_numpy(unet_output_gray))
#difference_tensor = torch.tensor(difference, dtype=torch.float32).to(options.device)
difference_tensor = difference.clone().to(options.device)
binary_mask = torch.where(difference_tensor > self.learnable_threshold, torch.tensor(1.0).to(options.device), torch.tensor(0.0).to(options.device))
binary_mask = binary_mask.unsqueeze(1)
inpainted_output = self.inpainter(unet_output , binary_mask)
return inpainted_output