-
Notifications
You must be signed in to change notification settings - Fork 0
/
wide_resnet.py
135 lines (111 loc) · 4.78 KB
/
wide_resnet.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import logging
import sys
import numpy as np
from keras.models import Model
from keras.layers import Input, Activation, add, Dense, Flatten, Dropout
from keras.layers.convolutional import Conv2D, AveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
from keras import backend as K
sys.setrecursionlimit(2 ** 10)
np.random.seed(2 ** 5)
class WideResNet:
def __init__(self, image_size, depth=16, k=8):
self._depth = depth
self._k = k
self._dropout_probability = 0
self._weight_decay = 0.0005
self._use_bias = False
self._weight_init = "he_normal"
if K.image_dim_ordering() == "th":
self._channel_axis = 1
self._input_shape = (3, image_size, image_size)
else:
self._channel_axis = -1
self._input_shape = (image_size, image_size, 3)
# Wide residual network http://arxiv.org/abs/1605.07146
def _wide_basic(self, n_input_plane, n_output_plane, stride):
def f(net):
# format of conv_params:
# [ [kernel_size=("kernel width", "kernel height"),
# strides="(stride_vertical,stride_horizontal)",
# padding="same" or "valid"] ]
# B(3,3): orignal <<basic>> block
conv_params = [[3, 3, stride, "same"],
[3, 3, (1, 1), "same"]]
n_bottleneck_plane = n_output_plane
# Residual block
for i, v in enumerate(conv_params):
if i == 0:
if n_input_plane != n_output_plane:
net = BatchNormalization(axis=self._channel_axis)(net)
net = Activation("relu")(net)
convs = net
else:
convs = BatchNormalization(axis=self._channel_axis)(net)
convs = Activation("relu")(convs)
convs = Conv2D(n_bottleneck_plane, kernel_size=(v[0], v[1]),
strides=v[2],
padding=v[3],
kernel_initializer=self._weight_init,
kernel_regularizer=l2(self._weight_decay),
use_bias=self._use_bias)(convs)
else:
convs = BatchNormalization(axis=self._channel_axis)(convs)
convs = Activation("relu")(convs)
if self._dropout_probability > 0:
convs = Dropout(self._dropout_probability)(convs)
convs = Conv2D(n_bottleneck_plane, kernel_size=(v[0], v[1]),
strides=v[2],
padding=v[3],
kernel_initializer=self._weight_init,
kernel_regularizer=l2(self._weight_decay),
use_bias=self._use_bias)(convs)
if n_input_plane != n_output_plane:
shortcut = Conv2D(n_output_plane, kernel_size=(1, 1),
strides=stride,
padding="same",
kernel_initializer=self._weight_init,
kernel_regularizer=l2(self._weight_decay),
use_bias=self._use_bias)(net)
else:
shortcut = net
return add([convs, shortcut])
return f
# "Stacking Residual Units on the same stage"
def _layer(self, block, n_input_plane, n_output_plane, count, stride):
def f(net):
net = block(n_input_plane, n_output_plane, stride)(net)
for i in range(2, int(count + 1)):
net = block(n_output_plane, n_output_plane, stride=(1, 1))(net)
return net
return f
# def create_model(self):
def __call__(self):
logging.debug("Creating model...")
assert ((self._depth - 4) % 6 == 0)
n = (self._depth - 4) / 6
inputs = Input(shape=self._input_shape)
n_stages = [16, 16 * self._k, 32 * self._k, 64 * self._k]
conv1 = Conv2D(filters=n_stages[0], kernel_size=(3, 3),
strides=(1, 1),
padding="same",
kernel_initializer=self._weight_init,
kernel_regularizer=l2(self._weight_decay),
use_bias=self._use_bias)(inputs) # "One conv at the beginning (spatial size: 32x32)"
# Add wide residual blocks
block_fn = self._wide_basic
conv2 = self._layer(block_fn, n_input_plane=n_stages[0], n_output_plane=n_stages[1], count=n, stride=(1, 1))(conv1)
conv3 = self._layer(block_fn, n_input_plane=n_stages[1], n_output_plane=n_stages[2], count=n, stride=(2, 2))(conv2)
conv4 = self._layer(block_fn, n_input_plane=n_stages[2], n_output_plane=n_stages[3], count=n, stride=(2, 2))(conv3)
batch_norm = BatchNormalization(axis=self._channel_axis)(conv4)
relu = Activation("relu")(batch_norm)
# Classifier block
pool = AveragePooling2D(pool_size=(8, 8), strides=(1, 1), padding="same")(relu)
flatten = Flatten()(pool)
predictions_g = Dense(units=2, kernel_initializer=self._weight_init, use_bias=self._use_bias,
kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)
predictions_a = Dense(units=101, kernel_initializer=self._weight_init, use_bias=self._use_bias,
kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)
model = Model(inputs=inputs, outputs=[predictions_g, predictions_a])
return model