I tried to use ResNet to extract features of images. After the baseline network, I add some convolutional layers to fuse some features. But when I try to make my code neater like the following, the Net1 is my original code part and the Net2 is the improved code part.
class Net1(nn.Module):
def __init__(self, ):
super(Net1, self).__init__()
self.fusion = nn.Sequential(nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
self.get_feat = nn.Sequential(nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
def forward(self, x):
feat = self.get_feat(self.fusion(x))
return feat
class Net2(nn.Module):
def __init__(self, ):
super(Net2, self).__init__()
self.feat_fusion = nn.Sequential(nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
def forward(self, x):
feat = self.feat_fusion(x)
return feat
Although I didn’t change any other configurations of my model, the loss of Net1 was converged about 0.8 and the loss of Net2 was converged about 0.2. Isn’t these two operations are equivalent? And why these losses are different?