2019-09-30 17:24:12 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* 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 {
|
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
CONFIGURABLE_OP_IMPL(adjust_contrast, 1, 1, true, 0, 0) {
|
2019-09-30 17:24:12 +02:00
|
|
|
|
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
|
|
|
|
2019-12-02 19:37:21 +01:00
|
|
|
// just skip op if input is empty
|
|
|
|
if (input->isEmpty())
|
|
|
|
return Status::OK();
|
|
|
|
|
2019-11-15 15:04:29 +01:00
|
|
|
REQUIRE_TRUE(block.numT() > 0 || block.width() > 1, 0, "ADJUST_CONTRAST: Scale factor required");
|
2019-09-30 17:24:12 +02:00
|
|
|
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));
|
2019-12-03 07:40:45 +01:00
|
|
|
|
|
|
|
NDArray* factor = nullptr;
|
|
|
|
|
|
|
|
if(block.width() > 1)
|
|
|
|
factor = INPUT_VARIABLE(1);
|
|
|
|
else {
|
|
|
|
factor = new NDArray(output->dataType(), block.launchContext());
|
|
|
|
factor->p(0, T_ARG(0));
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:13:09 +02:00
|
|
|
// fill up axes vector first
|
|
|
|
std::vector<int> axes(input->rankOf() - 1);
|
|
|
|
for (auto i = 0; i < axes.size(); ++i)
|
|
|
|
axes[i] = i;
|
2019-12-03 07:40:45 +01:00
|
|
|
|
2019-10-01 13:13:09 +02:00
|
|
|
// mean as reduction for last dimension set
|
|
|
|
auto mean = input->reduceAlongDims(reduce::Mean, axes);
|
|
|
|
|
2019-09-30 17:24:12 +02:00
|
|
|
// this is contrast calculation
|
2019-12-03 07:40:45 +01:00
|
|
|
output->assign((*input - mean) * (*factor) + mean);
|
|
|
|
|
|
|
|
if(block.width() == 1)
|
|
|
|
delete factor;
|
2019-10-01 13:13:09 +02:00
|
|
|
|
2019-09-30 17:24:12 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_TYPES(adjust_contrast) {
|
|
|
|
getOpDescriptor()->setAllowedInputTypes(nd4j::DataType::ANY)
|
|
|
|
->setAllowedOutputTypes({ALL_FLOATS})
|
|
|
|
->setSameMode(true);
|
|
|
|
}
|
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
CONFIGURABLE_OP_IMPL(adjust_contrast_v2, 1, 1, true, 0, 0) {
|
2019-09-30 17:24:12 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
2019-10-01 10:44:27 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
// just skip op if input is empty
|
|
|
|
if (input->isEmpty())
|
|
|
|
return Status::OK();
|
2019-10-01 10:44:27 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
REQUIRE_TRUE(input->rankOf() > 2, 0, "ADJUST_CONTRAST_V2: op expects rank of input array to be >= 3, but got %i instead", input->rankOf());
|
|
|
|
REQUIRE_TRUE(input->sizeAt(-1) == 3, 0, "ADJUST_CONTRAST_V2: operation expects image with 3 channels (R, G, B), but got %i instead", input->sizeAt(-1));
|
|
|
|
REQUIRE_TRUE(block.numT() > 0 || block.width() > 1, 0, "ADJUST_CONTRAST_V2: Scale factor required");
|
2019-11-15 15:04:29 +01:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
NDArray* factor = nullptr;
|
2019-10-01 10:44:27 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
if(block.width() > 1)
|
|
|
|
factor = INPUT_VARIABLE(1);
|
|
|
|
else {
|
|
|
|
factor = new NDArray(output->dataType(), block.launchContext());
|
|
|
|
factor->p(0, T_ARG(0));
|
|
|
|
}
|
2019-12-02 19:37:21 +01:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
// compute mean before
|
|
|
|
std::vector<int> axes(input->rankOf() - 1);
|
|
|
|
for (auto i = 0; i < axes.size(); ++i)
|
|
|
|
axes[i] = i;
|
2019-10-01 10:44:27 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
// mean as reduction for last dimension set
|
|
|
|
auto mean = input->reduceAlongDims(reduce::Mean, axes);
|
2019-10-01 13:13:09 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
// result as (x - mean) * factor + mean
|
|
|
|
auto temp = input->ulike();
|
|
|
|
input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), &mean, &temp);
|
|
|
|
temp.applyScalarArr(scalar::Multiply, factor);
|
|
|
|
temp.applyTrueBroadcast(BroadcastOpsTuple::Add(), &mean, output);
|
2019-10-01 13:13:09 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
if(block.width() == 1)
|
|
|
|
delete factor;
|
2019-10-01 10:44:27 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-10-01 10:44:27 +02:00
|
|
|
|
2019-12-03 07:40:45 +01:00
|
|
|
DECLARE_TYPES(adjust_contrast_v2) {
|
|
|
|
getOpDescriptor()->setAllowedInputTypes(nd4j::DataType::ANY)
|
|
|
|
->setAllowedOutputTypes({ALL_FLOATS})
|
|
|
|
->setSameMode(true);
|
|
|
|
}
|
2019-09-30 17:24:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|