-
Notifications
You must be signed in to change notification settings - Fork 4
/
alexnet.py
76 lines (58 loc) · 2.88 KB
/
alexnet.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
import os
os.environ['THEANO_FLAGS'] = "device=gpu"
# import sys
# sys.path.insert(0, '../convnets-keras')
from keras import backend as K
from theano import tensor as T
from keras.models import Model
from keras.layers import Flatten, Dense, Dropout, Reshape, Permute, Activation, \
Input, merge, Lambda
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from customlayers import convolution2Dgroup, crosschannelnormalization, \
splittensor, Softmax4D
def mean_subtract(img):
img = T.set_subtensor(img[:,0,:,:],img[:,0,:,:] - 123.68)
img = T.set_subtensor(img[:,1,:,:],img[:,1,:,:] - 116.779)
img = T.set_subtensor(img[:,2,:,:],img[:,2,:,:] - 103.939)
return img / 255.0
def get_alexnet(input_shape,nb_classes,mean_flag):
# code adapted from https://github.com/heuritech/convnets-keras
inputs = Input(shape=input_shape)
if mean_flag:
mean_subtraction = Lambda(mean_subtract, name='mean_subtraction')(inputs)
conv_1 = Convolution2D(96, 11, 11,subsample=(4,4),activation='relu',
name='conv_1', init='he_normal')(mean_subtraction)
else:
conv_1 = Convolution2D(96, 11, 11,subsample=(4,4),activation='relu',
name='conv_1', init='he_normal')(inputs)
conv_2 = crosschannelnormalization(name="convpool_1")(conv_1)
conv_2 = MaxPooling2D((3, 3), strides=(2,2))(conv_2)
conv_2 = ZeroPadding2D((2,2))(conv_2)
conv_2 = merge([
Convolution2D(128,5,5,activation="relu",init='he_normal', name='conv_2_'+str(i+1))(
splittensor(ratio_split=2,id_split=i)(conv_2)
) for i in range(2)], mode='concat',concat_axis=1,name="conv_2")
conv_3 = crosschannelnormalization()(conv_2)
conv_3 = MaxPooling2D((3, 3), strides=(2, 2))(conv_3)
conv_3 = ZeroPadding2D((1,1))(conv_3)
conv_3 = Convolution2D(384,3,3,activation='relu',name='conv_3',init='he_normal')(conv_3)
conv_4 = ZeroPadding2D((1,1))(conv_3)
conv_4 = merge([
Convolution2D(192,3,3,activation="relu", init='he_normal', name='conv_4_'+str(i+1))(
splittensor(ratio_split=2,id_split=i)(conv_4)
) for i in range(2)], mode='concat',concat_axis=1,name="conv_4")
conv_5 = ZeroPadding2D((1,1))(conv_4)
conv_5 = merge([
Convolution2D(128,3,3,activation="relu",init='he_normal', name='conv_5_'+str(i+1))(
splittensor(ratio_split=2,id_split=i)(conv_5)
) for i in range(2)], mode='concat',concat_axis=1,name="conv_5")
dense_1 = MaxPooling2D((3, 3), strides=(2,2),name="convpool_5")(conv_5)
dense_1 = Flatten(name="flatten")(dense_1)
dense_1 = Dense(4096, activation='relu',name='dense_1',init='he_normal')(dense_1)
dense_2 = Dropout(0.5)(dense_1)
dense_2 = Dense(4096, activation='relu',name='dense_2',init='he_normal')(dense_2)
dense_3 = Dropout(0.5)(dense_2)
dense_3 = Dense(nb_classes,name='dense_3_new',init='he_normal')(dense_3)
prediction = Activation("softmax",name="softmax")(dense_3)
alexnet = Model(input=inputs, output=prediction)
return alexnet