loadClassificationModel method

Future<ClassificationModel> loadClassificationModel(
  1. String pathImageModel,
  2. int imageWidth,
  3. int imageHeight, {
  4. String? labelPath,
})

Loads a classification model from the specified asset path.

This method wraps PytorchLite.loadClassificationModel to provide a testable interface while maintaining the same functionality.

Parameters

  • pathImageModel: The asset path to the PyTorch model file (.pt format)
  • imageWidth: The expected width for input images (typically 224)
  • imageHeight: The expected height for input images (typically 224)
  • labelPath: Optional path to a labels file (.txt or .csv format)

Model Requirements

The PyTorch model must be:

  • Exported using PyTorch's mobile optimization tools
  • Compatible with the specified input dimensions
  • Trained for classification tasks (outputs class probabilities)

Label File Format

If provided, the label file should contain:

  • Text format (.txt): One label per line
  • CSV format (.csv): Comma-separated labels

Example label file content:

Aedes aegypti
Aedes albopictus
Anopheles gambiae
Culex pipiens

Returns a ClassificationModel instance ready for inference.

Throws Exception if:

  • The model file is not found in assets
  • The model format is invalid or incompatible
  • The platform doesn't support PyTorch Lite
  • Insufficient memory is available for model loading

Example:

final wrapper = PytorchWrapper();
try {
  final model = await wrapper.loadClassificationModel(
    'assets/models/mosquito_classifier.pt',
    224,
    224,
    labelPath: 'assets/labels/mosquito_species.txt',
  );
  print('Model loaded successfully');
} catch (e) {
  print('Failed to load model: $e');
}

Implementation

Future<ClassificationModel> loadClassificationModel(
  String pathImageModel,
  int imageWidth,
  int imageHeight, {
  String? labelPath,
}) {
  return PytorchLite.loadClassificationModel(
    pathImageModel,
    imageWidth,
    imageHeight,
    labelPath: labelPath,
  );
}