Spaces:
Sleeping
Sleeping
Update model_tensorflow.py
Browse files- model_tensorflow.py +29 -34
model_tensorflow.py
CHANGED
|
@@ -1,48 +1,52 @@
|
|
| 1 |
import tensorflow as tf
|
| 2 |
from tensorflow.keras import layers, Model
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
x = layers.BatchNormalization()(x)
|
| 9 |
x = layers.Activation("relu")(x)
|
| 10 |
|
| 11 |
-
x = layers.SeparableConv2D(
|
| 12 |
-
filters, kernel_size=3, padding="same", use_bias=False)(x)
|
| 13 |
x = layers.BatchNormalization()(x)
|
| 14 |
x = layers.Activation("relu")(x)
|
| 15 |
|
| 16 |
-
x = layers.MaxPooling2D(
|
| 17 |
-
x = layers.SpatialDropout2D(
|
| 18 |
|
| 19 |
return x
|
| 20 |
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
inputs = tf.keras.Input(shape=input_shape
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
x = layers.BatchNormalization(
|
| 29 |
-
x = layers.Activation("relu"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
x = separable_block(x,
|
| 33 |
-
x = separable_block(x,
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
x = layers.Dense(128, use_bias=False
|
| 38 |
-
x = layers.BatchNormalization(
|
| 39 |
-
x = layers.Activation("relu"
|
| 40 |
-
x = layers.Dropout(0.5
|
| 41 |
|
| 42 |
-
outputs = layers.Dense(num_classes, activation="softmax"
|
| 43 |
-
name="predictions")(x)
|
| 44 |
|
| 45 |
-
model = Model(inputs
|
| 46 |
|
| 47 |
model.compile(
|
| 48 |
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
|
|
@@ -52,12 +56,3 @@ def build_sara_tf_model(input_shape=(150, 150, 3), num_classes: int = 6) -> Mode
|
|
| 52 |
|
| 53 |
return model
|
| 54 |
|
| 55 |
-
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
model = build_sara_tf_model()
|
| 58 |
-
|
| 59 |
-
model.build((None, 150, 150, 3))
|
| 60 |
-
|
| 61 |
-
model.load_weights("ton_modele_weights.h5")
|
| 62 |
-
|
| 63 |
-
print("Modèle chargé correctement !")
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
from tensorflow.keras import layers, Model
|
| 3 |
|
| 4 |
+
# ==============================
|
| 5 |
+
# Separable Convolution Block
|
| 6 |
+
# ==============================
|
| 7 |
+
def separable_block(x, filters, dropout_rate=0.25):
|
| 8 |
+
x = layers.SeparableConv2D(filters, 3, padding="same", use_bias=False)(x)
|
| 9 |
x = layers.BatchNormalization()(x)
|
| 10 |
x = layers.Activation("relu")(x)
|
| 11 |
|
| 12 |
+
x = layers.SeparableConv2D(filters, 3, padding="same", use_bias=False)(x)
|
|
|
|
| 13 |
x = layers.BatchNormalization()(x)
|
| 14 |
x = layers.Activation("relu")(x)
|
| 15 |
|
| 16 |
+
x = layers.MaxPooling2D()(x)
|
| 17 |
+
x = layers.SpatialDropout2D(dropout_rate)(x)
|
| 18 |
|
| 19 |
return x
|
| 20 |
|
| 21 |
|
| 22 |
+
# ==============================
|
| 23 |
+
# Model Definition
|
| 24 |
+
# ==============================
|
| 25 |
+
def build_sara_tf_model(input_shape=(150, 150, 3), num_classes=6):
|
| 26 |
|
| 27 |
+
inputs = tf.keras.Input(shape=input_shape)
|
| 28 |
|
| 29 |
+
# Stem
|
| 30 |
+
x = layers.Conv2D(32, 3, padding="same", use_bias=False)(inputs)
|
| 31 |
+
x = layers.BatchNormalization()(x)
|
| 32 |
+
x = layers.Activation("relu")(x)
|
| 33 |
|
| 34 |
+
# Feature extractor
|
| 35 |
+
x = separable_block(x, 64, 0.25)
|
| 36 |
+
x = separable_block(x, 128, 0.25)
|
| 37 |
+
x = separable_block(x, 256, 0.30)
|
| 38 |
|
| 39 |
+
# Head
|
| 40 |
+
x = layers.GlobalAveragePooling2D()(x)
|
| 41 |
|
| 42 |
+
x = layers.Dense(128, use_bias=False)(x)
|
| 43 |
+
x = layers.BatchNormalization()(x)
|
| 44 |
+
x = layers.Activation("relu")(x)
|
| 45 |
+
x = layers.Dropout(0.5)(x)
|
| 46 |
|
| 47 |
+
outputs = layers.Dense(num_classes, activation="softmax")(x)
|
|
|
|
| 48 |
|
| 49 |
+
model = Model(inputs, outputs, name="SaraCNN_TF")
|
| 50 |
|
| 51 |
model.compile(
|
| 52 |
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
|
|
|
|
| 56 |
|
| 57 |
return model
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|