1.Pytorch查看网络结构
from torchsummary import summary
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = models.resnet18().to(device)
summary(model, (3,256,256))
注意:以上三种方法在处理输出为tuple时(如:LSTM),会有BUG
pytorch 踩坑之'tuple' object has no attribute 'size'
解决方法:
安装最新的版本:pip install torch-summary
2. Tensor 与 Numpy 之间的相互转换
#from_numpy()与numpy() 是共享内存的,转换速度较快
a.numpy(b)
c = torch.from_numpy()
#不共享内存
e = torch.tensor(d)
#把tensor和numpy的数转化为int,float等类型
a[1].item()
1. Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
错误内容大概就是指输入类型是CPU(torch.FloatTensor),而参数类型是GPU(torch.cuda.FloatTensor)
错误记录:
1. cuda out of memeory
1)torch.atan(w/h),当h>>w时,w/h->0, 出现无限大的数(inf),导致显存溢出
解决办法:
torch.atan(w/h) => torch.atan(torch.exp(w)/torch.exp(h))
2)当w过大时,torch.exp(w) 也可能为 inf,导致显存溢出
解决办法:
对输入的像素坐标,进行torch.sigmod()
3) 当两个变量的形状不一样时,引发显存溢出
IoU.shape = (110484,1) #
v.shape = (110484) #
最终版本:
v = (4 / (math.pi ** 2)) * torch.pow((torch.atan(w_gt / h_gt) - torch.unsqueeze(torch.atan(w_pre / h_pre),1)), 2)