Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
How to draw tensorflow js results to canvas? Problems with tf.browser.toPixels - shows old results only

I am new to JS. I have a ML model which does image segmentation. I provide the model with one image, to which it predicts output. I get the correct output array, I've verified.

But when I try to draw this output to a canvas using tf.browser.toPixel I always get the old Image.

Meaning, If I provide input as:

Input : img1 -> img2 -> img3...

output: <random shape> -> img1_prediction -> img2_prediction...

Where the random shape is this always, no matter what the input is. I've checked, this is not the output of my model.

Here's the code for this:

async function detect_custom(imgTag, canvas) {

    let tensor = tf.browser.fromPixels(imgTag).toFloat(); //imgTag is img element in html (input image)
    tensor = tensor.expandDims(0);
    const res = model.predict(tensor).squeeze(); 
    tf.dispose(tensor);
    // Everything is working correctly up to this point.

    await tf.browser.toPixels(res, canvas); // Problematic (I think)
}

I found the await tf.browser.toPixels(res, canvas) from this answer. Can anyone help what am i doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've checked, this is not the output of my model.

What is the output of the model?

And are you sure it takes 0..255 as input range? if input is float32, its likely expecting values in range 0..1 - and it does seems like output is blown up.

try tensor = tensor.div(255.0) before inference to normalize to 0..1
or tensor = tensor.div(127.5).sub(1) to normalize to -1..1

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!