Skip to content

Integrated MobileNetV2 model directly on mobile devices, utilizing ImageNet normalization, center cropping, softmax activation, Top-K predictions, and confidence checks with fallback. This implementation enhances on-device performance and efficiency.

Notifications You must be signed in to change notification settings

PHom798/MobileNetV2-On-Device-Inference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

25 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  MobileNetV2 On-Device Inference

Real-Time Image Classification Powered by ONNX Runtime

Typing SVG

Made with Flutter ONNX Runtime MobileNetV2 Dart


Version Platform Model Size Status


๐Ÿš€ Bringing Computer Vision to Your Pocket

Complete on-device image classification with zero latency and maximum privacy

iphone-zoom-out-middle-move-out.1.online-video-cutter.com.mp4


โœจ Features

๐ŸŽฏ Core Capabilities

  • ๐Ÿ”ฅ MobileNetV2 Integration - Optimized CNN architecture running entirely on-device
  • โšก ONNX Runtime - High-performance inference engine for mobile platforms
  • ๐Ÿ“ธ ImageNet Classification - Recognizes 1000+ object categories with high accuracy
  • ๐ŸŽจ Production-Ready Preprocessing - ImageNet normalization and center crop
  • ๐Ÿ† Top-K Predictions - Configurable multi-class prediction output
  • โœ… Confidence Thresholding - Smart fallback for low-confidence predictions
  • ๐Ÿ”’ Privacy-First - All inference happens locally, no data leaves device
  • ๐Ÿ“ฑ Cross-Platform - Runs on Android and iOS with Flutter

๐Ÿ› ๏ธ Technical Highlights

  • Preprocessing Pipeline: ImageNet-standard normalization (mean/std)
  • Input Processing: Center crop to 224x224 model input size
  • Output Processing: Softmax activation for probability distribution
  • Confidence Filtering: Configurable threshold with fallback handling
  • Top-K Selection: Returns top N predictions with scores

โœจ Learn

Check out my blog post on Mastering On-Device ML in Flutter: A Guide to Softmax, Top-K, and Confidence Checks for more insights on implementing machine learning models efficiently.


๐ŸŽฅ Previews

Colorful QR Example 1
demoai.mp4
Colorful QR Example 1

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Camera/Gallery โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Image Loading   โ”‚
โ”‚  & Decoding     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Center Crop    โ”‚
โ”‚   (224x224)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   ImageNet      โ”‚
โ”‚ Normalization   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  MobileNetV2    โ”‚
โ”‚  ONNX Model     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Softmax      โ”‚
โ”‚   Activation    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Top-K + CI    โ”‚
โ”‚   Filtering     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Display Resultsโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ฆ Installation

Prerequisites

dependencies:
  flutter:
    sdk: flutter
  onnxruntime: ^1.15.0  # Or latest version
  image: ^4.0.0

Setup Steps

  1. Clone the repository
https://github.com/PHom798/MobileNetV2-On-Device-Inference.git
cd mobilenetv2-flutter
  1. Install dependencies
flutter pub get
  1. Add the ONNX model

    • Download MobileNetV2 ONNX model
    • Place in assets/models/mobilenetv2.onnx
    • Add to pubspec.yaml:
    flutter:
      assets:
        - assets/models/mobilenetv2.onnx
        - assets/labels/imagenet_classes.txt
  2. Run the app

flutter run

๐ŸŽฎ Usage

Basic Implementation

import 'package:onnxruntime/onnxruntime.dart';

class ImageClassifier {
  late OrtSession session;
  
  Future<void> initialize() async {
    // Load model
    final modelBytes = await rootBundle.load('assets/models/mobilenetv2.onnx');
    session = OrtSession.fromBuffer(modelBytes.buffer.asUint8List());
  }
  
  Future<List<Prediction>> classify(String imagePath) async {
    // 1. Load and preprocess image
    final preprocessed = await preprocessImage(imagePath);
    
    // 2. Run inference
    final inputs = {'input': OrtValueTensor.createTensorWithDataList(
      preprocessed,
      [1, 3, 224, 224],
    )};
    
    final outputs = await session.runAsync(
      OrtRunOptions(),
      inputs,
    );
    
    // 3. Apply softmax
    final logits = outputs[0]?.value as List<List<double>>;
    final probabilities = softmax(logits[0]);
    
    // 4. Get Top-K with confidence threshold
    return getTopKPredictions(probabilities, k: 5, threshold: 0.1);
  }
}

Preprocessing Pipeline

Future<List<double>> preprocessImage(String path) async {
  final img = decodeImage(File(path).readAsBytesSync())!;
  
  // Center crop to 224x224
  final cropped = copyCrop(img, 
    x: (img.width - 224) ~/ 2,
    y: (img.height - 224) ~/ 2,
    width: 224,
    height: 224,
  );
  
  // ImageNet normalization
  final mean = [0.485, 0.456, 0.406];
  final std = [0.229, 0.224, 0.225];
  
  List<double> normalized = [];
  for (var c = 0; c < 3; c++) {
    for (var y = 0; y < 224; y++) {
      for (var x = 0; x < 224; x++) {
        final pixel = cropped.getPixel(x, y);
        final value = (pixel[c] / 255.0 - mean[c]) / std[c];
        normalized.add(value);
      }
    }
  }
  
  return normalized;
}

๐Ÿ“Š Model Details

Property Value
Architecture MobileNetV2
Input Size 224 ร— 224 ร— 3
Parameters ~3.5M
Model Size ~14MB
Classes 1000 (ImageNet)
Top-1 Accuracy ~71.8%
Top-5 Accuracy ~90.3%
Inference Time 20-50ms (device dependent)

๐Ÿ”ง Configuration

Adjustable Parameters

class ModelConfig {
  static const int inputSize = 224;
  static const int topK = 5;
  static const double confidenceThreshold = 0.1;
  static const List<double> imagenetMean = [0.485, 0.456, 0.406];
  static const List<double> imagenetStd = [0.229, 0.224, 0.225];
}

๐ŸŒŸ Key Advantages

๐Ÿ”’ Privacy & Security

  • 100% On-Device - No internet required
  • Zero Data Transmission - Images never leave device
  • GDPR Compliant - No external data processing

โšก Performance

  • Low Latency - Instant results without network delay
  • Offline First - Works without connectivity
  • Efficient - Optimized for mobile CPUs

๐Ÿ’ฐ Cost-Effective

  • No API Costs - Zero inference fees
  • Scalable - No server infrastructure needed
  • Sustainable - Reduced carbon footprint

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ™ Acknowledgments

  • MobileNetV2 - Sandler et al., 2018
  • ONNX Runtime - Microsoft's cross-platform inference engine
  • ImageNet - Dataset and pretrained weights
  • Flutter Team - Amazing cross-platform framework

๐Ÿ’ฌ Connect & Support

For questions, feedback, or collaborations:

GitHub Twitter LinkedIn Email


โญ Star this repo if you find it useful!

Made with โค๏ธ and Flutter

Footer

About

Integrated MobileNetV2 model directly on mobile devices, utilizing ImageNet normalization, center cropping, softmax activation, Top-K predictions, and confidence checks with fallback. This implementation enhances on-device performance and efficiency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published