-
Notifications
You must be signed in to change notification settings - Fork 28
/
odir_model_vgg16.py
120 lines (108 loc) · 4.62 KB
/
odir_model_vgg16.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
# Copyright 2019-2020 Jordi Corbilla. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import tensorflow
from tensorflow.keras import models, layers
from tensorflow.keras.optimizers import SGD
from odir_model_base import ModelBase
class Vgg16(ModelBase):
def compile(self):
x = models.Sequential()
trainable = False
# Block 1
layer = layers.Conv2D(input_shape=self.input_shape, filters=64,kernel_size=(3,3),padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.MaxPooling2D((2, 2), strides=(2, 2))
layer.trainable = trainable
x.add(layer)
# Block 2
layer = layers.Conv2D(128, kernel_size=(3,3),padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(128, kernel_size=(3,3),padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.MaxPooling2D((2, 2), strides=(2, 2))
layer.trainable = trainable
x.add(layer)
# Block 3
layer = layers.Conv2D(256, kernel_size=(3,3),padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(256, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(256, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.MaxPooling2D((2, 2), strides=(2, 2))
layer.trainable = trainable
x.add(layer)
# Block 4
layer = layers.Conv2D(512, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(512, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(512, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.MaxPooling2D((2, 2), strides=(2, 2))
layer.trainable = trainable
x.add(layer)
# Block 5
layer = layers.Conv2D(512, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(512, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.Conv2D(512, kernel_size=(3, 3), padding="same", activation="relu")
layer.trainable = trainable
x.add(layer)
layer = layers.MaxPooling2D((2, 2), strides=(2, 2))
layer.trainable = trainable
x.add(layer)
layer = layers.Flatten()
layer.trainable = trainable
x.add(layer)
layer = layers.Dense(4096, activation='relu')
layer.trainable = trainable
x.add(layer)
layer = layers.Dense(4096, activation='relu')
layer.trainable = trainable
x.add(layer)
layer = layers.Dense(1000, activation='softmax')
layer.trainable = trainable
x.add(layer)
# Transfer learning, load previous weights
x.load_weights(r'C:\temp\vgg16_weights_tf_dim_ordering_tf_kernels.h5')
# Remove last layer
x.pop()
# Add new dense layer
x.add(layers.Dense(8, activation='sigmoid'))
#optimizer = tensorflow.keras.optimizers.SGD(learning_rate=1e-3)
sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
print('Configuration Start -------------------------')
print(sgd.get_config())
print('Configuration End -------------------------')
x.compile(optimizer=sgd, loss='binary_crossentropy', metrics=self.metrics)
self.show_summary(x)
self.plot_summary(x, 'model_vgg16net.png')
return x