def calc_gradient_penalty(self, netD, real_data, fake_data): alpha = torch.rand(1, 1) alpha = alpha.expand(real_data.size()) alpha = alpha.cuda() interpolates = alpha * real_data + ((1 - alpha) * fake_data) interpolates = interpolates.cuda() interpolates = Variable(interpolates, requires_grad=True) disc_interpolates, s = netD.forward(interpolates) gradients1 = autograd.grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=torch.ones(disc_interpolates.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0] print(gradients1) print(gradients1.shape) gradient_penalty = (((gradients1.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA) return gradient_penalty,已知上述代码中,s为超像素池化后的分数,如何将s加入到patchgan 的梯度惩罚中,输入图像的梯度是哪个,怎么计算,给出改进后的代码
时间: 2023-07-16 09:15:12 浏览: 142
可以将超像素池化的分数s与判别器的输出disc_interpolates相加,作为最终的判别结果。即将以下代码:
```
disc_interpolates, s = netD.forward(interpolates)
```
改为:
```
disc_interpolates = netD.forward(interpolates) + s
```
而输入图像的梯度应该是interpolates的梯度,即将以下代码:
```
gradients1 = autograd.grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=torch.ones(disc_interpolates.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0]
```
改为:
```
gradients1 = autograd.grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=torch.ones(disc_interpolates.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True, allow_unused=True)[0]
```
这样可以允许s没有梯度信息时,梯度惩罚仍能正常计算。最终改进后的代码如下:
```
def calc_gradient_penalty(self, netD, real_data, fake_data):
alpha = torch.rand(1, 1)
alpha = alpha.expand(real_data.size())
alpha = alpha.cuda()
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
interpolates = interpolates.cuda()
interpolates = Variable(interpolates, requires_grad=True)
disc_interpolates = netD.forward(interpolates) + s
gradients1 = autograd.grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=torch.ones(disc_interpolates.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True, allow_unused=True)[0]
gradient_penalty = (((gradients1.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA)
return gradient_penalty
```
阅读全文