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(input_nc, 18, kernel_size=3, stride=1, padding=1) init_offset1 = torch.Tensor(np.zeros([18, input_nc, 3, 3])) self.conv_offset1.weight = torch.nn.Parameter(init_offset1) # 初始化为0 self.conv_mask1 = nn.Conv2d(input_nc, 9, kernel_size=3, stride=1, padding=1) init_mask1 = torch.Tensor(np.zeros([9, input_nc, 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.sequence = [ 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.sequence += [ 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.sequence += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)] if use_sigmoid: self.sequence += [nn.Sigmoid()] def forward(self, 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)) ] sequence = sequence1 + self.sequence self.model = nn.Sequential(*sequence) return self.model(input),上述代码出现问题:TypeError: torch.cuda.FloatTensor is not a Module subclass,如何修改
时间: 2023-08-22 18:04:22 浏览: 432
这个错误一般出现在将张量作为模型输入时。需要将张量包装成一个 `nn.Parameter` 对象,或者将其转换为一个 `nn.Module` 对象。你可以尝试将最后一行代码修改为以下内容:
```python
return nn.Sequential(*sequence)(input)
```
这里直接将 `nn.Sequential` 对象应用于输入张量 `input`,然后返回模型的输出。这样就可以避免将张量作为模型输入时出现的 `TypeError` 错误。
相关问题
Defines the PatchGAN discriminator with the specified arguments. 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)) 上述代码中出现错误: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.),请问如何解决,给出修改后的代码
这个错误通常是由于卷积层的通道数和输入数据的通道数不匹配导致的。根据错误信息可以看出,这个错误是在第 49 行的 deform_conv2d 函数中出现的。根据代码,这个函数是使用可变形卷积进行特征提取的,因此需要注意卷积层的通道数和输入数据的通道数是否匹配。具体来说,需要检查卷积层的输入通道数和权重矩阵的通道数是否匹配。
下面是修改后的代码,主要是修改了可变形卷积的输入参数,确保通道数匹配:
```python
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 = []
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 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(2**n_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,
stride=1,
dilation=1
)
]
for layer in self.sequence2:
sequence1.append(layer(sequence1[-1]))
return sequence1[-1]
```
需要注意的是,由于 deform_conv2d 函数的输入参数有很多,需要根据实际情况进行调整。
如何将self.conv1 = nn.Conv2d(4 * num_filters, num_filters, kernel_size=3, padding=1) self.conv_offset1 = nn.Conv2d(512, 18, kernel_size=3, stride=1, padding=1) init_offset1 = torch.Tensor(np.zeros([18, 512, 3, 3])) self.conv_offset1.weight = torch.nn.Parameter(init_offset1) # 初始化为0 self.conv_mask1 = nn.Conv2d(512, 9, kernel_size=3, stride=1, padding=1) init_mask1 = torch.Tensor(np.zeros([9, 512, 3, 3]) + np.array([0.5])) self.conv_mask1.weight = torch.nn.Parameter(init_mask1) # 初始化为0.5 与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 kw = 4 padw = int(np.ceil((kw-1)/2)) sequence = [ nn.Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw), nn.LeakyReLU(0.2, True) ] nf_mult = 1 for n in range(1, n_layers): nf_mult_prev = nf_mult nf_mult = min(2n, 8) sequence += [ 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) sequence += [ 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) ] sequence += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)] if use_sigmoid: sequence += [nn.Sigmoid()] self.model = nn.Sequential(*sequence) def forward(self, input): return self.model(input)中,请给出修改后的代码
可以将self.conv1、self.conv_offset1和self.conv_mask1加入到NLayerDiscriminator中的nn.Sequential序列中,如下所示:
```
import torchvision
from torchvision.ops.deform_conv import DeformConv2d
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
kw = 4
padw = int(np.ceil((kw-1)/2))
nf_mult = 1
sequence = [
nn.Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw),
nn.LeakyReLU(0.2, True)
]
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 8)
sequence += [
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)
sequence += [
DeformConv2d(
ndf * nf_mult_prev, ndf * nf_mult, kernel_size=3, stride=1, padding=1,
dilation=1, groups=1, deformable_groups=1, bias=False
),
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:
sequence += [nn.Sigmoid()]
self.model = nn.Sequential(*sequence)
self.conv1 = nn.Conv2d(4 * ndf, 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)
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)
def forward(self, input):
x = input
for module in self.model:
if isinstance(module, DeformConv2d):
offset_mask = torch.cat([self.conv_offset1(x), self.conv_mask1(x)], dim=1)
x = module(x, offset_mask)
else:
x = module(x)
return x
```
这里使用了torchvision.ops.deform_conv2d中的DeformConv2d作为最后一层卷积层,因此需要将self.conv1、self.conv_offset1和self.conv_mask1的通道数修改为ndf。在forward中,对于DeformConv2d层,需要将self.conv_offset1和self.conv_mask1的输出拼接起来作为offset_mask输入。
阅读全文