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

tfjs raises an error in Jest(unit testing) environment and Node.js runtime #7175

Closed
ediskandarov opened this issue Dec 14, 2022 · 1 comment · Fixed by #7181
Closed

tfjs raises an error in Jest(unit testing) environment and Node.js runtime #7175

ediskandarov opened this issue Dec 14, 2022 · 1 comment · Fixed by #7181
Assignees
Labels
type:bug Something isn't working

Comments

@ediskandarov
Copy link

ediskandarov commented Dec 14, 2022

Please make sure that this is a bug. As per our
GitHub Policy,
we only address code/doc bugs, performance issues, feature requests and
build/installation issues on GitHub. tag:bug_template

👍

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): I used @tensorflow-models/coco-ssd package to experiment with object detection
  • OS Platform and Distribution: WSL with Ubuntu 22.04
  • TensorFlow.js installed from (npm or script link): npm
  • TensorFlow.js version (use command below):
      "Node.js": "v18.12.1",
      "@tensorflow-models/coco-ssd": "2.2.2",
      "@tensorflow/tfjs-node": "4.1.0"
      "typescript": "4.8.4"
      "@types/jest": "^29.2.4",
      "@types/node": "18.11.13",
      "jest": "^29.3.1",
    
  • Browser version: N/A
  • Tensorflow.js Converter Version: I don't know

Describe the current behavior

tensorflow js internals throw the following error when it used within jest test case.

tensor2d() requires values to be number[][] or flat/TypedArray

Describe the expected behavior

Tests should pass.

The code works, it only fails in Jest.

Standalone code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/CodePen/any notebook.

index.ts

import type { Buffer } from "node:buffer";
import { node as tfNode } from "@tensorflow/tfjs-node";
import { load as loadCocoSsd } from "@tensorflow-models/coco-ssd";

export async function detectObjects(imgBuf: Buffer) {
  const model = await loadCocoSsd();

  const img = new Uint8Array(imgBuf.buffer);
  const imgTensor = tfNode.decodeImage(img);
  const objects = await model.detect(imgTensor);
  return objects;
}

index.spec.ts

import { describe, it, expect } from "@jest/globals";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { detectObjects } from ".";

describe("test index module", () => {
  it("should pass object detection test", async () => {
    const peopleImgFilePath = path.join(
      __dirname,
      "assets",
      "brooke-cagle-g1Kr4Ozfoac-unsplash.jpg"
    );
    const imgBuf = await readFile(peopleImgFilePath);

    const detectedObjects = await detectObjects(imgBuf);
    expect(detectedObjects).toEqual([]);
  });
});

Other info / logs Include any logs or source code that would be helpful to
diagnose the problem. If including tracebacks, please include the full
traceback. Large logs and files should be attached.

Traceback

tensor2d() requires values to be number[][] or flat/TypedArray

      at Object.tensor2d (../../tfjs-core/src/ops/tensor2d.ts:57:11)
      at ../../node_modules/@tensorflow-models/coco-ssd/dist/coco-ssd.node.js:618:45
      at ../../node_modules/@tensorflow/tfjs/tfjs-core/src/engine.ts:469:20
      at Engine.Object.<anonymous>.Engine.scopedRun (../../node_modules/@tensorflow/tfjs/tfjs-core/src/engine.ts:480:19)
      at Engine.Object.<anonymous>.Engine.tidy (../../node_modules/@tensorflow/tfjs/tfjs-core/src/engine.ts:467:17)
      at Object.tidy (../../tfjs-core/src/globals.ts:192:17)
      at ObjectDetection.<anonymous> (../../node_modules/@tensorflow-models/coco-ssd/dist/coco-ssd.node.js:617:42)
      at step (../../node_modules/@tensorflow-models/coco-ssd/dist/coco-ssd.node.js:71:23)
      at Object.next (../../node_modules/@tensorflow-models/coco-ssd/dist/coco-ssd.node.js:52:53)
      at fulfilled (../../node_modules/@tensorflow-models/coco-ssd/dist/coco-ssd.node.js:42:58)

Potentially relates to #545

@ediskandarov ediskandarov added the type:bug Something isn't working label Dec 14, 2022
mattsoulanille added a commit to mattsoulanille/tfjs that referenced this issue Dec 15, 2022
isTypedArray is implemented with `instanceof`, which does not work in jest (jestjs/jest#11864). Instead, use node's builtin `util.types.isUint8Array`, `util.types.isFloat32Array`, etc to perform this check.

Fixes tensorflow#7175.
This may also address tensorflow#7064, but it does not fix the root cause.
@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

mattsoulanille added a commit that referenced this issue Dec 16, 2022
isTypedArray is implemented with `instanceof`, which does not work in jest (jestjs/jest#11864). Instead, use node's builtin `util.types.isUint8Array`, `util.types.isFloat32Array`, etc to perform this check.

Fixes #7175.
This may also address #7064, but it does not fix the root cause.
Linchenn pushed a commit to Linchenn/tfjs that referenced this issue Jan 9, 2023
)

isTypedArray is implemented with `instanceof`, which does not work in jest (jestjs/jest#11864). Instead, use node's builtin `util.types.isUint8Array`, `util.types.isFloat32Array`, etc to perform this check.

Fixes tensorflow#7175.
This may also address tensorflow#7064, but it does not fix the root cause.
mattsoulanille added a commit to mattsoulanille/tfjs that referenced this issue Mar 16, 2023
A new function, `isTypedArray` was added to the `platform` interface by tensorflow#7181
and first published in tfjs-core 4.2.0. This made 4.2.0 incompatible with
earlier versions of backends that implemented `platform`, such as node and
react-native. This change adds a fallback to the use of `isTypedArray` so
earlier versions of platforms that don't implement `isTypedArray` will not throw
an error. Note that the behavior may not be perfect, such as when running Jest
tests in node. See tensorflow#7175 for more details and upgrade all @tensorflow scoped
packages to ^4.2.0 to avoid this.

Fixes tensorflow#7273
mattsoulanille added a commit to mattsoulanille/tfjs that referenced this issue Mar 16, 2023
A new function, `isTypedArray` was added to the `platform` interface by tensorflow#7181
and first published in tfjs-core 4.2.0. This made 4.2.0 incompatible with
earlier versions of backends that implemented `platform`, such as node and
react-native. This change adds a fallback to the use of `isTypedArray` so
earlier versions of platforms that don't implement `isTypedArray` will not throw
an error.

Note that the fallback behavior may not be perfect, such as when running Jest
tests in node. See tensorflow#7175 for more details and upgrade all @tensorflow scoped
packages to ^4.2.0 to avoid this.

Fixes tensorflow#7273
mattsoulanille added a commit that referenced this issue Mar 21, 2023
…#7489)

A new function, `isTypedArray` was added to the `platform` interface by #7181
and first published in tfjs-core 4.2.0. This made 4.2.0 incompatible with
earlier versions of backends that implemented `platform`, such as node and
react-native. This change adds a fallback to the use of `isTypedArray` so
earlier versions of platforms that don't implement `isTypedArray` will not throw
an error.

Note that the fallback behavior may not be perfect, such as when running Jest
tests in node. See #7175 for more details and upgrade all @tensorflow scoped
packages to ^4.2.0 to avoid this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants