-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdeeplearning_models.py
More file actions
90 lines (63 loc) · 2.36 KB
/
deeplearning_models.py
File metadata and controls
90 lines (63 loc) · 2.36 KB
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
import tensorflow
from tensorflow.keras.layers import Conv2D, Input, Dense, MaxPool2D, BatchNormalization, GlobalAvgPool2D, Flatten
from tensorflow.keras import Model
# functional approach : function that returns a model
def functional_model():
my_input = Input(shape=(28,28,1))
x = Conv2D(32, (3,3), activation='relu')(my_input)
x = Conv2D(64, (3,3), activation='relu')(x)
x = MaxPool2D()(x)
x = BatchNormalization()(x)
x = Conv2D(128, (3,3), activation='relu')(x)
x = MaxPool2D()(x)
x = BatchNormalization()(x)
x = GlobalAvgPool2D()(x)
x = Dense(64, activation='relu')(x)
x = Dense(10, activation='softmax')(x)
model = tensorflow.keras.Model(inputs=my_input, outputs=x)
return model
# tensorflow.keras.Model : inherit from this class
class MyCustomModel(tensorflow.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = Conv2D(32, (3,3), activation='relu')
self.conv2 = Conv2D(64, (3,3), activation='relu')
self.maxpool1 = MaxPool2D()
self.batchnorm1 = BatchNormalization()
self.conv3 = Conv2D(128, (3,3), activation='relu')
self.maxpool2 = MaxPool2D()
self.batchnorm2 = BatchNormalization()
self.globalavgpool1 = GlobalAvgPool2D()
self.dense1 = Dense(64, activation='relu')
self.dense2 = Dense(10, activation='softmax')
def call(self, my_input):
x = self.conv1(my_input)
x = self.conv2(x)
x = self.maxpool1(x)
x = self.batchnorm1(x)
x = self.conv3(x)
x = self.maxpool2(x)
x = self.batchnorm2(x)
x = self.globalavgpool1(x)
x = self.dense1(x)
x = self.dense2(x)
return x
def streesigns_model(nbr_classes):
my_input = Input(shape=(60,60,3))
x = Conv2D(32, (3,3), activation='relu')(my_input)
x = MaxPool2D()(x)
x = BatchNormalization()(x)
x = Conv2D(64, (3,3), activation='relu')(x)
x = MaxPool2D()(x)
x = BatchNormalization()(x)
x = Conv2D(128, (3,3), activation='relu')(x)
x = MaxPool2D()(x)
x = BatchNormalization()(x)
# x = Flatten()(x)
x = GlobalAvgPool2D()(x)
x = Dense(128, activation='relu')(x)
x = Dense(nbr_classes, activation='softmax')(x)
return Model(inputs=my_input, outputs=x)
if __name__=='__main__':
model = streesigns_model(10)
model.summary()