? ? ? ? ?最近我在模型训练损失里加入了LPIPS深度感知损失,训练的时候就出现了如上的报错,具体解释为:调用梯度反向传播loss.backward()时,我们计算梯度,需要一个标量的loss(即该loss张量的维度为1,只包含一个元素);而LPIPS的输出的loss为一个[4,1,1,1]的4维张量(batch_size,c,h,w),因此报错。
修正:
?
def lpips_loss(img1, img2):
# loss_fn_alex = lpips.LPIPS(net='alex') # best forward scores
loss_fn_vgg = lpips.LPIPS(net='vgg') # closer to "traditional" perceptual loss, when used for optimization
loss_fn_vgg.cuda()
loss = loss_fn_vgg.forward(img1, img2)
loss = torch.mean(loss)
return loss
参考:
grad can be implicitly created only for scalar outputs-CSDN博客https://blog.csdn.net/qq_39208832/article/details/117415229
lpips · PyPIhttps://pypi.org/project/lpips/