class NLayerDiscriminator(nn.Module): def init(self, input_nc=3, ndf=64, n_layers=3, norm_layer=nn.BatchNorm2d, use_sigmoid=False, use_parallel=True): super(NLayerDiscriminator, self).init() self.use_parallel = use_parallel if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d self.conv1 = nn.Conv2d(input_nc, ndf, kernel_size=3, padding=1) self.conv_offset1 = nn.Conv2d(ndf, 18, kernel_size=3, stride=1, padding=1) init_offset1 = torch.Tensor(np.zeros([18, ndf, 3, 3])) self.conv_offset1.weight = torch.nn.Parameter(init_offset1) # 初始化为0 self.conv_mask1 = nn.Conv2d(ndf, 9, kernel_size=3, stride=1, padding=1) init_mask1 = torch.Tensor(np.zeros([9, ndf, 3, 3]) + np.array([0.5])) self.conv_mask1.weight = torch.nn.Parameter(init_mask1) # 初始化为0.5 kw = 4 padw = int(np.ceil((kw-1)/2)) nf_mult = 1 for n in range(1, n_layers): nf_mult_prev = nf_mult nf_mult = min(2n, 8) self.sequence2 = [ nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=2, padding=padw, bias=use_bias), norm_layer(ndf * nf_mult), nn.LeakyReLU(0.2, True) ] nf_mult_prev = nf_mult nf_mult = min(2n_layers, 8) self.sequence2 += [ nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=1, padding=padw, bias=use_bias), norm_layer(ndf * nf_mult), nn.LeakyReLU(0.2, True) ] self.sequence2 += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)] if use_sigmoid: self.sequence2 += [nn.Sigmoid()] def forward(self, input): input = self.conv1(input) offset1 = self.conv_offset1(input) mask1 = torch.sigmoid(self.conv_mask1(input)) sequence1 = [ torchvision.ops.deform_conv2d(input=input, offset=offset1, weight=self.conv1.weight, mask=mask1, padding=(1, 1)) ] sequence2 = sequence1 + self.sequence2 self.model = nn.Sequential(*sequence2) nn.LeakyReLU(0.2, True) return self.model(input),上述代码中出现错误:RuntimeError: Expected weight_c.size(1) * n_weight_grps == input_c.size(1) to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.),请问如何解决,给出修改后的代码
时间: 2023-08-21 09:06:26 浏览: 462
NDF.rar_NDF_牛顿法_牛顿法方程组
这个错误通常是由于在使用分组卷积时,输入通道数和卷积核数量不匹配引起的。根据错误提示,可以得知是在一次卷积中出现了此问题。根据代码,可以看出是在使用`torchvision.ops.deform_conv2d`函数时出现了此问题。
解决方法是检查输入通道数和卷积核数量是否匹配,如果不匹配,则需要对它们进行调整。
下面是修改后的代码:
```
class NLayerDiscriminator(nn.Module):
def __init__(self, input_nc=3, ndf=64, n_layers=3, norm_layer=nn.BatchNorm2d, use_sigmoid=False, use_parallel=True):
super(NLayerDiscriminator, self).__init__()
self.use_parallel = use_parallel
if type(norm_layer) == functools.partial:
use_bias = norm_layer.func == nn.InstanceNorm2d
else:
use_bias = norm_layer == nn.InstanceNorm2d
self.conv1 = nn.Conv2d(input_nc, ndf, kernel_size=3, padding=1)
self.conv_offset1 = nn.Conv2d(ndf, 18, kernel_size=3, stride=1, padding=1)
init_offset1 = torch.Tensor(np.zeros([18, ndf, 3, 3]))
self.conv_offset1.weight = torch.nn.Parameter(init_offset1) # 初始化为0
self.conv_mask1 = nn.Conv2d(ndf, 9, kernel_size=3, stride=1, padding=1)
init_mask1 = torch.Tensor(np.zeros([9, ndf, 3, 3]) + np.array([0.5]))
self.conv_mask1.weight = torch.nn.Parameter(init_mask1) # 初始化为0.5
kw = 4
padw = int(np.ceil((kw-1)/2))
nf_mult = 1
self.sequence2 = nn.ModuleList()
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 8)
self.sequence2.extend([
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=2, padding=padw, bias=use_bias),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
])
nf_mult_prev = nf_mult
nf_mult = min(2**n_layers, 8)
self.sequence2.extend([
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=1, padding=padw, bias=use_bias),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True),
nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)
])
if use_sigmoid:
self.sequence2.append(nn.Sigmoid())
def forward(self, input):
input = self.conv1(input)
offset1 = self.conv_offset1(input)
mask1 = torch.sigmoid(self.conv_mask1(input))
sequence1 = [torchvision.ops.deform_conv2d(input=input, offset=offset1, weight=self.conv1.weight, mask=mask1, padding=(1, 1))]
sequence2 = sequence1 + self.sequence2
for i, layer in enumerate(sequence2):
sequence2[i] = layer(sequence2[i-1])
return sequence2[-1]
```
修改的主要内容如下:
1. 将`self.sequence2`改为`nn.ModuleList()`类型,这样可以方便地添加和修改网络结构;
2. 在循环中使用`extend`方法添加卷积层,避免在循环中使用`+=`操作符引起的错误;
3. 在`forward`函数中,使用循环遍历`sequence2`中的每一层,并将其输入和输出连接起来。
希望这样的修改能够帮助到你。
阅读全文