B4A Library TensorFlowLite - an experimental machine/deep learning wrapper

inakigarm

Well-Known Member
Licensed User
New version: 0.20 (29/08/2018):
I have updated the library and the sample-models because the first version was based on older code.
I attach also the updated java-sources.
If you used the first version, please update the demo and the B4A-libs.

I also created my own model to use with the wrapper which works really well. I have put some links to some good resources/tutorials if you want to create a compatible model. Some sample screenshots of my guitar-model are shown in the spoiler in the first post.

Great! Any chance to have an B4J Lib? (It seems it supports Java access to their API https://www.tensorflow.org/install/install_java)
 

moster67

Expert
Licensed User
Great! Any chance to have an B4J Lib? (It seems it supports Java access to their API https://www.tensorflow.org/install/install_java)
Yes, yesterday I started writing a small wrapper for B4J. It works fine. There are some methods I'd like to add before publishing it such as dealing with Images (equivalent to Bitmaps in B4A) in memory. I also want to test it with a model created by me. Once I have this working, I will publish it.
 

microbox

Active Member
Licensed User
Hi moster67...thank you for this lib. I'm new to this (ML) and I would like to create an app to classify leaf if this possible. Any guide that will help me to start is much appreciated.
 

moster67

Expert
Licensed User
Follow the two tutorials mentioned in the first post

You need to set up Python on your computer, better if it is Mac or Linux but it will work with Windows too although you may need to change some things in the parameters you pass on to the training scripts.

You can probably record a video of your objects (leaves) i.e a video for each object. Make sure to record from different angles, in different light conditions etc. Then you can use ffmpeg to extract single photos of each object and then classify them according to the instructions in the tutorial

Then you train your model, trying to find the right resolution of the photos to use. You need to experiment a lot for best results in order to avoid overfitting and underfitting. It takes trial and error to get the best results

Watch tutorials on YouTube, get a book to understand better.

With patience you will get there. Good luck.
 

microbox

Active Member
Licensed User
@moster67
Hi...Thanks for again for the library. The example runs with no problem. I followed the tutorial at code lab. I have a running python and tensorflow. My question is... to test the flower photos on your given demo application. I need to drop existing graph.lite and labels.txt and replace it with the same filename and extension produced in tensorflow-for-poets-2 under android\tflite\app\src\main\assets folder?

I think I'm missing somewhere...it recognise daisy photos only...don't recognise rose and other flowers.

regards,
joe
 
Last edited:

moster67

Expert
Licensed User
Add your graph and labels file to your Asset-folder. I don't remember but I think you can name them whatever you want as long as you use the right extension names (i.e *.lite and *.txt). Then when you initialize the lib, you pass on the names of the files:
B4X:
ten.Initialize("", 224, "yourgraph.lite", "yourlabels.txt")
If you use my demo-app and use different names, make sure to change the code which verifies if a file exists in the asset-folder.
At this point, you should be able to remove any previous files (if present)
 

microbox

Active Member
Licensed User
Add your graph and labels file to your Asset-folder. I don't remember but I think you can name them whatever you want as long as you use the right extension names (i.e *.lite and *.txt). Then when you initialize the lib, you pass on the names of the files:
B4X:
ten.Initialize("", 224, "yourgraph.lite", "yourlabels.txt")
If you use my demo-app and use different names, make sure to change the code which verifies if a file exists in the asset-folder.
At this point, you should be able to remove any previous files (if present)
I think I know what I'm missing...the following code does not execute "tflite_convert --help" this tool is required to convert
retrained_graph.pb to *.lite file. How can install tflite converter? BTW i'm running on mac.

thanks,
Joe
 

moster67

Expert
Licensed User
My library allows user to use their graphs with Android and hence your question is rather off-topic since your problem seems related to setting up the tools needed on your Mac and generating the graphs.

As mentioned earlier, to create the graphs, to do the training avoiding overfitting/underfitting and so on take some time and lots of "trial and error" etc. There are many sources on the internet, such as You Tube tutorials, guides, information on StackOverflow which can help you. That is the way I did it and eventually I got it right :)

In your case, maybe something such as a dependency was not installed correctly or maybe the input files in the script(s) are not pointing to the correct file locations and so on. Hard to tell for me. You are probably better off asking on StackOverflow giving as much information as possible as to what you have done and perhaps someone can help you out.

I am sorry I cannot be of any further help now, also because I have moved on to other projects which is keeping me really busy and also because it was some time ago I worked on the wrapper and machine learning and I don't remember everything (I am getting old...).
 
Last edited:

peacemaker

Expert
Licensed User
Hi, All
Is it possible by this lib to identify the objectX among similar object0....object999 that have some image-hashs in a db?
 

moster67

Expert
Licensed User
I guess you mean through image recognition. If yes, then it should be possible but you need to train your model and test it properly.

The easiest way is to film your single objects (from different angles and with different light conditions), then label each video (maybe with your image-hashs) and then use ffmpeg to extract the frames. Using the frames, you can train the model. Once you have generated the model with all your existing objects, you can thereafter add other objects when required, repeating partial steps of the training.

I wrote some hints in this thread what regards tutorials etc. Be prepared it will take some time to get good results with the training and to fine-tune.

Good luck.
 

peacemaker

Expert
Licensed User
to get good results with the training and to fine-tune
Thanks. But anyway what can be the probability of correct recognition an object among the almost same ? Say, cups set with the various printings on them.
 

moster67

Expert
Licensed User
Thanks. But anyway what can be the probability of correct recognition an object among the almost same ? Say, caps set with the various printings on them
As long there is something which distinguishes them apart, I think you can expect good results. I know people who have implemented recognition of flowers. The source (video-frames) and the training (avoiding overfitting/underfitting) are important. Start practicing and testing and see what results you get.
 

peacemaker

Expert
Licensed User
recognition of flowers
Here yes, i can imagine rather noticeable difference in flowers. But if similar Internet-shop's items, with difference just in some part of the same surface...
And if for this kind the probability is < 99%, i guess, such solution won't be interesting to the customer...
But, it seems, unknown result without practical test.
 

moster67

Expert
Licensed User
unknown result without practical test
Yep, you need to test. That is part of the learning curve. Trial and error will give you experience and better understanding if a project is viable or not. In this case, there are no shortcuts. Sorry.
 

rkxo

Active Member
Licensed User
hi, i get error. Any Idea?
ok - model file is present in assets-foilder
** Activity (main) Resume **
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/tensorflow/lite/Interpreter;
at com.tillekesoft.tensorflowlite.TensorFlowImageClassifier.create(TensorFlowImageClassifier.java:58)
at com.tillekesoft.tensorflowlite.TensorflowWrapper$1.run(TensorflowWrapper.java:47)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.tensorflow.lite.Interpreter" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.tillekesoft-ZLxZoPw0sjbm9tX4UwpIjA==/base.apk"],nativeLibraryDirectories=[/data/app/com.tillekesoft-ZLxZoPw0sjbm9tX4UwpIjA==/lib/x86_64, /data/app/com.tillekesoft-ZLxZoPw0sjbm9tX4UwpIjA==/base.apk!/lib/x86_64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 5 more
 

ZJP

Active Member
Licensed User
Hi,

Great work! Building models for Tensorflow is still hard but an exciting new AI tool called Lobe.ai will change all of this very soon. With that tool you can create any AI model you like and export it to Tensorflow.

It's not released yet but it promises a lot!
Done : Lobe AI is available.


 
Top