Trying to get TFJS better supported at Edge Impulse

Edgeimpulse.com tinyML Machine Learning supports exports to Tensorflow, TensorflowLite, TensorflowMicro but not TensorflowJs. It can easily be converted from one of the other formats but typically takes a python script which is not Javascript.

Perhaps a few people could “like” the following Feature Request at EdgeImpulse.com to increase its chances of adoption.

1 Like

So it looks like Edge Impulse will not be helping with exporting to TFJS.

Anyone any opinions on how to convert from Tensorflow saved model to TensorflowJs layers model. I used to do it all the time but my code doesn’t seem to work anymore. Anyone got uptodate conversion examples and installation code.

I used to have several steps full automated on this github to take a model.json to several other forms, but I dont even thing the instalation is working anymore.

Any suggestions?

‘’’

pip install tf-nightly
pip install tensorflowjs
pip install netron “dask[delayed]”

tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras_saved_model ./model.json ./
tflite_convert --keras_model_file ./ --output_file ./model.tflite
xxd -i model.tflite model.h

‘’’

What is the error though? Did the model you are trying to convert change? On our side I am not aware of any major updates to the converter itself so it should still work assuming same model architecture / ops being used. If the issue is with the TFLite converter then maybe someone from the TFLite team can comment on that?

1 Like

Hi Jason: (sorry 2 different laptops seems to have 2 different logins)

The issue seems to be that the following are not installing correctly

Worked really well 12 months ago but now I get errors from these commands

tflite_convert --help
tensorflowjs_converter --help 

Errors like

tflite_convert --help
2021-08-08 21:03:08.629865: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory ...

The above was on a Gitpod Cloud Debian server, when I try to install directly onto my ubuntu machine I have installation issues.

My github that loads on Gitpod is here the auto gitpod load URL is here

This all worked great a year ago, so I wonder what is different.

I see. If you believe this is a bug and not an error with your environment etc then please submit a bug to the TFJS repo so it can be triaged.

We have had a few people using the converter recently on regular vanilla Ubuntu cloud instances and not reported this issue before AFAIK. Please confirm there were no issues with deps not installed on your local environment first though / file access permissions. If it still persists after checking those on your end feel free to open a bug using link above. Thanks!

1 Like

Thanks @Jason, presently away from my computer lab so I have a minimal number of computers to test the issues on. I will look into it in when I can. Does anyone else have ideas about possible changes to any dependencies for the converters over the last year?

@Jason So I fixed my tfjs converter installation issue with a few minor changes.

This is what finally worked

‘’’

      pip3 install --upgrade pip                                             
      pip3 install tensorflowjs                    

‘’’

Seems that installing tensorflow first or tf-nightly was messing something up, tensorflowjs seems to install the correct tensorflow. Also pip3 helped over just trying pip.

Next issue is converting edgeImpulse GRAYSCALE 120 x 120 tensorflow saved model to tfjs.

Conversion created the necessary files, now how to run it in the browser?

Typically I would take a mobilenet demo and upload my new saved file, but that might prove tricky.

So far getting errors just loading the saved layer model in yhe browser. I will have to dig a bit deeper.

Anyone got any GRAYSCALE TFJS webcam working demos they could share?

Perhaps an easier route is just to change my edgeimpulse model to RGB?

1 Like

These 2 codelabs I wrote may have some overlap and be of interest:

Converting and using Python models:

Hosting via Firebase:

1 Like

Thanks @Jason I forgot you have to change the “paths” in the model.json file.

Still got issues with loading my edge impulse converted model, but making some headway.

By the way we should chat before school starts about copePen, glitch, codeSandBox, repl.it … I have probably done most of them and have some opinions.

No worries! Glad that helped find the issue! Yes feel free to drop me an DM on the forum if you want to discuss privately your thoughts on those.

So I am having issues loading a model.json file from my website. Best to start from scratch:

@Jason does anyone know if there is an up-to-date mobilenet webcam example? I have these old single web page vanilla javascript ones which work on multiple devices. Has anyone put together a simple mobilenet webcam version. I can do it, just nice to see what others have done.

https://hpssjellis.github.io/beginner-tensorflowjs-examples-in-javascript/tfjs-models/blazeface/index.html

https://hpssjellis.github.io/beginner-tensorflowjs-examples-in-javascript/tfjs-models/mobilenet/index.html

My older version which does not work well on iPhone browsers

I have lots more but everything is a few years old, would love to see something that was modern simple and easy to load a TFJS layers model and test it using a webcam.

OK, @Jason I found your webcam Object Detection codeLab here

That is certain one route which uses our COCO-SSD model however if you wanted a mobile net example all my working code is available on Glitch.com and Codepen.io eg here:

Mobilenet:

Here I show how to apply to a single image and also webcam classification too. Mobilenet on its own tends to give interesting results so I much prefer to use COCO-SSD for object detection or retrain Mobilenet for specific things eg like TeachableMachine does. Anyhow my minimal COCO-SSD demo is here:

1 Like

Just checking something I read. Can a Tensorflow Saved model be converted to a TFJS Layers Model? I think I read somewhere that it can only be converted to a TFJS Graph Model.

I really want a layers model from Edge Impulse. Could a TFLite model be converted to a TFJS layers model?

Rare, but occasionally I make something that is useful.

https://hpssjellis.github.io/my-examples-of-edge-impulse/public/edge-models/single-heart-rock/forweb/index-just-load.html

A website to check your TFJS layers model loaded hopefully from any https site and prints a summary

Or a Vission only Graph Model either from TF hub or an https site that tests a zeros filled tensor. Kind of like the API examples.

Things are going good.

Anyone know how to directly take a video webcam element and convert it to shape 1, 224, 224, 3

I can do it with a canvas but wonder if there is an easier way.

I am testing this


       const input_tensor = await tf.browser.fromPixels(video).toFloat().sub(255 / 2).div(255 / 2).reshape([1, 224, 224, 3]);

ooooooo I see the issue I need to crop the video. Can that be done using the video element or do I have to convert it to a canvas. Also does anyone know the code to grab the middle pixels. Presently the webcam is 640 x 480 and I will need the middle 224 x 224.

Ok, getting there


       const input_tensor = await tf.data.webcam(video, {resizeWidth: 224, resizeHeight: 224, centerCrop : true,  });
       console.log('input_tensor')
       console.log(input_tensor)
       const myInputImage = await input_tensor.capture().

seems to give me tensors, now I just have to add a dimension.

The tensor is: 224,224,3
and I need 1,224,224,3

.expandDims(0); does not seem to work.

Anyone following along, what is working for loading vision data from a canvas to a model predict function is


   const image = await tf.browser.fromPixels(document.getElementById('my224x224Canvas')).toFloat().reshape([1, 224, 224, 3]) ;

The TFJS webpage now runs, but does not analyze correctly. The problem is most likely the input data from the canvas being formatted incorrectly. Anyone got any suggestions?

My last working web page demo should be here (since it is vanilla javascript just right click to view all the source code.)

My active model is here, but I routinely break it.