tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.38k stars 1.92k forks source link

model.executeAsync fails when loading multiple converted TF-DF models #8273

Closed akshaan closed 3 months ago

akshaan commented 4 months ago

System information

Describe the current behavior

Loading two decision forest models (trained in Python and converted) yields the following error when calling model.executeAsync:

Error: Unexpected numerical input feature shape
 ❯ eo.predictTFDFSignature node_modules/@tensorflow/tfjs-tfdf/dist/tf-tfdf.fesm.js:38:11327
    321|         throw new Error(`modelUrl IO Handler ${modelSource} has no loa…
    322|     }
    323|     const graphModelSource = { load: modelSource.loadModel };
       |                              ^
    324|     const assets = modelSource.loadAssets();
    325|     const graphModel = loadGraphModelSync(graphModelSource);
 ❯ Object.customExecutor tfjs-tfdf/src/register_tfdf_ops.ts:64:9
 ❯ tfjs-converter/src/operations/operation_executor.ts:108:31
 ❯ executeOp tfjs-converter/src/operations/operation_executor.ts:119:8
 ❯ _loop_1 tfjs-converter/src/executor/graph_executor.ts:630:13
 ❯ GraphExecutor.processStack node_modules/@tensorflow/tfjs-converter/dist/tf-converter.node.js:31391:13
 ❯ GraphExecutor.<anonymous> tfjs-converter/src/executor/graph_executor.ts:576:29
 ❯ step node_modules/tslib/tslib.es6.js:102:23
 ❯ Object.next node_modules/tslib/tslib.es6.js:83:53
 ❯ node_modules/tslib/tslib.es6.js:76:71

Describe the expected behavior Running predictions should work for multiple models loaded from files.

Standalone code to reproduce the issue

export class TFDFFileSystemLoadHandlerSync {
  static loadModel(path: string): tf.io.ModelArtifacts {
    const modelPath = join(path, 'model.json');
    try {
      const info = statSync(modelPath);

      // `path` can be either a directory or a file. If it is a file, assume
      // it is model.json file.
      if (info.isFile()) {
        const modelJSON = JSON.parse(readFileSync(modelPath, 'utf8'));
        const [weightsManifest, weightData] = this.loadWeights(modelJSON.weightsManifest, modelPath);
        return tf.io.getModelArtifactsForJSONSync(modelJSON, weightsManifest, weightData);
      } else {
        throw new Error('The path to load from must be a file. Loading from a directory ' + 'is not supported.');
      }
    } catch (e) {
      doesNotExistHandler('Path')(e as NodeJS.ErrnoException);
      // This line is never reached. It is added to pacify the TypeScript compiler.
      return {} as tf.io.ModelArtifacts;
    }
  }

  static loadAssets(path: string): Blob {
    const assetsPath = join(path, 'assets.zip');
    const buffer = readFileSync(assetsPath);
    return toArrayBuffer(buffer) as unknown as Blob;
  }

  static loadWeights(
    weightsManifest: tf.io.WeightsManifestConfig,
    path: string
  ): [tf.io.WeightsManifestEntry[], ArrayBuffer] {
    const dirName = dirname(path);
    const buffers: Buffer[] = [];
    const weightSpecs: tf.io.WeightsManifestEntry[] = [];
    for (const group of weightsManifest) {
      for (const path of group.paths) {
        const weightFilePath = join(dirName, path);
        try {
          const buffer = readFileSync(weightFilePath);
          buffers.push(buffer);
        } catch (e) {
          doesNotExistHandler('Weight file')(e as NodeJS.ErrnoException);
        }
      }
      weightSpecs.push(...group.weights);
    }
    return [weightSpecs, toArrayBuffer(buffers)];
  }
}

Loading model:

    return tfdf.loadTFDFModelSync({
      loadModel: () => TFDFFileSystemLoadHandlerSync.loadModel(modelPath),
      loadAssets: () => TFDFFileSystemLoadHandlerSync.loadAssets(modelPath),
    });
gaikwadrahul8 commented 4 months ago

Hi, @akshaan

I apologize for the delayed response and thank you for bringing this issue to our attention, if possible could you please help us with your Github repo along with converted TensorFlow.js models and complete steps to replicate the same behavior from our end to investigate this issue further from our end ?

Thank you from your cooperation and patience.

github-actions[bot] commented 4 months ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 3 months ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 3 months ago

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