question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CheckpointLoader that works in Node.js environment

See original GitHub issue

From @kimamula on March 18, 2018 7:21

CheckpointLoader only works in browser environment but I want to use it in Node.js environment.

Actually, I have already implemented it as follows and if this is acceptable I would like to make a PR.

import { Tensor } from 'deeplearn';
import * as path from 'path';
import * as fs from 'fs';

export interface CheckpointVariable {
  filename: string;
  shape: number[];
}

export type CheckpointManifest = {
  [varName: string]: CheckpointVariable
};

export interface Variables {
  [varName: string]: Tensor;
}

export class NodeCheckpointLoader {
  private checkpointManifest: CheckpointManifest;
  private variables: Variables;

  /**
   * NodeCheckpointLoader constructor
   * @param {string} checkpointFilePath should be either an absolute path or a relative path to the current working directory
   */
  constructor(private checkpointFilePath: string) {
    this.checkpointManifest = require(path.resolve(this.checkpointFilePath));
  }

  getAllVariables(): Promise<Variables> {
    if (this.variables) {
      return Promise.resolve(this.variables);
    }
    return Promise
      .all<Variables>(Object.keys(this.checkpointManifest).map(varName =>
        this.getVariable(varName)
          .then(tensor => ({[varName]: tensor}))
      ))
      .then(variables => {
        this.variables = Object.assign.apply(null, variables);
        return this.variables;
      });
  }

  getVariable(varName: string): Promise<Tensor> {
    if (!(varName in this.checkpointManifest)) {
      return Promise.reject(new Error(`Cannot load non-existant variable ${varName}`));
    }
    const fileName = this.checkpointManifest[varName].filename;
    const filePath = path.resolve(path.dirname(this.checkpointFilePath), fileName);
    return new Promise<Tensor>((resolve, reject) => fs.readFile(filePath, (err, buffer) => {
      if (err) {
        return reject(err);
      }
      const values = new Float32Array(buffer.buffer);
      resolve(Tensor.make(this.checkpointManifest[varName].shape, {values}));
    }));
  }
}

Copied from original issue: tensorflow/tfjs-core#864

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
nsthoratcommented, Apr 7, 2018

We will absolutely provide a way to do this in the Node.js world by loading a SavedModel directly, loadFrozenModel only supports browsers, you are right. We’re working on it 😃

1reaction
nsthoratcommented, Apr 7, 2018

From @kimamula on April 3, 2018 16:33

Well, when the Node.js binding is provided, it is probably possible to restore a TensorFlow SavedModel directory under Node.js environment?

Then the Node.js version of loadFrozenModel is absolutely not necessary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
Command-line API | Node.js v19.3.0 Documentation
The program entry point is a specifier-like string. If the string is not an absolute path, it's resolved as a relative path from...
Read more >
Process | Node.js v19.3.0 Documentation
js empties its event loop and has no additional work to schedule. Normally, the Node.js process will exit when there is no work...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
This behavior matches how import behaves in browser environments, ... This allows web browser-like imports to work in Node.js with a few differences...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found