-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.py
65 lines (54 loc) · 1.95 KB
/
links.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 3 17:33:56 2017
@author: sakurai
"""
import numpy as np
import chainer
import chainer.functions as F
import chainer.links as L
class BRCChain(chainer.Chain):
'''
This is a composite link of sequence of BatchNormalization, ReLU and
Convolution2D (a.k.a. pre-activation unit).
'''
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0,
nobias=False, initialW=None, initial_bias=None, decay=0.9,
**kwargs):
in_ch, out_ch = in_channels, out_channels
super(BRCChain, self).__init__(
bn=L.BatchNormalization(in_ch, decay=decay),
conv=L.Convolution2D(in_ch, out_ch, ksize=ksize, stride=stride,
pad=pad, nobias=nobias, initialW=initialW,
initial_bias=initial_bias, **kwargs))
def __call__(self, x):
h = self.bn(x)
h = F.relu(h)
y = self.conv(h)
return y
class BRPChain(chainer.Chain):
'''
This is a composite link of sequence of BatchNormalization, ReLU and
global AveragePooling2D.
'''
def __init__(self, in_channels):
super(BRPChain, self).__init__(
bn=L.BatchNormalization(in_channels))
def __call__(self, x):
h = self.bn(x)
h = F.relu(h)
y = F.average_pooling_2d(h, h.shape[2:])
return y
class SeparableConvolution2D(chainer.Chain):
def __init__(self, in_channels, out_channels, ksize, **kwargs):
if 'pad' in kwargs:
pad = kwargs.pop('pad')
else:
pad = 0
super(SeparableConvolution2D, self).__init__(
depthwise=L.DepthwiseConvolution2D(in_channels, 1, ksize, pad=pad,
**kwargs),
pointwise=L.Convolution2D(in_channels, out_channels, 1, **kwargs)
)
def __call__(self, x):
return self.pointwise(self.depthwise(x))