问题一
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)
问题五
最近遇到一个问题
在对Resnet做扩展的时候,意识到自己基础知识还没有掌握牢固。
开一个新文章记录一下啊。
1.在Resnet做扩展,增加卷积层容易出现的问题:
- 考虑卷积核的大小,用于提取特征还是降维
- 初始化问题,根据任务需求初始化还是根据原来网络内容做相同初始化
- 输出问题,是否存在某些方程对卷积输出的范围有要求(如开根号),那么是否需要增加BN以及激活函数
问题六
最近发现一个不错的研究网络的库,torchsummary,能够清楚地反应每个层的输出shape,从此网络不再是黑箱
问题七
调整学习率
torch.optim.lr_scheduler.ReduceLROnPlateau
这个东西可是调整学习率的神器,还是挺智能的。
初始化方法
torch.nn.init.kaiming_normal
这一看就是何凯明…的初始化方法。。好用