在test_offfical_demo中的predit.py遇见的错误是:
D:\deep-learning-for-image-processing-master\pytorch_classification\Test1_official_demo\predict.py:27: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.
错误原因:由以下代码行导致的:
在代码中,
torch.max(outputs, dim=1)[1]
返回的是一个包含单个元素的 NumPy 数组。当你尝试将这个数组直接转换为整数(int(predict)
)时,NumPy 会发出警告,提醒此操作在未来的版本中将不再被支持。这是因为 NumPy 正在逐步弃用将多维数组直接转换为标量值的做法。
错误解决方法
-
为了避免这种警告,应该先从数组中提取出标量值,然后再进行类型转换。可以使用
item()
方法来提取标量值,修改后的代码: