Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Add default values, fix imports, fix documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeep-krishnamurthy committed Jan 14, 2019
1 parent f6cf9d3 commit 7fdced9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion python/mxnet/gluon/data/vision/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Normalize(HybridBlock):
[-1.4711051 -1.5200229 ]]]
<NDArray 3x4x2 @cpu(0)>
"""
def __init__(self, mean, std):
def __init__(self, mean=0.0, std=1.0):
super(Normalize, self).__init__()
self._mean = mean
self._std = std
Expand Down
19 changes: 9 additions & 10 deletions src/operator/image/normalize_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@
#define MXNET_OPERATOR_IMAGE_NORMALIZE_OP_INL_H_


#include <mxnet/base.h>
#include <algorithm>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
#include <utility>
#include "../mxnet_op.h"
#include "../operator_common.h"
#include "../elemwise_op_common.h"

namespace mxnet {
Expand All @@ -44,12 +36,19 @@ namespace image {
// Parameter registration for image Normalize operator
struct NormalizeParam : public dmlc::Parameter<NormalizeParam> {
nnvm::Tuple<float> mean;
nnvm::Tuple<float> default_mean = {0.0f, 0.0f, 0.0f, 0.0f};
nnvm::Tuple<float> std;
nnvm::Tuple<float> default_std = {1.0f, 1.0f, 1.0f, 1.0f};

DMLC_DECLARE_PARAMETER(NormalizeParam) {
DMLC_DECLARE_FIELD(mean)
.describe("Sequence of means for each channel.");
.set_default(default_mean)
.describe("Sequence of means for each channel. "
"Default value is 0.");
DMLC_DECLARE_FIELD(std)
.describe("Sequence of standard deviations for each channel.");
.set_default(default_std)
.describe("Sequence of standard deviations for each channel. "
"Default value is 1.");
}
};

Expand Down
11 changes: 7 additions & 4 deletions src/operator/image/normalize_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ NNVM_REGISTER_OP(_image_normalize)
.describe(R"code(Normalize an tensor of shape (C x H x W) or (N x C x H x W) with mean and
standard deviation.
Given mean `(m1, ..., mn)` and std `(s1, ..., sn)` for `n` channels,
Given mean `(m1, ..., mn)` and std `(s\ :sub:`1`\ , ..., s\ :sub:`n`)` for `n` channels,
this transform normalizes each channel of the input tensor with:
.. math::
output[i] = (input[i] - mi) / si
output[i] = (input[i] - m\ :sub:`i`\ ) / s\ :sub:`i`
If mean or std is scalar, the same value will be applied to all channels.
Default value for mean is 0.0 and stand deviation is 1.0.
Example:
.. code-block:: python
Expand Down
6 changes: 6 additions & 0 deletions tests/python/gpu/test_gluon_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def test_normalize():
data_expected_4d[1][:][:][2] = data_expected_4d[1][:][:][2] - 2.0
assert_almost_equal(data_expected_4d, out_nd_4d.asnumpy())

# Default normalize values i.e., mean=0, std=1
data_in_3d_def = nd.random.uniform(0, 1, (3, 300, 300))
out_nd_3d_def = transforms.Normalize()(data_in_3d_def)
data_expected_3d_def = data_in_3d.asnumpy()
assert_almost_equal(data_expected_3d_def, out_nd_3d_def.asnumpy())

# Invalid Input - Neither 3D or 4D input
invalid_data_in = nd.random.uniform(0, 1, (5, 5, 3, 300, 300))
normalize_transformer = transforms.Normalize(mean=(0, 1, 2), std=(3, 2, 1))
Expand Down

0 comments on commit 7fdced9

Please sign in to comment.