-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
113 lines (88 loc) · 4 KB
/
models.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import tensorflow as tf
class MobileNetV2(tf.keras.Model):
def __init__(self, num_classes, size=224):
super(MobileNetV2, self).__init__()
# Create the base model from the pre-trained model MobileNet V2
self.base_model = tf.keras.applications.MobileNetV2(
input_shape=(size, size, 3), include_top=False, weights='imagenet')
# Freeze the base model
self.base_model.trainable = False
# Create new model on top
self.global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
self.output_layer = tf.keras.layers.Dense(num_classes, activation='softmax')
self.dense_1 = tf.keras.layers.Dense(1024, activation='relu')
self.dropout_1 = tf.keras.layers.Dropout(0.2)
self.dense_2 = tf.keras.layers.Dense(512, activation='relu')
self.dropout_2 = tf.keras.layers.Dropout(0.2)
self.dense_3 = tf.keras.layers.Dense(256, activation='relu')
self.dropout_3 = tf.keras.layers.Dropout(0.2)
def call(self, inputs):
x = self.base_model(inputs)
x = self.global_average_layer(x)
# Add dense layers on top
x = self.dense_1(x)
x = self.dropout_1(x)
x = self.dense_2(x)
x = self.dropout_2(x)
x = self.dense_3(x)
x = self.dropout_3(x)
x = self.output_layer(x)
return x
class ResNet50(tf.keras.Model):
def __init__(self, num_classes, size=224):
super(ResNet50, self).__init__()
# Create the base model from the pre-trained model MobileNet V2
self.base_model = tf.keras.applications.ResNet50(
input_shape=(size, size, 3), include_top=False, weights='imagenet')
# Freeze the base model
self.base_model.trainable = False
# Create new model on top
self.global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
self.output_layer = tf.keras.layers.Dense(num_classes, activation='softmax')
self.dense_1 = tf.keras.layers.Dense(1024, activation='relu')
self.dropout_1 = tf.keras.layers.Dropout(0.2)
self.dense_2 = tf.keras.layers.Dense(512, activation='relu')
self.dropout_2 = tf.keras.layers.Dropout(0.2)
self.dense_3 = tf.keras.layers.Dense(256, activation='relu')
self.dropout_3 = tf.keras.layers.Dropout(0.2)
def call(self, inputs):
x = self.base_model(inputs, training=False)
x = self.global_average_layer(x)
# Add dense layers on top
x = self.dense_1(x)
x = self.dropout_1(x)
x = self.dense_2(x)
x = self.dropout_2(x)
x = self.dense_3(x)
x = self.dropout_3(x)
x = self.output_layer(x)
return x
class EfficientNetB0(tf.keras.Model):
def __init__(self, num_classes, size=224):
super(EfficientNetB0, self).__init__()
# Create the base model from the pre-trained model MobileNet V2
self.base_model = tf.keras.applications.EfficientNetB0(
input_shape=(size, size, 3), include_top=False, weights='imagenet')
# Freeze the base model
self.base_model.trainable = False
# Create new model on top
self.global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
self.output_layer = tf.keras.layers.Dense(num_classes, activation='softmax')
self.dense_1 = tf.keras.layers.Dense(1024, activation='relu')
self.dropout_1 = tf.keras.layers.Dropout(0.2)
self.dense_2 = tf.keras.layers.Dense(512, activation='relu')
self.dropout_2 = tf.keras.layers.Dropout(0.2)
self.dense_3 = tf.keras.layers.Dense(256, activation='relu')
self.dropout_3 = tf.keras.layers.Dropout(0.2)
def call(self, inputs):
x = self.base_model(inputs)
x = self.global_average_layer(x)
# Add dense layers on top
x = self.dense_1(x)
x = self.dropout_1(x)
x = self.dense_2(x)
x = self.dropout_2(x)
x = self.dense_3(x)
x = self.dropout_3(x)
x = self.output_layer(x)
return x