Keras Deploy for Tensorflow.js Usage - firebase

I need to be able to deploy a keras model for Tensorflow.js prediction, but the Firebase docs only seem to support a TFLite object, which tf.js cannot accept. Tf.js appears to accept JSON files for loading (loadGraphModel() / loadLayersModel() ), but not a keras SavedModel (.pb + /assets + /variables).
How can I attain this goal?
Note for the Tensorflow.js portion: There are a lot of pointers to the tfjs_converter, but the closest API function offered to what I'm looking for is the loadFrozenModel() function, which requires both a .pb and a weights_manifest.json. It seem to me like I'd have to programmatically assemble this before before sending it up to GCloud as a keras SavedModel doesn't contain both (mine contains .pb + /assets + /variables).
This seems tedious for a straightforward deployment feature, and I'd imagine my question only hits upon common usage of each tool.
What I'm looking for is a simple pathway from Keras => Firebase/GCloud => Tensorflow.js.

So I understand your confusion but you have half part ready.
So your keras model has the following files and folders if I understand correctly:
saved_model.pb
/assests
/variables
This is enough to convert the keras model to tensorflow.js model.
Use the converter script in the following manner. Make sure you have the latest version of tfjs. If you do not have the latest version, try creating a virtual environment and install latest tfjs otherwise it will disrupt your tensorflow version.
import tensorflowjs as tfjs
import tensorflow as tf
model=tf.keras.models.load_model('path/to/keras/model')
tfjs.converters.save_keras_model(model, 'path/where/you/will/like/to/have/js/model/converted')
Once you have converted the model you will receive following files for js model.
model.json
something.bin
You will have to host those files using a webserver and just make it available for loadLayersModel API something like this:
const model = await tf.loadLayersModel(
'location/of/model.json');
That is it and you have converted the model from Keras to Tensorflowjs and uploaded as well in js.
I hope my answer helps you.

Related

Custom Machine learning code to import in the android studio

I am creating a project of fake news detection in android. I have trained the model and converted into the tensorflow lite. I tried to import it into the android studio in asset folder but it showed the message "This is not valid tensorflow lite model file".
enter image description here
can anyone help me to deploy it into android studio and to generate the input/output.
NOTE: I have tried export to firebase custom ML, I have run this code and didn't get any error
CustomModelDownloadConditions conditions = new CustomModelDownloadConditions.Builder().requireWifi().build();
FirebaseModelDownloader.getInstance().getModel("NewsCheckModel",
DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND, conditions)
.addOnSuccessListener(new OnSuccessListener<CustomModel>() {
#Override
public void onSuccess(CustomModel customModel) {
File modelFile = customModel.getFile();
if (modelFile != null) {
Interpreter interpreter = new Interpreter(modelFile);
}
}
});
I don't know the further steps.
.
.
.
if anyone can help in any way using asset folder or firebase way. I would be very thankful to you
Could you make sure that no compression option is enabled as follows while experimenting on the asset dir case?
android {
// ...
aaptOptions {
noCompress "tflite" // Your model's file extension: "tflite", "lite", etc.
}
}
See more details at this guide page.
There's not much information on your post. It would be useful to know if:
Are you able to deploy it on android?
Does the model initialize?
If yes, Can you perform inference?
If no, what is the exception you get?
In the past, I've had the same problem. So here are some basic steps:
Add Firebase to your project
2a. Make sure you are using the correct dependencies :
They have been updated recently so better follow this guide: here
Basically:
dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.0.1')
// Declare the dependency for the Firebase ML model downloader library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-ml-modeldownloader'
// Also declare the dependency for the TensorFlow Lite library and specify its version
implementation 'org.tensorflow:tensorflow-lite:2.3.0'
}
2b. Add to Manifest:
<uses-permission android:name="android.permission.INTERNET" />
3a. Double check the way you convert your model. Luckily you can convert and deploy to firebase easily:
3b. Convert the model to TensorFlow Lite and upload it to Cloud Storage
source = ml.TFLiteGCSModelSource.from_saved_model('./model_directory')
3c. Create the model object
tflite_format = ml.TFLiteFormat(model_source=source)
model = ml.Model(
display_name="example_model", # This is the name you use from your app to load the model.
tags=["examples"], # Optional tags for easier management.
model_format=tflite_format)
3d. Add the model to your Firebase project and publish it
new_model = ml.create_model(model)
ml.publish_model(new_model.model_id)
3a bis: If your model is already in .tflite:
source = ml.TFLiteGCSModelSource.from_tflite_model_file('example.tflite')
The guide is here
Regarding the inputs/outputs, I can't say much as it depends on your model. Also, be careful with the data types of input and output. Be aware of the type that your model requires (INT32, UINT8, FLOAT32....)
Another important thing is to have some metadata embedded in your model: here
Android Studio uses that metadata to display information when you open a .tflite file.
IMO is very likely that your issue is during your model conversion or lack of metadata.

