Sara-Adjo commited on
Commit
6b950bf
·
verified ·
1 Parent(s): 347aad6

Update model_tensorflow.py

Browse files
Files changed (1) hide show
  1. 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
- def separable_block(x, filters: int, dropout_rate: float = 0.25):
6
- x = layers.SeparableConv2D(
7
- filters, kernel_size=3, padding="same", use_bias=False)(x)
 
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(pool_size=2)(x)
17
- x = layers.SpatialDropout2D(rate=dropout_rate)(x)
18
 
19
  return x
20
 
21
 
22
- def build_sara_tf_model(input_shape=(150, 150, 3), num_classes: int = 6) -> Model:
 
 
 
23
 
24
- inputs = tf.keras.Input(shape=input_shape, name="image_input")
25
 
26
- x = layers.Conv2D(32, kernel_size=3, padding="same",
27
- use_bias=False, name="stem_conv")(inputs)
28
- x = layers.BatchNormalization(name="stem_bn")(x)
29
- x = layers.Activation("relu", name="stem_relu")(x)
30
 
31
- x = separable_block(x, filters=64, dropout_rate=0.25)
32
- x = separable_block(x, filters=128, dropout_rate=0.25)
33
- x = separable_block(x, filters=256, dropout_rate=0.30)
 
34
 
35
- x = layers.GlobalAveragePooling2D(name="gap")(x)
 
36
 
37
- x = layers.Dense(128, use_bias=False, name="fc1")(x)
38
- x = layers.BatchNormalization(name="fc1_bn")(x)
39
- x = layers.Activation("relu", name="fc1_relu")(x)
40
- x = layers.Dropout(0.5, name="fc1_drop")(x)
41
 
42
- outputs = layers.Dense(num_classes, activation="softmax",
43
- name="predictions")(x)
44
 
45
- model = Model(inputs=inputs, outputs=outputs, name="SaraCNN_TF")
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