PS E:\CNN_Gesture-master> python 录制手势.py Traceback (most recent call last): File "E:\CNN_Gesture-master\录制手势.py", line 4, in <module> from training import Training File "E:\CNN_Gesture-master\training.py", line 6, in <module> from keras.utils import np_utils ImportError: cannot import name 'np_utils' from 'keras.utils' (D:\PYTHON\ANAC\lib\site-packages\keras\utils\__init__.py) PS E:\CNN_Gesture-master>
时间: 2023-07-21 07:59:18 浏览: 191
这个错误与之前提到的错误类似,也是由于Keras版本不兼容导致的。`np_utils`模块已经从Keras中移除。
你可以尝试使用以下替代方法:
```python
from keras.utils import to_categorical
# 使用to_categorical进行one-hot编码
y_train_encoded = to_categorical(y_train)
```
如果你的代码中有其他地方用到了`np_utils`,也需要将其替换为合适的替代方法。
另外,请确保你已经正确安装了相关的依赖库,并且检查一下你的Keras版本是否与代码兼容。如果是较旧的Keras版本,可以尝试更新Keras或使用其他替代方法。
相关问题
D:\Downloads\Anaconda\python.exe "D:\Downloads\Pycharm\Mycode\Binding gesture\demo.py" Traceback (most recent call last): File "D:\Downloads\Pycharm\Mycode\Binding gesture\demo.py", line 407, in <module> handRecognize = HandRecognize() File "D:\Downloads\Pycharm\Mycode\Binding gesture\demo.py", line 115, in __init__ self.modelGCN.load_state_dict(torch.load('demo/saveModel/handsModel.pth')) File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 809, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 1172, in _load result = unpickler.load() File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 1142, in persistent_load typed_storage = load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location)) File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 1116, in load_tensor wrap_storage=restore_location(storage, location), File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 217, in default_restore_location result = fn(storage, location) File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 182, in _cuda_deserialize device = validate_cuda_device(location) File "C:\Users\CHL\AppData\Roaming\Python\Python310\site-packages\torch\serialization.py", line 166, in validate_cuda_device raise RuntimeError('Attempting to deserialize object on a CUDA ' RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU. 进程已结束,退出代码1
根据错误信息,您的代码尝试在CUDA设备上反序列化对象,但是`torch.cuda.is_available()`返回False,这意味着您的系统上没有可用的CUDA设备。因此,您应该使用`torch.load`函数的`map_location`参数将模型加载到CPU上。
您可以尝试修改加载模型的代码,如下所示:
```python
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
model = torch.load('demo/saveModel/handsModel.pth', map_location=device)
```
这样,在有可用的CUDA设备时,模型将被加载到CUDA设备上;如果没有可用的CUDA设备,则模型将被加载到CPU上。
尝试使用上述代码加载模型,看看是否可以解决问题。如果仍然遇到错误,请确保您已正确安装CUDA并配置了正确的环境。
阅读全文