1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import * as tf from '../index';
|
18 | import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
|
19 | import { expectPromiseToFail } from '../test_util';
|
20 | describeWithFlags('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 |
|
53 |
|
54 |
|
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 |
|
88 | expectPromiseToFail(() => tf.browser.toPixels(tf.scalar(1)), done);
|
89 | });
|
90 | it('throws for rank-1 tensors', done => {
|
91 | expectPromiseToFail(
|
92 |
|
93 | () => tf.browser.toPixels(tf.tensor1d([1])), done);
|
94 | });
|
95 | it('throws for rank-4 tensors', done => {
|
96 | expectPromiseToFail(
|
97 |
|
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 |
|
117 | expectPromiseToFail(() => tf.browser.toPixels({}), done);
|
118 | });
|
119 | it('accepts a tensor-like object', async () => {
|
120 | const x = [[10], [20]];
|
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]];
|
133 | const startNumTensors = tf.memory().numTensors;
|
134 | await tf.browser.toPixels(x);
|
135 | expect(tf.memory().numTensors).toEqual(startNumTensors);
|
136 | });
|
137 | });
|
138 |
|
\ | No newline at end of file |