In NodeJs, using sequential model in Coral USB "Axel"

Hello,
I work in NodeJs about images classification.
I have built a tf.Sequential model.
I have a Coral USB accelerator, and I have understand that it uses .tflite models.
I am not sure about the method to convert my model to .tflite one.
Here is my suggest:


const {loadTFLiteModel} = require('tfjs-tflite-node');
const tf = require('@tensorflow/tfjs');

const {CoralDelegate} = require('coral-tflite-delegate');
const fs = require('fs');

const coralModelPath = './coralModel/model_edgetpu.tflite';
// convert standard model (weights.bin) in .tflite
const SAVED_MODEL_PATH "./std_model"
let converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
let tflite_model = converter.convert();
fs.writeFileSync(coralModelPath,tflite_model);

const options = {delegates: [new CoralDelegate()]};
const coralModel = await loadTFLiteModel(coralModelPath, options);

Is it completly bad ?
Best regards

AFAIK we do not have any TFJS → TFLite converter. Most of the converters tend to go from TF Python to TFJS or TF Python to TFLite.

Maybe @Matthew_Soulanille knows of something here that I am unaware of?

The Coral USB accelerator is a hardware device that can accelerate machine learning inference tasks. In Node.js, you can use the Coral USB accelerator by integrating it into your application code. Here’s an example of how you can use the sequential model with the Coral USB “Axel” accelerator in Node.js:

  1. Install the required dependencies: First, you need to install the Coral USB accelerator driver and the TensorFlow.js library. You can do this by running the following commands:

kotlinCopy code

npm install @tensorflow/tfjs-node
sudo apt-get install libedgetpu1-std
  1. Load the model: Next, you need to load the model into your Node.js application. You can do this using the TensorFlow.js library:

jsCopy code

const tf = require('@tensorflow/tfjs-node');

const model = await tf.loadGraphModel('file://path/to/model/directory/model.json');
  1. Create the tensor: Once you have loaded the model, you need to create a tensor from your input data. You can do this using the TensorFlow.js library:

jsCopy code

const inputTensor = tf.tensor2d(inputData, [1, inputData.length]);
  1. Execute inference: Finally, you can execute the inference on the Coral USB “Axel” accelerator by passing the input tensor to the model:

jsCopy code

const result = await model.executeAsync({ 'input': inputTensor }, { accelerator: 'wasm' });

This code will execute the sequential model using the Coral USB “Axel” accelerator and return the output tensor. You can then process the output tensor to get the desired result.

Note that the exact code may vary depending on your specific use case and the details of your model. You may need to modify the code to match your requirements.

Great !
I will try that.
Thanks

To use the Sequential model from TensorFlow.js in Node.js with the Coral USB accelerator, you can follow these steps:

  1. Install the required dependencies: Make sure you have Node.js installed on your system. Additionally, you need to install the @tensorflow/tfjs-node package to work with TensorFlow.js in Node.js. You can install it using npm with the following command:
    npm install @tensorflow/tfjs-node
  2. Import the required libraries: In your Node.js script, import the necessary libraries to work with TensorFlow.js and the Coral USB accelerator.
    const tf = require(‘@tensorflow/tfjs-node’);
    const coral = require(‘edgetpu-corals’);
  3. Load the Sequential model: Use the tf.loadLayersModel() function to load your Sequential model from a saved model file.
    const modelPath = ‘path/to/your/model.json’;
    const model = await tf.loadLayersModel(‘file://’ + modelPath);
  4. Initialize the Coral USB accelerator: Use the coral.EdgeTpuManager class to initialize the Coral USB accelerator.
    const edgetpuManager = new coral.EdgeTpuManager();
    await edgetpuManager.openDevice();
  5. Execute inference with the Coral USB accelerator: To run inference using the Coral USB accelerator, you can use the tf.tidy() function to wrap your inference code.
    const input = tf.tensor2d(/* Your input data */);
    const output = tf.tidy(() => {
    const inputWithAccelerator = edgetpuManager.convertTensor(input);
    return model.predict(inputWithAccelerator);
    });
    Ensure that the input data matches the input shape expected by your model.
  6. Release resources: After you’re done with the inference, release the resources held by the Coral USB accelerator.
    Remember to replace 'path/to/your/model.json' with the actual path to your saved model file. Adjust the code as per your specific requirements and input/output shapes.
    Please note that the edgetpu-corals package may have its own installation requirements and compatibility considerations. Make sure to refer to the official documentation for the package and follow the instructions provided by the Coral team for working with the Coral USB accelerator in Node.js.