Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up.
  • Loading branch information
lina128 committed Mar 8, 2021
commit ba41e3d4e2a3e56b4502242549b417ac1a145dc0
2 changes: 1 addition & 1 deletion tfjs-backend-cpu/src/kernels/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function bilinearInterpolation(
// f(x, yFloor) = (xCeil - x) / (xCeil - xFloor) * f(xFloor, yFloor)
// + (x - xFloor) / (xCeil - xFloor) * f(xCeil, yFloor)
const valueYFloor =
(xCeil - x) / (xCeil - xFloor) *
(xCeil - x) *
readWithFillValue(
imageVals, imageHeight, imageWidth, batchStride, rowStride,
colStride, batch, yFloor, xFloor, channel, fillValue) +
Expand Down
29 changes: 14 additions & 15 deletions tfjs-backend-webgl/src/transform_gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,20 @@ export class TransformProgram implements GPGPUProgram {
outputValue = readWithFillValue(batch, coordY, coordX,
channel);
} else {
int yFloor = int(floor(mapY));
int xFloor = int(floor(mapX));
int yCeil = yFloor + 1;
int xCeil = xFloor + 1;
float valueYFloor = float((xCeil - x)) /
float((xCeil - xFloor)) *
readWithFillValue(batch, yFloor, xFloor, channel) +
float((x - xFloor)) *
readWithFillValue(batch, yFloor, xCeil, channel);
float valueYCeil = float((xCeil - x)) *
readWithFillValue(batch, yCeil, xFloor, channel) +
float((x - xFloor)) *
readWithFillValue(batch, yCeil, xCeil, channel);
outputValue = float((yCeil - y)) * valueYFloor +
float((y - yFloor)) * valueYCeil;
float yFloor = floor(mapY);
float xFloor = floor(mapX);
float yCeil = yFloor + 1.0;
float xCeil = xFloor + 1.0;
float valueYFloor = (xCeil - mapX) *
readWithFillValue(batch, int(yFloor), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yFloor), int(xCeil), channel);
float valueYCeil = (xCeil - mapX) *
readWithFillValue(batch, int(yCeil), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yCeil), int(xCeil), channel);
outputValue = (yCeil - mapY) * valueYFloor +
(mapY - yFloor) * valueYCeil;
}
}
setOutput(outputValue);
Expand Down
2 changes: 1 addition & 1 deletion tfjs-core/src/ops/image/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {op} from '../operation';
* Applies the given transform(s) to the image(s).
*
* @param image 4d tensor of shape `[batch, imageHeight, imageWidth, depth]`.
* @param transforms Projective transform matrix/matrices. A vector of length
* @param transforms Projective transform matrix/matrices. A tensor1d of length
* 8 or tensor of size N x 8. If one row of transforms is [a0, a1, a2, b0
* b1, b2, c0, c1], then it maps the output point (x, y) to a transformed
* input point (x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k),
Expand Down
67 changes: 60 additions & 7 deletions tfjs-core/src/ops/image/transform_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* =============================================================================
*/
import * as tf from '../../index';
import {BROWSER_ENVS, describeWithFlags} from '../../jasmine_util';
import {ALL_ENVS, describeWithFlags} from '../../jasmine_util';
import {expectArraysClose} from '../../test_util';

describeWithFlags('image.transform', BROWSER_ENVS, () => {
describeWithFlags('image.transform', ALL_ENVS, () => {
it('extreme projective transform.', async () => {
const images = tf.tensor4d(
[1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1], [1, 4, 4, 1]);
Expand All @@ -40,14 +40,67 @@ describeWithFlags('image.transform', BROWSER_ENVS, () => {
expectArraysClose(transformedImages.shape, [1, 3, 5, 1]);
});

it('rotate=90, scale=2, cx=1, cy=1.', async () => {
it('fill=constant, interpolation=nearest.', async () => {
const images = tf.tensor4d(
[1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1], [1, 4, 4, 1]);
const transform = tf.tensor2d([0, 1, 1, -1, 0, 3, 0, 0], [1, 8]);
const transformedImages = tf.image.transform(images, transform).toInt();
[1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0], [1, 4, 4, 1]);
const transform = tf.tensor2d([0, 0.5, 1, -1, 2, 3, 0, 0], [1, 8]);
const transformedImages = tf.image.transform(images, transform);
const transformedImagesData = await transformedImages.data();

const expected = [1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0];

expectArraysClose(expected, transformedImagesData);
});

it('fill=constant, interpolation=bilinear.', async () => {
const images = tf.tensor4d(
[1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0], [1, 4, 4, 1]);
const transform = tf.tensor2d([0, 0.5, 1, -1, 2, 3, 0, 0], [1, 8]);
const transformedImages = tf.image.transform(images, transform, 'bilinear');
const transformedImagesData = await transformedImages.data();

const expected = [1, 0, 1, 1, 0, 0, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0];

expectArraysClose(expected, transformedImagesData);
});

it('fill=reflect, interpolation=bilinear.', async () => {
const images = tf.tensor4d(
[1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0], [1, 4, 4, 1]);
const transform = tf.tensor2d([0, 0.5, 1, -1, 2, 3, 0, 0], [1, 8]);
const transformedImages =
tf.image.transform(images, transform, 'bilinear', 'reflect');
const transformedImagesData = await transformedImages.data();

const expected =
[1, 0, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 0, 1, 0, 0, 0.5, 0.5, 0];

expectArraysClose(expected, transformedImagesData);
});

it('fill=wrap, interpolation=bilinear.', async () => {
const images = tf.tensor4d(
[1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0], [1, 4, 4, 1]);
const transform = tf.tensor2d([0, 0.5, 1, -1, 2, 3, 0, 0], [1, 8]);
const transformedImages =
tf.image.transform(images, transform, 'bilinear', 'wrap');
const transformedImagesData = await transformedImages.data();

const expected =
[1, 0, 1, 1, 0.5, 1, 0.5, 0.5, 1, 1, 0, 1, 0.5, 0.5, 0.5, 0.5];

expectArraysClose(expected, transformedImagesData);
});

it('fill=nearest, interpolation=bilinear.', async () => {
const images = tf.tensor4d(
[1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0], [1, 4, 4, 1]);
const transform = tf.tensor2d([0, 0.5, 1, -1, 2, 3, 0, 0], [1, 8]);
const transformedImages =
tf.image.transform(images, transform, 'bilinear', 'nearest');
const transformedImagesData = await transformedImages.data();

const expected = [1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0];
const expected = [1, 0, 1, 1, 0.5, 0.5, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0];

expectArraysClose(expected, transformedImagesData);
});
Expand Down