【问题标题】:Convert and save PytorchTensor to .mat image on GPU in python在 python 中将 Pytorch Tensor 转换并保存为 GPU 上的 .mat 图像
【发布时间】:2021-05-07 05:16:53
【问题描述】:

我是 Pytorch 的初学者,我被一个问题困扰了好几天。我想将 Pytorch 张量形式的图像保存为 .mat 文件。我看了看,但似乎没有将 Pytoch 张量转换为 .mat 文件的直接方法。我发现的一种可能的解决方案是将其转换为 numpy 数组,但由于我使用的是 Nvidia GPU,当我尝试将 Pytorch 张量转换为 numpy 数组时,它给了我这个错误:

fake_images[0] = fake_images[0].numpy()

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first

如何在使用 GPU 时将 Pytorch 张量保存到 .mat 文件?

【问题讨论】:

    标签: python numpy scipy pytorch gpu


    【解决方案1】:

    照你说的做:

    from scipy.io import savemat
    
    with torch.no_grad():
      mdic = {'fake_images': [f_.cpu().numpy() for f_ in fake_images]}
      savemat('fake_images.mat', mdic)
    

    您可以查看更多关于将 pytorch 张量转换为 numpy 数组的信息here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-05
      • 2015-11-17
      • 2017-03-09
      • 2015-01-30
      • 2015-11-03
      • 2022-01-02
      • 2020-03-04
      相关资源
      最近更新 更多