From e06dfb5dcc26daa2cbe41f52e9c3f66df9522ab7 Mon Sep 17 00:00:00 2001 From: shugeo Date: Mon, 30 Sep 2019 18:24:12 +0300 Subject: [PATCH 1/3] Implementation of adjust_contrast op. --- .../generic/parity_ops/adjust_contrast.cpp | 68 +++++++++++++++++++ .../ops/declarable/headers/parity_ops.h | 15 ++++ .../layers_tests/DeclarableOpsTests15.cpp | 36 ++++++++++ 3 files changed, 119 insertions(+) create mode 100644 libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp diff --git a/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp b/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp new file mode 100644 index 000000000..91ecf3288 --- /dev/null +++ b/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2015-2018 Skymind, Inc. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +// +// @author George A. Shulinok +// + +#include +#if NOT_EXCLUDED(OP_adjust_contrast) + +#include +#include + +namespace nd4j { +namespace ops { + +CONFIGURABLE_OP_IMPL(adjust_contrast, 1, 1, true, 1, 0) { + + auto input = INPUT_VARIABLE(0); + auto output = OUTPUT_VARIABLE(0); + + const double factor = T_ARG(0); + + REQUIRE_TRUE(input->rankOf() > 2, 0, "ADJUST_CONTRAST: op expects rank of input array to be >= 3, but got %i instead", input->rankOf()); + REQUIRE_TRUE(input->sizeAt(-1) == 3, 0, "ADJUST_CONTRAST: operation expects image with 3 channels (R, G, B), but got %i instead", input->sizeAt(-1)); + + // compute mean before + reduce_mean meanOp; + auto axes = NDArrayFactory::create('c', {input->rankOf() - 1}, block.launchContext()); + for (int i = 0; i < input->rankOf() -1; i++) + axes.p(i, i); + + auto meanRes = meanOp.execute({input, &axes}, {}, {}); + REQUIRE_TRUE(meanRes->status() == Status::OK(), 0, "ADJUST_CONTRAST: op should be successful, but error code %i occured.", meanRes->status()); + auto mean = meanRes->at(0); + NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext()); + factorT.p(0, factor); + // this is contrast calculation + *output = (*input - *mean) * factorT + *mean; + delete meanRes; + return Status::OK(); +} + +DECLARE_TYPES(adjust_contrast) { + getOpDescriptor()->setAllowedInputTypes(nd4j::DataType::ANY) + ->setAllowedOutputTypes({ALL_FLOATS}) + ->setSameMode(true); +} + + + +} +} + +#endif \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/headers/parity_ops.h b/libnd4j/include/ops/declarable/headers/parity_ops.h index e30ff86a5..98769f4b9 100644 --- a/libnd4j/include/ops/declarable/headers/parity_ops.h +++ b/libnd4j/include/ops/declarable/headers/parity_ops.h @@ -600,6 +600,21 @@ namespace nd4j { DECLARE_CONFIGURABLE_OP(adjust_saturation, 1, 1, true, 1, -2); #endif + /** + * This operation adjusts image contrast by given factor ( z = (x - mean) * factor + mean ) + * Input arrays: + * 0 - input array with rank >= 3, must have last one dimension equal 3, that is dimension containing channels. + * + * T arguments: + * 0 - contrast factor + * + */ + #if NOT_EXCLUDED(OP_adjust_contrast) + DECLARE_CONFIGURABLE_OP(adjust_contrast, 1, 1, true, 1, 0); + #endif + + + /** * This operation rearranges data from depth into blocks of spatial data. This is the reverse transformation diff --git a/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp b/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp index 65cb470a7..e4bf2f9e9 100644 --- a/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp +++ b/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp @@ -157,6 +157,42 @@ TEST_F(DeclarableOpsTests15, Test_standarize_bp_1) { delete result; } +TEST_F(DeclarableOpsTests15, Test_AdjustContrast_1) { + auto x = NDArrayFactory::create('c', {4,4,3}); + auto e = NDArrayFactory::create('c', {4,4,3}, { + -21.5, -20.5, -19.5, -15.5, -14.5, -13.5, -9.5, -8.5, -7.5, -3.5, -2.5, -1.5, + 2.5, 3.5, 4.5, 8.5, 9.5, 10.5, 14.5, 15.5, 16.5, 20.5, 21.5, 22.5, + 26.5, 27.5, 28.5, 32.5, 33.5, 34.5, 38.5, 39.5, 40.5, 44.5, 45.5, 46.5, + 50.5, 51.5, 52.5, 56.5, 57.5, 58.5, 62.5, 63.5, 64.5, 68.5, 69.5, 70.5 + }); + x.linspace(1.); + nd4j::ops::adjust_contrast op; + auto result = op.execute({&x}, {2.}, {}, {}); + ASSERT_EQ(Status::OK(), result->status()); + auto out = result->at(0); +// out->printIndexedBuffer("Adjusted Constrast"); + ASSERT_TRUE(e.equalsTo(out)); + delete result; +} + +TEST_F(DeclarableOpsTests15, Test_AdjustContrast_2) { + auto x = NDArrayFactory::create('c', {1, 4,4,3}); + auto e = NDArrayFactory::create('c', {1, 4,4,3}, { + -21.5, -20.5, -19.5, -15.5, -14.5, -13.5, -9.5, -8.5, -7.5, -3.5, -2.5, -1.5, + 2.5, 3.5, 4.5, 8.5, 9.5, 10.5, 14.5, 15.5, 16.5, 20.5, 21.5, 22.5, + 26.5, 27.5, 28.5, 32.5, 33.5, 34.5, 38.5, 39.5, 40.5, 44.5, 45.5, 46.5, + 50.5, 51.5, 52.5, 56.5, 57.5, 58.5, 62.5, 63.5, 64.5, 68.5, 69.5, 70.5 + }); + x.linspace(1.); + nd4j::ops::adjust_contrast op; + auto result = op.execute({&x}, {2.}, {}, {}); + ASSERT_EQ(Status::OK(), result->status()); + auto out = result->at(0); +// out->printIndexedBuffer("Adjusted Constrast"); + ASSERT_TRUE(e.equalsTo(out)); + delete result; +} + TEST_F(DeclarableOpsTests15, Test_depthwise_bp_1) { auto in = NDArrayFactory::create('c', {4, 8, 64, 64}); auto w = NDArrayFactory::create('c', {2, 2, 8, 2}); From 1575c704ae29cb16786b2d5d33885a2ca9b8eeeb Mon Sep 17 00:00:00 2001 From: shugeo Date: Tue, 1 Oct 2019 11:44:27 +0300 Subject: [PATCH 2/3] Added implementation for adjust_contrast_v2 op and tests. --- .../generic/parity_ops/adjust_contrast.cpp | 37 +++++++++++++++++++ .../ops/declarable/headers/parity_ops.h | 1 + .../layers_tests/DeclarableOpsTests15.cpp | 36 ++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp b/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp index 91ecf3288..bcd4f833b 100644 --- a/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp +++ b/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp @@ -61,6 +61,43 @@ DECLARE_TYPES(adjust_contrast) { } + CONFIGURABLE_OP_IMPL(adjust_contrast_v2, 1, 1, true, 1, 0) { + + auto input = INPUT_VARIABLE(0); + auto output = OUTPUT_VARIABLE(0); + + const double factor = T_ARG(0); + + REQUIRE_TRUE(input->rankOf() > 2, 0, "ADJUST_CONTRAST: op expects rank of input array to be >= 3, but got %i instead", input->rankOf()); + REQUIRE_TRUE(input->sizeAt(-1) == 3, 0, "ADJUST_CONTRAST: operation expects image with 3 channels (R, G, B), but got %i instead", input->sizeAt(-1)); + + // compute mean before + reduce_mean meanOp; + auto axes = NDArrayFactory::create('c', {input->rankOf() - 1}, block.launchContext()); + for (int i = 0; i < input->rankOf() -1; i++) + axes.p(i, i); + + auto meanRes = meanOp.execute({input, &axes}, {}, {}); + REQUIRE_TRUE(meanRes->status() == Status::OK(), 0, "ADJUST_CONTRAST: op should be successful, but error code %i occured.", meanRes->status()); + auto mean = meanRes->at(0); +// NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext()); +// factorT.p(0, factor); + // this is contrast calculation + std::unique_ptr temp(input->dup()); + input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), mean, temp.get()); + temp->applyScalar(scalar::Multiply, factor); + temp->applyTrueBroadcast(BroadcastOpsTuple::Add(), mean, output); +// *output = (*input - *mean) * factorT + *mean; + + delete meanRes; + return Status::OK(); + } + + DECLARE_TYPES(adjust_contrast_v2) { + getOpDescriptor()->setAllowedInputTypes(nd4j::DataType::ANY) + ->setAllowedOutputTypes({ALL_FLOATS}) + ->setSameMode(true); + } } } diff --git a/libnd4j/include/ops/declarable/headers/parity_ops.h b/libnd4j/include/ops/declarable/headers/parity_ops.h index 98769f4b9..652c2be8c 100644 --- a/libnd4j/include/ops/declarable/headers/parity_ops.h +++ b/libnd4j/include/ops/declarable/headers/parity_ops.h @@ -611,6 +611,7 @@ namespace nd4j { */ #if NOT_EXCLUDED(OP_adjust_contrast) DECLARE_CONFIGURABLE_OP(adjust_contrast, 1, 1, true, 1, 0); + DECLARE_CONFIGURABLE_OP(adjust_contrast_v2, 1, 1, true, 1, 0); #endif diff --git a/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp b/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp index e4bf2f9e9..0bd05cec3 100644 --- a/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp +++ b/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests15.cpp @@ -193,6 +193,42 @@ TEST_F(DeclarableOpsTests15, Test_AdjustContrast_2) { delete result; } +TEST_F(DeclarableOpsTests15, Test_AdjustContrast_3) { + auto x = NDArrayFactory::create('c', {1, 4,4,3}); + auto e = NDArrayFactory::create('c', {1, 4,4,3}, { + -21.5, -20.5, -19.5, -15.5, -14.5, -13.5, -9.5, -8.5, -7.5, -3.5, -2.5, -1.5, + 2.5, 3.5, 4.5, 8.5, 9.5, 10.5, 14.5, 15.5, 16.5, 20.5, 21.5, 22.5, + 26.5, 27.5, 28.5, 32.5, 33.5, 34.5, 38.5, 39.5, 40.5, 44.5, 45.5, 46.5, + 50.5, 51.5, 52.5, 56.5, 57.5, 58.5, 62.5, 63.5, 64.5, 68.5, 69.5, 70.5 + }); + x.linspace(1.); + nd4j::ops::adjust_contrast_v2 op; + auto result = op.execute({&x}, {2.}, {}, {}); + ASSERT_EQ(Status::OK(), result->status()); + auto out = result->at(0); +// out->printIndexedBuffer("Adjusted Constrast"); + ASSERT_TRUE(e.equalsTo(out)); + delete result; +} + +TEST_F(DeclarableOpsTests15, Test_AdjustContrast_4) { + auto x = NDArrayFactory::create('c', {4, 4, 3}); + auto e = NDArrayFactory::create('c', {4, 4, 3}, { + -21.5, -20.5, -19.5, -15.5, -14.5, -13.5, -9.5, -8.5, -7.5, -3.5, -2.5, -1.5, + 2.5, 3.5, 4.5, 8.5, 9.5, 10.5, 14.5, 15.5, 16.5, 20.5, 21.5, 22.5, + 26.5, 27.5, 28.5, 32.5, 33.5, 34.5, 38.5, 39.5, 40.5, 44.5, 45.5, 46.5, + 50.5, 51.5, 52.5, 56.5, 57.5, 58.5, 62.5, 63.5, 64.5, 68.5, 69.5, 70.5 + }); + x.linspace(1.); + nd4j::ops::adjust_contrast_v2 op; + auto result = op.execute({&x}, {2.}, {}, {}); + ASSERT_EQ(Status::OK(), result->status()); + auto out = result->at(0); +// out->printIndexedBuffer("Adjusted Constrast"); + ASSERT_TRUE(e.equalsTo(out)); + delete result; +} + TEST_F(DeclarableOpsTests15, Test_depthwise_bp_1) { auto in = NDArrayFactory::create('c', {4, 8, 64, 64}); auto w = NDArrayFactory::create('c', {2, 2, 8, 2}); From afeb524238f40a475d980baf72200a5b12277144 Mon Sep 17 00:00:00 2001 From: shugeo Date: Tue, 1 Oct 2019 14:13:09 +0300 Subject: [PATCH 3/3] Refactored implementation for adjust_contrast ops. --- .../generic/parity_ops/adjust_contrast.cpp | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp b/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp index bcd4f833b..4011d5e32 100644 --- a/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp +++ b/libnd4j/include/ops/declarable/generic/parity_ops/adjust_contrast.cpp @@ -36,21 +36,19 @@ CONFIGURABLE_OP_IMPL(adjust_contrast, 1, 1, true, 1, 0) { REQUIRE_TRUE(input->rankOf() > 2, 0, "ADJUST_CONTRAST: op expects rank of input array to be >= 3, but got %i instead", input->rankOf()); REQUIRE_TRUE(input->sizeAt(-1) == 3, 0, "ADJUST_CONTRAST: operation expects image with 3 channels (R, G, B), but got %i instead", input->sizeAt(-1)); - // compute mean before - reduce_mean meanOp; - auto axes = NDArrayFactory::create('c', {input->rankOf() - 1}, block.launchContext()); - for (int i = 0; i < input->rankOf() -1; i++) - axes.p(i, i); + // fill up axes vector first + std::vector axes(input->rankOf() - 1); + for (auto i = 0; i < axes.size(); ++i) + axes[i] = i; + // mean as reduction for last dimension set + auto mean = input->reduceAlongDims(reduce::Mean, axes); - auto meanRes = meanOp.execute({input, &axes}, {}, {}); - REQUIRE_TRUE(meanRes->status() == Status::OK(), 0, "ADJUST_CONTRAST: op should be successful, but error code %i occured.", meanRes->status()); - auto mean = meanRes->at(0); NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext()); factorT.p(0, factor); // this is contrast calculation - *output = (*input - *mean) * factorT + *mean; - delete meanRes; + *output = (*input - mean) * factorT + mean; + return Status::OK(); } @@ -72,24 +70,19 @@ DECLARE_TYPES(adjust_contrast) { REQUIRE_TRUE(input->sizeAt(-1) == 3, 0, "ADJUST_CONTRAST: operation expects image with 3 channels (R, G, B), but got %i instead", input->sizeAt(-1)); // compute mean before - reduce_mean meanOp; - auto axes = NDArrayFactory::create('c', {input->rankOf() - 1}, block.launchContext()); - for (int i = 0; i < input->rankOf() -1; i++) - axes.p(i, i); + std::vector axes(input->rankOf() - 1); + for (auto i = 0; i < axes.size(); ++i) + axes[i] = i; - auto meanRes = meanOp.execute({input, &axes}, {}, {}); - REQUIRE_TRUE(meanRes->status() == Status::OK(), 0, "ADJUST_CONTRAST: op should be successful, but error code %i occured.", meanRes->status()); - auto mean = meanRes->at(0); -// NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext()); -// factorT.p(0, factor); - // this is contrast calculation + // mean as reduction for last dimension set + auto mean = input->reduceAlongDims(reduce::Mean, axes); + + // result as (x - mean) * factor + mean std::unique_ptr temp(input->dup()); - input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), mean, temp.get()); + input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), &mean, temp.get()); temp->applyScalar(scalar::Multiply, factor); - temp->applyTrueBroadcast(BroadcastOpsTuple::Add(), mean, output); -// *output = (*input - *mean) * factorT + *mean; + temp->applyTrueBroadcast(BroadcastOpsTuple::Add(), &mean, output); - delete meanRes; return Status::OK(); }