Tensorflow Keras load_model 来自内存还是变量?

Fra*_*iou 5 keras tensorflow tf.keras

因为 tensorflow.keras.models.load_model 输入是路径。

但我必须先从文件中加载它,然后解密它。然后指向

负载模型

如果有任何想法来实现它?

from tensorflow.keras.models import load_model
with open('mypath.h5'. mode='rb') as f:
    h5 = decrypt_func(f.read())

model = load_model(h5)
Run Code Online (Sandbox Code Playgroud)

有用。

解决方案是根据@jgorostegui

import tempfile
import h5py
from tensorflow.keras.models import load_model

temp = tempfile.TemporaryFile()
with open('mypath.h5'. mode='rb') as f:
    h5 = decrypt_func(f.read())
    temp.write(h5)
with h5py.File(temp, 'r') as h5file:
    model = load_model(h5file)
Run Code Online (Sandbox Code Playgroud)

jgo*_*gui 4

根据输出函数的格式,decrypt_func可以用于h5py加载解密的流,然后使用该keras.models.load_model函数加载模型,该模型支持h5py.File对象类型作为输入模型,除了您提到的字符串、保存模型的路径之外。

with open('model.hdf5', 'rb') as f_hdl:
    h5 = decrypt_func(f_hdl.read())
    with h5py.File(h5, 'r') as h5_file:
        model = keras.models.load_model(h5_file)
Run Code Online (Sandbox Code Playgroud)