来自内存或变量的 Tensorflow Keras load_model?
Tensorflow Keras load_model from Memory or Variable?
因为tensorflow.keras.models.load_model输入的是路径。
但我必须先从文件加载它,然后解密它。然后指向
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)
有效。
解决方案是根据@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)
根据输出函数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)
因为tensorflow.keras.models.load_model输入的是路径。
但我必须先从文件加载它,然后解密它。然后指向
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)
有效。
解决方案是根据@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)
根据输出函数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)