Python RuntimeError:应为设备类型cuda的对象,但为参数#0';获取了设备类型cpu;结果';打电话给bmm出去

Python RuntimeError:应为设备类型cuda的对象,但为参数#0';获取了设备类型cpu;结果';打电话给bmm出去,python,pytorch,Python,Pytorch,我测试了我的代码,但有些地方出错了。我使用Pytork加载数据和测试ICP,并使用“cuda()”向GPU添加一些变量。但是,pycharm显示了错误信息“RuntimeError:预期的对象为设备类型cuda,但在调用_th_bmm_out时为参数#0'result'获得了设备类型cpu”。我真的不知道如何解决这个问题。 这是ICP代码 import numpy as np from sklearn.neighbors import NearestNeighbors def best_fi

我测试了我的代码,但有些地方出错了。我使用Pytork加载数据和测试ICP,并使用“cuda()”向GPU添加一些变量。但是,pycharm显示了错误信息“RuntimeError:预期的对象为设备类型cuda,但在调用_th_bmm_out时为参数#0'result'获得了设备类型cpu”。我真的不知道如何解决这个问题。 这是ICP代码

import numpy as np
from sklearn.neighbors import NearestNeighbors


def best_fit_transform(A, B):
    '''
    Calculates the least-squares best-fit transform that maps corresponding points A to B in m spatial dimensions
    Input:
      A: Nxm numpy array of corresponding points
      B: Nxm numpy array of corresponding points
    Returns:
      T: (m+1)x(m+1) homogeneous transformation matrix that maps A on to B
      R: mxm rotation matrix
      t: mx1 translation vector
    '''

    assert A.shape == B.shape  #条件为true正常执行,条件为false触发异常

    # get number of dimensions
    m = A.shape[1]

    # translate points to their centroids
    #计算点云质心
    centroid_A = np.mean(A, axis=0)
    centroid_B = np.mean(B, axis=0)
    #源、目标点云计算去质心
    AA = A - centroid_A
    BB = B - centroid_B

    # rotation matrix
    H = np.dot(AA.T, BB)
    #SVD分解H协方差矩阵
    U, S, Vt = np.linalg.svd(H)
    R = np.dot(Vt.T, U.T)

    # special reflection case
    if np.linalg.det(R) < 0:
       Vt[m-1,:] *= -1
       R = np.dot(Vt.T, U.T)

    # translation
    #t:3X1
    t = centroid_B.T - np.dot(R,centroid_A.T)

    # homogeneous transformation
    #T为m+1维的对角矩阵
    T = np.identity(m+1)
    T[:m, :m] = R
    T[:m, m] = t

    return T, R, t


def nearest_neighbor(src, dst):
    '''
    Find the nearest (Euclidean) neighbor in dst for each point in src
    Input:
        src: Nxm array of points
        dst: Nxm array of points
    Output:
        distances: Euclidean distances of the nearest neighbor
        indices: dst indices of the nearest neighbor
    '''

    assert src.shape == dst.shape

    neigh = NearestNeighbors(n_neighbors=1)
    neigh.fit(dst)
    distances, indices = neigh.kneighbors(src, return_distance=True)
    return distances.ravel(), indices.ravel()


