Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_, c, _, _ = h.shape
assert maps // 2 == c or maps == c
maps1 = c if maps // 2 == c else maps
maps2 = maps
with nn.parameter_scope(scopename):
# LeakyRelu -> Conv
with nn.parameter_scope("conv1"):
#h = F.leaky_relu(h, 0.2, False)
h = F.relu(h, False)
h = convolution(h, maps1, kernel=kernel, pad=pad, stride=stride,
with_bias=True, sn=sn, test=test, init_scale=np.sqrt(2))
# LeakyRelu -> Conv -> Downsample
with nn.parameter_scope("conv2"):
#h = F.leaky_relu(h, 0.2, True)
h = F.relu(h, True)
h = convolution(h, maps2, kernel=kernel, pad=pad, stride=stride,
with_bias=True, sn=sn, test=test, init_scale=np.sqrt(2))
if downsample:
h = F.average_pooling(h, kernel=(2, 2))
# Shortcut: Conv -> Downsample
if c != maps2 or downsample:
with nn.parameter_scope("shortcut"):
s = convolution(s, maps2, kernel=(1, 1), pad=(0, 0), stride=(1, 1),
with_bias=True, sn=sn, test=test)
if downsample:
s = F.average_pooling(s, kernel=(2, 2))
return F.add2(h, s, True)
# return F.add2(h, s)
def res_unit(x, scope_name, rng, dn=False, test=False):
C = x.shape[1]
with nn.parameter_scope(scope_name):
# Conv -> BN -> Relu
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.relu(h)
# Conv -> BN -> Relu
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.relu(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)
def construct_networks(args, images, model, num_class, test):
try:
pooled = model(images, force_global_pooling=1,
use_up_to="pool", training=not test)
except:
pooled = model(images, use_up_to="pool", training=not test)
with nn.parameter_scope("finetuning"):
if args.model == "VGG":
pooled = F.relu(pooled)
with nn.parameter_scope("additional_fc_1"):
pooled = PF.affine(pooled, 4096)
pooled = F.relu(pooled)
if not test:
pooled = F.dropout(pooled, 0.5)
with nn.parameter_scope("additional_fc_2"):
pooled = PF.affine(pooled, 4096)
pooled = F.relu(pooled)
if not test:
pooled = F.dropout(pooled, 0.5)
with nn.parameter_scope("last_fc"):
pred = PF.affine(pooled, num_class)
return pred
def mlp(x, maps, num_res=4, num_layers=2, name="mlp"):
h = x
with nn.parameter_scope(name):
h = PF.affine(h, maps, name="affine-first")
h = F.relu(h, True)
h = PF.affine(h, maps, name="affine-mid")
h = F.relu(h, True)
h = PF.affine(h, 2 * maps * num_res * num_layers, name="affine-last")
return h
def mnist_lenet_prediction(image, test=False):
"""
Construct LeNet for MNIST.
"""
image /= 255.0
c1 = PF.convolution(image, 16, (5, 5), name='conv1')
c1 = F.relu(F.max_pooling(c1, (2, 2)), inplace=True)
c2 = PF.convolution(c1, 16, (5, 5), name='conv2')
c2 = F.relu(F.max_pooling(c2, (2, 2)), inplace=True)
c3 = F.relu(PF.affine(c2, 50, name='fc3'), inplace=True)
c4 = PF.affine(c3, 10, name='fc4')
return c4
def instance_norm_relu(self, x):
# return F.relu(PF.layer_normalization(x, **self.norm_opts), inplace=True)
return F.relu(PF.instance_normalization(x, **self.norm_opts), inplace=True)
with nn.parameter_scope("depthwise"):
if(stride == True):
h = PF.depthwise_convolution(x, (3, 3), stride=(2, 2), pad=(
1, 1), with_bias=False, fix_parameters=fix_params)
elif(aspp == True):
h = PF.depthwise_convolution(x, (3, 3), pad=(atrous_rate, atrous_rate), stride=(
1, 1), dilation=(atrous_rate, atrous_rate), with_bias=False, fix_parameters=fix_params)
else:
h = PF.depthwise_convolution(x, (3, 3), pad=(
1, 1), with_bias=False, fix_parameters=fix_params)
h = PF.batch_normalization(
h, batch_stat=not test, eps=eps, fix_parameters=fix_params)
if last_block == True:
h = F.relu(h)
with nn.parameter_scope("pointwise"):
h = PF.convolution(h, f, (1, 1), stride=(
1, 1), with_bias=False, fix_parameters=fix_params)
h = PF.batch_normalization(
h, batch_stat=not test, eps=eps, fix_parameters=fix_params)
if end_point == True:
global endpoints
endpoints['Decoder End Point 1'] = h
if act_fn == True:
h = F.relu(h)
return h
h = F.relu(PF.convolution(h, 256, (3, 3), pad=(1, 1), stride=(1, 1), name='conv5'))
h = F.relu(PF.convolution(h, 256, (3, 3), pad=(1, 1), stride=(1, 1), name='conv6'))
h = F.relu(PF.convolution(h, 256, (3, 3), pad=(1, 1), stride=(1, 1), name='conv7'))
h = F.max_pooling(h, (2, 2))
h = F.relu(PF.convolution(h, 512, (3, 3), pad=(1, 1), stride=(1, 1), name='conv8'))
h = F.relu(PF.convolution(h, 512, (3, 3), pad=(1, 1), stride=(1, 1), name='conv9'))
h = F.relu(PF.convolution(h, 512, (3, 3), pad=(1, 1), stride=(1, 1), name='conv10'))
h = F.max_pooling(h, (2, 2))
h = F.relu(PF.convolution(h, 512, (3, 3), pad=(1, 1), stride=(1, 1), name='conv11'))
h = F.relu(PF.convolution(h, 512, (3, 3), pad=(1, 1), stride=(1, 1), name='conv12'))
h = F.relu(PF.convolution(h, 512, (3, 3), pad=(1, 1), stride=(1, 1), name='conv13'))
h = F.max_pooling(h, (2, 2))
h = PF.affine(h, 4096, name='fc1')
h = F.relu(h)
h = PF.affine(h, 4096, name='fc2')
h = F.relu(h)
h = PF.affine(h, 1000, name='fc3')
return F.softmax(h)
def mnist_lenet_prediction(image, test=False):
"""
Construct LeNet for MNIST.
"""
image /= 255.0
c1 = PF.convolution(image, 16, (5, 5), name='conv1')
c1 = F.relu(F.max_pooling(c1, (2, 2)), inplace=True)
c2 = PF.convolution(c1, 16, (5, 5), name='conv2')
c2 = F.relu(F.max_pooling(c2, (2, 2)), inplace=True)
c3 = F.relu(PF.affine(c2, 50, name='fc3'), inplace=True)
c4 = PF.affine(c3, 10, name='fc4')
return c4