Oleh rgb to gray scale (#138)
* libnd4j: RgbToGrayscale op #8536 - raw implementation in user branch, need checks for integration and adding other orders Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j: RgbToGrayscale op #8536 next step of merging images Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j: RgbToGrayscale op #8536, Revert merge of hsv_to_rgb and rgb_to_hsv as cause conflicts in naming need refactoring before merge, implementation of rbg_to_grs added * libnd4j: RgbToGrayscale op #8536 imlementation and conflict resolve * libnd4j: RgbToGrayscale op #8536 merged operations with images into image, renamed methods and files * libnd4j: RgbToGrayscale op #8536 added test for rgbToGrayScale, need clarification and fixed tests case run Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j: RgbToGrayscale op #8536 bug fixing and need review * libnd4j: RgbToGrayscale op #8536 some additional corrections after review Signed-off-by: Oleg <oleg.semeniv@gmail.com> * - minor corrections in rgbToGrs test1 Signed-off-by: Yurii <iuriish@yahoo.com> * libnd4j: RgbToGrayscale op #8536, corrected tests and rbf_to_grs, fixed problems, refactoring, need review * libnd4j: RgbToGrayscale op #8536 fix for 'f' order in rgbToGrs * libnd4j: RgbToGrayscale op #8536 fixed several bugs with dimC, test case refactoring and improve Signed-off-by: Oleg <oleg.semeniv@gmail.com> * - add cuda kernel for rgbToGrs op Signed-off-by: Yurii <iuriish@yahoo.com> * - fix linkage errors Signed-off-by: Yurii <iuriish@yahoo.com> Co-authored-by: Yurii Shyrma <iuriish@yahoo.com>master
parent
67d8199165
commit
211c0df76f
|
@ -41,7 +41,7 @@
|
||||||
#include <ops/declarable/headers/tests.h>
|
#include <ops/declarable/headers/tests.h>
|
||||||
#include <ops/declarable/headers/kernels.h>
|
#include <ops/declarable/headers/kernels.h>
|
||||||
#include <ops/declarable/headers/BarnesHutTsne.h>
|
#include <ops/declarable/headers/BarnesHutTsne.h>
|
||||||
#include <ops/declarable/headers/color_models.h>
|
#include <ops/declarable/headers/images.h>
|
||||||
#include <dll.h>
|
#include <dll.h>
|
||||||
#include <helpers/shape.h>
|
#include <helpers/shape.h>
|
||||||
#include <helpers/TAD.h>
|
#include <helpers/TAD.h>
|
||||||
|
|
|
@ -1,85 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2019 Konduit K.K.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
|
|
||||||
#include <ops/declarable/headers/color_models.h>
|
|
||||||
#include <ops/declarable/CustomOperations.h>
|
|
||||||
#include <helpers/ConstantTadHelper.h>
|
|
||||||
#include <execution/Threads.h>
|
|
||||||
|
|
||||||
namespace nd4j {
|
|
||||||
namespace ops {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
CONFIGURABLE_OP_IMPL(hsv_to_rgb, 1, 1, false, 0, 0) {
|
|
||||||
|
|
||||||
auto input = INPUT_VARIABLE(0);
|
|
||||||
auto output = OUTPUT_VARIABLE(0);
|
|
||||||
|
|
||||||
if (input->isEmpty())
|
|
||||||
return Status::OK();
|
|
||||||
|
|
||||||
const int rank = input->rankOf();
|
|
||||||
const int arg_size = block.getIArguments()->size();
|
|
||||||
const int dimC = arg_size > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + rank) : rank - 1;
|
|
||||||
|
|
||||||
REQUIRE_TRUE(rank >= 1, 0, "HSVtoRGB: Fails to meet the rank requirement: %i >= 1 ", rank);
|
|
||||||
if (arg_size > 0) {
|
|
||||||
REQUIRE_TRUE(dimC >= 0 && dimC < rank, 0, "Index of the Channel dimension out of range: %i not in [%i,%i) ", INT_ARG(0), -rank, rank);
|
|
||||||
}
|
|
||||||
REQUIRE_TRUE(input->sizeAt(dimC) == 3, 0, "HSVtoRGB: operation expects 3 channels (H, S, V), but got %i instead", input->sizeAt(dimC));
|
|
||||||
|
|
||||||
helpers::transform_hsv_rgb(block.launchContext(), input, output, dimC);
|
|
||||||
|
|
||||||
return Status::OK();
|
|
||||||
}
|
|
||||||
|
|
||||||
CONFIGURABLE_OP_IMPL(rgb_to_hsv, 1, 1, false, 0, 0) {
|
|
||||||
|
|
||||||
auto input = INPUT_VARIABLE(0);
|
|
||||||
auto output = OUTPUT_VARIABLE(0);
|
|
||||||
|
|
||||||
if (input->isEmpty())
|
|
||||||
return Status::OK();
|
|
||||||
|
|
||||||
const int rank = input->rankOf();
|
|
||||||
const int arg_size = block.getIArguments()->size();
|
|
||||||
const int dimC = arg_size > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + rank) : rank - 1;
|
|
||||||
|
|
||||||
REQUIRE_TRUE(rank >= 1, 0, "RGBtoHSV: Fails to meet the rank requirement: %i >= 1 ", rank);
|
|
||||||
if (arg_size > 0) {
|
|
||||||
REQUIRE_TRUE(dimC >= 0 && dimC < rank, 0, "Index of the Channel dimension out of range: %i not in [%i,%i) ", INT_ARG(0), -rank, rank);
|
|
||||||
}
|
|
||||||
REQUIRE_TRUE(input->sizeAt(dimC) == 3, 0, "RGBtoHSV: operation expects 3 channels (H, S, V), but got %i instead", input->sizeAt(dimC));
|
|
||||||
|
|
||||||
helpers::transform_rgb_hsv(block.launchContext(), input, output, dimC);
|
|
||||||
|
|
||||||
return Status::OK();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DECLARE_TYPES(hsv_to_rgb) {
|
|
||||||
getOpDescriptor()->setAllowedInputTypes({ ALL_FLOATS })
|
|
||||||
->setSameMode(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
DECLARE_TYPES(rgb_to_hsv) {
|
|
||||||
getOpDescriptor()->setAllowedInputTypes({ ALL_FLOATS })
|
|
||||||
->setSameMode(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2019 Konduit K.K.
|
||||||
|
*
|
||||||
|
* 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 Adel Rauf (rauf@konduit.ai)
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <ops/declarable/headers/images.h>
|
||||||
|
#include <ops/declarable/CustomOperations.h>
|
||||||
|
#include <helpers/ConstantTadHelper.h>
|
||||||
|
#include <execution/Threads.h>
|
||||||
|
|
||||||
|
namespace nd4j {
|
||||||
|
namespace ops {
|
||||||
|
|
||||||
|
CONFIGURABLE_OP_IMPL(hsv_to_rgb, 1, 1, false, 0, 0) {
|
||||||
|
|
||||||
|
auto input = INPUT_VARIABLE(0);
|
||||||
|
auto output = OUTPUT_VARIABLE(0);
|
||||||
|
|
||||||
|
if (input->isEmpty())
|
||||||
|
return Status::OK();
|
||||||
|
|
||||||
|
const int rank = input->rankOf();
|
||||||
|
const int argSize = block.getIArguments()->size();
|
||||||
|
const int dimC = argSize > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + rank) : rank - 1;
|
||||||
|
|
||||||
|
REQUIRE_TRUE(rank >= 1, 0, "HSVtoRGB: Fails to meet the rank requirement: %i >= 1 ", rank);
|
||||||
|
if (argSize > 0) {
|
||||||
|
REQUIRE_TRUE(dimC >= 0 && dimC < rank, 0, "Index of the Channel dimension out of range: %i not in [%i,%i) ", INT_ARG(0), -rank, rank);
|
||||||
|
}
|
||||||
|
REQUIRE_TRUE(input->sizeAt(dimC) == 3, 0, "HSVtoRGB: operation expects 3 channels (H, S, V), but got %i instead", input->sizeAt(dimC));
|
||||||
|
|
||||||
|
helpers::transformHsvRgb(block.launchContext(), input, output, dimC);
|
||||||
|
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_TYPES(hsv_to_rgb) {
|
||||||
|
getOpDescriptor()->setAllowedInputTypes({ ALL_FLOATS })
|
||||||
|
->setSameMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,74 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2019 Konduit K.K.
|
||||||
|
*
|
||||||
|
* 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 Oleh Semeniv (oleg.semeniv@gmail.com)
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <ops/declarable/headers/images.h>
|
||||||
|
#include <ops/declarable/CustomOperations.h>
|
||||||
|
#include <helpers/ConstantTadHelper.h>
|
||||||
|
#include <execution/Threads.h>
|
||||||
|
|
||||||
|
namespace nd4j {
|
||||||
|
namespace ops {
|
||||||
|
|
||||||
|
CUSTOM_OP_IMPL(rgb_to_grs, 1, 1, false, 0, 0) {
|
||||||
|
|
||||||
|
const auto input = INPUT_VARIABLE(0);
|
||||||
|
auto output = OUTPUT_VARIABLE(0);
|
||||||
|
|
||||||
|
const int inRank = input->rankOf();
|
||||||
|
const int argSize = block.getIArguments()->size();
|
||||||
|
const int dimC = argSize > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + inRank) : inRank - 1;
|
||||||
|
|
||||||
|
REQUIRE_TRUE(inRank >= 1, 0, "RGBtoGrayScale: Fails to meet the inRank requirement: %i >= 1 ", inRank);
|
||||||
|
if (argSize > 0) {
|
||||||
|
REQUIRE_TRUE(dimC >= 0 && dimC < inRank, 0, "Index of the Channel dimension out of range: %i not in [%i,%i) ", INT_ARG(0), -inRank, inRank);
|
||||||
|
}
|
||||||
|
REQUIRE_TRUE(input->sizeAt(dimC) == 3, 0, "RGBGrayScale: operation expects 3 channels (R, G, B) in last dimention, but received %i instead", input->sizeAt(dimC));
|
||||||
|
|
||||||
|
helpers::transformRgbGrs(block.launchContext(), *input, *output, dimC);
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_TYPES(rgb_to_grs) {
|
||||||
|
getOpDescriptor()->setAllowedInputTypes( {ALL_INTS, ALL_FLOATS} )
|
||||||
|
->setSameMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
DECLARE_SHAPE_FN(rgb_to_grs) {
|
||||||
|
|
||||||
|
const auto input = INPUT_VARIABLE(0);
|
||||||
|
const int inRank = input->rankOf();
|
||||||
|
|
||||||
|
const int argSize = block.getIArguments()->size();
|
||||||
|
const int dimC = argSize > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + inRank) : inRank - 1;
|
||||||
|
|
||||||
|
REQUIRE_TRUE(inRank >= 1, 0, "RGBtoGrayScale: Fails to meet the inRank requirement: %i >= 1 ", inRank);
|
||||||
|
if (argSize > 0) {
|
||||||
|
REQUIRE_TRUE(dimC >= 0 && dimC < inRank, 0, "Index of the Channel dimension out of range: %i not in [%i,%i) ", INT_ARG(0), -inRank, inRank);
|
||||||
|
}
|
||||||
|
REQUIRE_TRUE(input->sizeAt(dimC) == 3, 0, "RGBtoGrayScale: operation expects 3 channels (R, B, G) in last dimention, but received %i", dimC);
|
||||||
|
|
||||||
|
auto nShape = input->getShapeAsVector();
|
||||||
|
nShape[dimC] = 1;
|
||||||
|
|
||||||
|
return SHAPELIST(ConstantShapeHelper::getInstance()->createShapeInfo(input->dataType(), input->ordering(), nShape));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2019 Konduit K.K.
|
||||||
|
*
|
||||||
|
* 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 Adel Rauf (rauf@konduit.ai)
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <ops/declarable/headers/images.h>
|
||||||
|
#include <ops/declarable/CustomOperations.h>
|
||||||
|
#include <helpers/ConstantTadHelper.h>
|
||||||
|
#include <execution/Threads.h>
|
||||||
|
|
||||||
|
namespace nd4j {
|
||||||
|
namespace ops {
|
||||||
|
|
||||||
|
CONFIGURABLE_OP_IMPL(rgb_to_hsv, 1, 1, false, 0, 0) {
|
||||||
|
|
||||||
|
auto input = INPUT_VARIABLE(0);
|
||||||
|
auto output = OUTPUT_VARIABLE(0);
|
||||||
|
|
||||||
|
if (input->isEmpty())
|
||||||
|
return Status::OK();
|
||||||
|
|
||||||
|
const int rank = input->rankOf();
|
||||||
|
const int argSize = block.getIArguments()->size();
|
||||||
|
const int dimC = argSize > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + rank) : rank - 1;
|
||||||
|
|
||||||
|
REQUIRE_TRUE(rank >= 1, 0, "RGBtoHSV: Fails to meet the rank requirement: %i >= 1 ", rank);
|
||||||
|
if (argSize > 0) {
|
||||||
|
REQUIRE_TRUE(dimC >= 0 && dimC < rank, 0, "Index of the Channel dimension out of range: %i not in [%i,%i) ", INT_ARG(0), -rank, rank);
|
||||||
|
}
|
||||||
|
REQUIRE_TRUE(input->sizeAt(dimC) == 3, 0, "RGBtoHSV: operation expects 3 channels (H, S, V), but got %i instead", input->sizeAt(dimC));
|
||||||
|
|
||||||
|
helpers::transformRgbHsv(block.launchContext(), input, output, dimC);
|
||||||
|
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DECLARE_TYPES(rgb_to_hsv) {
|
||||||
|
getOpDescriptor()->setAllowedInputTypes({ ALL_FLOATS })
|
||||||
|
->setSameMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,56 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2019 Konduit K.K.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef LIBND4J_HEADERS_COLOR_MODELS_H
|
|
||||||
#define LIBND4J_HEADERS_COLOR_MODELS_H
|
|
||||||
|
|
||||||
#include <ops/declarable/headers/common.h>
|
|
||||||
#include <ops/declarable/CustomOperations.h>
|
|
||||||
#include <helpers/ConstantTadHelper.h>
|
|
||||||
#include <execution/Threads.h>
|
|
||||||
#include <ops/declarable/helpers/color_models_conv.h>
|
|
||||||
|
|
||||||
namespace nd4j {
|
|
||||||
namespace ops {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Rgb To Hsv
|
|
||||||
* Input arrays:
|
|
||||||
* 0 - input array with rank >= 1, must have at least one dimension equal 3, that is dimension containing channels.
|
|
||||||
* Int arguments:
|
|
||||||
* 0 - optional argument, corresponds to dimension with 3 channels
|
|
||||||
*/
|
|
||||||
#if NOT_EXCLUDED(OP_rgb_to_hsv)
|
|
||||||
DECLARE_CONFIGURABLE_OP(rgb_to_hsv, 1, 1, false, 0, 0);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hsv To Rgb
|
|
||||||
* Input arrays:
|
|
||||||
* 0 - input array with rank >= 1, must have at least one dimension equal 3, that is dimension containing channels.
|
|
||||||
* Int arguments:
|
|
||||||
* 0 - optional argument, corresponds to dimension with 3 channels
|
|
||||||
*/
|
|
||||||
#if NOT_EXCLUDED(OP_hsv_to_rgb)
|
|
||||||
DECLARE_CONFIGURABLE_OP(hsv_to_rgb, 1, 1, false, 0, 0);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2019 Konduit K.K.
|
||||||
|
*
|
||||||
|
* 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 Oleh Semeniv (oleg.semeniv@gmail.com)
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// @author Adel Rauf (rauf@konduit.ai)
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LIBND4J_HEADERS_IMAGES_H
|
||||||
|
#define LIBND4J_HEADERS_IMAGES_H
|
||||||
|
|
||||||
|
#include <ops/declarable/headers/common.h>
|
||||||
|
#include <ops/declarable/CustomOperations.h>
|
||||||
|
#include <helpers/ConstantTadHelper.h>
|
||||||
|
#include <execution/Threads.h>
|
||||||
|
#include <ops/declarable/helpers/imagesHelpers.h>
|
||||||
|
|
||||||
|
namespace nd4j {
|
||||||
|
namespace ops {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rgb To Hsv
|
||||||
|
* Input arrays:
|
||||||
|
* 0 - input array with rank >= 1, must have at least one dimension equal 3, that is dimension containing channels.
|
||||||
|
* Int arguments:
|
||||||
|
* 0 - optional argument, corresponds to dimension with 3 channels
|
||||||
|
*/
|
||||||
|
#if NOT_EXCLUDED(OP_rgb_to_hsv)
|
||||||
|
DECLARE_CONFIGURABLE_OP(rgb_to_hsv, 1, 1, false, 0, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hsv To Rgb
|
||||||
|
* Input arrays:
|
||||||
|
* 0 - input array with rank >= 1, must have at least one dimension equal 3, that is dimension containing channels.
|
||||||
|
* Int arguments:
|
||||||
|
* 0 - optional argument, corresponds to dimension with 3 channels
|
||||||
|
*/
|
||||||
|
#if NOT_EXCLUDED(OP_hsv_to_rgb)
|
||||||
|
DECLARE_CONFIGURABLE_OP(hsv_to_rgb, 1, 1, false, 0, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rgb To GrayScale
|
||||||
|
* Input arrays:
|
||||||
|
* 0 - input array with rank >= 1, the RGB tensor to convert. Last dimension must have size 3 and should contain RGB values.
|
||||||
|
*/
|
||||||
|
#if NOT_EXCLUDED(OP_rgb_to_grs)
|
||||||
|
DECLARE_CUSTOM_OP(rgb_to_grs, 1, 1, false, 0, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,90 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2019 Konduit K.K.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#include <ops/declarable/helpers/adjust_hue.h>
|
|
||||||
#include <ops/declarable/helpers/color_models_conv.h>
|
|
||||||
#include <helpers/ConstantTadHelper.h>
|
|
||||||
#include <execution/Threads.h>
|
|
||||||
|
|
||||||
namespace nd4j {
|
|
||||||
namespace ops {
|
|
||||||
namespace helpers {
|
|
||||||
|
|
||||||
//local
|
|
||||||
template <typename T, typename Op>
|
|
||||||
FORCEINLINE static void triple_transformer(const NDArray* input, NDArray* output, const int dimC, Op op) {
|
|
||||||
|
|
||||||
const int rank = input->rankOf();
|
|
||||||
|
|
||||||
const T* x = input->bufferAsT<T>();
|
|
||||||
T* z = output->bufferAsT<T>();
|
|
||||||
|
|
||||||
if (dimC == rank - 1 && input->ews() == 1 && output->ews() == 1 && input->ordering() == 'c' && output->ordering() == 'c') {
|
|
||||||
|
|
||||||
auto func = PRAGMA_THREADS_FOR{
|
|
||||||
for (auto i = start; i < stop; i += increment) {
|
|
||||||
op(x[i], x[i + 1], x[i + 2], z[i], z[i + 1], z[i + 2]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
samediff::Threads::parallel_for(func, 0, input->lengthOf(), 3);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), dimC);
|
|
||||||
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), dimC);
|
|
||||||
|
|
||||||
const Nd4jLong numOfTads = packX.numberOfTads();
|
|
||||||
const Nd4jLong xDimCstride = input->stridesOf()[dimC];
|
|
||||||
const Nd4jLong zDimCstride = output->stridesOf()[dimC];
|
|
||||||
|
|
||||||
auto func = PRAGMA_THREADS_FOR{
|
|
||||||
for (auto i = start; i < stop; i += increment) {
|
|
||||||
const T* xTad = x + packX.platformOffsets()[i];
|
|
||||||
T* zTad = z + packZ.platformOffsets()[i];
|
|
||||||
op(xTad[0], xTad[xDimCstride], xTad[2 * xDimCstride], zTad[0], zTad[zDimCstride], zTad[2 * zDimCstride]);
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
samediff::Threads::parallel_tad(func, 0, numOfTads);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
FORCEINLINE static void hsv_rgb(const NDArray* input, NDArray* output, const int dimC) {
|
|
||||||
auto op = nd4j::ops::helpers::hsvToRgb<T>;
|
|
||||||
return triple_transformer<T>(input, output, dimC, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
FORCEINLINE static void rgb_hsv(const NDArray* input, NDArray* output, const int dimC) {
|
|
||||||
auto op = nd4j::ops::helpers::rgbToHsv<T>;
|
|
||||||
return triple_transformer<T>(input, output, dimC, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
void transform_hsv_rgb(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
|
||||||
BUILD_SINGLE_SELECTOR(input->dataType(), hsv_rgb, (input, output, dimC), FLOAT_TYPES);
|
|
||||||
}
|
|
||||||
|
|
||||||
void transform_rgb_hsv(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
|
||||||
BUILD_SINGLE_SELECTOR(input->dataType(), rgb_hsv, (input, output, dimC), FLOAT_TYPES);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,137 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2019 Konduit K.K.
|
||||||
|
*
|
||||||
|
* 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 Oleh Semeniv (oleg.semeniv@gmail.com)
|
||||||
|
// @author Adel Rauf (rauf@konduit.ai)
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <ops/declarable/helpers/adjust_hue.h>
|
||||||
|
#include <ops/declarable/helpers/imagesHelpers.h>
|
||||||
|
#include <helpers/ConstantTadHelper.h>
|
||||||
|
#include <execution/Threads.h>
|
||||||
|
|
||||||
|
namespace nd4j {
|
||||||
|
namespace ops {
|
||||||
|
namespace helpers {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static void rgbToGrs_(const NDArray& input, NDArray& output, const int dimC) {
|
||||||
|
|
||||||
|
const T* x = input.bufferAsT<T>();
|
||||||
|
T* z = output.bufferAsT<T>();
|
||||||
|
const int rank = input.rankOf();
|
||||||
|
|
||||||
|
if(dimC == rank - 1 && 'c' == input.ordering() && 1 == input.ews() &&
|
||||||
|
'c' == output.ordering() && 1 == output.ews()){
|
||||||
|
|
||||||
|
auto func = PRAGMA_THREADS_FOR{
|
||||||
|
for (auto i = start; i < stop; i += increment) {
|
||||||
|
const auto xStep = i*3;
|
||||||
|
z[i] = 0.2989f*x[xStep] + 0.5870f*x[xStep + 1] + 0.1140f*x[xStep + 2];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
samediff::Threads::parallel_for(func, 0, output.lengthOf(), 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto func = PRAGMA_THREADS_FOR{
|
||||||
|
|
||||||
|
Nd4jLong coords[MAX_RANK];
|
||||||
|
for (auto i = start; i < stop; i += increment) {
|
||||||
|
shape::index2coords(i, output.getShapeInfo(), coords);
|
||||||
|
const auto zOffset = shape::getOffset(output.getShapeInfo(), coords);
|
||||||
|
const auto xOffset0 = shape::getOffset(input.getShapeInfo(), coords);
|
||||||
|
const auto xOffset1 = xOffset0 + input.strideAt(dimC);
|
||||||
|
const auto xOffset2 = xOffset1 + input.strideAt(dimC);
|
||||||
|
z[zOffset] = 0.2989f*x[xOffset0] + 0.5870f*x[xOffset1] + 0.1140f*x[xOffset2];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
samediff::Threads::parallel_for(func, 0, output.lengthOf(), 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void transformRgbGrs(nd4j::LaunchContext* context, const NDArray& input, NDArray& output, const int dimC) {
|
||||||
|
BUILD_SINGLE_SELECTOR(input.dataType(), rgbToGrs_, (input, output, dimC), NUMERIC_TYPES);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T, typename Op>
|
||||||
|
FORCEINLINE static void tripleTransformer(const NDArray* input, NDArray* output, const int dimC, Op op) {
|
||||||
|
|
||||||
|
const int rank = input->rankOf();
|
||||||
|
|
||||||
|
const T* x = input->bufferAsT<T>();
|
||||||
|
T* z = output->bufferAsT<T>();
|
||||||
|
|
||||||
|
if (dimC == rank - 1 && input->ews() == 1 && output->ews() == 1 && input->ordering() == 'c' && output->ordering() == 'c') {
|
||||||
|
|
||||||
|
auto func = PRAGMA_THREADS_FOR{
|
||||||
|
for (auto i = start; i < stop; i += increment) {
|
||||||
|
op(x[i], x[i + 1], x[i + 2], z[i], z[i + 1], z[i + 2]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
samediff::Threads::parallel_for(func, 0, input->lengthOf(), 3);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), dimC);
|
||||||
|
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), dimC);
|
||||||
|
|
||||||
|
const Nd4jLong numOfTads = packX.numberOfTads();
|
||||||
|
const Nd4jLong xDimCstride = input->stridesOf()[dimC];
|
||||||
|
const Nd4jLong zDimCstride = output->stridesOf()[dimC];
|
||||||
|
|
||||||
|
auto func = PRAGMA_THREADS_FOR{
|
||||||
|
for (auto i = start; i < stop; i += increment) {
|
||||||
|
const T* xTad = x + packX.platformOffsets()[i];
|
||||||
|
T* zTad = z + packZ.platformOffsets()[i];
|
||||||
|
op(xTad[0], xTad[xDimCstride], xTad[2 * xDimCstride], zTad[0], zTad[zDimCstride], zTad[2 * zDimCstride]);
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
samediff::Threads::parallel_tad(func, 0, numOfTads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
FORCEINLINE static void hsvRgb(const NDArray* input, NDArray* output, const int dimC) {
|
||||||
|
auto op = nd4j::ops::helpers::hsvToRgb<T>;
|
||||||
|
return tripleTransformer<T>(input, output, dimC, op);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
FORCEINLINE static void rgbHsv(const NDArray* input, NDArray* output, const int dimC) {
|
||||||
|
auto op = nd4j::ops::helpers::rgbToHsv<T>;
|
||||||
|
return tripleTransformer<T>(input, output, dimC, op);
|
||||||
|
}
|
||||||
|
|
||||||
|
void transformHsvRgb(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
||||||
|
BUILD_SINGLE_SELECTOR(input->dataType(), hsvRgb, (input, output, dimC), FLOAT_TYPES);
|
||||||
|
}
|
||||||
|
|
||||||
|
void transformRgbHsv(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
||||||
|
BUILD_SINGLE_SELECTOR(input->dataType(), rgbHsv, (input, output, dimC), FLOAT_TYPES);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,139 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2019 Konduit K.K.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
******************************************************************************/
|
|
||||||
#include <ops/declarable/helpers/color_models_conv.h>
|
|
||||||
#include <ops/declarable/helpers/adjust_hue.h>
|
|
||||||
#include <ops/declarable/helpers/adjust_saturation.h>
|
|
||||||
#include <helpers/ConstantTadHelper.h>
|
|
||||||
#include <PointersManager.h>
|
|
||||||
|
|
||||||
namespace nd4j {
|
|
||||||
namespace ops {
|
|
||||||
namespace helpers {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
static void _CUDA_G rgbToHsvCuda(const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
|
||||||
void* vz, const Nd4jLong *zShapeInfo, const Nd4jLong* zTadOffsets,
|
|
||||||
const Nd4jLong numOfTads, const int dimC) {
|
|
||||||
|
|
||||||
const T* x = reinterpret_cast<const T*>(vx);
|
|
||||||
T* z = reinterpret_cast<T*>(vz);
|
|
||||||
|
|
||||||
__shared__ int rank;
|
|
||||||
__shared__ Nd4jLong xDimCstride, zDimCstride;
|
|
||||||
|
|
||||||
if (threadIdx.x == 0) {
|
|
||||||
rank = shape::rank(xShapeInfo);
|
|
||||||
xDimCstride = shape::stride(xShapeInfo)[dimC];
|
|
||||||
zDimCstride = shape::stride(zShapeInfo)[dimC];
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
||||||
|
|
||||||
for (Nd4jLong i = tid; i < numOfTads; i += gridDim.x * blockDim.x) {
|
|
||||||
const T* xTad = x + xTadOffsets[i];
|
|
||||||
T* zTad = z + zTadOffsets[i];
|
|
||||||
|
|
||||||
rgbToHsv<T>(xTad[0], xTad[xDimCstride], xTad[2 * xDimCstride], zTad[0], zTad[zDimCstride], zTad[2 * zDimCstride]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
static void _CUDA_G hsvToRgbCuda(const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
|
||||||
void* vz, const Nd4jLong *zShapeInfo, const Nd4jLong* zTadOffsets,
|
|
||||||
const Nd4jLong numOfTads, const int dimC) {
|
|
||||||
|
|
||||||
const T* x = reinterpret_cast<const T*>(vx);
|
|
||||||
T* z = reinterpret_cast<T*>(vz);
|
|
||||||
|
|
||||||
__shared__ int rank;
|
|
||||||
__shared__ Nd4jLong xDimCstride, zDimCstride;
|
|
||||||
|
|
||||||
if (threadIdx.x == 0) {
|
|
||||||
rank = shape::rank(xShapeInfo);
|
|
||||||
xDimCstride = shape::stride(xShapeInfo)[dimC];
|
|
||||||
zDimCstride = shape::stride(zShapeInfo)[dimC];
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
||||||
|
|
||||||
for (Nd4jLong i = tid; i < numOfTads; i += gridDim.x * blockDim.x) {
|
|
||||||
const T* xTad = x + xTadOffsets[i];
|
|
||||||
T* zTad = z + zTadOffsets[i];
|
|
||||||
|
|
||||||
hsvToRgb<T>(xTad[0], xTad[xDimCstride], xTad[2 * xDimCstride], zTad[0], zTad[zDimCstride], zTad[2 * zDimCstride]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////
|
|
||||||
template<typename T>
|
|
||||||
static _CUDA_H void hsvToRgbCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const cudaStream_t *stream,
|
|
||||||
const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
|
||||||
void* vz, const Nd4jLong* zShapeInfo, const Nd4jLong* zTadOffsets,
|
|
||||||
const Nd4jLong numOfTads, const int dimC) {
|
|
||||||
|
|
||||||
hsvToRgbCuda<T><<<blocksPerGrid, threadsPerBlock, 256, *stream>>>(vx, xShapeInfo, xTadOffsets, vz, zShapeInfo, zTadOffsets, numOfTads, dimC);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static _CUDA_H void rgbToHsvCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const cudaStream_t *stream,
|
|
||||||
const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
|
||||||
void* vz, const Nd4jLong* zShapeInfo, const Nd4jLong* zTadOffsets,
|
|
||||||
const Nd4jLong numOfTads, const int dimC) {
|
|
||||||
|
|
||||||
rgbToHsvCuda<T><<<blocksPerGrid, threadsPerBlock, 256, *stream>>>(vx, xShapeInfo, xTadOffsets, vz, zShapeInfo, zTadOffsets, numOfTads, dimC);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void transform_hsv_rgb(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
|
||||||
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), {dimC});
|
|
||||||
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), {dimC});
|
|
||||||
|
|
||||||
const Nd4jLong numOfTads = packX.numberOfTads();
|
|
||||||
|
|
||||||
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
||||||
const int blocksPerGrid = (numOfTads + threadsPerBlock - 1) / threadsPerBlock;
|
|
||||||
|
|
||||||
PointersManager manager(context, "hsv_to_rgb");
|
|
||||||
|
|
||||||
NDArray::prepareSpecialUse({output}, {input});
|
|
||||||
BUILD_SINGLE_SELECTOR(input->dataType(), hsvToRgbCudaLauncher, (blocksPerGrid, threadsPerBlock, context->getCudaStream(), input->getSpecialBuffer(), input->getSpecialShapeInfo(), packX.platformOffsets(), output->specialBuffer(), output->specialShapeInfo(), packZ.platformOffsets(), numOfTads, dimC), FLOAT_TYPES);
|
|
||||||
NDArray::registerSpecialUse({output}, {input});
|
|
||||||
|
|
||||||
manager.synchronize();
|
|
||||||
}
|
|
||||||
|
|
||||||
void transform_rgb_hsv(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
|
||||||
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), {dimC});
|
|
||||||
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), {dimC});
|
|
||||||
|
|
||||||
const Nd4jLong numOfTads = packX.numberOfTads();
|
|
||||||
|
|
||||||
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
||||||
const int blocksPerGrid = (numOfTads + threadsPerBlock - 1) / threadsPerBlock;
|
|
||||||
|
|
||||||
PointersManager manager(context, "rgb_to_hsv");
|
|
||||||
|
|
||||||
NDArray::prepareSpecialUse({output}, {input});
|
|
||||||
BUILD_SINGLE_SELECTOR(input->dataType(), rgbToHsvCudaLauncher, (blocksPerGrid, threadsPerBlock, context->getCudaStream(), input->getSpecialBuffer(), input->getSpecialShapeInfo(), packX.platformOffsets(), output->specialBuffer(), output->specialShapeInfo(), packZ.platformOffsets(), numOfTads, dimC), FLOAT_TYPES);
|
|
||||||
NDArray::registerSpecialUse({output}, {input});
|
|
||||||
|
|
||||||
manager.synchronize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,228 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2019 Konduit K.K.
|
||||||
|
*
|
||||||
|
* 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 Yurii Shyrma (iuriish@yahoo.com)
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <op_boilerplate.h>
|
||||||
|
#include <ops/declarable/helpers/imagesHelpers.h>
|
||||||
|
#include <helpers/ConstantTadHelper.h>
|
||||||
|
#include <ops/declarable/helpers/adjust_hue.h>
|
||||||
|
#include <PointersManager.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace nd4j {
|
||||||
|
namespace ops {
|
||||||
|
namespace helpers {
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
// for example xShapeInfo = {2,3,4}, zShapeInfo = {2,1,4}
|
||||||
|
template<typename T>
|
||||||
|
__global__ void rgbToGrsCuda(const void *vx, const Nd4jLong *xShapeInfo, void *vz, const Nd4jLong *zShapeInfo, const int dimC) {
|
||||||
|
|
||||||
|
const auto x = reinterpret_cast<const T*>(vx);
|
||||||
|
auto z = reinterpret_cast<T*>(vz);
|
||||||
|
|
||||||
|
__shared__ Nd4jLong zLen, *sharedMem;
|
||||||
|
__shared__ int rank; // xRank == zRank
|
||||||
|
|
||||||
|
if (threadIdx.x == 0) {
|
||||||
|
extern __shared__ unsigned char shmem[];
|
||||||
|
sharedMem = reinterpret_cast<Nd4jLong*>(shmem);
|
||||||
|
|
||||||
|
zLen = shape::length(zShapeInfo);
|
||||||
|
rank = shape::rank(zShapeInfo);
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
Nd4jLong* coords = sharedMem + threadIdx.x * rank;
|
||||||
|
|
||||||
|
for (Nd4jLong i = blockIdx.x * blockDim.x + threadIdx.x; i < zLen; i += gridDim.x * blockDim.x) {
|
||||||
|
|
||||||
|
if (dimC == (rank - 1) && 'c' == shape::order(xShapeInfo) && 1 == shape::elementWiseStride(xShapeInfo) && 'c' == shape::order(zShapeInfo) && 1 == shape::elementWiseStride(zShapeInfo)) {
|
||||||
|
const auto xStep = i*3;
|
||||||
|
z[i] = 0.2989f * x[xStep] + 0.5870f * x[xStep + 1] + 0.1140f * x[xStep + 2];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
shape::index2coords(i, zShapeInfo, coords);
|
||||||
|
|
||||||
|
const auto zOffset = shape::getOffset(zShapeInfo, coords);
|
||||||
|
const auto xOffset0 = shape::getOffset(xShapeInfo, coords);
|
||||||
|
const auto xOffset1 = xOffset0 + shape::stride(xShapeInfo)[dimC];
|
||||||
|
const auto xOffset2 = xOffset1 + shape::stride(xShapeInfo)[dimC];
|
||||||
|
|
||||||
|
z[zOffset] = 0.2989f * x[xOffset0] + 0.5870f * x[xOffset1] + 0.1140f * x[xOffset2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
template<typename T>
|
||||||
|
linkage void rgbToGrsCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const int sharedMem, const cudaStream_t *stream, const void *vx, const Nd4jLong *xShapeInfo, void *vz, const Nd4jLong *zShapeInfo, const int dimC) {
|
||||||
|
|
||||||
|
rgbToGrsCuda<T><<<blocksPerGrid, threadsPerBlock, sharedMem, *stream>>>(vx, xShapeInfo, vz, zShapeInfo, dimC);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
void transformRgbGrs(nd4j::LaunchContext* context, const NDArray& input, NDArray& output, const int dimC) {
|
||||||
|
|
||||||
|
PointersManager manager(context, "rgbToGrs");
|
||||||
|
|
||||||
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
||||||
|
const int blocksPerGrid = (input.lengthOf() + threadsPerBlock - 1) / threadsPerBlock;
|
||||||
|
const int sharedMem = input.rankOf() * sizeof(Nd4jLong) * threadsPerBlock + 128;
|
||||||
|
|
||||||
|
NDArray::prepareSpecialUse({&output}, {&input});
|
||||||
|
BUILD_SINGLE_SELECTOR(input.dataType(), rgbToGrsCudaLauncher, (blocksPerGrid, threadsPerBlock, sharedMem, context->getCudaStream(), input.getSpecialBuffer(), input.getSpecialShapeInfo(), output.getSpecialBuffer(), output.getSpecialShapeInfo(), dimC), NUMERIC_TYPES);
|
||||||
|
NDArray::registerSpecialUse({&output}, {&input});
|
||||||
|
|
||||||
|
manager.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
static void _CUDA_G rgbToHsvCuda(const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
||||||
|
void* vz, const Nd4jLong *zShapeInfo, const Nd4jLong* zTadOffsets,
|
||||||
|
const Nd4jLong numOfTads, const int dimC) {
|
||||||
|
|
||||||
|
const T* x = reinterpret_cast<const T*>(vx);
|
||||||
|
T* z = reinterpret_cast<T*>(vz);
|
||||||
|
|
||||||
|
__shared__ int rank;
|
||||||
|
__shared__ Nd4jLong xDimCstride, zDimCstride;
|
||||||
|
|
||||||
|
if (threadIdx.x == 0) {
|
||||||
|
rank = shape::rank(xShapeInfo);
|
||||||
|
xDimCstride = shape::stride(xShapeInfo)[dimC];
|
||||||
|
zDimCstride = shape::stride(zShapeInfo)[dimC];
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
|
||||||
|
for (Nd4jLong i = tid; i < numOfTads; i += gridDim.x * blockDim.x) {
|
||||||
|
const T* xTad = x + xTadOffsets[i];
|
||||||
|
T* zTad = z + zTadOffsets[i];
|
||||||
|
|
||||||
|
rgbToHsv<T>(xTad[0], xTad[xDimCstride], xTad[2 * xDimCstride], zTad[0], zTad[zDimCstride], zTad[2 * zDimCstride]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
static void _CUDA_G hsvToRgbCuda(const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
||||||
|
void* vz, const Nd4jLong *zShapeInfo, const Nd4jLong* zTadOffsets,
|
||||||
|
const Nd4jLong numOfTads, const int dimC) {
|
||||||
|
|
||||||
|
const T* x = reinterpret_cast<const T*>(vx);
|
||||||
|
T* z = reinterpret_cast<T*>(vz);
|
||||||
|
|
||||||
|
__shared__ int rank;
|
||||||
|
__shared__ Nd4jLong xDimCstride, zDimCstride;
|
||||||
|
|
||||||
|
if (threadIdx.x == 0) {
|
||||||
|
rank = shape::rank(xShapeInfo);
|
||||||
|
xDimCstride = shape::stride(xShapeInfo)[dimC];
|
||||||
|
zDimCstride = shape::stride(zShapeInfo)[dimC];
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
|
||||||
|
for (Nd4jLong i = tid; i < numOfTads; i += gridDim.x * blockDim.x) {
|
||||||
|
const T* xTad = x + xTadOffsets[i];
|
||||||
|
T* zTad = z + zTadOffsets[i];
|
||||||
|
|
||||||
|
hsvToRgb<T>(xTad[0], xTad[xDimCstride], xTad[2 * xDimCstride], zTad[0], zTad[zDimCstride], zTad[2 * zDimCstride]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
template<typename T>
|
||||||
|
static _CUDA_H void hsvToRgbCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const cudaStream_t *stream,
|
||||||
|
const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
||||||
|
void* vz, const Nd4jLong* zShapeInfo, const Nd4jLong* zTadOffsets,
|
||||||
|
const Nd4jLong numOfTads, const int dimC) {
|
||||||
|
|
||||||
|
hsvToRgbCuda<T><<<blocksPerGrid, threadsPerBlock, 256, *stream>>>(vx, xShapeInfo, xTadOffsets, vz, zShapeInfo, zTadOffsets, numOfTads, dimC);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static _CUDA_H void rgbToHsvCudaLauncher(const int blocksPerGrid, const int threadsPerBlock, const cudaStream_t *stream,
|
||||||
|
const void* vx, const Nd4jLong* xShapeInfo, const Nd4jLong* xTadOffsets,
|
||||||
|
void* vz, const Nd4jLong* zShapeInfo, const Nd4jLong* zTadOffsets,
|
||||||
|
const Nd4jLong numOfTads, const int dimC) {
|
||||||
|
|
||||||
|
rgbToHsvCuda<T><<<blocksPerGrid, threadsPerBlock, 256, *stream>>>(vx, xShapeInfo, xTadOffsets, vz, zShapeInfo, zTadOffsets, numOfTads, dimC);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
void transformHsvRgb(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
||||||
|
|
||||||
|
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), {dimC});
|
||||||
|
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), {dimC});
|
||||||
|
|
||||||
|
const Nd4jLong numOfTads = packX.numberOfTads();
|
||||||
|
|
||||||
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
||||||
|
const int blocksPerGrid = (numOfTads + threadsPerBlock - 1) / threadsPerBlock;
|
||||||
|
|
||||||
|
PointersManager manager(context, "hsv_to_rgb");
|
||||||
|
|
||||||
|
NDArray::prepareSpecialUse({output}, {input});
|
||||||
|
BUILD_SINGLE_SELECTOR(input->dataType(), hsvToRgbCudaLauncher, (blocksPerGrid, threadsPerBlock, context->getCudaStream(), input->getSpecialBuffer(), input->getSpecialShapeInfo(), packX.platformOffsets(), output->specialBuffer(), output->specialShapeInfo(), packZ.platformOffsets(), numOfTads, dimC), FLOAT_TYPES);
|
||||||
|
NDArray::registerSpecialUse({output}, {input});
|
||||||
|
|
||||||
|
manager.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////
|
||||||
|
void transformRgbHsv(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC) {
|
||||||
|
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), {dimC});
|
||||||
|
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), {dimC});
|
||||||
|
|
||||||
|
const Nd4jLong numOfTads = packX.numberOfTads();
|
||||||
|
|
||||||
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
||||||
|
const int blocksPerGrid = (numOfTads + threadsPerBlock - 1) / threadsPerBlock;
|
||||||
|
|
||||||
|
PointersManager manager(context, "rgb_to_hsv");
|
||||||
|
|
||||||
|
NDArray::prepareSpecialUse({output}, {input});
|
||||||
|
BUILD_SINGLE_SELECTOR(input->dataType(), rgbToHsvCudaLauncher, (blocksPerGrid, threadsPerBlock, context->getCudaStream(), input->getSpecialBuffer(), input->getSpecialShapeInfo(), packX.platformOffsets(), output->specialBuffer(), output->specialShapeInfo(), packZ.platformOffsets(), numOfTads, dimC), FLOAT_TYPES);
|
||||||
|
NDArray::registerSpecialUse({output}, {input});
|
||||||
|
|
||||||
|
manager.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,17 +14,30 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
|
//
|
||||||
|
// @author Oleh Semeniv (oleg.semeniv@gmail.com)
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// @author Adel Rauf (rauf@konduit.ai)
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LIBND4J_HELPERS_IMAGES_H
|
||||||
|
#define LIBND4J_HELPERS_IMAGES_H
|
||||||
|
|
||||||
#include <op_boilerplate.h>
|
#include <op_boilerplate.h>
|
||||||
#include <templatemath.h>
|
#include <templatemath.h>
|
||||||
#include <NDArray.h>
|
#include <NDArray.h>
|
||||||
|
|
||||||
namespace nd4j {
|
namespace nd4j {
|
||||||
namespace ops {
|
namespace ops {
|
||||||
namespace helpers {
|
namespace helpers {
|
||||||
|
|
||||||
void transform_hsv_rgb(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC);
|
void transformRgbGrs(nd4j::LaunchContext* context, const NDArray& input, NDArray& output, const int dimC);
|
||||||
void transform_rgb_hsv(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC);
|
void transformHsvRgb(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC);
|
||||||
|
void transformRgbHsv(nd4j::LaunchContext* context, const NDArray* input, NDArray* output, const int dimC);
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -919,3 +919,150 @@ TEST_F(DeclarableOpsTests15, test_empty_decreasing_1) {
|
||||||
|
|
||||||
ASSERT_EQ(true, z.e<bool>(0));
|
ASSERT_EQ(true, z.e<bool>(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_1) {
|
||||||
|
// rank 1
|
||||||
|
NDArray rgbs('c', { 3 }, { 10, 50, 200 }, nd4j::DataType::INT32);
|
||||||
|
NDArray expected('c', { 1 }, { 55 }, nd4j::DataType::INT32);
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({&rgbs}, {}, {});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_2) {
|
||||||
|
// rank 1
|
||||||
|
auto rgbs = NDArrayFactory::create<int>('f', { 3 }, { 1, 120, -25 });
|
||||||
|
auto expected = NDArrayFactory::create<int>('f', { 1 }, { 67 });
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_3) {
|
||||||
|
// rank 2
|
||||||
|
NDArray rgbs('c', { 4, 3 }, { -94, 99, 97, 90, 114, 101, 111, 96, 105, 100, 103, 102 }, nd4j::DataType::INT32);
|
||||||
|
NDArray expected('c', { 4, 1 }, { 41, 105, 101, 101 }, nd4j::DataType::INT32);
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_4) {
|
||||||
|
|
||||||
|
NDArray rgbs('c', { 3, 2 }, {14, 99, 207, 10, 114, 201 }, nd4j::DataType::INT32);
|
||||||
|
|
||||||
|
rgbs.permutei({1,0});
|
||||||
|
NDArray expected('c', { 2, 1 }, { 138, 58 }, nd4j::DataType::INT32);
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_5) {
|
||||||
|
// rank 2
|
||||||
|
NDArray rgbs('c', { 3, 4 }, { -94, 99, 97, 90, 114, 101, 111, 96, 105, 100, 103, 102 }, nd4j::DataType::INT32);
|
||||||
|
NDArray expected('c', { 1, 4 }, { 50, 100, 105, 94 }, nd4j::DataType::INT32);
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {0});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_6) {
|
||||||
|
// rank 3
|
||||||
|
auto rgbs = NDArrayFactory::create<float>('c', { 5,4,3 }, {1.7750e+01f, -7.1062e+01f, -1.0019e+02f,-2.3406e+01f, 5.2094e+01f, 9.5438e+01f, -6.7461e+00f, 3.8562e+01f, 6.5078e+00f,3.3562e+01f, -5.8844e+01f, 2.2750e+01f, -1.0477e+01f, 7.7344e+00f, 9.5469e+00f,2.1391e+01f, -8.5312e+01f, 7.5830e-01f,2.3125e+01f, 1.8145e+00f, 1.4602e+01f,-4.5859e+00f, 3.9344e+01f, 1.1617e+01f,-8.6562e+01f, 1.0038e+02f, 6.7938e+01f,5.9961e+00f, 6.7812e+01f, 2.9734e+01f,2.9609e+01f, -6.1438e+01f, 1.7750e+01f,6.8562e+01f, -7.4414e+00f, 3.9656e+01f,1.1641e+01f, -2.7516e+01f, 6.7562e+01f,7.8438e+01f, 5.4883e+00f, 2.9438e+01f,-3.1344e+01f, 6.5125e+01f, 1.2695e+01f,4.0531e+01f, -6.1211e+00f, 6.2219e+01f,4.6812e+01f, 5.2250e+01f, -1.1414e+01f,1.5404e-02f, 2.9938e+01f, 5.6719e+00f,-2.0125e+01f, 2.1531e+01f, 6.2500e+01f,7.2188e+01f, 9.3750e+00f, -4.8125e+01f});
|
||||||
|
auto expected = NDArrayFactory::create<float>('c', { 5,4,1 }, {-47.82958221f, 34.46305847f, 21.36137581f, -21.91625023f,2.49686432f, -43.59792709f, 9.64180183f, 23.04854202f,40.7946167f, 44.98754883f, -25.19047546f, 20.64586449f,-4.97033119f, 30.0226841f, 30.30688286f, 15.61459541f,43.36166f, 18.22480774f, 13.74833488f, 21.59387016f});
|
||||||
|
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_7) {
|
||||||
|
// rank 3
|
||||||
|
auto rgbs = NDArrayFactory::create<float>('c', { 5,3,4 }, { 1.7750e+01f, -7.1062e+01f, -1.0019e+02f,-2.3406e+01f, 5.2094e+01f, 9.5438e+01f, -6.7461e+00f, 3.8562e+01f, 6.5078e+00f,3.3562e+01f, -5.8844e+01f, 2.2750e+01f, -1.0477e+01f, 7.7344e+00f, 9.5469e+00f,2.1391e+01f, -8.5312e+01f, 7.5830e-01f,2.3125e+01f, 1.8145e+00f, 1.4602e+01f,-4.5859e+00f, 3.9344e+01f, 1.1617e+01f,-8.6562e+01f, 1.0038e+02f, 6.7938e+01f,5.9961e+00f, 6.7812e+01f, 2.9734e+01f,2.9609e+01f, -6.1438e+01f, 1.7750e+01f,6.8562e+01f, -7.4414e+00f, 3.9656e+01f,1.1641e+01f, -2.7516e+01f, 6.7562e+01f,7.8438e+01f, 5.4883e+00f, 2.9438e+01f,-3.1344e+01f, 6.5125e+01f, 1.2695e+01f,4.0531e+01f, -6.1211e+00f, 6.2219e+01f,4.6812e+01f, 5.2250e+01f, -1.1414e+01f,1.5404e-02f, 2.9938e+01f, 5.6719e+00f,-2.0125e+01f, 2.1531e+01f, 6.2500e+01f,7.2188e+01f, 9.3750e+00f, -4.8125e+01f});
|
||||||
|
auto expected = NDArrayFactory::create<float>('c', { 5,1,4 }, { 36.626545, 38.607746, -40.614971, 18.233341, -51.545094,2.234142, 20.913160, 8.783220, 15.955761, 55.273506, 36.838833, -29.751089, 8.148357, 13.676106, 1.097548, 68.766457, 38.690712, 27.176361, -14.156269, 7.157052 });
|
||||||
|
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {1});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_8) {
|
||||||
|
// rank 3
|
||||||
|
auto rgbs = NDArrayFactory::create<float>('c', { 3,5,4 }, {1.7750e+01f, -7.1062e+01f, -1.0019e+02f,-2.3406e+01f, 5.2094e+01f, 9.5438e+01f, -6.7461e+00f, 3.8562e+01f, 6.5078e+00f,3.3562e+01f, -5.8844e+01f, 2.2750e+01f, -1.0477e+01f, 7.7344e+00f, 9.5469e+00f,2.1391e+01f, -8.5312e+01f, 7.5830e-01f,2.3125e+01f, 1.8145e+00f, 1.4602e+01f,-4.5859e+00f, 3.9344e+01f, 1.1617e+01f,-8.6562e+01f, 1.0038e+02f, 6.7938e+01f,5.9961e+00f, 6.7812e+01f, 2.9734e+01f,2.9609e+01f, -6.1438e+01f, 1.7750e+01f,6.8562e+01f, -7.4414e+00f, 3.9656e+01f,1.1641e+01f, -2.7516e+01f, 6.7562e+01f,7.8438e+01f, 5.4883e+00f, 2.9438e+01f,-3.1344e+01f, 6.5125e+01f, 1.2695e+01f,4.0531e+01f, -6.1211e+00f, 6.2219e+01f,4.6812e+01f, 5.2250e+01f, -1.1414e+01f,1.5404e-02f, 2.9938e+01f, 5.6719e+00f,-2.0125e+01f, 2.1531e+01f, 6.2500e+01f,7.2188e+01f, 9.3750e+00f, -4.8125e+01f});
|
||||||
|
try {
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {});
|
||||||
|
ASSERT_EQ(Status::THROW(), result->status());
|
||||||
|
delete result;
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
nd4j_printf("Error should be here `%s'. It's OK.\n", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
TEST_F(DeclarableOpsTests15, test_rgb_to_grs_9) {
|
||||||
|
// rank 3
|
||||||
|
auto rgbs = NDArrayFactory::create<float>('f', { 2, 2, 3 }, { 1.7750e+01f,-7.1062e+01f, -1.0019e+02f, -2.3406e+01f,5.2094e+01f,9.5438e+01f, -6.7461e+00f,3.8562e+01f, 6.5078e+00f, 3.3562e+01f,-5.8844e+01f,2.2750e+01f});
|
||||||
|
auto expected = NDArrayFactory::create<float>('f', { 2,2,1 }, { 36.626545f, 38.607746f, -40.614971f, 18.233341f });
|
||||||
|
|
||||||
|
nd4j::ops::rgb_to_grs op;
|
||||||
|
auto result = op.execute({ &rgbs }, {}, {});
|
||||||
|
auto output = result->at(0);
|
||||||
|
|
||||||
|
ASSERT_EQ(Status::OK(), result->status());
|
||||||
|
ASSERT_TRUE(expected.isSameShape(output));
|
||||||
|
ASSERT_TRUE(expected.equalsTo(output));
|
||||||
|
|
||||||
|
delete result;
|
||||||
|
}
|
|
@ -239,7 +239,6 @@ TEST_F(DeclarableOpsTests16, test_reverse_1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_F(DeclarableOpsTests16, test_rgb_to_hsv_1) {
|
TEST_F(DeclarableOpsTests16, test_rgb_to_hsv_1) {
|
||||||
/*
|
/*
|
||||||
test case generated by python colorsys and scaled to suit our needs
|
test case generated by python colorsys and scaled to suit our needs
|
||||||
|
|
Loading…
Reference in New Issue