简体   繁体   中英

How to load .h5 CNN model with keras from .zip file with password

I want to load .h5 model from .zip file with password without extracting .h5 file. I can use only python==3.6, h5py==2.10, tensorflow==1.4.0 . My code is below:

import h5py
import zipfile
import tensorflow as tf

archive = zipfile.ZipFile('test_zip.zip')
for file in archive.infolist():
   model_h5_file = archive.open(file, pwd=pwd)
   h5_file1 = h5py.File(model_h5_file, 'r')  # gives an error "io.UnsupportedOperation: seek"
   h5_file2 = tf.keras.models.load_model(model_h5_file, custom_objects={'auc_roc': auc_roc})  # gives an error "io.UnsupportedOperation: seek"

How can I load my .h5 file with keras?

This is how I used to do it with a custom model that had multiple files

import zipfile
import tensorflow as tf

with zipfile.ZipFile('test_zip.zip') as zip_file:
   model = tf.saved_model.load("model.h5")

I hope it is what you were looking for.

Here are some resources if you want to div deep on that: https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.open https://www.tensorflow.org/api_docs/python/tf/saved_model/load

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM