Load the TensorFlow model from in-memory filesystems (PyfileSystem 2)

112 Views Asked by At

I am currently working on reloading the model from the in-memory folder (created by Pyfilesystem 2). I encrypted my model firstly and then decrypted the model to a in-memory temp folder using fs.MemoryFS(). I printed the structure of the temp folder using mem_fs.tree(), and I get the following results:

-- decrypted_model
    |-- variables
    |   |-- variables.data-00000-of-00001
    |   `-- variables.index
    |-- keras_metadata.pb
    `-- saved_model.pb 

When I want to reload the model using tf.keras.models.load_model, I got the following error: OSError: Unable to load model. Filepath is not an hdf5 file (or h5py is not available) or SavedModel. Received: filepath=<memfs>/decrypted_model My question is: How can I reload the model from in-memory folder directly? Is it possible?

mem_fs = MemoryFS()
temp_dir = mem_fs.makedir('/decrypted_model', recreate=True)
mem_fs.makedirs('/decrypted_model/variables', recreate=True)

# Save the decrypted file to the in-memory folder
for root, dirs, files in os.walk(encrypted_model_dir):
    for file in files:
        file_path = os.path.join(root, file)

        # Read the encrypted file data
        with open(file_path, 'rb') as f:
            encrypted_file_data = f.read()

        # Decrypt the file data
        decrypted_file_data = Decryption(encrypted_file_data)

        if file_path.split('\\')[-2] == 'variables':
            decrypted_file_path = '/decrypted_model/variables/' + file_path.split('\\')[-1]
        else:
            decrypted_file_path = '/decrypted_model/' + file_path.split('\\')[-1]

        with mem_fs.open(decrypted_file_path, 'wb') as f:
            f.write(decrypted_file_data)

# Load the SavedModel from the in-memory folder
model = tf.keras.models.load_model(temp_dir)
0

There are 0 best solutions below