UNPKG

6.69 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google Inc. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import * as tf from '../index';
18import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
19import { expectPromiseToFail } from '../test_util';
20describeWithFlags('toPixels no canvas', ALL_ENVS, () => {
21 it('draws a rank-2 float32 tensor', async () => {
22 const x = tf.tensor2d([.15, .2], [2, 1], 'float32');
23 const data = await tf.browser.toPixels(x);
24 const expected = new Uint8ClampedArray([
25 Math.round(.15 * 255), Math.round(.15 * 255), Math.round(.15 * 255), 255,
26 Math.round(.2 * 255), Math.round(.2 * 255), Math.round(.2 * 255), 255
27 ]);
28 expect(data).toEqual(expected);
29 });
30 it('draws a rank-2 int32 tensor', async () => {
31 const x = tf.tensor2d([10, 20], [2, 1], 'int32');
32 const data = await tf.browser.toPixels(x);
33 const expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]);
34 expect(data).toEqual(expected);
35 });
36 it('draws a rank-3 float32 tensor, 1 channel', async () => {
37 const x = tf.tensor3d([.15, .2], [2, 1, 1], 'float32');
38 const data = await tf.browser.toPixels(x);
39 const expected = new Uint8ClampedArray([
40 Math.round(.15 * 255), Math.round(.15 * 255), Math.round(.15 * 255), 255,
41 Math.round(.2 * 255), Math.round(.2 * 255), Math.round(.2 * 255), 255
42 ]);
43 expect(data).toEqual(expected);
44 });
45 it('draws a rank-3 int32 tensor, 1 channel', async () => {
46 const x = tf.tensor3d([10, 20], [2, 1, 1], 'int32');
47 const data = await tf.browser.toPixels(x);
48 const expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]);
49 expect(data).toEqual(expected);
50 });
51 it('draws a rank-3 float32 tensor, 3 channel', async () => {
52 // 0.1 and 0.3 are changed to 0.1001 and 0.3001 to avoid boundary conditions
53 // such as Math.round(~25.5) which on Mobile Safari gives 25 and Desktop
54 // gives 26.
55 const x = tf.tensor3d([.05, .1001, .15, .2, .25, .3001], [2, 1, 3], 'float32');
56 const data = await tf.browser.toPixels(x);
57 const expected = new Uint8ClampedArray([
58 Math.round(.05 * 255), Math.round(.1001 * 255), Math.round(.15 * 255),
59 255, Math.round(.2 * 255), Math.round(.25 * 255), Math.round(.3001 * 255),
60 255
61 ]);
62 expect(data).toEqual(expected);
63 });
64 it('draws a rank-3 int32 tensor, 3 channel', async () => {
65 const x = tf.tensor3d([10, 20, 30, 40, 50, 60], [2, 1, 3], 'int32');
66 const data = await tf.browser.toPixels(x);
67 const expected = new Uint8ClampedArray([10, 20, 30, 255, 40, 50, 60, 255]);
68 expect(data).toEqual(expected);
69 });
70 it('draws a rank-3 float32 tensor, 4 channel', async () => {
71 const x = tf.tensor3d([.05, .1001, .15, .2, .25, .3001, .35, .4], [2, 1, 4], 'float32');
72 const data = await tf.browser.toPixels(x);
73 const expected = new Uint8ClampedArray([
74 Math.round(.05 * 255), Math.round(.1001 * 255), Math.round(.15 * 255),
75 Math.round(.20 * 255), Math.round(.25 * 255), Math.round(.3001 * 255),
76 Math.round(.35 * 255), Math.round(.4 * 255)
77 ]);
78 expect(data).toEqual(expected);
79 });
80 it('draws a rank-3 int32 tensor, 4 channel', async () => {
81 const x = tf.tensor3d([10, 20, 30, 40, 50, 60, 70, 80], [2, 1, 4], 'int32');
82 const data = await tf.browser.toPixels(x);
83 const expected = new Uint8ClampedArray([10, 20, 30, 40, 50, 60, 70, 80]);
84 expect(data).toEqual(expected);
85 });
86 it('throws for scalars', done => {
87 // tslint:disable-next-line:no-any
88 expectPromiseToFail(() => tf.browser.toPixels(tf.scalar(1)), done);
89 });
90 it('throws for rank-1 tensors', done => {
91 expectPromiseToFail(
92 // tslint:disable-next-line:no-any
93 () => tf.browser.toPixels(tf.tensor1d([1])), done);
94 });
95 it('throws for rank-4 tensors', done => {
96 expectPromiseToFail(
97 // tslint:disable-next-line:no-any
98 () => tf.browser.toPixels(tf.tensor4d([1], [1, 1, 1, 1])), done);
99 });
100 it('throws for bool dtype', done => {
101 expectPromiseToFail(() => tf.browser.toPixels(tf.tensor2d([1], [1, 1], 'bool')), done);
102 });
103 it('throws for rank-3 depth = 2', done => {
104 expectPromiseToFail(() => tf.browser.toPixels(tf.tensor3d([1, 2], [1, 1, 2])), done);
105 });
106 it('throws for rank-3 depth = 5', done => {
107 expectPromiseToFail(() => tf.browser.toPixels(tf.tensor3d([1, 2, 3, 4, 5], [1, 1, 5])), done);
108 });
109 it('throws for float32 tensor with values not in [0 - 1]', done => {
110 expectPromiseToFail(() => tf.browser.toPixels(tf.tensor2d([-1, .5], [1, 2])), done);
111 });
112 it('throws for int32 tensor with values not in [0 - 255]', done => {
113 expectPromiseToFail(() => tf.browser.toPixels(tf.tensor2d([-1, 100], [1, 2], 'int32')), done);
114 });
115 it('throws when passed a non-tensor', done => {
116 // tslint:disable-next-line:no-any
117 expectPromiseToFail(() => tf.browser.toPixels({}), done);
118 });
119 it('accepts a tensor-like object', async () => {
120 const x = [[10], [20]]; // 2x1;
121 const data = await tf.browser.toPixels(x);
122 const expected = new Uint8ClampedArray([10, 10, 10, 255, 20, 20, 20, 255]);
123 expect(data).toEqual(expected);
124 });
125 it('does not leak memory', async () => {
126 const x = tf.tensor2d([[.1], [.2]], [2, 1]);
127 const startNumTensors = tf.memory().numTensors;
128 await tf.browser.toPixels(x);
129 expect(tf.memory().numTensors).toEqual(startNumTensors);
130 });
131 it('does not leak memory given a tensor-like object', async () => {
132 const x = [[10], [20]]; // 2x1;
133 const startNumTensors = tf.memory().numTensors;
134 await tf.browser.toPixels(x);
135 expect(tf.memory().numTensors).toEqual(startNumTensors);
136 });
137});
138//# sourceMappingURL=to_pixels_test.js.map
\No newline at end of file