-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_definitions.py
132 lines (102 loc) · 4.56 KB
/
cnn_definitions.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
import math
import keras
# This class contains methods to create different CNN models.
def cnn1(data_shape, optimizer):
"""
Original model from the website https://www.kaggle.com/amro91/planes-classification-with-cnn
:param data_shape:
:param optimizer:
:return:
"""
kernel_size = 3
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(16, (kernel_size), strides=(1, 1), padding='valid',
input_shape=(data_shape[1], data_shape[2], data_shape[3])))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(MaxPooling2D((2,2)))
model.add(keras.layers.Conv2D(32, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(MaxPooling2D((2,2)))
model.add(keras.layers.Conv2D(64, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(MaxPooling2D((2,2)))
model.add(keras.layers.Conv2D(64, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
model.add(keras.layers.MaxPooling2D((2, 2)))
model.add(keras.layers.Conv2D(64, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
model.add(keras.layers.MaxPooling2D((2, 2)))
model.add(keras.layers.Flatten())
# model.add(Dense(64, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
def step_decay1(epoch):
"""
Returns current learning rate for the given epoch.
"""
initial_lrate = 0.0001
drop = 0.5
epochs_drop = 10.0
lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))
# if epoch:
# lrate = initial_lrate/np.sqrt(epoch)
# else:
# return initial_lrate
return lrate
def cnn2(data_shape, optimizer):
"""
Modification of the original CNN from the website, i.e. a few commented lines https://www.kaggle.com/amro91/planes-classification-with-cnn
:param data_shape:
:param optimizer:
:return:
"""
kernel_size = 3
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(16, (kernel_size), strides=(1, 1), padding='valid',
input_shape=(data_shape[1], data_shape[2], data_shape[3])))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(keras.layers.MaxPooling2D((2,2))) # 1
model.add(keras.layers.Conv2D(32, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(keras.layers.MaxPooling2D((2,2))) # 2
model.add(keras.layers.Conv2D(64, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(keras.layers.MaxPooling2D((2,2))) # 3
model.add(keras.layers.Conv2D(64, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(keras.layers.MaxPooling2D((2,2))) # 4
model.add(keras.layers.Conv2D(64, (kernel_size), strides=(1, 1), padding='valid'))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
# model.add(keras.layers.MaxPooling2D((2,2))) # 5
model.add(keras.layers.Flatten())
# model.add(keras.layers.Dense(64, activation='relu')) # 6
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
def cnn3(data_shape, optimizer):
"""
Custom CNN
:return:
"""
kernel_size = 3
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(512, (kernel_size), strides=(1, 1), padding='valid',
input_shape=(data_shape[1], data_shape[2], data_shape[3])))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model