while using the "Detect, track and classify objects with a custom classification model on Android"
as explained here:
link to tutorial
I am bounded to use the remote model only while wi-fi network is connected (as supposes to be to save users from cellular data usage). I want to use remote model BUT save it after downloaded for later usage in cache. I cannot use all the models as local to begin with because the APK will be too big (it will bundle the models).
This is how my supposes to load local model:
LocalModel localModel =
new LocalModel.Builder()
.setAssetFilePath("model.tflite")
// or .setAbsoluteFilePath(absolute file path to model file)
// or .setUri(URI to model file)
.build();
This is how to load a model from Firebase:
DownloadConditions downloadConditions = new DownloadConditions.Builder()
.requireWifi()
.build();
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
RemoteModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(aBoolean -> {
tensorDownloaded = aBoolean;
AppendLogError("loadTesnsor::tensor model loaded::3");
});
}
})
Maybe – I can save the model when the download finished? if yes – how to save it?
something like this:
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
RemoteModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(aBoolean -> {
RemoteModelManager.getInstance().getDownloadedModels(HOW TO SAVE THE MODEL)
tensorDownloaded = aBoolean;
});
}
})
Any other recommendation of how to save APK file size and still manage several models will be great.
2
Answers
Following the answer from @BugCreator & google MLKit support, MLKit support I have concluded that the best way is to check if the model was downloaded & download if needed. no need to save it locally. (although the "save to cache" feature can be useful if you want to save a local copy to prevent future downloading if cache erase/ data optimizations was done by the google APIs)
I am posting my final code in case it will help someone.
After successfully downloading the remote model, you can obtain the downloaded model using getDownloadedModels() method of RemoteModelManager as you mentioned:
In the addOnSuccessListener callback, you can retrieve the downloaded model using the getDownloadedModels() method. This will give you a list of DownloadedModel objects representing the downloaded models. You can save this model to the desired location in your cache or storage using standard file operations. For example, you can copy or move the model to a specific directory:
Adjust the destinationPath variable to specify the desired location and file name for the saved model.
By following these steps, you can save the downloaded remote model to your cache or storage for later usage while managing the APK file size. Remember to handle any necessary error handling and permissions related to file operations.