Skip to content

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

Closed
@ediskandarov

Description

@ediskandarov

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

Metadata

Metadata

Labels

type:bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions