More compilation units (#246)

* weird edge case

Signed-off-by: raver119 <raver119@gmail.com>

* weird edge case

Signed-off-by: raver119 <raver119@gmail.com>

* get rid of it

Signed-off-by: raver119 <raver119@gmail.com>

* crop and resize reorganized

Signed-off-by: raver119 <raver119@gmail.com>

* restore test

Signed-off-by: raver119 <raver119@gmail.com>

* remove unwanted unit refs in cmale

Signed-off-by: raver119 <raver119@gmail.com>
master
raver119 2020-02-17 10:23:05 +03:00 committed by GitHub
parent 011c272fde
commit f9d51b7278
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 641 additions and 147 deletions

View File

@ -23,7 +23,8 @@
//#include <ops/declarable/headers/parity_ops.h>
#include <ops/declarable/CustomOperations.h>
#include <ops/declarable/helpers/image_resize.h>
#include <ops/declarable/helpers/crop_and_resize.h>
namespace nd4j {
namespace ops {
CUSTOM_OP_IMPL(crop_and_resize, 4, 1, false, 0, 0) {

View File

@ -374,6 +374,28 @@ namespace nd4j {
template <typename X, typename Y>
static void addBias_(const NDArray& input, const NDArray& bias, NDArray& output, const bool isNCHW) {
/*
if (input.rankOf() == 2 && bias.rankOf() == 1 && input.sizeAt(1) == bias.sizeAt(0) && input.ordering() == 'c') {
int rows = input.sizeAt(0);
int biasLen = bias.lengthOf();
auto inB = input.bufferAsT<X>();
auto bB = bias.bufferAsT<Y>();
auto outB = output.bufferAsT<X>();
for (int e = 0; e < rows; e++) {
auto row = inB + (e * biasLen);
auto out = outB + (e * biasLen);
for (int t = 0; t < biasLen; t++) {
out[t] = row[t] + bB[t];
}
}
return;
}
*/
Nd4jLong* x_shapeInfo = input.getShapeInfo();
Nd4jLong* z_shapeInfo = output.getShapeInfo();
X* x = input.bufferAsT<X>();

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_0, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_1, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_2, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_3, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_4, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_5, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_6, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_7, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_8, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include "../crop_and_resize.hpp"
namespace nd4j {
namespace ops {
namespace helpers {
BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_9, FLOAT_TYPES, INTEGER_TYPES);
}
}
}

View File

@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019-2020 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
******************************************************************************/
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://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.
==============================================================================*/
//
// @author sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include <execution/Threads.h>
namespace nd4j {
namespace ops {
namespace helpers {
// ------------------------------------------------------------------------------------------------------------------ //
// ------------------------------------------------------------------------------------------------------------------ //
// crop and resize helper functor:
// \@param context - launch context for operation
// \@param images - batch of images (4D tensor) with shape {batch, width, height, channels} with given type
// \@param boxes - float boxes for crop
// \@param indices - integer boxes indices for crop
// \@param cropSize - integer size (newWidth, newHeight)
// \@param method - one of bilinear (0) or nearest neighbour (1) interpolation algorithm
// \@param extrapolationVal - radix to increase/decrease image
// \@param crops - output image batch (4D with given type)
//
void
cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const *images, NDArray const *boxes,
NDArray const *indices, NDArray const *cropSize,
int method, double extrapolationVal, NDArray *crops) {
BUILD_TRIPLE_SELECTOR(images->dataType(), boxes->dataType(), indices->dataType(), cropAndResizeFunctor_, (images, boxes, indices, cropSize, method, extrapolationVal, crops), NUMERIC_TYPES, FLOAT_TYPES, INTEGER_TYPES);
}
}
}
}

View File

@ -0,0 +1,123 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#include <ops/declarable/helpers/crop_and_resize.h>
#include <execution/Threads.h>
namespace nd4j {
namespace ops {
namespace helpers {
template<typename T, typename F, typename I>
void cropAndResizeFunctor_(NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops) {
const int batchSize = images->sizeAt(0);
const int imageHeight = images->sizeAt(1);
const int imageWidth = images->sizeAt(2);
const int numBoxes = crops->sizeAt(0);
const int cropHeight = crops->sizeAt(1);
const int cropWidth = crops->sizeAt(2);
const int depth = crops->sizeAt(3);
for (auto b = 0; b < numBoxes; ++b) {
T y1 = boxes->t<F>(b, 0);
T x1 = boxes->t<F>(b, 1);
T y2 = boxes->t<F>(b, 2);
T x2 = boxes->t<F>(b, 3);
int bIn = indices->e<int>(b);
if (bIn >= batchSize) {
continue;
}
T heightScale = (cropHeight > 1) ? (y2 - y1) * (imageHeight - 1) / (cropHeight - 1) : T(0);
T widthScale = (cropWidth > 1) ? (x2 - x1) * (imageWidth - 1) / (cropWidth - 1) : T(0);
auto func = PRAGMA_THREADS_FOR {
for (auto y = start; y < stop; y += increment) {
const float inY = (cropHeight > 1)
? y1 * (imageHeight - 1) + y * heightScale
: 0.5 * (y1 + y2) * (imageHeight - 1);
if (inY < 0 || inY > imageHeight - 1) {
for (auto x = 0; x < cropWidth; ++x) {
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, extrapolationVal);
}
}
continue;
}
if (method == 0 /* bilinear */) {
const int topYIndex = nd4j::math::p_floor(inY);
const int bottomYIndex = nd4j::math::p_ceil(inY);
const float y_lerp = inY - topYIndex;
for (auto x = 0; x < cropWidth; ++x) {
const float in_x = (cropWidth > 1)
? x1 * (imageWidth - 1) + x * widthScale
: 0.5 * (x1 + x2) * (imageWidth - 1);
if (in_x < 0 || in_x > imageWidth - 1) {
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, extrapolationVal);
}
continue;
}
int left_x_index = math::p_floor(in_x);
int right_x_index = math::p_ceil(in_x);
T x_lerp = in_x - left_x_index;
for (auto d = 0; d < depth; ++d) {
const float topLeft(images->e<float>(bIn, topYIndex, left_x_index, d));
const float topRight(images->e<float>(bIn, topYIndex, right_x_index, d));
const float bottomLeft(images->e<float>(bIn, bottomYIndex, left_x_index, d));
const float bottomRight(images->e<float>(bIn, bottomYIndex, right_x_index, d));
const float top = topLeft + (topRight - topLeft) * x_lerp;
const float bottom = bottomLeft + (bottomRight - bottomLeft) * x_lerp;
crops->p(b, y, x, d, top + (bottom - top) * y_lerp);
}
}
} else { // method is "nearest neighbor"
for (auto x = 0; x < cropWidth; ++x) {
const float inX = (cropWidth > 1)
? x1 * (imageWidth - 1) + x * widthScale
: 0.5 * (x1 + x2) * (imageWidth - 1);
if (inX < 0 || inX > imageWidth - 1) {
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, extrapolationVal);
}
continue;
}
const int closestXIndex = roundf(inX);
const int closestYIndex = roundf(inY);
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, images->e<T>(bIn, closestYIndex, closestXIndex, d));
}
}
}
}
};
samediff::Threads::parallel_for(func, 0, cropHeight);
}
}
}
}
}

