In VS code I am having 2 notebooks say final.ipynb and main.ipynb. In final.ipynb, I have defined my model architecture compiled it with Adam and loaded weights and saved the model as main.keras. While loading main.keras in main.ipynb I am getting errors like this
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[20], line 1
----> 1 model = keras.models.load_model('main4.keras')
File c:UserskanchAppDataLocalProgramsPythonPython312Libsite-packageskerassrcsavingsaving_api.py:176, in load_model(filepath, custom_objects, compile, safe_mode)
173 is_keras_zip = True
175 if is_keras_zip:
--> 176 return saving_lib.load_model(
177 filepath,
178 custom_objects=custom_objects,
179 compile=compile,
180 safe_mode=safe_mode,
181 )
182 if str(filepath).endswith((".h5", ".hdf5")):
183 return legacy_h5_format.load_model_from_hdf5(
184 filepath, custom_objects=custom_objects, compile=compile
185 )
File c:UserskanchAppDataLocalProgramsPythonPython312Libsite-packageskerassrcsavingsaving_lib.py:152, in load_model(filepath, custom_objects, compile, safe_mode)
147 raise ValueError(
148 "Invalid filename: expected a `.keras` extension. "
149 f"Received: filepath={filepath}"
150 )
151 with open(filepath, "rb") as f:
--> 152 return _load_model_from_fileobj(
153 f, custom_objects, compile, safe_mode
154 )
File c:UserskanchAppDataLocalProgramsPythonPython312Libsite-packageskerassrcsavingsaving_lib.py:207, in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
204 asset_store.close()
206 if failed_trackables:
--> 207 _raise_loading_failure(error_msgs)
208 return model
File c:UserskanchAppDataLocalProgramsPythonPython312Libsite-packageskerassrcsavingsaving_lib.py:295, in _raise_loading_failure(error_msgs, warn_only)
293 warnings.warn(msg)
294 else:
--> 295 raise ValueError(msg)
ValueError: A total of 2 objects could not be loaded. Example error message for object <LSTMCell name=lstm_cell, built=True>:
Layer 'lstm_cell' expected 3 variables, but received 0 variables during loading. Expected: ['kernel', 'recurrent_kernel', 'bias']
List of objects that could not be loaded:
[<LSTMCell name=lstm_cell, built=True>, <LSTMCell name=lstm_cell, built=True>]
and this is my code of final.ipynb
model = Sequential()
model.add(Conv3D(128, 3, input_shape=(75,46,140,1), padding='same'))
model.add(Activation('relu'))
model.add(MaxPool3D((1,2,2)))
model.add(Conv3D(256, 3, padding='same'))
model.add(Activation('relu'))
model.add(MaxPool3D((1,2,2)))
model.add(Conv3D(75, 3, padding='same'))
model.add(Activation('relu'))
model.add(MaxPool3D((1,2,2)))
model.add(TimeDistributed(Flatten()))
model.add(Bidirectional(LSTM(128, return_sequences=True)))
model.add(Dropout(.5))
model.add(Bidirectional(LSTM(128, return_sequences=True)))
model.add(Dropout(.5))
model.add(Dense(char_to_num.vocabulary_size()+1, kernel_initializer='he_normal', activation='softmax'))
model.compile(optimizer=Adam(learning_rate=0.0001), loss=CTCLoss)
model.load_weights('model_weights.weights.h5')
model.save('main4.keras')
This is my code of main.ipynb
@register_keras_serializable()
def CTCLoss(y_true, y_pred):
batch_len = tf.cast(tf.shape(y_true)[0], dtype="int64")
input_length = tf.cast(tf.shape(y_pred)[1], dtype="int64")
label_length = tf.cast(tf.shape(y_true)[1], dtype="int64")
input_length = input_length * tf.ones(shape=(batch_len, 1), dtype="int64")
label_length = label_length * tf.ones(shape=(batch_len, 1), dtype="int64")
loss = tf.keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)
return loss
model = keras.models.load_model('main4.keras')
I tried reading the documentations restarting the kernel and running from the start, deleted the model and saved it again but nothing worked for me. I am expecting my model to be loaded in a new notebook and give predictions