From e06dfb5dcc26daa2cbe41f52e9c3f66df9522ab7 Mon Sep 17 00:00:00 2001 From: shugeo Date: Mon, 30 Sep 2019 18:24:12 +0300 Subject: [PATCH] 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});