View File

@ -366,102 +366,6 @@ namespace helpers {
BUILD_SINGLE_SELECTOR(images->dataType(), return resizeNeighborFunctor_, (images, width, height, alignCorners, halfPixelCenter, output), LIBND4J_TYPES);
}
template<typename T, typename F, typename I>
static void cropAndResizeFunctor_(NDArray const *images, NDArray const *boxes, NDArray const *indices,
NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops) {
const int batchSize = images->sizeAt(0);
const int imageHeight = images->sizeAt(1);
const int imageWidth = images->sizeAt(2);
const int numBoxes = crops->sizeAt(0);
const int cropHeight = crops->sizeAt(1);
const int cropWidth = crops->sizeAt(2);
const int depth = crops->sizeAt(3);
for (auto b = 0; b < numBoxes; ++b) {
T y1 = boxes->t<F>(b, 0);
T x1 = boxes->t<F>(b, 1);
T y2 = boxes->t<F>(b, 2);
T x2 = boxes->t<F>(b, 3);
int bIn = indices->e<int>(b);
if (bIn >= batchSize) {
continue;
}
T heightScale = (cropHeight > 1) ? (y2 - y1) * (imageHeight - 1) / (cropHeight - 1) : T(0);
T widthScale = (cropWidth > 1) ? (x2 - x1) * (imageWidth - 1) / (cropWidth - 1) : T(0);
auto func = PRAGMA_THREADS_FOR {
for (auto y = start; y < stop; y += increment) {
const float inY = (cropHeight > 1)
? y1 * (imageHeight - 1) + y * heightScale
: 0.5 * (y1 + y2) * (imageHeight - 1);
if (inY < 0 || inY > imageHeight - 1) {
for (auto x = 0; x < cropWidth; ++x) {
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, extrapolationVal);
}
}
continue;
}
if (method == 0 /* bilinear */) {
const int topYIndex = nd4j::math::p_floor(inY);
const int bottomYIndex = nd4j::math::p_ceil(inY);
const float y_lerp = inY - topYIndex;
for (auto x = 0; x < cropWidth; ++x) {
const float in_x = (cropWidth > 1)
? x1 * (imageWidth - 1) + x * widthScale
: 0.5 * (x1 + x2) * (imageWidth - 1);
if (in_x < 0 || in_x > imageWidth - 1) {
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, extrapolationVal);
}
continue;
}
int left_x_index = math::p_floor(in_x);
int right_x_index = math::p_ceil(in_x);
T x_lerp = in_x - left_x_index;
for (auto d = 0; d < depth; ++d) {
const float topLeft(images->e<float>(bIn, topYIndex, left_x_index, d));
const float topRight(images->e<float>(bIn, topYIndex, right_x_index, d));
const float bottomLeft(images->e<float>(bIn, bottomYIndex, left_x_index, d));
const float bottomRight(images->e<float>(bIn, bottomYIndex, right_x_index, d));
const float top = topLeft + (topRight - topLeft) * x_lerp;
const float bottom = bottomLeft + (bottomRight - bottomLeft) * x_lerp;
crops->p(b, y, x, d, top + (bottom - top) * y_lerp);
}
}
} else { // method is "nearest neighbor"
for (auto x = 0; x < cropWidth; ++x) {
const float inX = (cropWidth > 1)
? x1 * (imageWidth - 1) + x * widthScale
: 0.5 * (x1 + x2) * (imageWidth - 1);
if (inX < 0 || inX > imageWidth - 1) {
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, extrapolationVal);
}
continue;
}
const int closestXIndex = roundf(inX);
const int closestYIndex = roundf(inY);
for (auto d = 0; d < depth; ++d) {
crops->p(b, y, x, d, images->e<T>(bIn, closestYIndex, closestXIndex, d));
}
}
}
}
};
samediff::Threads::parallel_for(func, 0, cropHeight);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------------------------------------------------ //
// Bicubic interpolation
@ -1105,25 +1009,7 @@ namespace helpers {
return ND4J_STATUS_OK;
}
// ------------------------------------------------------------------------------------------------------------------ //
// ------------------------------------------------------------------------------------------------------------------ //
// crop and resize helper functor:
// \@param context - launch context for operation
// \@param images - batch of images (4D tensor) with shape {batch, width, height, channels} with given type
// \@param boxes - float boxes for crop
// \@param indices - integer boxes indices for crop
// \@param cropSize - integer size (newWidth, newHeight)
// \@param method - one of bilinear (0) or nearest neighbour (1) interpolation algorithm
// \@param extrapolationVal - radix to increase/decrease image
// \@param crops - output image batch (4D with given type)
//
void
cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const *images, NDArray const *boxes,
NDArray const *indices, NDArray const *cropSize,
int method, double extrapolationVal, NDArray *crops) {
BUILD_TRIPLE_SELECTOR(images->dataType(), boxes->dataType(), indices->dataType(), cropAndResizeFunctor_,
(images, boxes, indices, cropSize, method, extrapolationVal, crops), NUMERIC_TYPES, FLOAT_TYPES, INTEGER_TYPES);
}
}
}
}

