如何使用Python保存和加载自定义构建的MobileNetV2模型
我正在尝试保存一个自定义的构建模型,但遇到了像这样的错误:
Error During Save: ValueError: Unable to synchronously create dataset (name already exists)
Error During Load: KeyError: "Unable to synchronously open object (object 'time_distributed_2' doesn't exist)"**
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[36], line 2
1 # Load the model with custom objects
----> 2 loaded_model = load_model("MoBiLSTM_model.h5", custom_objects={"MobileNetV2": MobileNetV2, "time_distributed_2":TimeDistributed})
File ~/akash_ai_ml/video_analysis/.venv/lib64/python3.11/site-packages/keras/src/saving/saving_api.py:183, in load_model(filepath, custom_objects, compile, safe_mode)
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(filepath)
184 elif str(filepath).endswith(".keras"):
185 raise ValueError(
186 f"File not found: filepath={filepath}. "
187 "Please ensure the file is an accessible `.keras` "
188 "zip file."
189 )
File ~/akash_ai_ml/video_analysis/.venv/lib64/python3.11/site-packages/keras/src/legacy/saving/legacy_h5_format.py:138, in load_model_from_hdf5(filepath, custom_objects, compile)
133 model = saving_utils.model_from_config(
134 model_config, custom_objects=custom_objects
...
File h5py/_objects.pyx:55, in h5py._objects.with_phil.wrapper()
File h5py/h5o.pyx:189, in h5py.h5o.open()
KeyError: "Unable to synchronously open object (object 'time_distributed_2' doesn't exist)"
这是我的代码:
# Specify the height and width to which each video frame will be resized in our dataset.
IMAGE_HEIGHT , IMAGE_WIDTH = 64, 64
# Specify the number of frames of a video that will be fed to the model as one sequence.
SEQUENCE_LENGTH = 2
DATASET_DIR = "voilence_dataset/Real Life Violence Dataset"
CLASSES_LIST = ["NonViolence","Violence"]
from keras.applications.mobilenet_v2 import MobileNetV2
mobilenet = MobileNetV2( include_top=False , weights="imagenet")
#Fine-Tuning to make the last 40 layer trainable
mobilenet.trainable=True
for layer in mobilenet.layers[:-40]:
layer.trainable=False
#mobilenet.summary()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, TimeDistributed, Dropout, Flatten,
Bidirectional, LSTM, Dense
from keras.applications.mobilenet_v2 import MobileNetV2
def create_model(input_shape, num_classes, mobilenet):
model = Sequential()
model.add(TimeDistributed(mobilenet, input_shape=input_shape))
model.add(Dropout(0.25))
model.add(TimeDistributed(Flatten()))
lstm_fw = LSTM(units=32)
lstm_bw = LSTM(units=32, go_backwards=True)
model.add(Bidirectional(lstm_fw, backward_layer=lstm_bw))
model.add(Dropout(0.25))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
return model
input_shape = (SEQUENCE_LENGTH, IMAGE_HEIGHT, IMAGE_WIDTH, 3)
num_classes = len(CLASSES_LIST)
MoBiLSTM_model = create_model(input_shape, num_classes, mobilenet)
# Construct the model
MoBiLSTM_model = create_model(input_shape, num_classes, mobilenet)
# Plot the structure of the constructed model
plot_model(MoBiLSTM_model, to_file='MobBiLSTM_model_structure_plot.png',
show_shapes=True, show_layer_names=True)
# Create Early Stopping Callback to monitor the accuracy
early_stopping_callback = EarlyStopping(monitor = 'val_accuracy', patience = 10,
restore_best_weights = True)
# Create ReduceLROnPlateau Callback to reduce overfitting by decreasing learning
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.6,
patience=5,
min_lr=0.00005,
verbose=1)
# Compiling the model
MoBiLSTM_model.compile(loss = 'categorical_crossentropy', optimizer = 'sgd', metrics =
["accuracy"])
# Fitting the model
MobBiLSTM_model_history = MoBiLSTM_model.fit(x = features_train, y = labels_train,
epochs = 30, batch_size = 8 ,
shuffle = True, validation_split = 0.2,
callbacks = [early_stopping_callback,reduce_lr])
# Define the file path where you want to save the model
model_save_path = 'model_new.h5'
# Save the trained model
MoBiLSTM_model.save(model_save_path)
print("Model saved successfully at:", model_save_path)
1 个回答
0
看起来你遇到了两个不同的问题。关于保存错误,确保你没有用一个已经存在的名字来保存模型。每次保存时使用一个独特的名字可能会有帮助,或者在重新保存之前先删除已有的文件。至于加载错误,关键错误提示你正在尝试加载的模型引用了一个层('time_distributed_2'),但这个层和你提供的自定义对象不匹配。仔细检查一下你的模型结构,确保在尝试加载模型时,所有自定义层或对象都正确指定。