forked from nttuan8/DeepLearningForComputerVisionWithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeeperGoogleNet.py
More file actions
89 lines (72 loc) · 4.12 KB
/
deeperGoogleNet.py
File metadata and controls
89 lines (72 loc) · 4.12 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 11 15:10:53 2019
@author: DELL
"""
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.convolutional import AveragePooling2D
from keras.layers.core import Dense
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dropout
from keras.layers import Input
from keras.layers import concatenate
from keras.regularizers import l2
from keras.models import Model
class DeeperGoogleNet:
@staticmethod
def conv_module(x, k, kx, ky, stride, axis, pad='same', reg=0.0005, name=None):
if name != None:
convName = name+'_conv'
bnName = name+'_bn'
actName = name+'_act'
x = Conv2D(k, (kx, ky), strides=stride, padding=pad, kernel_regularizer=l2(reg), name=convName)(x)
x = BatchNormalization(axis=axis, name=bnName)(x)
x = Activation('relu', name=actName)(x)
return x
@staticmethod
def inception_module(x, num1x1, num3x3Reduce, num3x3, num5x5Reduce, num5x5,
num1x1Proj, axis, stage, reg=0.005):
first = DeeperGoogleNet.conv_module(x, num1x1, 1, 1, (1,1), axis, reg=reg, name=stage+'_first')
second = DeeperGoogleNet.conv_module(x, num3x3Reduce, 1, 1, (1,1), axis, reg=reg, name=stage+'_second1')
second = DeeperGoogleNet.conv_module(second, num3x3, 3, 3, (1,1), axis, reg=reg, name=stage+'_second2')
third = DeeperGoogleNet.conv_module(x, num5x5Reduce, 1, 1, (1,1), axis, reg=reg, name=stage+'_third1')
third = DeeperGoogleNet.conv_module(third, num5x5, 5, 5, (1,1), axis, reg=reg, name=stage+'_third2')
fourth = MaxPooling2D((3,3), strides=(1,1), padding='same', name=stage+'_pool')(x)
fourth = DeeperGoogleNet.conv_module(fourth, num1x1Proj, 1, 1, (1,1), axis, reg=reg, name=stage+'_fourth')
x = concatenate([first, second, third, fourth], axis=axis, name=stage+'_fixed')
return x
@staticmethod
def dowsample_module(x, k, axis):
conv_3x3 = DeeperGoogleNet.conv_module(x, k, 3, 3, (2, 2), axis, pad='valid')
pool = MaxPooling2D(pool_size=(3,3), strides=(2,2))(x)
x = concatenate([conv_3x3, pool], axis=axis)
return x
@staticmethod
def build(width, height, depth, classes, reg=0.005):
input_shape = (width, height, depth)
axis = -1
inputs = Input(shape=input_shape)
x = DeeperGoogleNet.conv_module(inputs, 64, 5, 5, (1,1), axis, reg=reg, name='block1')
x = MaxPooling2D((3,3), strides=(2,2), padding='same', name='pool1')(x)
x = DeeperGoogleNet.conv_module(x, 64, 1, 1, (1, 1), axis, reg=reg, name="block2")
x = DeeperGoogleNet.conv_module(x, 192, 3, 3, (1, 1), axis, reg=reg, name="block3")
x = MaxPooling2D((3,3), strides=(2,2), padding='same', name='pool2')(x)
x = DeeperGoogleNet.inception_module(x, 64, 96, 128, 16, 32, 32, axis, "3a", reg=reg)
x = DeeperGoogleNet.inception_module(x, 128, 128, 192, 32, 96, 64, axis, "3b", reg=reg)
x = MaxPooling2D((3, 3), strides=(2, 2), padding="same", name="pool3")(x)
x = DeeperGoogleNet.inception_module(x, 192, 96, 208, 16, 48, 64, axis, "4a", reg=reg)
x = DeeperGoogleNet.inception_module(x, 160, 112, 224, 24, 64, 64, axis, "4b", reg=reg)
x = DeeperGoogleNet.inception_module(x, 128, 128, 256, 24, 64, 64, axis, "4c", reg=reg)
x = DeeperGoogleNet.inception_module(x, 112, 144, 288, 32, 64, 64, axis, "4d", reg=reg)
x = DeeperGoogleNet.inception_module(x, 256, 160, 320, 32, 128, 128, axis, "4e", reg=reg)
x = MaxPooling2D((3, 3), strides=(2, 2), padding="same", name="pool4")(x)
x = AveragePooling2D((4, 4), name="pool5")(x)
x = Dropout(0.4, name='dropout')(x)
x = Flatten(name='flatten')(x)
x = Dense(classes, name='label')(x)
x = Activation('softmax', name='softmax')(x)
model = Model(inputs, x, name='googlenet')
return model