File size: 1,305 Bytes
45742a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import tensorflow as tf
import os

# Use relative import for use as a module
try:
    from . import config
except ImportError:
    import config

# --- Configuration ---
# 1. Set the path to your existing .h5 model file
h5_model_path = os.path.join(config.MODEL_DIR, "cnn_lstm_video_model.h5")

# 2. Set the desired path for the new .keras model file
keras_model_path = os.path.join(config.MODEL_DIR, "video_model_v1.keras")
# ---------------------

print(f"Loading model from: {h5_model_path}...")

try:
    # 1. Load the model from the .h5 file
    model = tf.keras.models.load_model(h5_model_path, compile=False)
    print("Model loaded successfully.")

    # 2. Save the model in the .keras format
    # TensorFlow automatically detects the format from the .keras extension
    print(f"Saving model to: {keras_model_path}...")
    model.save(keras_model_path)
    
    print("-" * 30)
    print("✅ Conversion Successful!")
    print(f"New model saved at: {keras_model_path}")
    print("-" * 30)

except FileNotFoundError:
    print(f"ERROR: The file '{h5_model_path}' was not found.")
except Exception as e:
    print(f"An error occurred: {e}")
    print("\nIf your model has custom layers or functions, you may need to register them.")
    print("See the 'Handling Custom Objects' section below.")