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 all 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
11 changes: 4 additions & 7 deletions tfjs-backend-cpu/src/kernels/CropAndResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ export function cropAndResize(args: {
const {image, boxes, boxInd} = inputs;
const {cropSize, method, extrapolationValue} = attrs;

const $method = method || 'bilinear';
const $extrapolationValue = extrapolationValue || 0;

const [batch, imageHeight, imageWidth, numChannels] = image.shape;
const numBoxes = boxes.shape[0];

Expand Down Expand Up @@ -77,13 +74,13 @@ export function cropAndResize(args: {
for (let c = 0; c < numChannels; c++) {
const ind =
c + x * outStride[2] + y * outStride[1] + b * outStride[0];
output.values[ind] = $extrapolationValue;
output.values[ind] = extrapolationValue;
}
}
continue;
}

if ($method === 'bilinear') {
if (method === 'bilinear') {
const topInd = Math.floor(yInd);
const bottomInd = Math.ceil(yInd);
const yLerp = yInd - topInd;
Expand All @@ -97,7 +94,7 @@ export function cropAndResize(args: {
for (let c = 0; c < numChannels; c++) {
const ind =
c + x * outStride[2] + y * outStride[1] + b * outStride[0];
output.values[ind] = $extrapolationValue;
output.values[ind] = extrapolationValue;
}
continue;
}
Expand Down Expand Up @@ -140,7 +137,7 @@ export function cropAndResize(args: {
for (let c = 0; c < numChannels; c++) {
const ind =
c + x * outStride[2] + y * outStride[1] + b * outStride[0];
output.values[ind] = $extrapolationValue;
output.values[ind] = extrapolationValue;
}
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions tfjs-core/src/ops/image/crop_and_resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function cropAndResize_(
boxes: Tensor2D|TensorLike,
boxInd: Tensor1D|TensorLike,
cropSize: [number, number],
method?: 'bilinear'|'nearest',
extrapolationValue?: number,
method: 'bilinear'|'nearest' = 'bilinear',
extrapolationValue = 0,
): Tensor4D {
const $image = convertToTensor(image, 'image', 'cropAndResize');
const $boxes = convertToTensor(boxes, 'boxes', 'cropAndResize', 'float32');
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