-
Notifications
You must be signed in to change notification settings - Fork 0
/
BarkNet.py
135 lines (114 loc) · 6.03 KB
/
BarkNet.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
from tensorflow import keras
# BarkNet v1.0
# After 10 epochs it's overfitting
# [train_accuracy = 0.97, valid_accuracy = 0.51]
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[224,224]))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Dense(224, kernel_initializer="he_normal", use_bias=False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(112, kernel_initializer="he_normal", use_bias=False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(56, kernel_initializer="he_normal", use_bias=False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(28, kernel_initializer="he_normal", use_bias=False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(7, kernel_initializer="he_normal", use_bias=False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(5, activation="softmax"))
# BarkNet v1.2
# slightly different architecture with convolution layers after input. Still overfitting
# [train_accuracy = 0.97, valid_accuracy = 0.80] 35 epochs
# after initial 20 epochs learning rate has been reduced to 0.0001
from functools import partial
RegularDense = partial(keras.layers.Dense,
activation="elu",
kernel_initializer="he_normal",
kernel_regularizer=keras.regularizers.l2(0.01))
from tensorflow.keras.optimizers import Nadam
BarkNet1_2.compile(loss="sparse_categorical_crossentropy",
optimizer=Nadam(learning_rate=0.001),
metrics=["accuracy"])
BarkNet1_2 = keras.models.Sequential()
BarkNet1_2.add(keras.layers.InputLayer(input_shape=[224,224,3]))
BarkNet1_2.add(keras.layers.BatchNormalization())
BarkNet1_2.add(keras.layers.ZeroPadding2D(padding=3))
BarkNet1_2.add(keras.layers.Conv2D(filters=32, kernel_size=(7,7), data_format='channels_last'))
BarkNet1_2.add(keras.layers.MaxPooling2D())
BarkNet1_2.add(keras.layers.Flatten())
BarkNet1_2.add(keras.layers.BatchNormalization())
BarkNet1_2.add(RegularDense(112))
BarkNet1_2.add(keras.layers.BatchNormalization())
BarkNet1_2.add(RegularDense(56))
BarkNet1_2.add(keras.layers.BatchNormalization())
BarkNet1_2.add(RegularDense(28))
BarkNet1_2.add(keras.layers.BatchNormalization())
BarkNet1_2.add(RegularDense(14))
BarkNet1_2.add(keras.layers.Dropout(0.3))
BarkNet1_2.add(keras.layers.Dense(5, activation="softmax",
kernel_initializer="glorot_uniform"))
# BarkNet v1.3
# This network doesn't show major overfitting symptoms unlike its predecessors
# [train_accuracy = 0.97, valid_accuracy = 0.90] 15 epochs
# Learning rate has been changed every 5 epochs 0.003 -> 0.001 -> 0.0001
from functools import partial
RegularDense = partial(keras.layers.Dense,
activation="elu",
kernel_initializer="he_normal",
kernel_regularizer=keras.regularizers.l2(0.01))
from tensorflow.keras.optimizers import Nadam
BarkNet1_3.compile(loss="sparse_categorical_crossentropy",
optimizer=Nadam(learning_rate=0.0001),
metrics=["accuracy"])
BarkNet1_3 = keras.models.Sequential()
BarkNet1_3.add(keras.layers.InputLayer(input_shape=[224,224,3]))
BarkNet1_3.add(keras.layers.BatchNormalization())
BarkNet1_3.add(keras.layers.ZeroPadding2D(padding=3))
BarkNet1_3.add(keras.layers.Conv2D(filters=32, kernel_size=(7,7), data_format='channels_last'))
BarkNet1_3.add(keras.layers.MaxPooling2D())
BarkNet1_3.add(keras.layers.Conv2D(filters=32, kernel_size=(5,5), data_format='channels_last', padding="same"))
BarkNet1_3.add(keras.layers.MaxPooling2D())
BarkNet1_3.add(keras.layers.Conv2D(filters=32, kernel_size=(3,3), data_format='channels_last', padding="same"))
BarkNet1_3.add(keras.layers.MaxPooling2D())
BarkNet1_3.add(keras.layers.Flatten())
BarkNet1_3.add(keras.layers.BatchNormalization())
BarkNet1_3.add(RegularDense(28))
BarkNet1_3.add(keras.layers.BatchNormalization())
BarkNet1_3.add(RegularDense(28))
BarkNet1_3.add(keras.layers.BatchNormalization())
BarkNet1_3.add(RegularDense(14))
BarkNet1_3.add(keras.layers.BatchNormalization())
BarkNet1_3.add(RegularDense(7))
BarkNet1_3.add(keras.layers.Dropout(0.3))
BarkNet1_3.add(keras.layers.Dense(5, activation="softmax",
kernel_initializer="glorot_uniform"))
# BarkNet v1.4
# Much deeper convoluted layer than previous architectures but shows lower performance
# [train_accuracy = 0.99, valid_accuracy = 0.85] 20 epochs
BarkNet1_4 = keras.models.Sequential()
BarkNet1_4.add(keras.layers.InputLayer(input_shape=[224,224,3]))
BarkNet1_4.add(keras.layers.ZeroPadding2D(padding=2))
BarkNet1_4.add(keras.layers.Conv2D(filters=32, kernel_size=(5,5), data_format='channels_last'))
BarkNet1_4.add(keras.layers.Conv2D(filters=32, kernel_size=(5,5), data_format='channels_last', padding="same"))
BarkNet1_4.add(keras.layers.MaxPooling2D())
BarkNet1_4.add(keras.layers.Conv2D(filters=64, kernel_size=(3,3), data_format='channels_last', padding="same"))
BarkNet1_4.add(keras.layers.Conv2D(filters=64, kernel_size=(3,3), data_format='channels_last', padding="same"))
BarkNet1_4.add(keras.layers.MaxPooling2D())
BarkNet1_4.add(keras.layers.Conv2D(filters=128, kernel_size=(1,1), data_format='channels_last', padding="same"))
BarkNet1_4.add(keras.layers.MaxPooling2D())
BarkNet1_4.add(keras.layers.Conv2D(filters=128, kernel_size=(1,1), data_format='channels_last', padding="same"))
BarkNet1_4.add(keras.layers.Flatten())
BarkNet1_4.add(keras.layers.BatchNormalization())
BarkNet1_4.add(RegularDense(28))
BarkNet1_4.add(keras.layers.BatchNormalization())
BarkNet1_4.add(RegularDense(28))
BarkNet1_4.add(keras.layers.BatchNormalization())
BarkNet1_4.add(RegularDense(14))
BarkNet1_4.add(keras.layers.Dropout(0.3))
BarkNet1_4.add(keras.layers.Dense(5, activation="softmax",
kernel_initializer="glorot_uniform"))