1. GPU空间充足,但训练和测试同时进行时,报空间不足,即RuntimeError: CUDA out of memory.
答:很多博文建议batch改小,但是可能很多人的错误在于没有固定网络,导致测试集进入网络时保存了大量参数值,因此:
model.eval()
with torch.no_grad():
for k, test_data in enumerate(test_loader):
https://blog.csdn.net/xiaoxifei/article/details/84377204
2. soft-argmax梯度消失
def softargmax2d(input, beta=10):
# print(input.type)
*_, h, w = input.shape
input = beta * input.reshape(*_, h * w)
input = nn.functional.softmax(input, dim=-1)
indices_c, indices_r = np.meshgrid(
np.linspace(0, 1, w),
np.linspace(0, 1, h),
indexing='xy'
)
indices_r = torch.from_numpy(np.reshape(indices_r, (-1, h * w))).float().cuda()
indices_c = torch.from_numpy(np.reshape(indices_c, (-1, h * w))).float().cuda()
result_r = torch.sum((h - 1) * input * indices_r, dim=-1)
result_c = torch.sum((w - 1) * input * indices_c, dim=-1)
result = torch.stack([result_r, result_c], dim=-1)
return result
增大beta能更快得到积分数值解,即概率最大的图像坐标点。但是,在求导过程中,由于soft-argmax的分母存在exp(beta*x),会导致梯度消失。所以,beta不能任意大。