forked from wlkjyh/dianxuan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg16.py
72 lines (59 loc) · 3.3 KB
/
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
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D
"""
VGG16代码来源于https://github.com/bubbliiiing/Siamese-keras
也可以使用tensorflow.keras.applications.VGG16实现
"""
class VGG16:
def __init__(self):
# 第一个卷积部分
# 105, 105, 3 -> 105, 105, 64 -> 52, 52, 64
self.block1_conv1 = Conv2D(64, (3,3), activation = 'relu', padding = 'same',name = 'block1_conv1')
self.block1_conv2 = Conv2D(64, (3,3), activation = 'relu', padding = 'same',name = 'block1_conv2')
self.block1_pool = MaxPooling2D((2,2), strides = (2,2), name = 'block1_pool')
# 第二个卷积部分
# 52, 52, 64 -> 52, 52, 128 -> 26, 26, 128
self.block2_conv1 = Conv2D(128, (3,3), activation = 'relu', padding = 'same',name = 'block2_conv1')
self.block2_conv2 = Conv2D(128, (3,3), activation = 'relu', padding = 'same',name = 'block2_conv2')
self.block2_pool = MaxPooling2D((2,2), strides = (2,2), name = 'block2_pool')
# 第三个卷积部分
# 26, 26, 128-> 26, 26, 256 -> 13, 13, 256
self.block3_conv1 = Conv2D(256, (3,3), activation = 'relu', padding = 'same',name = 'block3_conv1')
self.block3_conv2 = Conv2D(256, (3,3), activation = 'relu', padding = 'same',name = 'block3_conv2')
self.block3_conv3 = Conv2D(256, (3,3), activation = 'relu', padding = 'same',name = 'block3_conv3')
self.block3_pool = MaxPooling2D((2,2), strides = (2,2), name = 'block3_pool')
# 第四个卷积部分
# 13, 13, 256-> 13, 13, 512 -> 6, 6, 512
self.block4_conv1 = Conv2D(512, (3,3), activation = 'relu', padding = 'same', name = 'block4_conv1')
self.block4_conv2 = Conv2D(512, (3,3), activation = 'relu', padding = 'same', name = 'block4_conv2')
self.block4_conv3 = Conv2D(512, (3,3), activation = 'relu', padding = 'same', name = 'block4_conv3')
self.block4_pool = MaxPooling2D((2,2), strides = (2,2), name = 'block4_pool')
# 第五个卷积部分
# 6, 6, 512-> 6, 6, 512 -> 3, 3, 512
self.block5_conv1 = Conv2D(512, (3,3), activation = 'relu', padding = 'same', name = 'block5_conv1')
self.block5_conv2 = Conv2D(512, (3,3), activation = 'relu', padding = 'same', name = 'block5_conv2')
self.block5_conv3 = Conv2D(512, (3,3), activation = 'relu', padding = 'same', name = 'block5_conv3')
self.block5_pool = MaxPooling2D((2,2), strides = (2,2), name = 'block5_pool')
# 3*3*512 = 4500 + 90 + 18 = 4608
self.flatten = Flatten(name = 'flatten')
def call(self, inputs):
x = inputs
x = self.block1_conv1(x)
x = self.block1_conv2(x)
x = self.block1_pool(x)
x = self.block2_conv1(x)
x = self.block2_conv2(x)
x = self.block2_pool(x)
x = self.block3_conv1(x)
x = self.block3_conv2(x)
x = self.block3_conv3(x)
x = self.block3_pool(x)
x = self.block4_conv1(x)
x = self.block4_conv2(x)
x = self.block4_conv3(x)
x = self.block4_pool(x)
x = self.block5_conv1(x)
x = self.block5_conv2(x)
x = self.block5_conv3(x)
x = self.block5_pool(x)
outputs = self.flatten(x)
return outputs