How to use Pytorch mobile on Flutter

overview Dec 14, 2020

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.

  1. Create method channel to run native java code
  2. Send data to method channel in case of a image an uint8list
  3. Receive the data in the backend and parse it, it case of a an image use BitmapFactory.decodeByteArray(byteList, boffset, blength);
  4. 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.
  5. 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.

Tags

Ramiro

Hey hi! My name is Ramiro, this is my blog about artificial intelligence, coding and things that are in my head. Follow me on twitter @ramgendeploy so we can chat!