easily convert image to in-memory tfLite model


In the early ML Kit example there was an example that used 'org.deeplearning4j' to easily convert an image to an in-memory tflite model:

tfLiteModel model = tf.keras.convert(JPEGimage);

sure you can train Machine Learning with single images, but you have to retrain once images change.
in above sample you could just replace the whole image.

i don't find the example anymore, it has not been moved to github.

is there any solution similarily easy?

-1
Nov 13 at 9:53 AM
User AvatarMaria Sumer
#android#ml

Accepted Answer

That example probably never existed in that form. org.deeplearning4j is not a TFLite converter, and tf.keras.convert(image) is not a real call. A single image can't become a trained model anyway.

What you likely want is matching against a reference image you can swap without retraining. Use image embeddings: a pretrained model turns each image into a vector, MediaPipe's Image Embedder does this:

val options = ImageEmbedder.ImageEmbedderOptions.builder()
    .setBaseOptions(BaseOptions.builder().setModelAssetPath("mobilenet_v3_small.tflite").build())
    .setL2Normalize(true)
    .build()
val embedder = ImageEmbedder.createFromOptions(context, options)

fun embed(bmp: Bitmap) =
    embedder.embed(BitmapImageBuilder(bmp).build()).embeddingResult().embeddings().first()

var reference = embed(referenceBitmap)   // swap anytime, no retraining

fun matches(bmp: Bitmap) =
    ImageEmbedder.cosineSimilarity(reference, embed(bmp)) > 0.8   // tune the threshold

If you need an exact match rather than a similar-looking one, OpenCV feature matching (ORB/SIFT) may work better. I haven’t run this, so test the threshold on your own images.

User AvatarTanveer Munir
Nov 13 at 10:13 AM
0