1. 准备工作

安装

pip install torch==0.3.1 --user

查看版本

import torch
print(torch.__version__)

2. 矩阵相关

变形

imgs = image.view(-1,1,256,256).repeat(1,3,1,1)

3. 其他

3.1. 设置GPU的方法

torch.cuda.set_device(7)

3.2. debug

查看feature map大小

import torch
from torch.autograd import Variable

fms = model(Variable(torch.randn(1,1,256,256)))
for fm in fms:
    print(fm.size())

3.3. 模型

3.3.1. 加载模型

针对invalid device ordinal错误,解决方案是map_location

checkpoint = torch.load(self.model_path, map_location=lambda storage, loc: storage)
self.model.load_state_dict(checkpoint['model'], strict=False)

针对python2到python3的迁移:

from functools import partial
import pickle
pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
model = torch.load(model_file, map_location=lambda storage, loc: storage, pickle_module=pickle)

3.3.2. 测试模型

记得 volatile=True,否则容易爆显存。

input_img_var = torch.autograd.Variable(images.cuda(), volatile=True)
input_mask_var = torch.autograd.Variable(masks.cuda(), volatile=True)

3.4. cuda

model.cuda() #RAM + 0.9G

如果输入图片大小一致:

torch.backends.cudnn.benchmark = True

僵尸进程占用gpu,kill的方法:

ps x |grep python|awk '{print $1}'|xargs kill

3.5. 资源

results matching ""

    No results matching ""