Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Add dilate and bn_kwargs to Bottleneck and ResBlock #549

Merged
merged 4 commits into from
Apr 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions chainercv/links/connection/conv_2d_activ.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ class Conv2DActiv(chainer.Chain):
If :obj:`None`, parameter initialization will be deferred until the
first forward data pass at which time the size will be determined.
out_channels (int): Number of channels of output arrays.
ksize (int or pair of ints): Size of filters (a.k.a. kernels).
ksize (int or tuple of ints): Size of filters (a.k.a. kernels).
:obj:`ksize=k` and :obj:`ksize=(k, k)` are equivalent.
stride (int or pair of ints): Stride of filter applications.
stride (int or tuple of ints): Stride of filter applications.
:obj:`stride=s` and :obj:`stride=(s, s)` are equivalent.
pad (int or pair of ints): Spatial padding width for input arrays.
pad (int or tuple of ints): Spatial padding width for input arrays.
:obj:`pad=p` and :obj:`pad=(p, p)` are equivalent.
dilate (int or pair of ints): Dilation factor of filter applications.
dilate (int or tuple of ints): Dilation factor of filter applications.
:obj:`dilate=d` and :obj:`dilate=(d, d)` are equivalent.
nobias (bool): If :obj:`True`,
then this link does not use the bias term.
initialW (4-D array): Initial weight value. If :obj:`None`, the default
initialW (callable): Initial weight value. If :obj:`None`, the default
initializer is used.
May also be a callable that takes :obj:`numpy.ndarray` or
:obj:`cupy.ndarray` and edits its value.
initial_bias (1-D array): Initial bias value. If :obj:`None`, the bias
initial_bias (callable): Initial bias value. If :obj:`None`, the bias
is set to 0.
May also be a callable that takes :obj:`numpy.ndarray` or
:obj:`cupy.ndarray` and edits its value.
Expand Down
12 changes: 6 additions & 6 deletions chainercv/links/connection/conv_2d_bn_activ.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ class Conv2DBNActiv(chainer.Chain):
If :obj:`None`, parameter initialization will be deferred until the
first forward data pass at which time the size will be determined.
out_channels (int): Number of channels of output arrays.
ksize (int or pair of ints): Size of filters (a.k.a. kernels).
ksize (int or tuple of ints): Size of filters (a.k.a. kernels).
:obj:`ksize=k` and :obj:`ksize=(k, k)` are equivalent.
stride (int or pair of ints): Stride of filter applications.
stride (int or tuple of ints): Stride of filter applications.
:obj:`stride=s` and :obj:`stride=(s, s)` are equivalent.
pad (int or pair of ints): Spatial padding width for input arrays.
pad (int or tuple of ints): Spatial padding width for input arrays.
:obj:`pad=p` and :obj:`pad=(p, p)` are equivalent.
dilate (int or pair of ints): Dilation factor of filter applications.
dilate (int or tuple of ints): Dilation factor of filter applications.
:obj:`dilate=d` and :obj:`dilate=(d, d)` are equivalent.
nobias (bool): If :obj:`True`,
then this link does not use the bias term.
initialW (4-D array): Initial weight value. If :obj:`None`, the default
initialW (callable): Initial weight value. If :obj:`None`, the default
initializer is used.
May also be a callable that takes :obj:`numpy.ndarray` or
:obj:`cupy.ndarray` and edits its value.
initial_bias (1-D array): Initial bias value. If :obj:`None`, the bias
initial_bias (callable): Initial bias value. If :obj:`None`, the bias
is set to 0.
May also be a callable that takes :obj:`numpy.ndarray` or
:obj:`cupy.ndarray` and edits its value.
Expand Down
50 changes: 33 additions & 17 deletions chainercv/links/model/resnet/resblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ class ResBlock(PickableSequentialChain):
mid_channels (int): The number of channels of intermediate arrays.
out_channels (int): The number of channels of the output array.
stride (int or tuple of ints): Stride of filter application.
initialW (4-D array): Initial weight value used in
dilate (int or tuple of ints): Dilation factor of filter applications.
:obj:`dilate=d` and :obj:`dilate=(d, d)` are equivalent.
initialW (callable): Initial weight value used in
the convolutional layers.
bn_kwargs (dict): Keyword arguments passed to initialize
:class:`chainer.links.BatchNormalization`.
stride_first (bool): This determines the behavior of the
bottleneck with a shortcut. If :obj:`True`, apply strided
convolution with the first convolution layer.
Expand All @@ -28,17 +32,21 @@ class ResBlock(PickableSequentialChain):
"""

def __init__(self, n_layer, in_channels, mid_channels,
out_channels, stride, initialW=None, stride_first=False):
out_channels, stride, dilate=1, initialW=None,
bn_kwargs={}, stride_first=False):
super(ResBlock, self).__init__()
# Dilate option is applied to all bottlenecks.
with self.init_scope():
self.a = Bottleneck(
in_channels, mid_channels, out_channels, stride,
initialW, residual_conv=True, stride_first=stride_first)
in_channels, mid_channels, out_channels, stride, dilate,
initialW, bn_kwargs=bn_kwargs, residual_conv=True,
stride_first=stride_first)
for i in range(n_layer - 1):
name = 'b{}'.format(i + 1)
bottleneck = Bottleneck(
out_channels, mid_channels, out_channels, stride=1,
initialW=initialW, residual_conv=False)
dilate=dilate, initialW=initialW, bn_kwargs=bn_kwargs,
residual_conv=False)
self.add_link(name, bottleneck)


Expand All @@ -51,8 +59,12 @@ class Bottleneck(chainer.Chain):
mid_channels (int): The number of channels of intermediate arrays.
out_channels (int): The number of channels of the output array.
stride (int or tuple of ints): Stride of filter application.
initialW (4-D array): Initial weight value used in
dilate (int or tuple of ints): Dilation factor of filter applications.
:obj:`dilate=d` and :obj:`dilate=(d, d)` are equivalent.
initialW (callable): Initial weight value used in
the convolutional layers.
bn_kwargs (dict): Keyword arguments passed to initialize
:class:`chainer.links.BatchNormalization`.
residual_conv (bool): If :obj:`True`, apply a 1x1 convolution
to the residual.
stride_first (bool): If :obj:`True`, apply strided convolution
Expand All @@ -62,8 +74,8 @@ class Bottleneck(chainer.Chain):
"""

def __init__(self, in_channels, mid_channels, out_channels,
stride=1, initialW=None, residual_conv=False,
stride_first=False):
stride=1, dilate=1, initialW=None, bn_kwargs={},
residual_conv=False, stride_first=False):
if stride_first:
first_stride = stride
second_stride = 1
Expand All @@ -72,19 +84,23 @@ def __init__(self, in_channels, mid_channels, out_channels,
second_stride = stride
super(Bottleneck, self).__init__()
with self.init_scope():
self.conv1 = Conv2DBNActiv(in_channels, mid_channels, 1,
first_stride, 0, initialW=initialW,
nobias=True)
self.conv2 = Conv2DBNActiv(mid_channels, mid_channels, 3,
second_stride, 1, initialW=initialW,
nobias=True)
self.conv1 = Conv2DBNActiv(in_channels, mid_channels,
1, first_stride, 0,
nobias=True, initialW=initialW,
bn_kwargs=bn_kwargs)
# pad = dilate
self.conv2 = Conv2DBNActiv(mid_channels, mid_channels,
3, second_stride, dilate, dilate,
nobias=True, initialW=initialW,
bn_kwargs=bn_kwargs)
self.conv3 = Conv2DBNActiv(mid_channels, out_channels, 1, 1, 0,
initialW=initialW, nobias=True,
activ=None)
nobias=True, initialW=initialW,
activ=None, bn_kwargs=bn_kwargs)
if residual_conv:
self.residual_conv = Conv2DBNActiv(
in_channels, out_channels, 1, stride, 0,
nobias=True, initialW=initialW, activ=None)
nobias=True, initialW=initialW,
activ=None, bn_kwargs=bn_kwargs)

def __call__(self, x):
h = self.conv1(x)
Expand Down