mindspore_lite_flutter

pub.dev Platform

A Flutter plugin for on-device AI inference using Huawei MindSpore Lite 1.7.0.

Built for the AgriChain project — crop disease detection and yield prediction for smallholder farmers in Africa.

Features

  • 🧠 Run .ms MindSpore Lite models on-device (no internet required)
  • 📷 Image classification from camera or file path
  • ⚡ ARM64 optimised — runs on any Android device with ARM64 architecture
  • 🌽 Pre-trained models for maize and coffee disease detection included in AgriChain

Platform Support

Android iOS
✅ ARM64 ❌ Not yet

iOS support is planned. MindSpore Lite has an iOS runtime but integration is not yet complete.

Installation

dependencies:
  mindspore_lite_flutter:
    git:
      url: https://github.com/Mas-aaron/agri-chain.git
      path: mindspore_lite_flutter

Or once published to pub.dev:

dependencies:
  mindspore_lite_flutter: ^1.0.0

Usage

1. Add your .ms model to Flutter assets

# pubspec.yaml
flutter:
  assets:
    - assets/models/maize_model.ms
    - assets/models/maize_labels.txt

2. Initialize the model

import 'package:mindspore_lite_flutter/mindspore_lite_flutter.dart';

final success = await MindsporeLiteFlutter.initModel(
  'assets/models/maize_model.ms',
);

3. Run inference

final result = await MindsporeLiteFlutter.predictImage(imageFile.path);

// result contains:
// {
//   'predictions': [0.92, 0.05, 0.03],  // raw logits per class
//   'inferenceMs': 45,                   // inference time in ms
// }

4. Apply softmax and get top-K results

import 'dart:math' as math;

List<double> softmax(List<double> logits) {
  final maxVal = logits.reduce(math.max);
  final expVals = logits.map((v) => math.exp(v - maxVal)).toList();
  final sumExp = expVals.reduce((a, b) => a + b);
  return expVals.map((v) => v / sumExp).toList();
}

5. Dispose when done

await MindsporeLiteFlutter.disposeModel();

API Reference

Method Description
initModel(String modelPath) Load a .ms model from Flutter assets
predictImage(String imagePath) Run inference on an image file
disposeModel() Release native resources

Model Format

Models must be in MindSpore Lite .ms format. To convert from .mindir:

# Using MindSpore Lite converter
converter_lite \
  --fmk=MINDIR \
  --modelFile=model.mindir \
  --outputFile=model.ms \
  --targetDevice=ARM64

Training Your Own Models

See the AgriChain training notebook for a complete example using MobileNetV2 on Huawei ModelArts.

Requirements

  • Android API 21+ (Android 5.0)
  • ARM64-v8a architecture
  • MindSpore Lite 1.7.0 AAR (bundled)

License

MIT — see LICENSE

Credits

Built by the AgriChain team. Uses Huawei MindSpore Lite runtime.