How to use Pytorch mobile on Flutter
Using pytorch on mobile is a great idea, because it allows the user to use the models that we create without the need of connecting to a server.
We need to be careful tho because sometimes our models can be really big and can be detrimental to the user, so using a server for the processing can be really good.
That being said if the model can run really fast it's a great thing if the user has it locally.
So the overview of how to use pytorch on flutter is using the method channel to run native code in this case java or kotlin.
- Create method channel to run native java code
- Send data to method channel in case of a image an uint8list
- Receive the data in the backend and parse it, it case of a an image use
BitmapFactory.decodeByteArray(byteList, boffset, blength);
- Run the forward method of the module
Keep in mind that the model needs to output a tensor or something that you really understand and can parse it easily in java.
Sometimes the model outputs a map so the output of the model is in the "out" key of the map.
- Send the data to the frontend and display it
You do this by result.success(data_to_send);
then keep in mind the type conversion from java to dart or the rest of the programming languages.