def icp(A, B, init_pose=None, max_iterations=20, tolerance=0.001):
    '''
    The Iterative Closest Point method: finds best-fit transform that maps points A on to points B
    Input:
        A: Nxm numpy array of source mD points
        B: Nxm numpy array of destination mD point
        init_pose: (m+1)x(m+1) homogeneous transformation
        max_iterations: exit algorithm after max_iterations
        tolerance: convergence criteria
    Output:
        T: final homogeneous transformation that maps A on to B
        distances: Euclidean distances (errors) of the nearest neighbor
        i: number of iterations to converge
    '''

    assert A.shape == B.shape

    # get number of dimensions
    m = A.shape[1]

    # make points homogeneous, copy them to maintain the originals
    src = np.ones((m+1,A.shape[0]))
    # print(type(src))
    dst = np.ones((m+1,B.shape[0]))
    # print(dst.shape)
    src[:m,:] = np.copy(A.T)
    dst[:m,:] = np.copy(B.T)

    # apply the initial pose estimation
    if init_pose is not None:
        src = np.dot(init_pose, src)

    prev_error = 0

    for i in range(max_iterations):
        # find the nearest neighbors between the current source and destination points
        distances, indices = nearest_neighbor(src[:m,:].T, dst[:m,:].T)

        # compute the transformation between the current source and nearest destination points
        T,_,_ = best_fit_transform(src[:m,:].T, dst[:m,indices].T)

        # update the current source
        src = np.dot(T, src)

        # check error
        mean_error = np.mean(distances)
        if np.abs(prev_error - mean_error) < tolerance:
            break
        prev_error = mean_error

    # calculate final transformation
    T,_,_ = best_fit_transform(A, src[:m,:].T)

    return T, distances, i

这是data.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import sys
import glob
import h5py
import numpy as np
from scipy.spatial.transform import Rotation
from torch.utils.data import Dataset


# Part of the code is referred from: https://github.com/charlesq34/pointnet

def download():
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    DATA_DIR = os.path.join(BASE_DIR, 'data')
    if not os.path.exists(DATA_DIR):
        os.mkdir(DATA_DIR)
    if not os.path.exists(os.path.join(DATA_DIR, 'modelnet40_ply_hdf5_2048')):
        www = 'https://shapenet.cs.stanford.edu/media/modelnet40_ply_hdf5_2048.zip'
        zipfile = os.path.basename(www)
        os.system('wget %s; unzip %s' % (www, zipfile))
        os.system('mv %s %s' % (zipfile[:-4], DATA_DIR))
        os.system('rm %s' % (zipfile))


def load_data(partition):
    download()
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    DATA_DIR = os.path.join(BASE_DIR, 'data')
    all_data = []
    all_label = []
    for h5_name in glob.glob(os.path.join(DATA_DIR, 'modelnet40_ply_hdf5_2048', 'ply_data_%s*.h5' % partition)):
        f = h5py.File(h5_name)
        data = f['data'][:].astype('float32')
        label = f['label'][:].astype('int64')
        f.close()
        all_data.append(data)
        all_label.append(label)
    all_data = np.concatenate(all_data, axis=0)
    all_label = np.concatenate(all_label, axis=0)
    return all_data, all_label


def translate_pointcloud(pointcloud):
    xyz1 = np.random.uniform(low=2. / 3., high=3. / 2., size=[3])
    xyz2 = np.random.uniform(low=-0.2, high=0.2, size=[3])

    translated_pointcloud = np.add(np.multiply(pointcloud, xyz1), xyz2).astype('float32')
    return translated_pointcloud


def jitter_pointcloud(pointcloud, sigma=0.01, clip=0.05):
    N, C = pointcloud.shape
    pointcloud += np.clip(sigma * np.random.randn(N, C), -1 * clip, clip)
    return pointcloud


