欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pytorch问题记录(1)

程序员文章站 2022-03-27 09:37:35
...

问题一

RuntimeError: cuda runtime error (59) : device-side assert triggered at /home/loop/pytorch-master/torch/lib/THC/generic/THCTensorMath.cu:15

参考文章 https://blog.csdn.net/u011276025/article/details/73826562
通过输出label的值发现,error出现的地方label中均有最大值。
所以可以判断为pytorch所设计的分类器的分类label为[0,max-1],而true ground的标签值为[1,max]
所以可以通过修改label = (label-1).to(opt.device)

问题二

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 1 and 3 in dimension 1 at /pytorch/aten/src/TH/generic/THTensorMath.cpp:3616

这种情况一般是由于图片并不是三通道造成的,可能是BW图像,也可能是PNG图像图像,所以就可以将图像调整为RGB格式既可。(不过还是要查一下哪张图片在作祟比较好…)

class MyDataset(Dataset):
    def __init__(self, image_paths, transforms=transforms):
        self.image_paths = image_paths
        self.transforms = transforms

    def __getitem__(self, index):
        image = Image.open(self.image_paths[index])
        image = image.convert('RGB')
        if self.transforms:
            image = self.transforms(image)
        return image

问题三

optim.lr_scheduler.ReduceLROnPlateau gives error value cannot be converted to type float without overflow: inf

这个问题是在scheduler对loss进行GPU上优化的错误,问题可能会有converted to type float, int and etc.
所以将loss转到cpu上进行优化即可。

   # after each epoch
   # Note that step should be called after validate()
   self.scheduler.step(loss.cpu().data.numpy())

问题四:

RuntimeError: Error(s) in loading state_dict for Missing key(s) in
state_dict: “fc.weight”, “fc.bias”.

像这种出现丢失key missing key
If you have partial state_dict, which is missing some keys you can do the following:

state = model.state_dict()
state.update(partial)
model.load_state_dict(state)

还有一种多出的key

RuntimeError: Error(s) in loading state_dict for Missing key(s) in
Unexpected key(s) in state_dict: “classifier.0.weight”,

解决办法

# original saved file with DataParallel
state_dict = torch.load('myfile.pth.tar')
# 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
# load params
model.load_state_dict(new_state_dict)

有问题请向我提问

pytorch问题记录(1)