RuntimeError: The size of tensor a (4144) must match the size of tensor b (256) at non-singleton dimension 3 site:stackoverflow.com
RuntimeError: The size of tensor a (4144) must match the size of tensor b (256) at non-singleton dimension 3 site:stackoverflow.com
我正在训练图像大小为 (3, 256, 256)
的生成器网络。网络如下图
# Number of channels in the training images. For color images this is 3
nc = 3
# Size of z latent vector (i.e. size of generator input)
nz = 3
# Size of feature maps in generator
ngf = 64
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 64 x 64
)
def forward(self, input):
return self.main(input)
net_input_saved = net_input.detach().clone()
noise = net_input.detach().clone()
out_avg = None
last_net = None
psrn_noisy_last = 0
loss = []
psnr_noise = []
psnr_ground = []
i = 0
def closure():
global i, out_avg, psrn_noisy_last, last_net, net_input, loss
if reg_noise_std > 0:
net_input = net_input_saved + (noise.normal_() * reg_noise_std) #changing the input to the netwok
out = net(net_input)
# Smoothing
if out_avg is None:
out_avg = out.detach()
else:
out_avg = out_avg * exp_weight + out.detach() * (1 - exp_weight) # calculating average network output
total_loss = mse(out, img_noisy_torch)
total_loss.backward()
loss.append(total_loss.item())
# caculating psrn
psrn_noisy = compare_psnr(img_noisy_np, out.detach().cpu().numpy()[0]) # comparing psnr for the output image and the actual noisy image
psrn_gt = compare_psnr(img_noisy_np, out.detach().cpu().numpy()[0]) # comparing psnr for the output image and the original image
psrn_gt_sm = compare_psnr(img_np, out_avg.detach().cpu().numpy()[0]) # comparing psnr for the output average and the original image
psnr_noise.append(psrn_noisy)
psnr_ground.append(psrn_gt)
if PLOT and i % show_every == 0:
out_np = torch_to_np(out)
# plotting the output image along the average image calculated
print(f'\n\nAfter {i} iterations: ')
print ('Iteration %05d Loss %f PSNR_noisy: %f PSRN_gt: %f PSNR_gt_sm: %f' % (i, total_loss.item(), psrn_noisy, psrn_gt, psrn_gt_sm), '\r', end='\n')
plot_image_grid([np.clip(out_np, 0, 1),
np.clip(torch_to_np(out_avg), 0, 1)], factor=figsize, nrow=1)
# Backtracking
if i % show_every:
if psrn_noisy - psrn_noisy_last < -5:
print('Falling back to previous checkpoint.')
for new_param, net_param in zip(last_net, net.parameters()):
net_param.data.copy_(new_param.cuda())
return total_loss*0
else:
last_net = [x.detach().cpu() for x in net.parameters()]
psrn_noisy_last = psrn_noisy
i += 1
return total_loss
p = get_params(OPT_OVER, net, net_input)
optimize(OPTIMIZER, p, closure, LR, num_iter)
当我尝试训练时出现错误
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py:528: UserWarning: Using a target size (torch.Size([1, 3, 256, 256])) that is different to the input size (torch.Size([1, 3, 4144, 4144])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-83-ed04cb48a6ab> in <module>()
67
68 p = get_params(OPT_OVER, net, net_input)
---> 69 optimize(OPTIMIZER, p, closure, LR, num_iter)
5 frames
/content/utils/common_utils.py in optimize(optimizer_type, parameters, closure, LR, num_iter)
227 for j in range(num_iter):
228 optimizer.zero_grad()
--> 229 closure()
230 optimizer.step()
231 else:
<ipython-input-83-ed04cb48a6ab> in closure()
25 out_avg = out_avg * exp_weight + out.detach() * (1 - exp_weight) # calculating average network output
26
---> 27 total_loss = mse(out, img_noisy_torch)
28 total_loss.backward()
29
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
526
527 def forward(self, input: Tensor, target: Tensor) -> Tensor:
--> 528 return F.mse_loss(input, target, reduction=self.reduction)
529
530
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction)
3087 reduction = _Reduction.legacy_get_string(size_average, reduce)
3088
-> 3089 expanded_input, expanded_target = torch.broadcast_tensors(input, target)
3090 return torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction))
3091
/usr/local/lib/python3.7/dist-packages/torch/functional.py in broadcast_tensors(*tensors)
71 if has_torch_function(tensors):
72 return handle_torch_function(broadcast_tensors, tensors, *tensors)
---> 73 return _VF.broadcast_tensors(tensors) # type: ignore[attr-defined]
74
75
RuntimeError: The size of tensor a (4144) must match the size of tensor b (256) at non-singleton dimension 3
我知道这个错误是由于张量大小不匹配引起的,但我无法纠正这个错误。
- 我正在向网络提供大小为
torch.Size([1, 3, 256, 256])
的噪声 z
,但出现错误。
我认为问题是 the net_input
在通过模型之前不是 size=(1,3,256,256)
而不是 (1,3,4144,4144)
。尝试调整大小 net_input
.
我正在训练图像大小为 (3, 256, 256)
的生成器网络。网络如下图
# Number of channels in the training images. For color images this is 3
nc = 3
# Size of z latent vector (i.e. size of generator input)
nz = 3
# Size of feature maps in generator
ngf = 64
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 64 x 64
)
def forward(self, input):
return self.main(input)
net_input_saved = net_input.detach().clone()
noise = net_input.detach().clone()
out_avg = None
last_net = None
psrn_noisy_last = 0
loss = []
psnr_noise = []
psnr_ground = []
i = 0
def closure():
global i, out_avg, psrn_noisy_last, last_net, net_input, loss
if reg_noise_std > 0:
net_input = net_input_saved + (noise.normal_() * reg_noise_std) #changing the input to the netwok
out = net(net_input)
# Smoothing
if out_avg is None:
out_avg = out.detach()
else:
out_avg = out_avg * exp_weight + out.detach() * (1 - exp_weight) # calculating average network output
total_loss = mse(out, img_noisy_torch)
total_loss.backward()
loss.append(total_loss.item())
# caculating psrn
psrn_noisy = compare_psnr(img_noisy_np, out.detach().cpu().numpy()[0]) # comparing psnr for the output image and the actual noisy image
psrn_gt = compare_psnr(img_noisy_np, out.detach().cpu().numpy()[0]) # comparing psnr for the output image and the original image
psrn_gt_sm = compare_psnr(img_np, out_avg.detach().cpu().numpy()[0]) # comparing psnr for the output average and the original image
psnr_noise.append(psrn_noisy)
psnr_ground.append(psrn_gt)
if PLOT and i % show_every == 0:
out_np = torch_to_np(out)
# plotting the output image along the average image calculated
print(f'\n\nAfter {i} iterations: ')
print ('Iteration %05d Loss %f PSNR_noisy: %f PSRN_gt: %f PSNR_gt_sm: %f' % (i, total_loss.item(), psrn_noisy, psrn_gt, psrn_gt_sm), '\r', end='\n')
plot_image_grid([np.clip(out_np, 0, 1),
np.clip(torch_to_np(out_avg), 0, 1)], factor=figsize, nrow=1)
# Backtracking
if i % show_every:
if psrn_noisy - psrn_noisy_last < -5:
print('Falling back to previous checkpoint.')
for new_param, net_param in zip(last_net, net.parameters()):
net_param.data.copy_(new_param.cuda())
return total_loss*0
else:
last_net = [x.detach().cpu() for x in net.parameters()]
psrn_noisy_last = psrn_noisy
i += 1
return total_loss
p = get_params(OPT_OVER, net, net_input)
optimize(OPTIMIZER, p, closure, LR, num_iter)
当我尝试训练时出现错误
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py:528: UserWarning: Using a target size (torch.Size([1, 3, 256, 256])) that is different to the input size (torch.Size([1, 3, 4144, 4144])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-83-ed04cb48a6ab> in <module>()
67
68 p = get_params(OPT_OVER, net, net_input)
---> 69 optimize(OPTIMIZER, p, closure, LR, num_iter)
5 frames
/content/utils/common_utils.py in optimize(optimizer_type, parameters, closure, LR, num_iter)
227 for j in range(num_iter):
228 optimizer.zero_grad()
--> 229 closure()
230 optimizer.step()
231 else:
<ipython-input-83-ed04cb48a6ab> in closure()
25 out_avg = out_avg * exp_weight + out.detach() * (1 - exp_weight) # calculating average network output
26
---> 27 total_loss = mse(out, img_noisy_torch)
28 total_loss.backward()
29
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
526
527 def forward(self, input: Tensor, target: Tensor) -> Tensor:
--> 528 return F.mse_loss(input, target, reduction=self.reduction)
529
530
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction)
3087 reduction = _Reduction.legacy_get_string(size_average, reduce)
3088
-> 3089 expanded_input, expanded_target = torch.broadcast_tensors(input, target)
3090 return torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction))
3091
/usr/local/lib/python3.7/dist-packages/torch/functional.py in broadcast_tensors(*tensors)
71 if has_torch_function(tensors):
72 return handle_torch_function(broadcast_tensors, tensors, *tensors)
---> 73 return _VF.broadcast_tensors(tensors) # type: ignore[attr-defined]
74
75
RuntimeError: The size of tensor a (4144) must match the size of tensor b (256) at non-singleton dimension 3
我知道这个错误是由于张量大小不匹配引起的,但我无法纠正这个错误。
- 我正在向网络提供大小为
torch.Size([1, 3, 256, 256])
的噪声z
,但出现错误。
我认为问题是 the net_input
在通过模型之前不是 size=(1,3,256,256)
而不是 (1,3,4144,4144)
。尝试调整大小 net_input
.