class ModelNet40(Dataset):
    def __init__(self, num_points, partition='train', gaussian_noise=False, unseen=False, factor=4):
        self.data, self.label = load_data(partition)
        self.num_points = num_points
        self.partition = partition
        self.gaussian_noise = gaussian_noise
        self.unseen = unseen
        self.label = self.label.squeeze()
        self.factor = factor
        if self.unseen:
            ######## simulate testing on first 20 categories while training on last 20 categories
            if self.partition == 'test':
                self.data = self.data[self.label>=20]
                self.label = self.label[self.label>=20]
            elif self.partition == 'train':
                self.data = self.data[self.label<20]
                self.label = self.label[self.label<20]

    def __getitem__(self, item):
        pointcloud = self.data[item][:self.num_points]
        if self.gaussian_noise:
            pointcloud = jitter_pointcloud(pointcloud)
        if self.partition != 'train':
            np.random.seed(item)
        anglex = np.random.uniform() * np.pi / self.factor
        angley = np.random.uniform() * np.pi / self.factor
        anglez = np.random.uniform() * np.pi / self.factor

        cosx = np.cos(anglex)
        cosy = np.cos(angley)
        cosz = np.cos(anglez)
        sinx = np.sin(anglex)
        siny = np.sin(angley)
        sinz = np.sin(anglez)
        Rx = np.array([[1, 0, 0],
                        [0, cosx, -sinx],
                        [0, sinx, cosx]])
        Ry = np.array([[cosy, 0, siny],
                        [0, 1, 0],
                        [-siny, 0, cosy]])
        Rz = np.array([[cosz, -sinz, 0],
                        [sinz, cosz, 0],
                        [0, 0, 1]])
        R_ab = Rx.dot(Ry).dot(Rz)
        R_ba = R_ab.T
        translation_ab = np.array([np.random.uniform(-0.5, 0.5), np.random.uniform(-0.5, 0.5),
                                   np.random.uniform(-0.5, 0.5)])
        translation_ba = -R_ba.dot(translation_ab)

        pointcloud1 = pointcloud.T

        rotation_ab = Rotation.from_euler('zyx', [anglez, angley, anglex])
        pointcloud2 = rotation_ab.apply(pointcloud1.T).T + np.expand_dims(translation_ab, axis=1)

        euler_ab = np.asarray([anglez, angley, anglex])
        euler_ba = -euler_ab[::-1]

        pointcloud1 = np.random.permutation(pointcloud1.T).T
        pointcloud2 = np.random.permutation(pointcloud2.T).T

        return pointcloud1.astype('float32'), pointcloud2.astype('float32'), R_ab.astype('float32'), \
               translation_ab.astype('float32'), R_ba.astype('float32'), translation_ba.astype('float32'), \
               euler_ab.astype('float32'), euler_ba.astype('float32')

    def __len__(self):
        return self.data.shape[0]


if __name__ == '__main__':
    train = ModelNet40(1024)
    test = ModelNet40(1024, 'test')
    for data in train:
        print(len(data))
        break

当我运行testICP.py时,错误信息如下所示

D:\Envs\DCP\Scripts\python.exe D:/pyproject/dcp-master/ICP/testICP.py
D:\pyproject\dcp-master\data.py:51: H5pyDeprecationWarning: The default file mode will change to 'r' (read-only) in h5py 3.0. To suppress this warning, pass the mode you need to h5py.File(), or set the global default h5.get_config().default_file_mode, or set the environment variable H5PY_DEFAULT_READONLY=1. Available modes are: 'r', 'r+', 'w', 'w-'/'x', 'a'. See the docs for details.
  f = h5py.File(h5_name)
  0%|          | 0/2468 [00:00<?, ?it/s]D:/pyproject/dcp-master/ICP/testICP.py:62: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  rotation_ab_pred = torch.tensor(rotation_ab_pred, dtype=torch.float32)
D:/pyproject/dcp-master/ICP/testICP.py:67: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  translation_ab_pred = torch.tensor(translation_ab_pred,dtype=torch.float32)
  0%|          | 0/2468 [00:02<?, ?it/s]
Traceback (most recent call last):
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 180, in <module>
    main()
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 175, in main
    test(test_loader)
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 138, in test
    test_eulers_ab = test_one_epoch(test_loader)
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 83, in test_one_epoch
    transformed_src = transform_point_cloud(src, rotation_ab_pred, translation_ab_pred)
  File "D:\pyproject\dcp-master\util.py", line 38, in transform_point_cloud
    a = torch.matmul(rot_mat, point_cloud) + translation.unsqueeze(2)
RuntimeError: Expected object of device type cuda but got device type cpu for argument #0 'result' in call to _th_bmm_out

Process finished with exit code 1

