pytorch_mobile 0.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

pytorch_mobile #

A flutter plugin for pytorch model inference. Since this is still being developed, the plugin is only supported for Android. An iOS version is going to come soon

Usage #

Installation #

To use this plugin, add pytorch_mobile as a dependency in your pubspec.yaml file.

Create a assets folder with your pytorch model and labels if needed. Modify pubspec.yaml accoringly.

assets:
 - assets/models/model.pt
 - assets/labels.csv

Run flutter pub get

Import the library #

import 'package:pytorch_mobile/pytorch_mobile.dart';

Load model #

Either custom model:

Model customModel = await PyTorchMobile
        .loadModel(model: 'assets/models/custom_model.pt');

Or image model:

Model imageModel = await PyTorchMobile
        .loadModel(model: 'assets/models/resnet18.pt');

Get custom prediction #

List prediction = await customModel
        .getPrediction([1, 2, 3, 4], [1, 2, 2], DType.float32);

Get prediction for an image #

String prediction = await _imageModel
        .getImagePrediction(image, 224, 224, "assets/labels/labels.csv");

Contact #

fynnmaarten.business@gmail.com

0.0.1 #

  • Initial Release

0.0.2 #

  • Documented code

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';

import 'package:pytorch_mobile/pytorch_mobile.dart';
import 'package:pytorch_mobile/model.dart';
import 'package:pytorch_mobile/enums/dtype.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Model _imageModel, _customModel;

  String _imagePrediction;
  List _prediction;
  File _image;

  @override
  void initState() {
    super.initState();
    //load your model
    try {
      PyTorchMobile.loadModel("assets/models/resnet.pt")
          .then((model) => _imageModel = model);
      PyTorchMobile.loadModel("assets/models/custom_model.pt")
          .then((model) => _customModel = model);
    } on PlatformException {
      print("only supported for android so far");
    }
  }

  //run an image model
  Future runImageModel() async {
    //pick a random image
    File image = await ImagePicker.pickImage(
        source: ImageSource.camera, maxHeight: 224, maxWidth: 224);
    //get prediction
    //labels are 1000 random english words for show purposes
    _imagePrediction = await _imageModel.getImagePrediction(
        image, 224, 224, "assets/labels/labels.csv");

    setState(() {
      _image = image;
    });
  }

  //run a custom model with number inputs
  Future runCustomModel() async {
    _prediction = await _customModel
        .getPrediction([1, 2, 3, 4], [1, 2, 2], DType.float32);

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Pytorch Mobile Example'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _image == null ? Text('No image selected.') : Image.file(_image),
            Center(
              child: Visibility(
                visible: _imagePrediction != null,
                child: Text("$_imagePrediction"),
              ),
            ),
            Center(
              child: FlatButton(
                onPressed: runImageModel,
                child: Icon(Icons.add_a_photo),
              ),
            ),
            FlatButton(
              onPressed: runCustomModel,
              color: Colors.blue,
              child: Text(
                "Run custom model",
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            Center(
              child: Visibility(
                visible: _prediction != null,
                child: Text(_prediction != null
                    ? "${_prediction[0]}"
                    : ""),
              ),
            )
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pytorch_mobile: ^0.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pytorch_mobile/pytorch_mobile.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
46
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:pytorch_mobile/pytorch_mobile.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform ios

Because:

  • package:pytorch_mobile/pytorch_mobile.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform linux

Because:

  • package:pytorch_mobile/pytorch_mobile.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:pytorch_mobile/pytorch_mobile.dart that declares support for platforms: android, ios

Package does not support Flutter platform web

Because:

  • package:pytorch_mobile/pytorch_mobile.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:pytorch_mobile/pytorch_mobile.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • pytorch_mobile that is a package requiring null.

Health suggestions

Format lib/pytorch_mobile.dart.

Run flutter format to format lib/pytorch_mobile.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
path ^1.6.4 1.7.0
path_provider ^1.6.5 1.6.11
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
intl 0.16.1
meta 1.1.8 1.2.2
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test