-
Notifications
You must be signed in to change notification settings - Fork 0
/
alex.py
72 lines (57 loc) · 2.7 KB
/
alex.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
import numpy as np
import chainer
import chainer.functions as F
from chainer import initializers
import chainer.links as L
class Alex(chainer.Chain):
"""Single-GPU AlexNet without partition toward the channel axis."""
insize = 227
def __init__(self):
super(Alex, self).__init__()
with self.init_scope():
self.conv1 = L.Convolution2D(None, 96, 11, stride=4)
self.conv2 = L.Convolution2D(None, 256, 5, pad=2)
self.conv3 = L.Convolution2D(None, 384, 3, pad=1)
self.conv4 = L.Convolution2D(None, 384, 3, pad=1)
self.conv5 = L.Convolution2D(None, 256, 3, pad=1)
self.fc6 = L.Linear(None, 4096)
self.fc7 = L.Linear(None, 4096)
self.fc8 = L.Linear(None, 1000)
def __call__(self, x, t):
h = F.max_pooling_2d(F.local_response_normalization(
F.relu(self.conv1(x))), 3, stride=2)
h = F.max_pooling_2d(F.local_response_normalization(
F.relu(self.conv2(h))), 3, stride=2)
h = F.relu(self.conv3(h))
h = F.relu(self.conv4(h))
h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2)
h = F.dropout(F.relu(self.fc6(h)))
h = F.dropout(F.relu(self.fc7(h)))
h = self.fc8(h)
loss = F.softmax_cross_entropy(h, t)
chainer.report({'loss': loss, 'accuracy': F.accuracy(h, t)}, self)
return loss
class AlexFp16(Alex):
"""Single-GPU AlexNet without partition toward the channel axis."""
insize = 227
def __init__(self):
chainer.Chain.__init__(self)
self.dtype = np.float16
W = initializers.HeNormal(1 / np.sqrt(2), self.dtype)
bias = initializers.Zero(self.dtype)
with self.init_scope():
self.conv1 = L.Convolution2D(None, 96, 11, stride=4,
initialW=W, initial_bias=bias)
self.conv2 = L.Convolution2D(None, 256, 5, pad=2,
initialW=W, initial_bias=bias)
self.conv3 = L.Convolution2D(None, 384, 3, pad=1,
initialW=W, initial_bias=bias)
self.conv4 = L.Convolution2D(None, 384, 3, pad=1,
initialW=W, initial_bias=bias)
self.conv5 = L.Convolution2D(None, 256, 3, pad=1,
initialW=W, initial_bias=bias)
self.fc6 = L.Linear(None, 4096, initialW=W, initial_bias=bias)
self.fc7 = L.Linear(None, 4096, initialW=W, initial_bias=bias)
self.fc8 = L.Linear(None, 1000, initialW=W, initial_bias=bias)
def __call__(self, x, t):
return Alex.__call__(self, F.cast(x, self.dtype), t)