如何将经过预训练的Tensorflow模型从Google Cloud Storage加载到Datalab中

问题描述 投票:1回答:1

我已经在Tensorflow(v2.0)Keras中训练了一个模型。现在,我需要将此经过预训练的模型上传到Google Datalab中,以便对大量数据进行预测。 Datalab上可用的Tenserflow版本是1.8,但我认为是向后兼容。

我已将保存的模型(model.h5)上传到Google Cloud Storage。我试图像这样将其加载到Datalab的Jupyter Notebook中:

%%gcs read --object gs://project-xxx/data/saved_model.h5 --variable ldmodel
model = keras.models.load_model(ldmodel)

这将引发错误:

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-18-07c40785a14b> in <module>()
----> 1 model = keras.models.load_model(ldmodel)

/usr/local/envs/py3env/lib/python3.5/site- 
   packages/tensorflow/python/keras/_impl/keras/engine/saving.py in load_model(filepath, 
custom_objects, 
compile)
    233     return obj
    234 
--> 235   with h5py.File(filepath, mode='r') as f:
    236     # instantiate model
    237     model_config = f.attrs.get('model_config')

/usr/local/envs/py3env/lib/python3.5/site-packages/h5py/_hl/files.py in __init__(self, name, mode, 
driver, libver, userblock_size, swmr, **kwds)
    267             with phil:
    268                 fapl = make_fapl(driver, libver, **kwds)
--> 269                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    270 
    271                 if swmr_support:

    /usr/local/envs/py3env/lib/python3.5/site-packages/h5py/_hl/files.py in make_fid(name, mode, 
   userblock_size, fapl, fcpl, swmr)
     97         if swmr and swmr_support:
     98             flags |= h5f.ACC_SWMR_READ
---> 99         fid = h5f.open(name, flags, fapl=fapl)
    100     elif mode == 'r+':
    101         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

h5py/defs.pyx in h5py.defs.H5Fopen()

h5py/_errors.pyx in h5py._errors.set_exception()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 29: invalid start byte

任何帮助将不胜感激!

python tensorflow google-cloud-platform google-cloud-storage google-cloud-datalab
1个回答
1
投票

我不会打赌向后兼容。 Here的更多细节。

此外,您的版本较旧。 1.8已于2018发行。最新的1(1.15)版本已于上个月发布。

最后,Keras在Tensorflow的版本1中集成得不是很好。 V2会在此级别上进行所有更改,并且您的问题会一直存在此不兼容问题。

© www.soinside.com 2019 - 2024. All rights reserved.