Python mxnet.gluon.HybridBlock() Examples
The following are 30
code examples of mxnet.gluon.HybridBlock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mxnet.gluon
, or try the search function
.
Example #1
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_deconv(): class Net(gluon.HybridBlock): def __init__(self, shape, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.conv0 = nn.Conv2DTranspose(64, (3, 3)) def hybrid_forward(self, F, x): x_reshape = x.reshape(self.reshape) out = self.conv0(x_reshape) return out x = mx.nd.random.uniform(shape=(4, 16, 32, 32)) shape = (4, 16, 64, -1) net = Net(shape) check_layer_forward_withinput(net, x)
Example #2
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_deconv_reshape_deconv(): class Net(gluon.HybridBlock): def __init__(self, shape, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.slice = slice self.conv0 = nn.Conv2DTranspose(32, (3, 3)) self.conv1 = nn.Conv2DTranspose(96, (3, 3), strides=(2, 2)) def hybrid_forward(self, F, x): x_slice = x.slice(begin=self.slice[0], end=self.slice[1]) y = self.conv0(x_slice) "shape of y is (4, 32, 34, 34)" y_reshape = y.reshape(self.reshape) out = self.conv1(y_reshape) return out x = mx.nd.random.uniform(shape=(8, 32, 64, 64)) shape = (4, 64, 34, -1) slice = [(4, 0, 0, 0), (8, 16, 32, 32)] net = Net(shape, slice) check_layer_forward_withinput(net, x)
Example #3
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_conv_reshape_conv(): class Net(gluon.HybridBlock): def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(64, (3, 3)) self.conv1 = nn.Conv2D(128, (3, 3)) def hybrid_forward(self, F, x): x_reshape = x.reshape((0, 0, 128, 32)) y = self.conv0(x_reshape) "spatial shape of y is (62, 62)" y_reshape = y.reshape((0, 0, 124, 31)) out = self.conv1(y_reshape) return out x = mx.nd.random.uniform(shape=(4, 3, 64, 64)) net = Net() check_layer_forward_withinput(net, x)
Example #4
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_activation(): class Net(gluon.HybridBlock): def __init__(self, act, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.slice = slice self.act = nn.Activation(act) def hybrid_forward(self, F, x): x_slice = x.slice(begin=self.slice[0], end=self.slice[1]) out = self.act(x_slice) return out acts = ["relu", "sigmoid", "tanh", "softrelu"] for act in acts: x = mx.nd.random.uniform(-1, 1, shape=(8, 32, 64, 64)) slice = [(0, 16, 32, 32), (4, 32, 64, 64)] net = Net(act, slice) check_layer_forward_withinput(net, x)
Example #5
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_activation(): class Net(gluon.HybridBlock): def __init__(self, act, shape, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.act = nn.Activation(act) def hybrid_forward(self, F, x): x_reshape = x.reshape(self.reshape) out = self.act(x_reshape) return out acts = ["relu", "sigmoid", "tanh", "softrelu"] for act in acts: x = mx.nd.random.uniform(-1, 1, shape=(4, 16, 32, 32)) shape = (4, 32, 32, -1) net = Net(act, shape) check_layer_forward_withinput(net, x)
Example #6
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_activation_reshape_activation(): class Net(gluon.HybridBlock): def __init__(self, act0, act1, shape, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.act0 = nn.Activation(act0) self.act1 = nn.Activation(act1) def hybrid_forward(self, F, x): x_reshape = x.reshape(self.reshape[0]) y = self.act0(x_reshape) y_reshape = y.reshape(self.reshape[1]) out = self.act1(y_reshape) return out acts = ["relu", "sigmoid", "tanh", "softrelu"] for idx0, act0 in enumerate(acts): for idx1, act1 in enumerate(acts): if idx1 == idx0: continue x = mx.nd.random.uniform(-1, 1, shape=(4, 16, 32, 32)) shape = [(4, 32, 32, -1), (4, 32, 16, -1)] net = Net(act0, act1, shape) check_layer_forward_withinput(net, x)
Example #7
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_conv2d_16c(): chn_list = [16, 256] kernel_list = [1, 3] kernel_list.append(224) batch_size = 4 class Net(gluon.HybridBlock): def __init__(self, chn_num, kernel, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = gluon.nn.Conv2D(chn_num, (kernel, kernel)) def hybrid_forward(self, F, x): out = self.conv0(x) return out x = mx.nd.random.uniform(-1.0, 1.0, shape=(batch_size, 3, 224, 224)) for i in range(len(chn_list)): for j in range(len(kernel_list)): net = Net(chn_list[i], kernel_list[j]) check_layer_forward_withinput(net, x)
Example #8
Source File: darknet.py From gluon-cv with Apache License 2.0 | 6 votes |
def darknet53(**kwargs): """Darknet v3 53 layer network. Reference: https://arxiv.org/pdf/1804.02767.pdf. Parameters ---------- norm_layer : object Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`) Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`. norm_kwargs : dict Additional `norm_layer` arguments, for example `num_devices=4` for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`. Returns ------- mxnet.gluon.HybridBlock Darknet network. """ return get_darknet('v3', 53, **kwargs)
Example #9
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_constant(): class Test(gluon.HybridBlock): def __init__(self, **kwargs): super(Test, self).__init__(**kwargs) self.value = np.asarray([[1,2], [3,4]]) self.const = self.params.get_constant('const', self.value) def hybrid_forward(self, F, x, const): return x + const test = Test() test.initialize() trainer = gluon.Trainer(test.collect_params(), 'sgd', {'learning_rate': 1.0, 'momentum': 0.5}) with mx.autograd.record(): x = mx.nd.ones((2,2)) x.attach_grad() y = test(x) y.backward() trainer.step(1) assert (test.const.data().asnumpy() == test.value).all() assert (x.grad.asnumpy() == 1).all()
Example #10
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_conv_reshape_conv(): class Net(gluon.HybridBlock): def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(64, (3, 3)) self.conv1 = nn.Conv2D(128, (3, 3)) def hybrid_forward(self, F, x): x_slice = x.slice(begin=(0, 0, 1, 1), end=(4, 16, 33, 33)) y = self.conv0(x_slice) "shape of y is (4, 64, 30, 30)" y_reshape = y.reshape((0, 0, 60, 15)) out = self.conv1(y_reshape) return out x = mx.nd.random.uniform(shape=(4, 32, 64, 64)) net = Net() check_layer_forward_withinput(net, x)
Example #11
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_dense(): class Net(gluon.HybridBlock): def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): channel0 = np.random.randint(1, 17) self.dense0 = nn.Dense(channel0) def hybrid_forward(self, F, x): x_reshape = x.reshape((8, 64, 128, -1)) out = self.dense0(x_reshape) return out x = mx.nd.random.uniform(shape=(4, 32, 64, 64)) net = Net() check_layer_forward_withinput(net, x)
Example #12
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_dense(): class Net(gluon.HybridBlock): def __init__(self, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): channel0 = np.random.randint(1, 17) self.dense0 = nn.Dense(channel0) self.slice = slice def hybrid_forward(self, F, x): x_slice = x.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1])) out = self.dense0(x_slice) return out x = mx.nd.random.uniform(shape=(16, 32, 64, 64)) slice = [[0, 16, 0, 0], [4, 32, 32, 32]] net = Net(slice) check_layer_forward_withinput(net, x)
Example #13
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_dense_slice_dense(): class Net(gluon.HybridBlock): def __init__(self, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): channel0 = 32 channel1 = np.random.randint(1, 17) self.dense0 = nn.Dense(channel0) self.dense1 = nn.Dense(channel1) self.slice = slice def hybrid_forward(self, F, x): x_slice = x.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1])) y = self.dense0(x_slice) y_slice = y.slice(begin=(1, 0), end=(3, 10)) out = self.dense1(y_slice) return out x = mx.nd.random.uniform(shape=(16, 32, 64, 64)) slice = [[0, 16, 0, 0], [4, 32, 32, 32]] net = Net(slice) check_layer_forward_withinput(net, x)
Example #14
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_dense_slice_dense(): class Net(gluon.HybridBlock): def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): channel0 = 64 channel1 = np.random.randint(1, 17) self.dense0 = nn.Dense(channel0) self.dense1 = nn.Dense(channel1) def hybrid_forward(self, F, x): x_reshape = x.reshape((4, 16, 128, 32)) y = self.dense0(x_reshape) y_slice = y.slice(begin=(1, 32), end=(3, 64)) out = self.dense1(y_slice) return out x = mx.nd.random.uniform(shape=(4, 16, 64, 64)) net = Net() check_layer_forward_withinput(net, x)
Example #15
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_conv_slice_conv(): """ This test will test gluon Conv2d computation with ndarray reshape and slice """ class Net(gluon.HybridBlock): def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(16, (3, 3)) self.conv1 = nn.Conv2D(32, (3, 3)) def hybrid_forward(self, F, x): x_reshape = x.reshape((0, 0, 64, 16)) y = self.conv0(x_reshape) "shape of y is (4, 16, 62, 14)" y_slice = y.slice(begin=(0, 0, 0, 0), end=(2, 16, 14, 14)) out = self.conv1(y_slice) return out x = mx.nd.random.uniform(shape=(4, 3, 32, 32)) net = Net() check_layer_forward_withinput(net, x)
Example #16
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_deconv(): class Net(gluon.HybridBlock): def __init__(self, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.slice = slice self.conv0 = nn.Conv2DTranspose(64, (3, 3)) def hybrid_forward(self, F, x): x_slice = x.slice(begin=self.slice[0], end=self.slice[1]) out = self.conv0(x_slice) return out x = mx.nd.random.uniform(shape=(8, 32, 64, 64)) slice = [(0, 16, 0, 0), (4, 32, 32, 32)] net = Net(slice) check_layer_forward_withinput(net, x)
Example #17
Source File: network.py From gluon-cv with Apache License 2.0 | 6 votes |
def plot_mxboard(block, logdir='./logs'): """Plot network to visualize internal structures. Parameters ---------- block : mxnet.gluon.HybridBlock A hybridizable network to be visualized. logdir : str The directory to save. """ try: from mxboard import SummaryWriter except ImportError: print('mxboard is required. Please install via `pip install mxboard` ' + 'or refer to https://github.com/awslabs/mxboard.') raise data = mx.sym.var('data') sym = block(data) if isinstance(sym, tuple): sym = mx.sym.Group(sym) with SummaryWriter(logdir=logdir) as sw: sw.add_graph(sym) usage = '`tensorboard --logdir={} --host=127.0.0.1 --port=8888`'.format(logdir) print('Log saved. Use: {} to visualize it'.format(usage))
Example #18
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_deconv_reshape_deconv(): class Net(gluon.HybridBlock): def __init__(self, shape, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.conv0 = nn.Conv2DTranspose(32, (3, 3)) self.conv1 = nn.Conv2DTranspose(64, (3, 3), strides=(2, 2)) def hybrid_forward(self, F, x): x_reshape = x.reshape(self.reshape[0]) y = self.conv0(x_reshape) "shape of y is (4, 32, 66, 18)" y_reshape = y.reshape(self.reshape[1]) out = self.conv1(y_reshape) return out x = mx.nd.random.uniform(shape=(4, 16, 32, 32)) shape = [(4, 16, 64, -1), (4, 32, 33, -1)] net = Net(shape) check_layer_forward_withinput(net, x)
Example #19
Source File: coder.py From gluon-cv with Apache License 2.0 | 6 votes |
def hybrid_forward(self, F, samples, matches, refs): """HybridBlock, handle multi batch correctly Parameters ---------- samples: (B, N), value +1 (positive), -1 (negative), 0 (ignore) matches: (B, N), value range [0, M) refs: (B, M), value range [0, num_fg_class), excluding background Returns ------- targets: (B, N), value range [0, num_fg_class + 1), including background """ # samples (B, N) (+1, -1, 0: ignore), matches (B, N) [0, M), refs (B, M) # reshape refs (B, M) -> (B, 1, M) -> (B, N, M) refs = F.broadcast_like(F.reshape(refs, (0, 1, -1)), matches, lhs_axes=1, rhs_axes=1) # ids (B, N, M) -> (B, N), value [0, M + 1), 0 reserved for background class target_ids = F.pick(refs, matches, axis=2) + 1 # samples 0: set ignore samples to ignore_label targets = F.where(samples > 0.5, target_ids, F.ones_like(target_ids) * self._ignore_label) # samples -1: set negative samples to 0 targets = F.where(samples < -0.5, F.zeros_like(targets), targets) return targets
Example #20
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_deconv_slice_deconv(): class Net(gluon.HybridBlock): def __init__(self, shape, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.slice = slice self.conv0 = nn.Conv2DTranspose(32, (3, 3)) self.conv1 = nn.Conv2DTranspose(64, (3, 3), strides=(2, 2)) def hybrid_forward(self, F, x): x_reshape = x.reshape(self.reshape) y = self.conv0(x_reshape) "shape of y is (4, 32, 66, 18)" y_slice = y.slice(begin=self.slice[0], end=self.slice[1]) out = self.conv1(y_slice) return out x = mx.nd.random.uniform(shape=(4, 16, 32, 32)) shape = (4, 16, 64, -1) slice = [(0, 0, 0, 0), (2, 16, 16, 16)] net = Net(shape, slice) check_layer_forward_withinput(net, x)
Example #21
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_batchnorm_slice_batchnorm(): class Net(gluon.HybridBlock): def __init__(self, shape, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(128, (1, 1)) self.bn0 = nn.BatchNorm() self.bn1 = nn.BatchNorm() self.reshape = shape self.slice = slice def hybrid_forward(self, F, x): x_in = self.conv0(x) x_reshape = x_in.reshape(self.reshape) y = self.bn0(x_reshape) y_slice = y.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1])) out = self.bn1(y_slice) return out x = mx.nd.random.uniform(shape=(4, 32, 64, 64)) slice = [[0, 0, 0, 0], [2, 64, 32, 32]] shape = (4, 64, 64, -1) net = Net(shape, slice) check_layer_forward_withinput(net, x)
Example #22
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_batchnorm_reshape_batchnorm(): class Net(gluon.HybridBlock): def __init__(self, shape, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(128, (1, 1)) self.bn0 = nn.BatchNorm() self.bn1 = nn.BatchNorm() self.reshape = shape self.slice = slice def hybrid_forward(self, F, x): x_in = self.conv0(x) x_slice = x_in.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1])) y = self.bn0(x_slice) y_reshape = y.reshape(self.reshape) out = self.bn1(y_reshape) return out x = mx.nd.random.uniform(shape=(16, 128, 256, 256)) slice = [[0, 0, 0, 0], [4, 32, 32, 32]] shape = (1, 128, 64, -1) net = Net(shape, slice) check_layer_forward_withinput(net, x)
Example #23
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_batchnorm_slice_batchnorm(): class Net(gluon.HybridBlock): def __init__(self, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(128, (1, 1)) self.bn0 = nn.BatchNorm() self.bn1 = nn.BatchNorm() self.slice = slice def hybrid_forward(self, F, x): x_in = self.conv0(x) x_slice = x_in.slice(begin=tuple(self.slice[0][0]), end=tuple(self.slice[0][1])) y = self.bn0(x_slice) y_slice = y.slice(begin=tuple(self.slice[1][0]), end=tuple(self.slice[1][1])) out = self.bn1(y_slice) return out x = mx.nd.random.uniform(shape=(16, 128, 256, 256)) slice = [[[0, 0, 0, 0], [4, 32, 32, 32]], [[0, 0, 0, 0], [2, 64, 16, 16]]] net = Net(slice) check_layer_forward_withinput(net, x)
Example #24
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_batchnorm(): class Net(gluon.HybridBlock): def __init__(self, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(128, (1, 1)) self.bn0 = nn.BatchNorm() self.slice = slice def hybrid_forward(self, F, x): x_in = self.conv0(x) x_slice = x_in.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1])) out = self.bn0(x_slice) return out x = mx.nd.random.uniform(shape=(16, 128, 256, 256)) slice = [[0, 0, 0, 0], [4, 32, 32, 32]] net = Net(slice) check_layer_forward_withinput(net, x)
Example #25
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_reshape_batchnorm(): class Net(gluon.HybridBlock): def __init__(self, shape, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(96, (1, 1)) self.bn0 = nn.BatchNorm() self.reshape = shape def hybrid_forward(self, F, x): x_in = self.conv0(x) x_reshape = x_in.reshape(self.reshape) out = self.bn0(x_reshape) return out x = mx.nd.random.uniform(shape=(4, 32, 64, 64)) shape = (4, 64, 64, -1) net = Net(shape) check_layer_forward_withinput(net, x)
Example #26
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_slice_dense_reshape_dense(): class Net(gluon.HybridBlock): def __init__(self, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): channel0 = np.random.randint(1, 17) channel1 = np.random.randint(1, 17) self.dense0 = nn.Dense(channel0) self.dense1 = nn.Dense(channel1) self.slice = slice def hybrid_forward(self, F, x): x_slice = x.slice(begin=tuple(self.slice[0]), end=tuple(self.slice[1])) y = self.dense0(x_slice) y_reshape = y.reshape((1, -1)) out = self.dense1(y_reshape) return out x = mx.nd.random.uniform(shape=(16, 32, 64, 64)) slice = [[0, 16, 0, 0], [4, 32, 32, 32]] net = Net(slice) check_layer_forward_withinput(net, x)
Example #27
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_reshape_activation_slice_activation(): class Net(gluon.HybridBlock): def __init__(self, act0, act1, shape, slice, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.reshape = shape self.slice = slice self.act0 = nn.Activation(act0) self.act1 = nn.Activation(act1) def hybrid_forward(self, F, x): x_reshape = x.reshape(self.reshape) y = self.act0(x_reshape) y_slice = y.slice(begin=self.slice[0], end=self.slice[1]) out = self.act1(y_slice) return out acts = ["relu", "sigmoid", "tanh", "softrelu"] for idx0, act0 in enumerate(acts): for idx1, act1 in enumerate(acts): if idx1 == idx0: continue x = mx.nd.random.uniform(-1, 1, shape=(4, 16, 32, 32)) shape = (4, 32, 32, -1) slice = [(0, 0, 0, 0), (2, 16, 16, 16)] net = Net(act0, act1, shape, slice) check_layer_forward_withinput(net, x)
Example #28
Source File: test_mkldnn.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_slice_before_conv(): class Net(gluon.HybridBlock): """ test Net """ def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(4, (3, 3)) self.conv1 = nn.Conv2D(4, (3, 3)) def hybrid_forward(self, F, x, *args, **kwargs): x_slice = x.slice(begin=(0, 0, 0, 0), end=(2, 4, 10, 10)) y = self.conv0(x_slice) y_slice = y.slice(begin=(1, 0, 2, 2), end=(2, 1, 7, 7)) out = self.conv1(y_slice) return out x = mx.nd.random.uniform(shape=(2, 10, 10, 10)) x.attach_grad() net = Net() net.collect_params().initialize() with mx.autograd.record(): out1 = net(x) out1.backward() dx1 = x.grad net.hybridize() with mx.autograd.record(): out2 = net(x) out2.backward() mx.test_utils.assert_almost_equal(dx1.asnumpy(), x.grad.asnumpy(), rtol=1e-5, atol=1e-6) mx.test_utils.assert_almost_equal(out1.asnumpy(), out2.asnumpy(), rtol=1e-5, atol=1e-6)
Example #29
Source File: test_mkldnn.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_slice_reshape_before_conv(): class Net(gluon.HybridBlock): """ test Net """ def __init__(self, **kwargs): super(Net, self).__init__(**kwargs) with self.name_scope(): self.conv0 = nn.Conv2D(4, (3, 3)) self.conv1 = nn.Conv2D(4, (3, 3)) def hybrid_forward(self, F, x, *args, **kwargs): x_slice = x.slice(begin=(0, 0, 0, 0), end=(2, 4, 8, 9)) y = self.conv0(x_slice) y_reshape = y.reshape((0, 0, 14, 3)) out = self.conv1(y_reshape) return out x = mx.nd.random.uniform(shape=(2, 10, 10, 10)) x.attach_grad() net = Net() net.collect_params().initialize() with mx.autograd.record(): out1 = net(x) out1.backward() dx1 = x.grad net.hybridize() with mx.autograd.record(): out2 = net(x) out2.backward() mx.test_utils.assert_almost_equal(dx1.asnumpy(), x.grad.asnumpy(), rtol=1e-5, atol=1e-6) mx.test_utils.assert_almost_equal(out1.asnumpy(), out2.asnumpy(), rtol=1e-5, atol=1e-6)
Example #30
Source File: yolo3.py From gluon-cv with Apache License 2.0 | 5 votes |
def yolo3_darknet53_coco(pretrained_base=True, pretrained=False, norm_layer=BatchNorm, norm_kwargs=None, **kwargs): """YOLO3 multi-scale with darknet53 base network on COCO dataset. Parameters ---------- pretrained_base : boolean Whether fetch and load pretrained weights for base network. pretrained : bool or str Boolean value controls whether to load the default pretrained weights for model. String value represents the hashtag for a certain version of pretrained weights. norm_layer : object Normalization layer used (default: :class:`mxnet.gluon.nn.BatchNorm`) Can be :class:`mxnet.gluon.nn.BatchNorm` or :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`. norm_kwargs : dict Additional `norm_layer` arguments, for example `num_devices=4` for :class:`mxnet.gluon.contrib.nn.SyncBatchNorm`. Returns ------- mxnet.gluon.HybridBlock Fully hybrid yolo3 network. """ from ...data import COCODetection pretrained_base = False if pretrained else pretrained_base base_net = darknet53( pretrained=pretrained_base, norm_layer=norm_layer, norm_kwargs=norm_kwargs, **kwargs) stages = [base_net.features[:15], base_net.features[15:24], base_net.features[24:]] anchors = [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]] strides = [8, 16, 32] classes = COCODetection.CLASSES return get_yolov3( 'darknet53', stages, [512, 256, 128], anchors, strides, classes, 'coco', pretrained=pretrained, norm_layer=norm_layer, norm_kwargs=norm_kwargs, **kwargs)