commit
62326542e9
|
@ -0,0 +1,98 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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 <sgazeos@gmail.com>
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <op_boilerplate.h>
|
||||||
|
#if NOT_EXCLUDED(OP_adjust_contrast)
|
||||||
|
|
||||||
|
#include <ops/declarable/headers/parity_ops.h>
|
||||||
|
#include <NDArrayFactory.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
// fill up axes vector first
|
||||||
|
std::vector<int> 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);
|
||||||
|
|
||||||
|
NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext());
|
||||||
|
factorT.p(0, factor);
|
||||||
|
// this is contrast calculation
|
||||||
|
*output = (*input - mean) * factorT + mean;
|
||||||
|
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_TYPES(adjust_contrast) {
|
||||||
|
getOpDescriptor()->setAllowedInputTypes(nd4j::DataType::ANY)
|
||||||
|
->setAllowedOutputTypes({ALL_FLOATS})
|
||||||
|
->setSameMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
std::vector<int> 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);
|
||||||
|
|
||||||
|
// result as (x - mean) * factor + mean
|
||||||
|
std::unique_ptr<NDArray> temp(input->dup());
|
||||||
|
input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), &mean, temp.get());
|
||||||
|
temp->applyScalar(scalar::Multiply, factor);
|
||||||
|
temp->applyTrueBroadcast(BroadcastOpsTuple::Add(), &mean, output);
|
||||||
|
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_TYPES(adjust_contrast_v2) {
|
||||||
|
getOpDescriptor()->setAllowedInputTypes(nd4j::DataType::ANY)
|
||||||
|
->setAllowedOutputTypes({ALL_FLOATS})
|
||||||
|
->setSameMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -600,6 +600,22 @@ namespace nd4j {
|
||||||
DECLARE_CONFIGURABLE_OP(adjust_saturation, 1, 1, true, 1, -2);
|
DECLARE_CONFIGURABLE_OP(adjust_saturation, 1, 1, true, 1, -2);
|
||||||
#endif
|
#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);
|
||||||
|
DECLARE_CONFIGURABLE_OP(adjust_contrast_v2, 1, 1, true, 1, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This operation rearranges data from depth into blocks of spatial data. This is the reverse transformation
|
* This operation rearranges data from depth into blocks of spatial data. This is the reverse transformation
|
||||||
|
|
|
@ -157,6 +157,78 @@ TEST_F(DeclarableOpsTests15, Test_standarize_bp_1) {
|
||||||
delete result;
|
delete result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(DeclarableOpsTests15, Test_AdjustContrast_1) {
|
||||||
|
auto x = NDArrayFactory::create<double>('c', {4,4,3});
|
||||||
|
auto e = NDArrayFactory::create<double>('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<float>('c', {1, 4,4,3});
|
||||||
|
auto e = NDArrayFactory::create<float>('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_AdjustContrast_3) {
|
||||||
|
auto x = NDArrayFactory::create<float>('c', {1, 4,4,3});
|
||||||
|
auto e = NDArrayFactory::create<float>('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<double>('c', {4, 4, 3});
|
||||||
|
auto e = NDArrayFactory::create<double>('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) {
|
TEST_F(DeclarableOpsTests15, Test_depthwise_bp_1) {
|
||||||
auto in = NDArrayFactory::create<float>('c', {4, 8, 64, 64});
|
auto in = NDArrayFactory::create<float>('c', {4, 8, 64, 64});
|
||||||
auto w = NDArrayFactory::create<float>('c', {2, 2, 8, 2});
|
auto w = NDArrayFactory::create<float>('c', {2, 2, 8, 2});
|
||||||
|
|
Loading…
Reference in New Issue