D:\Envs\DCP\Scripts\python.exe D:/pyproject/DCP master/ICP/testICP.py
D:\pyproject\dcp master\data.py:51:h5pydeprecation警告:在h5py 3.0中,默认文件模式将更改为“r”(只读)。若要抑制此警告,请将所需的模式传递到h5py.File(),或设置全局默认h5.get_config().default_File_模式,或设置环境变量h5py_default_READONLY=1。可用的模式有:‘r’、‘r+’、‘w’、‘w-’/‘x’、‘a’。详见文档。
f=h5py.File(h5_名称)

0%| | 0/2468[00:00双重检查
src.设备
rot|u pred.设备
translation|u pred.设备
转换点云(src,rotation|u pred,translation|u pred)
旋转点云设备
之前
#!/usr/bin/env python
# -*- coding: utf-8 -*-


from __future__ import print_function
import os
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from scipy.spatial.transform import Rotation


# Part of the code is referred from: https://github.com/ClementPinard/SfmLearner-Pytorch/blob/master/inverse_warp.py

def quat2mat(quat):
    x, y, z, w = quat[:, 0], quat[:, 1], quat[:, 2], quat[:, 3]

    B = quat.size(0)

    w2, x2, y2, z2 = w.pow(2), x.pow(2), y.pow(2), z.pow(2)
    wx, wy, wz = w*x, w*y, w*z
    xy, xz, yz = x*y, x*z, y*z

    rotMat = torch.stack([w2 + x2 - y2 - z2, 2*xy - 2*wz, 2*wy + 2*xz,
                          2*wz + 2*xy, w2 - x2 + y2 - z2, 2*yz - 2*wx,
                          2*xz - 2*wy, 2*wx + 2*yz, w2 - x2 - y2 + z2], dim=1).reshape(B, 3, 3)
    return rotMat


def transform_point_cloud(point_cloud, rotation, translation):
    if len(rotation.size()) == 2:
        rot_mat = quat2mat(rotation)
    else:
        rot_mat = rotation
    return torch.matmul(rot_mat, point_cloud) + translation.unsqueeze(2)


def npmat2euler(mats, seq='zyx'):
    eulers = []
    for i in range(mats.shape[0]):
        r = Rotation.from_dcm(mats[i])
        eulers.append(r.as_euler(seq, degrees=True))
    return np.asarray(eulers, dtype='float32')
D:\Envs\DCP\Scripts\python.exe D:/pyproject/dcp-master/ICP/testICP.py
D:\pyproject\dcp-master\data.py:51: H5pyDeprecationWarning: The default file mode will change to 'r' (read-only) in h5py 3.0. To suppress this warning, pass the mode you need to h5py.File(), or set the global default h5.get_config().default_file_mode, or set the environment variable H5PY_DEFAULT_READONLY=1. Available modes are: 'r', 'r+', 'w', 'w-'/'x', 'a'. See the docs for details.
  f = h5py.File(h5_name)
  0%|          | 0/2468 [00:00<?, ?it/s]D:/pyproject/dcp-master/ICP/testICP.py:62: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  rotation_ab_pred = torch.tensor(rotation_ab_pred, dtype=torch.float32)
D:/pyproject/dcp-master/ICP/testICP.py:67: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  translation_ab_pred = torch.tensor(translation_ab_pred,dtype=torch.float32)
  0%|          | 0/2468 [00:02<?, ?it/s]
Traceback (most recent call last):
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 180, in <module>
    main()
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 175, in main
    test(test_loader)
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 138, in test
    test_eulers_ab = test_one_epoch(test_loader)
  File "D:/pyproject/dcp-master/ICP/testICP.py", line 83, in test_one_epoch
    transformed_src = transform_point_cloud(src, rotation_ab_pred, translation_ab_pred)
  File "D:\pyproject\dcp-master\util.py", line 38, in transform_point_cloud
    a = torch.matmul(rot_mat, point_cloud) + translation.unsqueeze(2)
RuntimeError: Expected object of device type cuda but got device type cpu for argument #0 'result' in call to _th_bmm_out

Process finished with exit code 1