Hi all
I keep getting this error:
ValueError: Expected more than 1 spatial element when training, got input size torch.Size([8, 64, 1, 1]).
Here is my code:
class DepthWiseConv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, groups, stride):
super(DepthWiseConv2d, self).__init__()
self.depthwise = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size = kernel_size,
groups = groups, stride = stride, padding = 1),
nn.InstanceNorm2d(in_channels),
nn.ReLU(True)
)
self.pointwise = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size = kernel_size,
stride = stride),
nn.InstanceNorm2d(out_channels),
nn.ReLU(True)
)
def forward(self, x):
out = self.depthwise(x)
out = self.pointwise(out)
return out
class VGGEncoder(nn.Module):
def __init__(self):
super().__init__()
vgg = vgg19(pretrained=True).features
self.slice1 = vgg[: 2]
self.slice2 = vgg[2: 7]
self.slice3 = vgg[7: 12]
self.slice4 = vgg[12: 21]
for p in self.parameters():
p.requires_grad = False
def forward(self, images, output_last_feature=False):
h1 = self.slice1(images)
h2 = self.slice2(h1)
h3 = self.slice3(h2)
h4 = self.slice4(h3)
if output_last_feature:
return h4
else:
return h1, h2, h3, h4
class WeightAndBias(nn.Module):
“”“Weight/Bias Network”“”
def __init__(self, in_channels = 512):
super(WeightAndBias,self).__init__()
self.dwconv1 = DepthWiseConv2d(in_channels, 128, 3, 128, 2)
self.dwconv2 = DepthWiseConv2d(128, 64, 3, 64, 2)
# self.adapool1 = nn.AdaptiveMaxPool2d()
self.dwconv3 = DepthWiseConv2d(64, 64, 3, 64, 2)
# self.adapool2 = nn.AdaptiveMaxPool2d()
def forward(self, x):
out = self.dwconv1(x)
out = self.dwconv2(out)
print(out.shape)
# out = self.adapool1(out)
out = self.dwconv3(out)
# out = self.adapool2(out)
return out
Here is my test case:
s = torch.rand(8,3,256,256)
out = VGGEncoder()(s, True)
out = WeightAndBias(512)(out)
print(out.shape)
Thank you!