패키지, 저장소 및 앱 카테고리 검색

GitHub 저장소abdelaziz-mahdy/pytorch_lite

pytorch_lite

PyTorch Lite 모델 분류 및 YoloV5와 YoloV8를 실행하는 데 도움이 되는 플러터 패키지입니다.

pytorch_lite 프로젝트 이미지
스타
63
포크
28
최근 푸시(UTC)
2025. 10. 12.
프로젝트 상태
활성

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

pytorch_lite

⚠️ This package has been migrated to ExecutorTorch

This package (pytorch_lite) is deprecated. Please use the new executorch_flutter package instead:

  • GitHub: https://github.com/abdelaziz-mahdy/executorch_flutter
  • pub.dev: https://pub.dev/packages/executorch_flutter

  • flutter package to help run pytorch lite models classification and YoloV5 and YoloV8.

example for Classification

image

example for Object detection

image

Usage

preparing the model

  • classification
import torch
from torch.utils.mobile_optimizer import optimize_for_mobile


model = torch.load('model_scripted.pt',map_location="cpu")
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
optimized_traced_model = optimize_for_mobile(traced_script_module)
optimized_traced_model._save_for_lite_interpreter("model.pt")
  • object detection (yolov5)
!python export.py --weights "the weights of your model" --include torchscript --img 640 --optimize

example

!python export.py --weights yolov5s.pt --include torchscript --img 640 --optimize
  • object detection (yolov8)
!yolo mode=export model="your model" format=torchscript optimize

example

!yolo mode=export model=yolov8s.pt format=torchscript optimize
Installation

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

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

assets:
  - assets/models/model_classification.pt
  - assets/labels_classification.txt
  - assets/models/model_objectDetection.torchscript
  - assets/labels_objectDetection.txt

Run flutter pub get

For release
  • Go to android/app/build.gradle
  • Add those next lines in the release config
shrinkResources false
minifyEnabled false

example

    buildTypes {
        release {
            shrinkResources false
            minifyEnabled false
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
Import the library
import 'package:pytorch_lite/pytorch_lite.dart';
Load model

Either classification model:

ClassificationModel classificationModel= await PytorchLite.loadClassificationModel(
          "assets/models/model_classification.pt", 224, 224,
          labelPath: "assets/labels/label_classification_imageNet.txt");

Or objectDetection model:

ModelObjectDetection objectModel = await PytorchLite.loadObjectDetectionModel(
          "assets/models/yolov5s.torchscript", 80, 640, 640,
          labelPath: "assets/labels/labels_objectDetection_Coco.txt",
          objectDetectionModelType: ObjectDetectionModelType.yolov5);
Get classification prediction as label
String imagePrediction = await classificationModel.getImagePrediction(await File(image.path).readAsBytes());
Get classification prediction as label from camera image
String imagePrediction = await _objectModel.getCameraImagePrediction(
        cameraImage,
        rotation, // check example for rotation values
        );
Get classification prediction as raw output layer
List<double>? predictionList = await _imageModel!.getImagePredictionList(
      await File(image.path).readAsBytes(),
    );
Get classification prediction as raw output layer from camera image
List<double>? predictionList = await _imageModel!.getCameraImagePredictionList(
        cameraImage,
        rotation, // check example for rotation values
    );
Get classification prediction as Probabilities (incase model is not using softmax)
List<double>? predictionListProbabilities = await _imageModel!.getImagePredictionListProbabilities(
      await File(image.path).readAsBytes(),
    );
Get classification prediction as Probabilities (incase model is not using softmax)
List<double>? predictionListProbabilities = await _imageModel!.getCameraPredictionListProbabilities(
        cameraImage,
        rotation, // check example for rotation values
    );
Get object detection prediction for an image
 List<ResultObjectDetection> objDetect = await _objectModel.getImagePrediction(await File(image.path).readAsBytes(),
        minimumScore: 0.1, iOUThreshold: 0.3);
Get object detection prediction from camera image
 List<ResultObjectDetection> objDetect = await _objectModel.getCameraImagePrediction(
        cameraImage,
        rotation, // check example for rotation values
        minimumScore: 0.1, iOUThreshold: 0.3);
Get render boxes with image
objectModel.renderBoxesOnImage(_image!, objDetect)
Image prediction for an image with custom mean and std
final mean = [0.5, 0.5, 0.5];
final std = [0.5, 0.5, 0.5];
String prediction = await classificationModel
        .getImagePrediction(image, mean: mean, std: std);
Abdelaziz Mahdy GitHub avatar
GitHub 작성자

Abdelaziz Mahdy ↗

Canada
언어DartJavaObjective-C++Objective-CCMakeRubyShellSwiftC++C

사용 중인 의존성

의존성 목록 10 개

프로젝트에 맞는 저장소를 선택하려면?

사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.

  1. 이름, 작성자 또는 설명으로 검색하고 기술 주제로 범위를 좁힙니다.
  2. 스타, 포크, 최근 푸시를 함께 비교하세요. 스타는 관심의 지표이며 품질을 보장하지 않습니다.
  3. 사용하기 전에 GitHub에서 README, 라이선스, Issues를 확인하세요.

출처 및 관리

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