Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webgl] Add default arguments to CropAndResize. #4407

Merged
merged 9 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions tfjs-backend-cpu/src/kernels/CropAndResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export function cropAndResize(args: {

const $method = method || 'bilinear';
const $extrapolationValue = extrapolationValue || 0;
util.assert(
$method === 'bilinear' || $method === 'nearest',
() => `method must be bilinear or nearest, but was ${$method}`);

const [batch, imageHeight, imageWidth, numChannels] = image.shape;
const numBoxes = boxes.shape[0];
Expand Down
10 changes: 8 additions & 2 deletions tfjs-backend-webgl/src/kernels/CropAndResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* =============================================================================
*/

import {CropAndResize, CropAndResizeAttrs, CropAndResizeInputs, KernelConfig, KernelFunc, TensorInfo} from '@tensorflow/tfjs-core';
import {CropAndResize, CropAndResizeAttrs, CropAndResizeInputs, KernelConfig, KernelFunc, TensorInfo, util} from '@tensorflow/tfjs-core';

import {MathBackendWebGL} from '../backend_webgl';
import {CropAndResizeProgram} from '../crop_and_resize_gpu';
Expand All @@ -29,9 +29,15 @@ export const cropAndResize = (args: {
const {image, boxes, boxInd} = inputs;
const {cropSize, method, extrapolationValue} = attrs;

const $method = method || 'bilinear';
const $extrapolationValue = extrapolationValue || 0;
util.assert(
$method === 'bilinear' || $method === 'nearest',
() => `method must be bilinear or nearest, but was ${$method}`);

const program = new CropAndResizeProgram(
image.shape as [number, number, number, number],
boxes.shape as [number, number], cropSize, method, extrapolationValue);
boxes.shape as [number, number], cropSize, $method, $extrapolationValue);
return backend.runWebGLProgram(program, [image, boxes, boxInd], 'float32');
};

Expand Down
3 changes: 0 additions & 3 deletions tfjs-core/src/ops/image/crop_and_resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ function cropAndResize_(
util.assert(
cropSize[0] >= 1 && cropSize[1] >= 1,
() => `cropSize must be atleast [1,1], but was ${cropSize}`);
util.assert(
method === 'bilinear' || method === 'nearest',
() => `method must be bilinear or nearest, but was ${method}`);

const inputs:
CropAndResizeInputs = {image: $image, boxes: $boxes, boxInd: $boxInd};
Expand Down
13 changes: 13 additions & 0 deletions tfjs-core/src/ops/image/crop_and_resize_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ describeWithFlags('cropAndResize', ALL_ENVS, () => {
expectArraysClose(await output.data(), await image.data());
});

it('5x5-bilinear, no arguments passed in for method or extrapolation',
async () => {
const image: tf.Tensor4D = tf.ones([1, 5, 5, 3]);
const boxes: tf.Tensor2D = tf.tensor2d([0, 0, 1, 1], [1, 4]);
const boxInd: tf.Tensor1D = tf.tensor1d([0], 'int32');

const output = tf.image.cropAndResize(image, boxes, boxInd, [5, 5]);

expect(output.shape).toEqual([1, 5, 5, 3]);
expect(output.dtype).toBe('float32');
expectArraysClose(await output.data(), await image.data());
});

it('5x5-bilinear, just a crop, no resize', async () => {
const image: tf.Tensor4D = tf.ones([1, 6, 6, 3]);
const boxes: tf.Tensor2D = tf.tensor2d([0.5, 0.5, 1, 1], [1, 4]);
Expand Down
12 changes: 9 additions & 3 deletions tfjs-node/src/kernels/CropAndResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* =============================================================================
*/

import {CropAndResize, CropAndResizeAttrs, CropAndResizeInputs, KernelConfig, tensor1d} from '@tensorflow/tfjs';
import {CropAndResize, CropAndResizeAttrs, CropAndResizeInputs, KernelConfig, tensor1d, util} from '@tensorflow/tfjs';

import {createTensorsTypeOpAttr, NodeJSKernelBackend} from '../nodejs_kernel_backend';

Expand All @@ -28,12 +28,18 @@ export const cropAndResizeConfig: KernelConfig = {
const {cropSize, method, extrapolationValue} =
args.attrs as {} as CropAndResizeAttrs;

const $method = method || 'bilinear';
const $extrapolationValue = extrapolationValue || 0;
util.assert(
$method === 'bilinear' || $method === 'nearest',
() => `method must be bilinear or nearest, but was ${$method}`);

const opAttrs = [
createTensorsTypeOpAttr('T', image.dtype),
{name: 'method', type: backend.binding.TF_ATTR_STRING, value: method}, {
{name: 'method', type: backend.binding.TF_ATTR_STRING, value: $method}, {
name: 'extrapolation_value',
type: backend.binding.TF_ATTR_FLOAT,
value: extrapolationValue
value: $extrapolationValue
}
];
const cropSizeTensor = tensor1d(cropSize, 'int32');
Expand Down