-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
152 lines (128 loc) · 5.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from tensorflow.keras import layers, models
from tensorflow.keras.applications import ResNet50
# define model
def create_cnn_model(img_shape, n_classes, false_labels_ratio):
"""
Create a basic CNN
Parameters
----------
img_shape : tuple of two ints
shape of image
n_classes : int
number of classes
false_labels_ratio : float
ratio of labels that are falsified
Returns
-------
model : keras.Sequential
cnn resnet model
"""
activation_func = None
kernel_tupel = (3, 3)
pool_size = (2, 2)
leaky_alpha = 0.3
dropout_ratio_conv = 0.1
dropout_ratio_dense = 0.1
model = models.Sequential()
model.add(layers.Conv2D(32,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.Conv2D(32,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.MaxPooling2D(pool_size))
model.add(layers.Dropout(dropout_ratio_conv))
model.add(layers.Conv2D(64,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.Conv2D(64,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.MaxPooling2D(pool_size))
model.add(layers.Dropout(dropout_ratio_conv))
model.add(layers.Conv2D(128,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.Conv2D(128,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.MaxPooling2D(pool_size))
model.add(layers.Dropout(dropout_ratio_conv))
model.add(layers.Conv2D(256,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.MaxPooling2D(pool_size))
model.add(layers.Dropout(dropout_ratio_conv))
model.add(layers.Conv2D(512,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.MaxPooling2D(pool_size))
model.add(layers.Dropout(dropout_ratio_conv))
model.add(layers.Conv2D(1024,
kernel_tupel,
activation=activation_func,
input_shape=(img_shape[0], img_shape[1], 3)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.MaxPooling2D(pool_size))
model.add(layers.Flatten())
model.add(layers.Dropout(dropout_ratio_dense))
model.add(layers.Dense(512))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.Dropout(dropout_ratio_dense))
model.add(layers.Dense(256))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=leaky_alpha))
model.add(layers.Dropout(dropout_ratio_dense))
model.add(layers.Dense(n_classes, activation='softmax'))
model._name=f'basic_{format(int(false_labels_ratio*10000),"05d")}r_{n_classes}c'
return model
def create_resnet_model(img_shape, n_classes, false_labels_ratio):
"""
Create a CNN based on ResNet50
Parameters
----------
img_shape : tuple of two ints
shape of image
n_classes : int
number of classes
false_labels_ratio : float
ratio of labels that are falsified
Returns
-------
model : keras.Sequential
cnn resnet model
"""
resnet_model = ResNet50(include_top=False, weights="imagenet", input_shape=(img_shape[0], img_shape[1], 3))
model = models.Sequential()
model.add(resnet_model)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dense(2048, activation="relu"))
model.add(layers.Dense(n_classes, activation='softmax'))
model._name=f'resnet_{format(int(false_labels_ratio*10000),"05d")}r_{n_classes}c'
return model