From 83db15edb4bc3b1b00c2338eca3fe5807934d010 Mon Sep 17 00:00:00 2001 From: Pariksheet Date: Wed, 17 Oct 2018 18:47:58 +0530 Subject: [PATCH 1/2] l2 normalization --- nnvm/python/nnvm/frontend/darknet.py | 12 ++++++++++++ nnvm/python/nnvm/testing/darknet.py | 1 + .../python/frontend/darknet/test_forward.py | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/nnvm/python/nnvm/frontend/darknet.py b/nnvm/python/nnvm/frontend/darknet.py index bf3a16cdb23e..4da2e90bca42 100644 --- a/nnvm/python/nnvm/frontend/darknet.py +++ b/nnvm/python/nnvm/frontend/darknet.py @@ -267,6 +267,13 @@ def _darknet_upsampling(inputs, attrs): new_attrs['scale'] = attrs.get('scale', 1) return _darknet_get_nnvm_op(op_name)(*inputs, **new_attrs), None +def _darknet_l2normalize(inputs, attrs): + """Process the l2 normalization operation.""" + op_name, new_attrs = 'l2_normalize', {} + new_attrs['eps'] = attrs.get('eps', 0) + new_attrs['axis'] = attrs.get('axis', 1) + return _darknet_get_nnvm_op(op_name)(*inputs, **new_attrs), None + def _darknet_softmax_output(inputs, attrs): """Process the softmax operation.""" temperature = attrs.get('temperature', 1) @@ -370,6 +377,7 @@ def _darknet_op_not_support(inputs, attrs): LAYERTYPE.REGION : _darknet_region, LAYERTYPE.SHORTCUT : _darknet_shortcut, LAYERTYPE.UPSAMPLE : _darknet_upsampling, + LAYERTYPE.L2NORM : _darknet_l2normalize, LAYERTYPE.YOLO : _darknet_yolo, LAYERTYPE.DETECTION : _darknet_op_not_support, LAYERTYPE.CROP : _darknet_op_not_support, @@ -630,6 +638,10 @@ def _get_darknet_attrs(self, layer, layer_num): elif LAYERTYPE.UPSAMPLE == layer.type: attr.update({'scale' : layer.stride}) + + elif LAYERTYPE.L2NORM == layer.type: + pass + else: err = "Darknet layer type {} is not supported in nnvm.".format(layer.type) raise NotImplementedError(err) diff --git a/nnvm/python/nnvm/testing/darknet.py b/nnvm/python/nnvm/testing/darknet.py index d4d33a6949f3..328ad2ae6a10 100644 --- a/nnvm/python/nnvm/testing/darknet.py +++ b/nnvm/python/nnvm/testing/darknet.py @@ -512,6 +512,7 @@ class ACTIVATION(object): layer make_lstm_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); layer make_upsample_layer(int batch, int w, int h, int c, int stride); +layer make_l2norm_layer(int batch, int inputs); void free_network(network *net); """ ) diff --git a/nnvm/tests/python/frontend/darknet/test_forward.py b/nnvm/tests/python/frontend/darknet/test_forward.py index b1d5e735611a..2667b370f27b 100644 --- a/nnvm/tests/python/frontend/darknet/test_forward.py +++ b/nnvm/tests/python/frontend/darknet/test_forward.py @@ -107,7 +107,7 @@ def get_darknet_output(net, img): out.insert(0, attributes) out.insert(0, _read_memory_buffer((layer.total*2, ), layer.biases)) out.insert(0, _read_memory_buffer((layer.n, ), layer.mask, dtype='int32')) - layer_ou tshape = (layer.batch, layer.out_c, + layer_outshape = (layer.batch, layer.out_c, layer.out_h, layer.out_w) out.insert(0, _read_memory_buffer(layer_outshape, layer.output)) elif i == net.n-1: @@ -361,6 +361,19 @@ def test_forward_upsample(): test_forward(net) LIB.free_network(net) +def test_forward_l2normalize(): + '''test l2 normalization layer''' + net = LIB.make_network(1) + layer = LIB.make_l2norm_layer(1, 224*224*3) + layer.h = layer.out_h = 224 + layer.w = layer.out_w = 224 + layer.c = layer.out_c = 3 + net.layers[0] = layer + net.w = net.h = 224 + LIB.resize_network(net, 224, 224) + test_forward(net) + LIB.free_network(net) + def test_forward_elu(): '''test elu activation layer''' net = LIB.make_network(1) @@ -520,6 +533,7 @@ def test_forward_activation_logistic(): test_forward_region() test_forward_yolo_op() test_forward_upsample() + test_forward_l2normalize() test_forward_elu() test_forward_rnn() test_forward_crnn() From 564a6e394c866715a9b0d8a0de4a13810bb36241 Mon Sep 17 00:00:00 2001 From: Pariksheet Date: Thu, 18 Oct 2018 09:11:42 +0530 Subject: [PATCH 2/2] retrigger CI --- nnvm/tests/python/frontend/darknet/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nnvm/tests/python/frontend/darknet/test_forward.py b/nnvm/tests/python/frontend/darknet/test_forward.py index 2667b370f27b..bca50db5470e 100644 --- a/nnvm/tests/python/frontend/darknet/test_forward.py +++ b/nnvm/tests/python/frontend/darknet/test_forward.py @@ -365,9 +365,9 @@ def test_forward_l2normalize(): '''test l2 normalization layer''' net = LIB.make_network(1) layer = LIB.make_l2norm_layer(1, 224*224*3) + layer.c = layer.out_c = 3 layer.h = layer.out_h = 224 layer.w = layer.out_w = 224 - layer.c = layer.out_c = 3 net.layers[0] = layer net.w = net.h = 224 LIB.resize_network(net, 224, 224)