Sara-Adjo commited on
Commit
335db64
Β·
verified Β·
1 Parent(s): 257fcc5

Update model_tensorflow.py

Browse files
Files changed (1) hide show
  1. model_tensorflow.py +71 -0
model_tensorflow.py CHANGED
@@ -8,6 +8,77 @@ Architecture summary:
8
 
9
  """
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  import tensorflow as tf
12
  from tensorflow.keras import layers, Model
13
 
 
8
 
9
  """
10
 
11
+ """
12
+ Architecture summary:
13
+ Stem : Conv(32, 3Γ—3) β†’ BN β†’ ReLU
14
+ Stage 1: SepConv(64) β†’ BN β†’ ReLU β†’ MaxPool β†’ Dropout
15
+ Stage 2: SepConv(128) β†’ BN β†’ ReLU β†’ MaxPool β†’ Dropout
16
+ Stage 3: SepConv(256) β†’ BN β†’ ReLU β†’ MaxPool β†’ Dropout
17
+ Head : GlobalAvgPool β†’ Dense(128) β†’ BN β†’ ReLU β†’ Dropout β†’ Dense(6, softmax)
18
+
19
+ """
20
+ import os
21
+ os.environ["TF_USE_LEGACY_KERAS"] = "1"
22
+ import tensorflow as tf
23
+ from tensorflow.keras import layers, Model
24
+
25
+
26
+ def separable_block(x, filters: int, dropout_rate: float = 0.25):
27
+ x = layers.SeparableConv2D(
28
+ filters, kernel_size=3, padding="same", use_bias=False)(x)
29
+ x = layers.BatchNormalization()(x)
30
+ x = layers.Activation("relu")(x)
31
+
32
+ x = layers.SeparableConv2D(
33
+ filters, kernel_size=3, padding="same", use_bias=False)(x)
34
+ x = layers.BatchNormalization()(x)
35
+ x = layers.Activation("relu")(x)
36
+
37
+ x = layers.MaxPooling2D(pool_size=2)(x)
38
+
39
+ x = layers.SpatialDropout2D(rate=dropout_rate)(x)
40
+
41
+ return x
42
+
43
+
44
+ def build_sara_tf_model(input_shape=(150, 150, 3), num_classes: int = 6) -> Model:
45
+
46
+ inputs = tf.keras.Input(shape=input_shape, name="image_input")
47
+
48
+ x = layers.Conv2D(32, kernel_size=3, padding="same",
49
+ use_bias=False, name="stem_conv")(inputs)
50
+ x = layers.BatchNormalization(name="stem_bn")(x)
51
+ x = layers.Activation("relu", name="stem_relu")(x)
52
+
53
+ x = separable_block(x, filters=64, dropout_rate=0.25)
54
+ x = separable_block(x, filters=128, dropout_rate=0.25)
55
+ x = separable_block(x, filters=256, dropout_rate=0.30)
56
+
57
+
58
+ x = layers.GlobalAveragePooling2D(name="gap")(x)
59
+
60
+ x = layers.Dense(128, use_bias=False, name="fc1")(x)
61
+ x = layers.BatchNormalization(name="fc1_bn")(x)
62
+ x = layers.Activation("relu", name="fc1_relu")(x)
63
+ x = layers.Dropout(0.5, name="fc1_drop")(x)
64
+
65
+ outputs = layers.Dense(num_classes, activation="softmax",
66
+ name="predictions")(x)
67
+
68
+ model = Model(inputs=inputs, outputs=outputs, name="SaraCNN_TF")
69
+
70
+ model.compile(
71
+ optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
72
+ loss="categorical_crossentropy",
73
+ metrics=["accuracy"],
74
+ )
75
+
76
+ return model
77
+
78
+
79
+ if __name__ == "__main__":
80
+ model = build_sara_tf_model()
81
+ model.summary()
82
  import tensorflow as tf
83
  from tensorflow.keras import layers, Model
84