-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg19_final.py
57 lines (39 loc) · 1.52 KB
/
vgg19_final.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
'''
vgg19 model with transformed fully connected layers
'''
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.layers import Input, Flatten, Dense
from tensorflow.keras.optimizers import Adam
def pretrained_model(img_shape, num_classes):
# LOAD VGG19
input_tensor = Input(shape=img_shape)
model = VGG19(weights='imagenet',
include_top=False,
input_tensor=input_tensor)
#freeze first 12 layers
for layer in model.layers[:12]:
layer.trainable = False
print(layer)
print('****************')
for layer in model.layers[12:]:
layer.trainable = True
print(layer)
# CREATE A TOP MODEL
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(4096, activation='relu', name='fc1'))
top_model.add(Dense(4096, activation='relu', name='fc2'))
top_model.add(Dense(num_classes, activation='softmax', name='predictions'))
# CREATE AMODEL FROM VGG16 BY COPYING ALL THE LAYERS OF VGG19
new_model = Sequential()
for l in model.layers:
new_model.add(l)
# CONCATENATE THE TWO MODELS
new_model.add(top_model)
# COMPILE THE MODEL
adam = Adam(lr=0.000000001)
new_model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
return new_model
if __name__ == '__main__':
model=pretrained_model((64, 64, 3), 3)
model.summary()