View File

@ -0,0 +1,40 @@
/*******************************************************************************
* 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 sgazeos@gmail.com
//
#ifndef SD_CROP_AND_RESIZE_H
#define SD_CROP_AND_RESIZE_H
#include <op_boilerplate.h>
#include <NDArray.h>
namespace nd4j {
namespace ops {
namespace helpers {
template<typename T, typename F, typename I>
void cropAndResizeFunctor_(NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops);
void cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const* images, NDArray const* boxes, NDArray const* indices, NDArray const* cropSize, int method, double extrapolationVal, NDArray* crops);
}
}
}
#endif //SD_CROP_AND_RESIZE_H

View File

@ -1326,7 +1326,7 @@ namespace helpers {
// crops - output (4D tensor - [batch, outWidth, outHeight, pixels])
//
template <typename T, typename Z, typename I>
static void cropAndResizeFunctor_(nd4j::LaunchContext* context, NDArray const *images, NDArray const *boxes, NDArray const *indices,
void cropAndResizeFunctor_(nd4j::LaunchContext* context, NDArray const *images, NDArray const *boxes, NDArray const *indices,
NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops) {
const int batchSize = images->sizeAt(0);
const int imageHeight = images->sizeAt(1);

View File

@ -50,9 +50,6 @@ namespace helpers {
int resizeFunctor(nd4j::LaunchContext * context, NDArray const* image, int const width, int const height,
ImageResizeMethods method, bool preserveAspectRatio, bool antialias, NDArray* output);
void cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const* images, NDArray const* boxes,
NDArray const* indices, NDArray const* cropSize, int method, double extrapolationVal, NDArray* crops);
}
}
}

View File

@ -159,6 +159,38 @@
(nd4j::DataType::INT64, Nd4jLong), \
(nd4j::DataType::BFLOAT16, bfloat16)
#define NUMERIC_TYPES_0 \
(nd4j::DataType::HALF, float16)
#define NUMERIC_TYPES_1 \
(nd4j::DataType::FLOAT32, float)
#define NUMERIC_TYPES_2 \
(nd4j::DataType::DOUBLE, double)
#define NUMERIC_TYPES_3 \
(nd4j::DataType::INT8, int8_t), \
(nd4j::DataType::BFLOAT16, bfloat16)
#define NUMERIC_TYPES_4 \
(nd4j::DataType::UINT8, uint8_t)
#define NUMERIC_TYPES_5 \
(nd4j::DataType::UINT16, uint16_t)
#define NUMERIC_TYPES_6 \
(nd4j::DataType::UINT32, uint32_t)
#define NUMERIC_TYPES_7 \
(nd4j::DataType::UINT64, uint64_t)
#define NUMERIC_TYPES_8 \
(nd4j::DataType::INT16, int16_t)
#define NUMERIC_TYPES_9 \
(nd4j::DataType::INT32, int32_t), \
(nd4j::DataType::INT64, Nd4jLong)
#define GENERIC_NUMERIC_TYPES \
(nd4j::DataType::HALF, float16), \

View File

@ -65,7 +65,32 @@ TEST_F(PlaygroundTests, test_avx) {
nd4j_printf("Optimal level: %i; Binary level: %i;\n", ::optimalLevel(), ::binaryLevel());
}
/*
TEST_F(PlaygroundTests, test_biasAdd_1) {
auto x = NDArrayFactory::create<float>('c', {512, 3072});
auto y = NDArrayFactory::create<float>('c', {3072});
std::vector<Nd4jLong> values;
nd4j::ops::biasadd op;
for (int e = 0; e < 100; e++) {
auto timeStart = std::chrono::system_clock::now();
op.execute({&x, &y}, {&x});
auto timeEnd = std::chrono::system_clock::now();
auto outerTime = std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeStart).count();
values.emplace_back(outerTime);
}
std::sort(values.begin(), values.end());
nd4j_printf("Time: %lld us;\n", values[values.size() / 2]);
}
TEST_F(PlaygroundTests, test_bert_1) {
// this test will run ONLY if this model exists
if (nd4j::graph::getFileSize("/home/raver119/Downloads/Bert_minimal_model/bert_minimal_model.fb") < 0)
@ -86,15 +111,15 @@ TEST_F(PlaygroundTests, test_bert_1) {
graph->getVariableSpace()->putVariable(86,0, u);
graph->getVariableSpace()->putVariable(87,0, v);
/*
// validating graph now
// auto status = GraphExecutioner::execute(graph);
// ASSERT_EQ(Status::OK(), status);
// ASSERT_TRUE(graph->getVariableSpace()->hasVariable(198));
// auto array = graph->getVariableSpace()->getVariable(198)->getNDArray();
// ASSERT_EQ(z, *array);
auto status = GraphExecutioner::execute(graph);
ASSERT_EQ(Status::OK(), status);
ASSERT_TRUE(graph->getVariableSpace()->hasVariable(198));
auto array = graph->getVariableSpace()->getVariable(198)->getNDArray();
ASSERT_EQ(z, *array);
*/
nd4j::Environment::getInstance()->setProfiling(true);
auto profile = GraphProfilingHelper::profile(graph, 1);
@ -104,43 +129,47 @@ TEST_F(PlaygroundTests, test_bert_1) {
nd4j::Environment::getInstance()->setProfiling(false);
delete profile;
/*
std::vector<Nd4jLong> values;
// std::vector<Nd4jLong> values;
for (int e = 0; e < 1; e++) {
auto timeStart = std::chrono::system_clock::now();
// for (int e = 0; e < 1; e++) {
// auto timeStart = std::chrono::system_clock::now();
GraphExecutioner::execute(graph);
// GraphExecutioner::execute(graph);
auto timeEnd = std::chrono::system_clock::now();
auto outerTime = std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeStart).count();
values.emplace_back(outerTime);
}
// auto timeEnd = std::chrono::system_clock::now();
// auto outerTime = std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeStart).count();
// values.emplace_back(outerTime);
// }
// std::sort(values.begin(), values.end());
// nd4j_printf("Time: %lld us;\n", values[values.size() / 2]);
std::sort(values.begin(), values.end());
nd4j_printf("Time: %lld us;\n", values[values.size() / 2]);
*/
delete graph;
}
/*
TEST_F(PlaygroundTests, test_broadcast_1) {
int pool = 10;
int pool = 1000;
std::vector<NDArray*> aX(pool);
std::vector<NDArray*> aY(pool);
std::vector<NDArray*> aZ(pool);
for (int e = 0; e < pool; e++) {
aX[e] = NDArrayFactory::create_<float>('c', {64, 128, 1});
aY[e] = NDArrayFactory::create_<float>('c', {768});
aZ[e] = NDArrayFactory::create_<float>('c', {64, 128, 768});
aX[e] = NDArrayFactory::create_<float>('c', {512, 3072});
aY[e] = NDArrayFactory::create_<float>('c', {3072});
aZ[e] = NDArrayFactory::create_<float>('c', {512, 3072});
aX[e]->assign(119 * (e+1));
aY[e]->assign(119 * (e+3));
}
std::vector<Nd4jLong> values;
Context ctx(1);
nd4j::ops::biasadd op;
for (int e = 0; e < 1000; e++) {
auto x = aX[e < pool ? e : e % pool];
@ -149,7 +178,8 @@ TEST_F(PlaygroundTests, test_broadcast_1) {
auto timeStart = std::chrono::system_clock::now();
x->applyTrueBroadcast(BroadcastOpsTuple::Multiply(), *y, *z);
//op.execute({x, y}, {z});
nd4j::ops::helpers::addBias(ctx, *x, *y, *z, false);
auto timeEnd = std::chrono::system_clock::now();
auto outerTime = std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeStart).count();
@ -166,7 +196,7 @@ TEST_F(PlaygroundTests, test_broadcast_1) {
delete aZ[e];
}
}
*/
/*
TEST_F(PlaygroundTests, test_broadcast_1) {

View File

@ -273,7 +273,7 @@ add_executable(runtests ${LOOPS_SOURCES} ../../blas/cpu/NativeOps.cpp ../../blas
../../include/cnpy/cnpy.cpp ../../include/nd4jmemset.h ../../include/nd4jmalloc.h
../../blas/Environment.cpp ../../blas/Environment.h ${EXEC_SOURCES} ${HELPERS_SOURCES} ${ARRAY_SOURCES} ${TYPES_SOURCES}
${MEMORY_SOURCES} ${GRAPH_SOURCES} ${CUSTOMOPS_SOURCES} ${EXCEPTIONS_SOURCES} ${INDEXING_SOURCES} ${CUSTOMOPS_PLATFORM_SOURCES} ${CUSTOMOPS_GENERIC_SOURCES}
${OPS_SOURCES} ${TEST_SOURCES} ${PERF_SOURCES} ../../include/loops/cpu/compilation_units/TrueBroadcastHelper_1.cpp)
${OPS_SOURCES} ${TEST_SOURCES} ${PERF_SOURCES})
target_link_libraries(runtests gtest ${MKLDNN} gtest_main ${BLAS_LIBRARIES})