onnx 1.2 models (Custom Vision) on HoloLens

This is a more specific continuation of my previous Post: Custom Vision on HoloLens
I'm still using the Unity Project from this blogpost: https://mtaulty.com/2018/03/29/third-experiment-with-image-classification-on-windows-ml-from-uwp-on-hololens-in-unity/
I had issues that my own exported models don't work with the the code at some point. Now it is possible to export onnx models of version 1.2, but the the old code seems to not be compatible with the new version.
in the line var evalOutput = await this.learningModel.EvaluateAsync(this.inputData); in the MainScript it throws The binding is incomplete or does not match the input/output description. (Exception from HRESULT: 0x88900002)
Does someone know what I need to change so it works with the new version on HoloLens? Thanks in advance!
You can find a similar question here: Windows ML's OS requirement
In summary, you are right about needing to update the PC and the Hololens, but the build number you need is 17763 to be on the production version of RS5.
You could also be hitting this issue: Cannot load model using WinML
where the binding isn't quite setup properly.
If you're still having issues, please post the SDK and OS version you're on, as well as the ONNX model version.

Lightweight Rendering Pipeline error

I downloaded the lightweight rendering pipeline using Unity's package manager, but I cannot create a lightweight pipeline asset. When I try to create the asset, the console throws me:
Invalid generated unique path 'ProjectSettings/LightweightAsset.asset'
(input path
'LightweightAsset.asset')UnityEngine.Experimental.Rendering.LightweightPipeline.LightweightPipelineAsset:CreateLightweightPipeline()
I had a similar issue but it went away after I did the following:
Using the new package manager: Remove all rendering-related packages from the Unity project and then only add the Lightweight
Rendering Pipeline package. All dependencies (like the Shader Graph) were
added automatically and correctly after I did that.
Be sure to get the newest preview release (1.1.5-preview or newer). The 0.1-releases are too old.
If all else fails, try to follow the instructions and clone it directly from the Github repository: https://github.com/Unity-Technologies/ScriptableRenderPipeline

Meteor Testing Using Spies

I'm using Velocity with the mike:mocha framework and the chai assertions. Everything is working great, but when it comes time to do stubbing, mocking and spying, I've hit a bit of a roadblock. These aren't core features of mike:mocha or chai, so I found practicalmeteor:chai, which should/may have added spies.
My first stab at finding out whether this is true was to write the following code:
it 'calls update when both documents are present but different', ->
spies.create('log', console, 'log')
which gives me:
ReferenceError: spies is not defined
at packages/velocity:test-proxy/tests/mocha/server/charger_server_doc_spec.coffee:88:9
at wrappedFunc (packages/mike:mocha/server.js:200:1)
at runWithEnvironment (packages/mike:mocha/server.js:156:1)
That implies to me that I've misunderstood what practicalmeteor:chai provides, however, the code I showed in the first example is copied verbatim from the README.
Question: Any tips on getting this to work? Is it a load order issue? The code on Github shows spies and so on are implemented in this package. So I'm a bit stuck.
Thanks!
The package practicalmeteor:chai does not include the practicalmeteor:sinon package which is required to get the spies API included.
They are separate packages because you may not have to use spies when creating basic tests with chai.
If you look at the package.js file in the practicalmeteor:chai package, it does not include the sinon files.
So, just running meteor add practicalmeteor:sinon should solve your problem.

SquashTA - Integrating existing tests

We are looking forward to using squashTA to manage our tests. The problem we are facing is that we already have a big automated tests collection and aren't able to make them run via squash TM using squash TA.
Our tests are using junit+selenium WebDriver+SpringFramework.
Currently, we launch our automated tests via maven (in commandLine), and we have a jenkins server running them regularly.
We tried to reuse our tests in a squash TA project, putting them in src/squashta/resources/selenium/java
But code in this folder doesn't even support java packages. It's like the java in the example isn't real java but a fake java parse by squashTA.
Is there any mean of using such already existing tests with squash(TA/TM) ?
Or, any alternatives you know that could do the job ? (we are currently using testlink and must change).
If your selenium test is in :
src/squashTA/resources/selenium-test/src/main/java/org/squashtest/ta/selenium/PetStoreTest.java
With a such structure, the test automation script to run the selenium test (which is in the package org.squashtest.ta.selenium) is :
TEST :
LOAD selenium-test/src/test AS seleniumTestSource
CONVERT seleniumTestSource TO script.java(compile) AS seleniumTestCompiled
CONVERT seleniumTestCompiled TO script.java.selenium2(script) USING $(org.squashtest.ta.selenium.PetStoreTest) AS seleniumTest
EXECUTE execute WITH seleniumTest AS seleniumResult
ASSERT seleniumResult IS success
If your selenium test has some dependencies to other libraries (like to spring in your case), you have to add those depencencies as dependency of the squash-ta-maven-plugin in the pom.xml of your Squash TA project

Resources