Tflite input data type (int8) conversion in javascript

Posted on
  • I have a tflite model which expects the input to be in int8 datatype

    var tf = require("tensorflow").create(2048, model);
    tf.getInput()[0] = input_array;
    tf.invoke();
    print(tf.getOutput()[0]);
    

    I have loaded my data in the same shape as my model expects but when i print (tf.getInput()[0] ) it always shows zero even when input_array has values. I have encoded my model with base64. Can anyone help me out @Gordon please help me out here

    Also tflite has standard get_tensor and set_tensor functions in interpreter to set the input data in proper format. I cannot see those functions in banglejs after loading the tflite model.

  • Hi - tf.getInput() actually gives you a 1D array, so what you'd be doing with tf.getInput()[0] = input_array is casting input_array to a single integer and writing it to the first element.

    Probably what you want is just: tf.getInput().set(input_array)

    Also tflite has standard get_tensor and set_tensor functions in interpreter to set the input data in proper format. I cannot see those functions in banglejs after loading the tflite model.

    They don't exist in Espruino - you just get tf.getInput/Output that do the formatting for you. Right now there's no support for multiple tensors of different types - you just get access to the first input and output one

  • Hi @Gordon thanks for the reply
    so you mean my model input should be always a flat 1D array and then internal conversion should be made while creating model. Currently the my input_array is 4D array so can i directly input this to the model or should i recreate my model to take only 1D array inputs.

  • I'm not entirely sure it matters. Even if you specify a 4D array, I believe internally Tensorflow flattens it into a 1D array which is what you see in Espruino, so it shouldn't matter too much.

    However, if you used a 1D array from the start, it might be less confusing as then it's very explicit what each part of the input represents

  • var tf = require("tensorflow").create(3072, model);
    
    function get_data(a){
    
    var d = [Math.round(a.x*100),Math.round(a.y*100),Math.round(a.z*100)];
      
    input_array.push(d);
    
    
    if (input_array.length > SAMPLE_SIZE-1){
    
    
    var row_array = [];
    var time_step_array = [];
    var final_input = [];
    var depth_array = [];
    
    for (i=0; i<input_array.length;i++){
        row_array = [];
        depth_array = [];
    
    for (j=0;j<input_array[0].length;j++){
        depth_array.push(input_array[i][j]);  
    }
    row_array.push(depth_array);
    time_step_array.push(row_array);
    }
    
    final_input.push(time_step_array);
    
    
    tf.getInput().set(final_input);
    
    //Terminal.println(tf.getInput());
    
    tf.invoke();
    
    Terminal.println(tf.getOutput());
    
    input_array = [];
    
    }
    
    
    }
    
    
    Bangle.on('accel', get_data);
    
    

    This is my code and my model takes input as array i am always getting the same output when printed. I am pretty sure there is no problem in model as i loaded the tflite model and tested the output in my desktop and it is working fine.

  • Maybe you could print the contents of final_input?

    The way you're doing final_input.push(time_step_array); makes me think you might be ending up with an array of arrays, rather than just a single flat 1D array. You probably need to use concat for that - or ideally to use tf.getInput().set(sub_array, offset);

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Tflite input data type (int8) conversion in javascript

Posted by Avatar for user122861 @user122861

Actions