pytorch备忘录
1.测试时不需要梯度:
with torch.no_grad(): out = net(input)
2.可视化网络生成的图片(整个batch)
from torchvision.utils import save_image out = net(input) save_image(out,'./a.jpg')
3.按条件修改tensor值
out = torch.where(out>0.5, torch.full_like(out, 1), torch.full_like(out, 0))
4.加载预训练模型:
net.load_state_dict(torch.load('/home/dell/checkpoint.pt'))
5.预训练模型键值不对应:
def load_GPU(model, model_path, mapLoc='cpu'): state_dict = torch.load(model_path, map_location=mapLoc)['net'] # create new OrderedDict that does not contain `module.` from collections import OrderedDict new_state_dict = OrderedDict() for k, v in state_dict.items(): name = k[7:] # remove `module.` new_state_dict[name] = v model.load_state_dict(new_state_dict) return model
6.tensor赋值:
out[out > 0.5] = 1
7.torch.backends.cudnn.deterministic = False
#该函数的作用是通过预先统计特征图的大小,然后在之后的卷积过程中选用合适的矩阵计算来代替卷积,从而达到加速的目的,
#然而空洞卷积是一个动态的过程,因此需要每次都重新统计,反而使计算速度极大的减慢,因此在带有空洞卷积的网络中要将该参数设置为False
8.在gpu上运行的程序可能报错位置与真实出错位置不同,可以使用:
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
让其相同