Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tfjs-backend-cpu/src/kernels/RotateWithOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const rotateWithOffsetConfig: KernelConfig = {
backend_util.getImageCenter(center, imageHeight, imageWidth);
const fullOpacityValue = 255;

const sinFactor = Math.sin(-radians);
const cosFactor = Math.cos(-radians);
const sinFactor = Math.sin(radians);
const cosFactor = Math.cos(radians);
const imageVals = cpuBackend.data.get(image.dataId).values as TypedArray;

for (let batchIdx = 0; batchIdx < batch; batchIdx++) {
Expand Down
1 change: 1 addition & 0 deletions tfjs-backend-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build-deps": "cd ../tfjs-core && yarn && yarn build",
"build-deps-ci": "cd ../tfjs-core && yarn && yarn build-ci",
"build": "rimraf dist/ && tsc && ./scripts/build-wasm.sh",
"publish-local": "rimraf dist/ && yarn build && rollup -c && yalc push",
"build-ci": "./scripts/build-ci.sh",
"build-npm": "./scripts/build-npm.sh",
"build-benchmark": "./scripts/build-benchmark.sh",
Expand Down
4 changes: 2 additions & 2 deletions tfjs-backend-wasm/src/cc/kernels/RotateWithOffset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void RotateWithOffset(const size_t image_id, const size_t batch,
const float* image_buf = image_info.f32();
float* out_buf = out_info.f32_write();

const float sin_factor = sin(-radians);
const float cos_factor = cos(-radians);
const float sin_factor = sin(radians);
const float cos_factor = cos(radians);

for (size_t batch_idx = 0; batch_idx < batch; ++batch_idx) {
const size_t batch_offset =
Expand Down
4 changes: 2 additions & 2 deletions tfjs-backend-webgl/src/rotate_gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class RotateProgram implements GPGPUProgram {
center: number|[number, number]) {
const imageHeight = imageShape[1];
const imageWidth = imageShape[2];
const sinFactor = Math.sin(-radians).toFixed(3);
const cosFactor = Math.cos(-radians).toFixed(3);
const sinFactor = Math.sin(radians).toFixed(3);
const cosFactor = Math.cos(radians).toFixed(3);
this.outputShape = imageShape;

const [centerX, centerY] =
Expand Down
4 changes: 2 additions & 2 deletions tfjs-core/src/ops/rotate_with_offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import * as util from '../util';
import {op} from './operation';

/**
* Rotates the input image tensor with an optional offset center of rotation.
* Currently available in the CPU, WebGL, and WASM backends.
* Rotates the input image tensor counter-clockwise with an optional offset
* center of rotation. Currently available in the CPU, WebGL, and WASM backends.
*
* @param image 4d tensor of shape `[batch, imageHeight, imageWidth, depth]`.
* @param radians The amount of rotation.
Expand Down
136 changes: 68 additions & 68 deletions tfjs-core/src/ops/rotate_with_offset_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describeWithFlags('rotateWithOffset', BROWSER_ENVS, () => {
'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAAAigAwAEAAAAAQAAAAgAAAAA/+0AOFBob3Rvc2hvcCAzLjAAOEJJTQQEAAAAAAAAOEJJTQQlAAAAAAAQ1B2M2Y8AsgTpgAmY7PhCfv/AABEIAAgACAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2wBDAAkGBw0HCA0HBw0HBwcHBw0HBwcHDQ8IDQcNFREWFhURExMYHSggGBolGxUTITEhMSk3Ojo6Fx8zODMtNygtLiv/2wBDAQoKCg0NDRUNDRUrGRUZKysrKy0rKy0rKysrKy0rKysrKystKzctKysrKy0rKysrLSsrKysrKzcrLSsrKy0rKyv/3QAEAAH/2gAMAwEAAhEDEQA/AOin1Kxs7JgEVSsZAPU5xWF/wkdp6L+QqlrX/Hm/0P8AKuSqsRk+FlUcmnd+Z4WBzzGulrO+vY//2Q==';
const size = 8;

it('should rotate 90 degrees', async () => {
it('should rotate counterclockwise 90 degrees', async () => {
const img = new Image();
img.src = imageBase64String;

Expand All @@ -43,30 +43,30 @@ describeWithFlags('rotateWithOffset', BROWSER_ENVS, () => {
const rotatedPixelsData = await rotatedPixels.data();

const expected = [
0, 0, 0, 0, 0, 193, 228, 255, 18, 200, 224, 255, 55, 207, 212,
255, 108, 214, 202, 255, 163, 208, 187, 255, 179, 176, 159, 255, 168, 129,
130, 255, 0, 0, 0, 0, 0, 192, 221, 255, 19, 204, 222, 255, 62,
217, 213, 255, 119, 226, 206, 255, 179, 226, 194, 255, 199, 198, 168, 255,
186, 152, 140, 255, 0, 0, 0, 0, 0, 189, 209, 255, 19, 203, 211,
255, 64, 219, 201, 255, 121, 231, 192, 255, 184, 234, 181, 255, 211, 216,
162, 255, 202, 174, 137, 255, 0, 0, 0, 0, 0, 194, 204, 255, 30,
209, 205, 255, 76, 225, 193, 255, 130, 235, 179, 255, 191, 239, 165, 255,
225, 228, 151, 255, 220, 190, 128, 255, 0, 0, 0, 0, 4, 182, 186,
255, 35, 200, 186, 255, 87, 220, 175, 255, 141, 232, 162, 255, 201, 236,
142, 255, 235, 227, 128, 255, 230, 193, 104, 255, 0, 0, 0, 0, 3,
158, 162, 255, 37, 177, 164, 255, 94, 206, 156, 255, 155, 226, 146, 255,
213, 230, 126, 255, 243, 220, 106, 255, 241, 188, 82, 255, 0, 0, 0,
0, 6, 133, 140, 255, 39, 150, 141, 255, 98, 181, 135, 255, 162, 206,
127, 255, 218, 210, 103, 255, 247, 201, 81, 255, 250, 177, 64, 255, 0,
0, 0, 0, 0, 102, 113, 255, 15, 115, 105, 255, 71, 143, 97, 255,
135, 166, 86, 255, 191, 170, 61, 255, 222, 164, 41, 255, 233, 148, 31,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 230, 133, 18, 255, 233, 148, 31, 255, 222, 164, 41, 255, 191,
170, 61, 255, 135, 166, 86, 255, 71, 143, 97, 255, 15, 115, 105, 255,
0, 102, 113, 255, 241, 153, 43, 255, 250, 177, 64, 255, 247, 201, 81,
255, 218, 210, 103, 255, 162, 206, 127, 255, 98, 181, 135, 255, 39, 150,
141, 255, 6, 133, 140, 255, 224, 156, 55, 255, 241, 188, 82, 255, 243,
220, 106, 255, 213, 230, 126, 255, 155, 226, 146, 255, 94, 206, 156, 255,
37, 177, 164, 255, 3, 158, 162, 255, 212, 157, 75, 255, 230, 193, 104,
255, 235, 227, 128, 255, 201, 236, 142, 255, 141, 232, 162, 255, 87, 220,
175, 255, 35, 200, 186, 255, 4, 182, 186, 255, 200, 155, 98, 255, 220,
190, 128, 255, 225, 228, 151, 255, 191, 239, 165, 255, 130, 235, 179, 255,
76, 225, 193, 255, 30, 209, 205, 255, 0, 194, 204, 255, 183, 138, 109,
255, 202, 174, 137, 255, 211, 216, 162, 255, 184, 234, 181, 255, 121, 231,
192, 255, 64, 219, 201, 255, 19, 203, 211, 255, 0, 189, 209, 255, 171,
120, 117, 255, 186, 152, 140, 255, 199, 198, 168, 255, 179, 226, 194, 255,
119, 226, 206, 255, 62, 217, 213, 255, 19, 204, 222, 255, 0, 192, 221,
255
];

expectArraysClose(expected, rotatedPixelsData, 10);
});

it('should rotate negative 90 degrees', async () => {
it('should rotate clockwise 90 degrees', async () => {
const img = new Image();
img.src = imageBase64String;

Expand All @@ -85,23 +85,23 @@ describeWithFlags('rotateWithOffset', BROWSER_ENVS, () => {
const rotatedPixelsData = await rotatedPixels.data();

const expected = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 230, 133, 18, 255, 233, 148, 31, 255, 222, 164, 41, 255, 191,
170, 61, 255, 135, 166, 86, 255, 71, 143, 97, 255, 15, 115, 105, 255,
0, 102, 113, 255, 241, 153, 43, 255, 250, 177, 64, 255, 247, 201, 81,
255, 218, 210, 103, 255, 162, 206, 127, 255, 98, 181, 135, 255, 39, 150,
141, 255, 6, 133, 140, 255, 224, 156, 55, 255, 241, 188, 82, 255, 243,
220, 106, 255, 213, 230, 126, 255, 155, 226, 146, 255, 94, 206, 156, 255,
37, 177, 164, 255, 3, 158, 162, 255, 212, 157, 75, 255, 230, 193, 104,
255, 235, 227, 128, 255, 201, 236, 142, 255, 141, 232, 162, 255, 87, 220,
175, 255, 35, 200, 186, 255, 4, 182, 186, 255, 200, 155, 98, 255, 220,
190, 128, 255, 225, 228, 151, 255, 191, 239, 165, 255, 130, 235, 179, 255,
76, 225, 193, 255, 30, 209, 205, 255, 0, 194, 204, 255, 183, 138, 109,
255, 202, 174, 137, 255, 211, 216, 162, 255, 184, 234, 181, 255, 121, 231,
192, 255, 64, 219, 201, 255, 19, 203, 211, 255, 0, 189, 209, 255, 171,
120, 117, 255, 186, 152, 140, 255, 199, 198, 168, 255, 179, 226, 194, 255,
119, 226, 206, 255, 62, 217, 213, 255, 19, 204, 222, 255, 0, 192, 221,
0, 0, 0, 0, 0, 193, 228, 255, 18, 200, 224, 255, 55, 207, 212,
255, 108, 214, 202, 255, 163, 208, 187, 255, 179, 176, 159, 255, 168, 129,
130, 255, 0, 0, 0, 0, 0, 192, 221, 255, 19, 204, 222, 255, 62,
217, 213, 255, 119, 226, 206, 255, 179, 226, 194, 255, 199, 198, 168, 255,
186, 152, 140, 255, 0, 0, 0, 0, 0, 189, 209, 255, 19, 203, 211,
255, 64, 219, 201, 255, 121, 231, 192, 255, 184, 234, 181, 255, 211, 216,
162, 255, 202, 174, 137, 255, 0, 0, 0, 0, 0, 194, 204, 255, 30,
209, 205, 255, 76, 225, 193, 255, 130, 235, 179, 255, 191, 239, 165, 255,
225, 228, 151, 255, 220, 190, 128, 255, 0, 0, 0, 0, 4, 182, 186,
255, 35, 200, 186, 255, 87, 220, 175, 255, 141, 232, 162, 255, 201, 236,
142, 255, 235, 227, 128, 255, 230, 193, 104, 255, 0, 0, 0, 0, 3,
158, 162, 255, 37, 177, 164, 255, 94, 206, 156, 255, 155, 226, 146, 255,
213, 230, 126, 255, 243, 220, 106, 255, 241, 188, 82, 255, 0, 0, 0,
0, 6, 133, 140, 255, 39, 150, 141, 255, 98, 181, 135, 255, 162, 206,
127, 255, 218, 210, 103, 255, 247, 201, 81, 255, 250, 177, 64, 255, 0,
0, 0, 0, 0, 102, 113, 255, 15, 115, 105, 255, 71, 143, 97, 255,
135, 166, 86, 255, 191, 170, 61, 255, 222, 164, 41, 255, 233, 148, 31,
255
];

Expand All @@ -128,24 +128,24 @@ describeWithFlags('rotateWithOffset', BROWSER_ENVS, () => {
const rotatedPixelsData = await rotatedPixels.data();

const expected = [
224, 156, 55, 255, 250, 177, 64, 255, 247, 201, 81, 255, 222, 164, 41,
255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 230, 193, 104, 255, 243, 220, 106, 255, 247, 201, 81, 255, 218,
210, 103, 255, 135, 166, 86, 255, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 225, 228, 151, 255, 235, 227, 128, 255, 213, 230, 126,
255, 162, 206, 127, 255, 98, 181, 135, 255, 71, 143, 97, 255, 0, 0,
0, 0, 0, 0, 0, 0, 225, 228, 151, 255, 191, 239, 165, 255, 141,
232, 162, 255, 94, 206, 156, 255, 98, 181, 135, 255, 39, 150, 141, 255,
0, 102, 113, 255, 0, 0, 0, 0, 184, 234, 181, 255, 130, 235, 179,
255, 76, 225, 193, 255, 87, 220, 175, 255, 37, 177, 164, 255, 6, 133,
140, 255, 6, 133, 140, 255, 0, 0, 0, 0, 119, 226, 206, 255, 64,
219, 201, 255, 76, 225, 193, 255, 30, 209, 205, 255, 4, 182, 186, 255,
3, 158, 162, 255, 0, 0, 0, 0, 0, 0, 0, 0, 62, 217, 213,
255, 62, 217, 213, 255, 19, 203, 211, 255, 0, 194, 204, 255, 0, 194,
204, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55,
207, 212, 255, 19, 204, 222, 255, 0, 192, 221, 255, 0, 189, 209, 255,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 156, 100, 111, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 168, 129, 130, 255, 171, 120, 117, 255, 171, 120, 117, 255,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 179, 176, 159, 255, 199, 198, 168, 255, 186, 152, 140, 255, 183, 138,
109, 255, 200, 155, 98, 255, 0, 0, 0, 0, 0, 0, 0, 0, 108,
214, 202, 255, 179, 226, 194, 255, 199, 198, 168, 255, 211, 216, 162, 255,
220, 190, 128, 255, 200, 155, 98, 255, 0, 0, 0, 0, 55, 207, 212,
255, 62, 217, 213, 255, 119, 226, 206, 255, 184, 234, 181, 255, 225, 228,
151, 255, 225, 228, 151, 255, 230, 193, 104, 255, 0, 193, 228, 255, 19,
204, 222, 255, 62, 217, 213, 255, 64, 219, 201, 255, 130, 235, 179, 255,
191, 239, 165, 255, 235, 227, 128, 255, 243, 220, 106, 255, 0, 192, 221,
255, 0, 192, 221, 255, 19, 203, 211, 255, 76, 225, 193, 255, 76, 225,
193, 255, 141, 232, 162, 255, 213, 230, 126, 255, 247, 201, 81, 255, 0,
0, 0, 0, 0, 189, 209, 255, 0, 194, 204, 255, 30, 209, 205, 255,
87, 220, 175, 255, 94, 206, 156, 255, 162, 206, 127, 255, 218, 210, 103,
255
0
];

expectArraysClose(expected, rotatedPixelsData, 10);
Expand All @@ -172,23 +172,23 @@ describeWithFlags('rotateWithOffset', BROWSER_ENVS, () => {
const rotatedPixelsData = await rotatedPixels.data();

const expected = [
224, 156, 55, 255, 250, 177, 64, 255, 247, 201, 81, 255, 222, 164, 41,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 156, 100, 111, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 168, 129, 130, 255, 171, 120, 117, 255, 171, 120, 117, 255,
255, 255, 230, 193, 104, 255, 243, 220, 106, 255, 247, 201, 81, 255, 218,
210, 103, 255, 135, 166, 86, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 225, 228, 151, 255, 235, 227, 128, 255, 213, 230, 126,
255, 162, 206, 127, 255, 98, 181, 135, 255, 71, 143, 97, 255, 255, 255,
255, 255, 255, 255, 255, 255, 225, 228, 151, 255, 191, 239, 165, 255, 141,
232, 162, 255, 94, 206, 156, 255, 98, 181, 135, 255, 39, 150, 141, 255,
0, 102, 113, 255, 255, 255, 255, 255, 184, 234, 181, 255, 130, 235, 179,
255, 76, 225, 193, 255, 87, 220, 175, 255, 37, 177, 164, 255, 6, 133,
140, 255, 6, 133, 140, 255, 255, 255, 255, 255, 119, 226, 206, 255, 64,
219, 201, 255, 76, 225, 193, 255, 30, 209, 205, 255, 4, 182, 186, 255,
3, 158, 162, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 217, 213,
255, 62, 217, 213, 255, 19, 203, 211, 255, 0, 194, 204, 255, 0, 194,
204, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 55,
207, 212, 255, 19, 204, 222, 255, 0, 192, 221, 255, 0, 189, 209, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 179, 176, 159, 255, 199, 198, 168, 255, 186, 152, 140, 255, 183, 138,
109, 255, 200, 155, 98, 255, 255, 255, 255, 255, 255, 255, 255, 255, 108,
214, 202, 255, 179, 226, 194, 255, 199, 198, 168, 255, 211, 216, 162, 255,
220, 190, 128, 255, 200, 155, 98, 255, 255, 255, 255, 255, 55, 207, 212,
255, 62, 217, 213, 255, 119, 226, 206, 255, 184, 234, 181, 255, 225, 228,
151, 255, 225, 228, 151, 255, 230, 193, 104, 255, 0, 193, 228, 255, 19,
204, 222, 255, 62, 217, 213, 255, 64, 219, 201, 255, 130, 235, 179, 255,
191, 239, 165, 255, 235, 227, 128, 255, 243, 220, 106, 255, 0, 192, 221,
255, 0, 192, 221, 255, 19, 203, 211, 255, 76, 225, 193, 255, 76, 225,
193, 255, 141, 232, 162, 255, 213, 230, 126, 255, 247, 201, 81, 255, 255,
255, 255, 255, 0, 189, 209, 255, 0, 194, 204, 255, 30, 209, 205, 255,
87, 220, 175, 255, 94, 206, 156, 255, 162, 206, 127, 255, 218, 210, 103,
255
];

Expand Down