【Python报错&解决】总结记录(3)

在Pycharm终端运行脚本

  1. >python进入python终端
  2. 修改路径(./是当前目录)

Torch not compiled with CUDA enabled

解释

“未编译带有CUDA启用的Torch”的错误消息,意思是使用的Torch版本不支持CUDA,或CUDA工具包未在系统上安装。

解决

  1. 检查CUDA是否已安装在系统上:

终端运行:nvcc -V
返回版本号则已安装;
未安装需从NVIDIA网站下载并安装。

  1. 已安装但是在python中无法调用时检查是否cuda与pytorch版本不兼容
    尝试多种办法后, 最好的解决办法是卸了重装。
    参考解决:关于cuda和pytorch不兼容问题
  • nvcc -V查询到的Cuda版本(11.7);
  • 创建、激活并进入自己的虚拟环境;
  • conda list查看已有的安装包及版本号,卸载;
  • 进入官网https://pytorch.org/复制版本对应的命令安装pytorch;(默认的)
  • conda list查看是否下载成功;
  • Pycharm配置解释器为刚才创建的虚拟环境
  • 代码验证:(输出正确版本号、True)
import torch

print('hello {}'.format(torch.__version__))
print(torch.cuda.is_available())

Ps此时出现的问题是,在pycharm中项目文件能运行,在终端却无法运行,需要给终端也配置环境。

nibabel.deprecator.ExpiredDeprecationError: get_data() is deprecated in favor of get_fdata(), which has a more predictable return type. To obtain get_data() behavior going forward, use numpy.asanyarray(img.dataobj).

解释

DeprecationWarning,意思是在nibabel 5.0版本中,get_data()方法已经被弃用,建议使用get_fdata()方法来代替。
【get_data()方法是nibabel中的一个方法,用于获取图像数据。然而,由于它的返回类型不够明确,可能会导致一些问题。因此,nibabel团队决定弃用get_data()方法,并建议使用get_fdata()方法来代替。】

解决

使用get_fdata()方法代替。

torch.cuda.OutOfMemoryError: CUDA out of memory.

解释

GPU已经达到了最大内存限制,无法再分配更多的内存空间。
【在PyTorch中,如果你的计算机内存不足以处理任务,PyTorch会尝试动态分配内存。但是,如果内存不足,PyTorch会抛出OutOfMemoryError异常。】

解决

参考:pytorch遇见RuntimeError: CUDA out of memory的解决

方法一

  1. 查看服务器显存占用情况:nvidia-smi
    在这里插入图片描述仅一个GPU:0,所以跳过指定GPU的解决办法。

方法二

  1. 清除垃圾,释放内存:
# 第一种
torch.cuda.empty_cache()

# 第二种,在测试代码之前使用
with torch.no_grad()

#也可以两种并用

最终通过在测试代码之前加with torch.no_grad():解决此问题,注意一定要加在测试代码之前

ValueError: Image data has type int64, which may cause incompatibilities with other tools. To use this type, pass an explicit header or dtype argument to Nifti1Image().

解决

方法一

Convert the Nifti1Image object to a float data type:

import numpy as np

# Load the Nifti1Image object
nifti_image = ...

# Convert the data type to float
nifti_image = nifti_image.astype(np.float64)

方法二

Pass an explicit header or dtype argument when creating the Nifti1Image object:

# Load the Nifti1Image object with an explicit header or dtype argument
nifti_image = nifti1.Nifti1Image(data=np.array([...]), header=header, dtype=np.float64)

最终通过修改图像为浮点型解决:

pred_orig_nib = nibabel.Nifti1Image(pred_orig, None)

改为

pred_orig_nib = nibabel.Nifti1Image(pred_orig.astype(np.float32),None)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 当使用Python中的softmax函数时,可能会遇到一些。其中一个常见的误是"TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64……"。这个误通常是由于输入的数据类型不正确导致的。解决这个问题的方法是确保输入的数据类型是float64类型。\[1\] 另一个可能的误是在使用nn.Softmax函数时返回了误的类型。如果直接使用nn.Softmax(x),返回的类型是<class 'torch.nn.modules.activation.Softmax'>,而不是期望的Tensor类型。解决这个问题的方法是先定义一个Softmax层,例如layer=nn.Softmax(),然后使用layer(x)来得到正确的输出类型。\[2\] 还有一种情况是在一条语句中同时使用Softmax和Parameter时解决这个问题的方法是去掉Softmax,只使用Parameter。\[3\] 总结起来,解决Python中softmax的方法包括确保输入数据类型正确,使用正确的函数或方法来得到期望的输出类型,以及避免在一条语句中同时使用Softmax和Parameter。 #### 引用[.reference_title] - *1* *3* [【Python荟萃](https://blog.csdn.net/qq_43166192/article/details/130491524)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [问题记录:使用nn.Softmax,F.softmax却不](https://blog.csdn.net/weixin_50594261/article/details/128164593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值