-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_model.py
56 lines (49 loc) · 1.38 KB
/
convert_model.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
reconstruct the model from h5 format to .keras format
"""
import tensorflow as tf
from tensorflow import keras
def create_model():
model = keras.Sequential([
keras.layers.BatchNormalization(
input_shape=(80, 160, 3),
name='batch_normalization_1'
),
keras.layers.Conv2D(
filters=8,
kernel_size=(3, 3),
activation='relu',
name='Conv1'
),
keras.layers.Conv2D(
filters=16,
kernel_size=(3, 3),
activation='relu',
name='Conv2'
)
])
return model
# Create the model
model = create_model()
# Try to load weights
try:
model.load_weights('model.h5')
print("Weights loaded successfully.")
except Exception as e:
print(f"Error loading weights: {str(e)}")
# Print model summary
model.summary()
# Save in .keras format
try:
model.save('model.keras')
print("Model saved successfully in .keras format.")
except Exception as e:
print(f"Error saving model: {str(e)}")
# Optionally, export as SavedModel
try:
tf.saved_model.save(model, 'saved_model')
print("Model exported successfully as SavedModel.")
except Exception as e:
print(f"Error exporting SavedModel: {str(e)}")
# Print TensorFlow version
print(f"TensorFlow version: {tf.__version__}")