关于AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘问题的解决

  • Post author:
  • Post category:其他




1.该问题错误情况如下

  File "E:\PythonCode\STANet-master\options\base_options.py", line 140, in parse
    torch.cuda.set_device(opt.gpu_ids[0])
  File "E:\Python\lib\site-packages\torch\cuda\__init__.py", line 281, in set_device
    torch._C._cuda_setDevice(device)
AttributeError: module 'torch._C' has no attribute '_cuda_setDevice'



2.解决方法

1)修改执行命令

由于我的pytorch装的是CPU版本的,因此需要

在python执行命令后面加上 –gpu_ids -1

E:\PythonCode\Net>python train.py --gpu_ids -1

这样运行就没有出错了,不过,还可以用另一种方式解决~

2)修改”

base_options.py

“文件

这个文件在错误信息里面可以找到。

    def initialize(self, parser):
        """Define the common options that are used in both training and test."""
        # basic parameters
        parser.add_argument('--dataroot', type=str, default='./LEVIR-CD', help='path to images (should have subfolders A, B, label)')
        parser.add_argument('--val_dataroot', type=str, default='./LEVIR-CD', help='path to images in the val phase (should have subfolders A, B, label)')

        parser.add_argument('--name', type=str, default='experiment_name', help='name of the experiment. It decides where to store samples and models')
        parser.add_argument('--gpu_ids', type=str, default='-1', help='gpu ids: e.g. 0  0,1,2, 0,2. use -1 for CPU')
        parser.add_argument('--checkpoints_dir', type=str, default='./checkpoints', help='models are saved here')
        # model parameters
        parser.add_argument('--model', type=str, default='CDF0', help='chooses which model to use. [CDF0 | CDFA]')
        parser.add_argument('--input_nc', type=int, default=3, help='# of input image channels: 3 for RGB ')
        parser.add_argument('--output_nc', type=int, default=3, help='# of output image channels: 3 for RGB')
        parser.add_argument('--arch', type=str, default='mynet3', help='feature extractor architecture | mynet3')
        parser.add_argument('--f_c', type=int, default=64, help='feature extractor channel num')
        parser.add_argument('--n_class', type=int, default=2, help='# of output pred channels: 2 for num of classes')
        parser.add_argument('--init_type', type=str, default='normal', help='network initialization [normal | xavier | kaiming | orthogonal]')
        parser.add_argument('--init_gain', type=float, default=0.02, help='scaling factor for normal, xavier and orthogonal.')
        parser.add_argument('--SA_mode', type=str, default='BAM', help='choose self attention mode for change detection, | ori |1 | 2 |pyramid, ...')

直接

把’–gpu_ids’的 default改成’-1’

,简单粗暴。(原来默认值是0)

以上,完美解决了问题。



版权声明:本文为qq_39578356原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。