我使用 Gadgetron 的 Docker 来通过 Keras 实现 Gadget。 Theano 作为后端。这是 Dockerfile,pip list
的结果是
Package Version
-------------------- -----------
absl-py 0.7.1
astor 0.8.0
certifi 2019.6.16
chardet 3.0.4
cycler 0.10.0
Cython 0.26.1
decorator 4.4.0
dicom 0.9.9.post1
dicom-numpy 0.1.6
engineering-notation 0.6.0
gast 0.2.2
google-pasta 0.1.7
grpcio 1.21.1
h5py 2.9.0
idna 2.8
imageio 2.5.0
ismrmrd 1.6.1
ismrmrd-python-tools 0.3
joblib 0.13.2
Keras 2.2.4
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.0
kiwisolver 1.1.0
lxml 4.2.1
Markdown 3.1.1
matplotlib 3.1.0
networkx 2.3
numpy 1.16.4
opencv-python 4.1.0.25
Pillow 6.0.0
pip 19.2.3
protobuf 3.8.0
psutil 5.4.2
pydicom 1.2.2
pygobject 3.26.1
pynrrd 0.4.0
pyparsing 2.4.0
PyQt5 5.12.2
PyQt5-sip 4.19.17
python-apt 1.6.4
python-dateutil 2.8.0
pytz 2019.2
PyWavelets 1.0.3
PyXB 1.2.6
PyYAML 5.1.1
pyzmq 18.0.2
requests 2.22.0
scikit-image 0.15.0
scikit-learn 0.21.2
scipy 1.3.0
setuptools 41.0.1
six 1.12.0
sklearn 0.0
tensorboard 1.14.0
tensorboardX 1.8
tensorflow 1.14.0
tensorflow-estimator 1.14.0
tensorflow-gpu 1.14.0
termcolor 1.1.0
Theano 1.0.4
tk-tools 0.11.0
torch 1.1.0
torchfile 0.1.0
torchvision 0.3.0
tornado 6.0.3
urllib3 1.25.3
visdom 0.1.8.8
websocket-client 0.56.0
Werkzeug 0.15.4
wheel 0.33.4
wrapt 1.11.2
我的代码是这样的:
from keras.models import model_from_json
with open('/opt/data/model.json', 'r') as f:
model = model_from_json(f.read())
model.load_weights('/opt/data/weight.h5')
加载权重时出现错误:
File "/opt/data/CNNArt/Gadgetron/gadget_cnnart.py", line 29, in process
model.load_weights('/opt/data/weight.h5')
File "/usr/local/lib/python3.6/dist-packages/keras/engine/network.py", line 1157, in load_weights
with h5py.File(filepath, mode='r') as f:
File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py", line 391, in __init__
fapl = make_fapl(driver, libver, rdcc_nslots, rdcc_nbytes, rdcc_w0, **kwds)
File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py", line 108, in make_fapl
plist.set_libver_bounds(low, high)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5p.pyx", line 1140, in h5py.h5p.PropFAID.set_libver_bounds
ValueError: Invalid high library version bound (invalid high library version bound)
但是,在 Docker 中,当我在终端
中打开 python 时,我可以加载模型及其参数。
>>> from keras.models import model_from_json
>>> with open('/opt/data/model.json', 'r') as f:
... model = model_from_json(f.read())
...
>>> model.load_weights('/opt/data/weight.h5')
>>>
我尝试用
h5py
重新安装
pip
,但不起作用。 问题是它可以直接在 python 交互终端中工作,但在 Gadget 脚本中不起作用。
sudo apt-get install python3-h5py
并确保 numpy 版本与 h5py 兼容
正如@so2liu提到的,请尝试:
pip install --no-binary=h5py h5py
但是,请确保选择与tensorflow或其他一些库兼容的版本,例如:
pip install --no-binary=h5py h5py==2.10.0
最后,请记住检查您的环境,因为您可能在 venv / conda / docker 中运行代码,但您在错误的环境中安装了 h5py