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.
After successfully downloading the remote model, you can obtain the downloaded model using getDownloadedModels() method of RemoteModelManager as you mentioned:
RemoteModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(aBoolean -> {
if (aBoolean) {
RemoteModelManager.getInstance().getDownloadedModels(remoteModel)
.addOnSuccessListener(downloadedModels -> {
// Save the downloaded model for later usage
// You can copy or move the model to the desired location
});
}
});
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:
String destinationPath = "/path/to/destination/model.tflite";
File sourceFile = downloadedModels.get(0).getFile();
File destinationFile = new File(destinationPath);
try {
// Copy the downloaded model to the destination file
Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Or you can move the downloaded model to the destination file
// Files.move(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Now the model is saved at the destination path for later usage
} catch (IOException e) {
// Handle the exception
}
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.