Skip to content
Advertisement

Access denied to subclassed Keras model when loaded from a .py script

I have the following subclassed Keras model which I have already trained. I want to be able to call all the methods in B_frame_CNN (e.g., get_embedding()) on the loaded model. The following code works perfectly and does what I need when run in an ipython notebook.

import tensorflow as tf

class B_frame_CNN(tf.keras.Model):
    def __init__(self, filter_size, activation, padding):
        super().__init__()
        self.c1 = tf.keras.layers.Conv2D(32, filter_size, strides=(2,1), activation=activation, padding=padding)
        self.c2 = tf.keras.layers.Conv2D(64, filter_size, strides=2, activation=activation, padding=padding)
        self.p1 = tf.keras.layers.MaxPooling2D(filter_size, strides=2)
        self.c3 = tf.keras.layers.Conv2D(128, filter_size, strides=2, activation=activation, padding=padding, name="gradmaps")
        self.p2 = tf.keras.layers.MaxPooling2D(filter_size, strides=2)
        self.c4 = tf.keras.layers.Conv2D(256, filter_size, strides=2, activation=activation, padding=padding)
        self.c5 = tf.keras.layers.Conv2D(256, filter_size, strides=2, activation=activation, padding=padding)

        self.f1 = tf.keras.layers.Flatten()
        self.dropout1 = tf.keras.layers.Dropout(0.5)
        self.d1 = tf.keras.layers.Dense(64, activation=None, name="embedding")
        self.dropout2 = tf.keras.layers.Dropout(0.5)
        self.d2 = tf.keras.layers.Dense(2, activation='softmax')

        inputs = tf.keras.Input(shape=(200,100,1))
        self.full_model = tf.keras.Model(inputs=[inputs], outputs=self.call(inputs))

    def call(self, inputs):
        x = self.c1(inputs)
        x = self.c2(x)
        x = self.p1(x)
        x = self.c3(x)
        x = self.p2(x)
        x = self.c4(x)
        x = self.c5(x)
        
        x = self.f1(x)
        x = self.dropout1(x, training=True)
        x = self.d1(x)
        x = self.dropout2(x, training=True)
        return self.d2(x)

    def get_embedding(self):
        intermediate = tf.keras.models.Model(inputs=self.full_model.input, outputs=self.full_model.get_layer("embedding").output)
        return intermediate

model1 = B_frame_CNN(3, 'relu', 'same')
model1.load_weights("D:\brain_cancer_octsaved_modelsCNN_bframe")

However, when I run it in a python script (.py), I get the following error:

2022-09-10 01:44:08.287034: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open D:brain_cancer_octsaved_modelsCNN_bframe: Unknown: NewRandomAccessFile failed to Create/Open: D:brain_cancer_octsaved_modelsCNN_bframe : Access is denied. ; Input/output error

I would appreciate any help deciphering this error.

Advertisement

Answer

In case anyone stumbles upon the same problem, here is the workaround I found:

bframe_model = tf.keras.models.load_model("D:\brain_cancer_octsaved_modelsCNN_bframe")
bframe_cnn = BFrameCNN(3, 'relu', 'same')
bframe_cnn.set_weights(bframe_model.get_weights())
bframe_embedding_cnn = bframe_cnn.get_intermediate("embedding")

I can’t use my class methods on bframe_model alone, but once I load its weights into an object of the BFrameCNN class that I instantiate, I am able to do what I want (e.g., call the get_intermediate() method).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement