Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def res_unit(x, scope_name, dn=False):
C = x.shape[1]
with nn.parameter_scope(scope_name):
# Conv -> BN -> Relu
with nn.parameter_scope("conv1"):
h = PF.convolution(x, C // 2, kernel=(1, 1), pad=(0, 0),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h)
# Conv -> BN -> Relu
with nn.parameter_scope("conv2"):
h = svd_convolution(h, C // 2, kernel=(3, 3), pad=(1, 1),
with_bias=False, cr=compression_ratio)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h)
# Conv -> BN
with nn.parameter_scope("conv3"):
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
# Residual -> Relu
h = F.relu(h + x)
# Maxpooling
with nn.parameter_scope("conv1"):
h = PF.convolution(x, C // 2, kernel=(1, 1), pad=(0, 0),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h)
# Conv -> BN -> Relu
with nn.parameter_scope("conv2"):
h = PF.convolution(h, C // 2, kernel=(3, 3), pad=(1, 1),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h)
# Conv -> BN
with nn.parameter_scope("conv3"):
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
# Residual -> Relu
h = F.relu(h + x)
# Maxpooling
if dn:
h = F.max_pooling(h, kernel=(2, 2), stride=(2, 2))
return h
def res_unit(x, scope_name, rng, dn=False, test=False):
C = x.shape[1]
with nn.parameter_scope(scope_name):
# Conv -> BN -> Elu
with nn.parameter_scope("conv1"):
w_init = UniformInitializer(
calc_uniform_lim_glorot(C, C / 2, kernel=(1, 1)),
rng=rng)
h = PF.convolution(x, C / 2, kernel=(1, 1), pad=(0, 0),
w_init=w_init, with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.elu(h)
# Conv -> BN -> Elu
with nn.parameter_scope("conv2"):
w_init = UniformInitializer(
calc_uniform_lim_glorot(C / 2, C / 2, kernel=(3, 3)),
rng=rng)
h = PF.convolution(h, C / 2, kernel=(3, 3), pad=(1, 1),
w_init=w_init, with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.elu(h)
# Conv -> BN
with nn.parameter_scope("conv3"):
w_init = UniformInitializer(
calc_uniform_lim_glorot(C / 2, C, kernel=(1, 1)),
rng=rng)
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
def res_unit(x, scope_name, dn=False):
C = x.shape[1]
with nn.parameter_scope(scope_name):
# Conv -> BN -> Relu
with nn.parameter_scope("conv1"):
h = PF.convolution(x, C / 2, kernel=(1, 1), pad=(0, 0),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h)
# Conv -> BN -> Relu
with nn.parameter_scope("conv2"):
h = PF.convolution(h, C / 2, kernel=(3, 3), pad=(1, 1),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h)
# Conv -> BN
with nn.parameter_scope("conv3"):
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
# Residual -> Relu
h = F.relu(h + x)
# Maxpooling
if dn:
h = F.max_pooling(h, kernel=(2, 2), stride=(2, 2))
return h
def atrous_spatial_pyramid_pooling(x, output_stride, test=False, fix_params=False):
if output_stride not in [8, 16]:
raise ValueError('output_stride neither 8 nor 16.')
depth = 256
atrous_rates = [6, 12, 18]
if output_stride == 8:
atrous_rates = [2*rate for rate in atrous_rates]
with nn.parameter_scope("aspp0"):
atrous_conv0 = PF.convolution(
x, depth, (1, 1), with_bias=False, fix_parameters=fix_params)
atrous_conv0 = F.relu(PF.batch_normalization(
atrous_conv0, batch_stat=not test, fix_parameters=fix_params))
atrous_conv = []
for i in range(3):
with nn.parameter_scope("aspp"+str(i+1)):
atrous_conv.append(xception.separable_conv_with_bn(x, depth, stride=False, aspp=True,
atrous_rate=atrous_rates[i], last_block=True, eps=1e-05, test=test, fix_params=fix_params))
# Image-level features
with nn.parameter_scope("image_pooling"):
x_shape = x.shape[2]
h = F.average_pooling(
x, (x.shape[2], x.shape[3]), stride=(x.shape[2], x.shape[2]))
h = PF.convolution(h, depth, (1, 1), with_bias=False,
fix_parameters=fix_params)
def csc(x, scope_name, dn=False):
C = x.shape[1]
h = x
with nn.parameter_scope(scope_name):
with nn.parameter_scope("conv1"):
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h, True)
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
with_bias=False)
with nn.parameter_scope("shift"): # no meaning but semantics
h = shift(h)
with nn.parameter_scope("conv2"):
h = PF.batch_normalization(h, batch_stat=not test)
h = F.relu(h, True)
stride = (2, 2) if dn else (1, 1)
if p > 0:
h = F.dropout(h, p=0.5) if not test else h
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
stride=stride,
with_bias=False)
s = F.average_pooling(x, (2, 2)) if dn else x
return h + s
def mnist_binary_connect_lenet_prediction(image, test=False):
"""
Construct LeNet for MNIST (BinaryNet version).
"""
with nn.parameter_scope("conv1"):
c1 = PF.binary_connect_convolution(image, 16, (5, 5))
c1 = PF.batch_normalization(c1, batch_stat=not test)
c1 = F.elu(F.average_pooling(c1, (2, 2)))
with nn.parameter_scope("conv2"):
c2 = PF.binary_connect_convolution(c1, 16, (5, 5))
c2 = PF.batch_normalization(c2, batch_stat=not test)
c2 = F.elu(F.average_pooling(c2, (2, 2)))
with nn.parameter_scope("fc3"):
c3 = PF.binary_connect_affine(c2, 50)
c3 = PF.batch_normalization(c3, batch_stat=not test)
c3 = F.elu(c3)
with nn.parameter_scope("fc4"):
c4 = PF.binary_connect_affine(c3, 10)
c4 = PF.batch_normalization(c4, batch_stat=not test)
return c4
Args:
x(`~nnabla.Variable`): N-D array
n_h(int): number of units in an intermediate layer
n_y(int): number of classes
test: operation type train=True, test=False
Returns:
~nnabla.Variable: h
"""
h = x
with nn.parameter_scope("fc1"):
h = F.relu(PF.batch_normalization(
PF.affine(h, n_h), batch_stat=not test), inplace=True)
with nn.parameter_scope("fc2"):
h = F.relu(PF.batch_normalization(
PF.affine(h, n_h), batch_stat=not test), inplace=True)
with nn.parameter_scope("fc3"):
h = PF.affine(h, n_y)
return h
with nn.parameter_scope("conv2"):
w_init = UniformInitializer(
calc_uniform_lim_glorot(C / 2, C / 2, kernel=(3, 3)),
rng=rng)
h = PF.convolution(h, C / 2, kernel=(3, 3), pad=(1, 1),
w_init=w_init, with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
h = F.elu(h)
# Conv -> BN
with nn.parameter_scope("conv3"):
w_init = UniformInitializer(
calc_uniform_lim_glorot(C / 2, C, kernel=(1, 1)),
rng=rng)
h = PF.convolution(h, C, kernel=(1, 1), pad=(0, 0),
w_init=w_init, with_bias=False)
h = PF.batch_normalization(h, batch_stat=not test)
# Residual -> Elu
h = F.elu(h + x)
# Maxpooling
if dn:
h = F.max_pooling(h, kernel=(2, 2), stride=(2, 2))
return h
tiny (bool): Tiny imagenet mode. Input image must be (3, 56, 56).
"""
layers = {
18: ((2, 2, 2, 2), basicblock, 1),
34: ((3, 4, 6, 3), basicblock, 1),
50: ((3, 4, 6, 3), bottleneck, 4),
101: ((3, 4, 23, 3), bottleneck, 4),
152: ((3, 8, 36, 3), bottleneck, 4)}
counts, block, ocoef = layers[num_layers]
logger.debug(x.shape)
with nn.parameter_scope("conv1"):
stride = (1, 1) if tiny else (2, 2)
r = PF.convolution(x, 64, (7, 7),
pad=(3, 3), stride=stride, with_bias=False)
r = F.relu(PF.batch_normalization(
r, batch_stat=not test), inplace=True)
r = F.max_pooling(r, (3, 3), stride, pad=(1, 1))
hidden = {}
hidden['r0'] = r
ochannels = [64, 128, 256, 512]
strides = [1, 2, 2, 2]
logger.debug(r.shape)
for i in range(4):
with nn.parameter_scope("res{}".format(i + 1)):
r = layer(r, block, ochannels[i] * ocoef,
counts[i], (strides[i], strides[i]), shortcut_type, test)
hidden['r{}'.format(i + 1)] = r
logger.debug(r.shape)
r = F.average_pooling(r, r.shape[-2:])
with nn.parameter_scope("fc"):
r = PF.affine(r, num_classes)