Refactored implementation for adjust_contrast ops.

master
shugeo 2019-10-01 14:13:09 +03:00
parent 1575c704ae
commit afeb524238
1 changed files with 17 additions and 24 deletions

View File

@ -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->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)); 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 // compute mean before
reduce_mean meanOp; // fill up axes vector first
auto axes = NDArrayFactory::create<int>('c', {input->rankOf() - 1}, block.launchContext()); std::vector<int> axes(input->rankOf() - 1);
for (int i = 0; i < input->rankOf() -1; i++) for (auto i = 0; i < axes.size(); ++i)
axes.p(i, 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()); NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext());
factorT.p(0, factor); factorT.p(0, factor);
// this is contrast calculation // this is contrast calculation
*output = (*input - *mean) * factorT + *mean; *output = (*input - mean) * factorT + mean;
delete meanRes;
return Status::OK(); 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)); 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 // compute mean before
reduce_mean meanOp; std::vector<int> axes(input->rankOf() - 1);
auto axes = NDArrayFactory::create<int>('c', {input->rankOf() - 1}, block.launchContext()); for (auto i = 0; i < axes.size(); ++i)
for (int i = 0; i < input->rankOf() -1; i++) axes[i] = i;
axes.p(i, i);
auto meanRes = meanOp.execute({input, &axes}, {}, {}); // mean as reduction for last dimension set
REQUIRE_TRUE(meanRes->status() == Status::OK(), 0, "ADJUST_CONTRAST: op should be successful, but error code %i occured.", meanRes->status()); auto mean = input->reduceAlongDims(reduce::Mean, axes);
auto mean = meanRes->at(0);
// NDArray factorT(output->dataType(), block.launchContext()); // = NDArrayFactory::create(factor, block.launchContext()); // result as (x - mean) * factor + mean
// factorT.p(0, factor);
// this is contrast calculation
std::unique_ptr<NDArray> temp(input->dup()); std::unique_ptr<NDArray> temp(input->dup());
input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), mean, temp.get()); input->applyTrueBroadcast(BroadcastOpsTuple::Subtract(), &mean, temp.get());
temp->applyScalar(scalar::Multiply, factor); temp->applyScalar(scalar::Multiply, factor);
temp->applyTrueBroadcast(BroadcastOpsTuple::Add(), mean, output); temp->applyTrueBroadcast(BroadcastOpsTuple::Add(), &mean, output);
// *output = (*input - *mean) * factorT + *mean;
delete meanRes;
return Status::OK(); return Status::OK();
} }