PyTorch编写代码遇到的问题

PyTorch编写代码遇到的问题

flyfish

错误提示:no module named xxx

xxx为自定义文件夹的名字
因为搜索不到,所以将当前路径加入到包的搜索目录
解决方法:

import sys
sys.path.append('..') #将上层目录加入到搜索路径中
sys.path.append('/home/xxx') # 绝对路径
import os
sys.path.append(os.getcwd()) #		#将当前工作路径加入到搜索路径中

还可以在当前终端的命令行设置

export PYTHONPATH=$PYTHONPATH:./
错误提示:AttributeError: ‘NoneType’ object has no attribute ‘shape’

height, width, channel = img.shape

在Linux系统下img.shape报错AttributeError: ‘NoneType’ object has no attribute ‘shape’
img=cv2.imread(),读取一张图片时,img.shape是包含三个量的元组,分别是:
img.shape[0]:图像的高度
img.shape[1]:图像的宽度
img.shape[2]:图像的通道数
解决方法:读的文件出错 或者查看文件路径是否正确

错误提示 :TypeError: slice indices must be integers or None or have an index method

cropped_im = img[ny1 : ny2, nx1 : nx2, :]
解决方法:需要将ny1 : ny2, nx1 : nx2转换成int类型

错误提示 :Input type (torch.cuda.DoubleTensor) and weight type (torch.cuda.FloatTensor) should be the same

以下三小段分别是Data type CPU tensor GPU tensor
32-bit floating point torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.DoubleTensor torch.cuda.DoubleTensor

出错在类型转换

np.float更改为np.float32

import torchvision.transforms as transforms
import numpy as np

transform = transforms.ToTensor()

def convert_image_to_tensor(image):
    """convert an image to pytorch tensor
        image: numpy array , h * w * c
        image_tensor: pytorch.FloatTensor, c * h * w
        """
    image = image.astype(np.float32)  

    return transform(image)
错误提示:RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated

版本问题 旧式写法

import torch
x = torch.tensor(0.1)
y = torch.tensor(0.2)
z = torch.cat((x, y))

改成新式写法

x = torch.tensor([0.1])
y = torch.tensor([0.2])
z = torch.cat((x, y))
print(z)

结果

tensor([0.1000, 0.2000])
错误提示:TypeError: ‘float’ object is not subscriptable

多了下标 a = x.tolist()[0]
去除下标 a = x.tolist()

错误提示:argument ‘input’ (position 1) must be Tensor, not list

需要将list转换成tensor
假设a是list
torch.tensor(a)

GPU模型和CPU模型之间的转换

假设原来保存的是GPU模型,要转换为CPU模型

torch.save(model, os.path.join( "./complete.pth"))
cpu_model = torch.load("./complete.pth", map_location=lambda storage, loc: storage)
dummy_input = torch.randn(1, 3, 224, 224)

假设原来保存的是CPU模型,要转换为GPU模型

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.save(model, os.path.join( "./complete.pth"))
gpu_model = torch.load("./complete.pth", map_location=lambda storage, loc: storage.cuda)
dummy_input = torch.randn(1, 3, 224, 224)
dummy_input = dummy_input.to(device)
错误提示 RuntimeError: Subtraction, the - operator, with a bool tensor is not supported. If you are trying to invert a mask, use the ~ or logical_not() operator instead.

原代码

# Store only unsuppressed boxes for this class
image_boxes.append(class_decoded_locs[1 - suppress])
image_labels.append(torch.LongTensor((1 - suppress).sum().item() * [c]).to(device))
image_scores.append(class_scores[1 - suppress])

更改为

image_boxes.append(class_decoded_locs[~suppress])
image_labels.append(torch.LongTensor((~ suppress).sum().item() * [c]).to(device))
image_scores.append(class_scores[~suppress])
错误提示 RuntimeError: Expected object of scalar type Byte but got scalar type Bool for argument #2 ‘other’ in call to _th_max

原代码

   suppress = torch.zeros((n_above_min_score), dtype=torch.uint8).to(device) 

更改为

   suppress = torch.zeros((n_above_min_score), dtype=torch.bool).to(device)  
UserWarning: volatile was removed and now has no effect. Use with torch.no_grad(): instead.
#之前旧版本
...
x = Variable(torch.randn(1), volatile=True)
return x

#新版
with torch.no_grad():
    ...
    x = torch.randn(1)
return x
错误提示

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1. Please use torch.load with map_location to map your storages to an existing device.
或者是 RuntimeError: expected device cuda:0 but got device cuda:1

错误原因之一
使用了CUDA 1显卡训练保存的模型文件,使用CUDA 0验证
代码中写了
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)

可以在命令行设置让哪些GPU可见
export CUDA_VISIBLE_DEVICES=1 #GPU编号
export CUDA_VISIBLE_DEVICES=0,1,2,3#4张显卡可见
也可以在代码里改成
checkpoint = torch.load(checkpoint,map_location=‘cuda:0’)

错误提示

raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /update (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f3111915e80>: Failed to establish a new connection: [Errno 111] Connection refused',))
Exception in user code:

解决方案
因为没有启动visdom可视化程序,所有报错
在终端执行命令 visdom之后就能看到如下信息

Checking for scripts.
It's Alive!
INFO:root:Application Started
You can navigate to http://localhost:8097
  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西笑生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值