PytorchWrapper class
Wrapper class for PyTorch Lite operations.
This class provides a testable interface around the static methods of PytorchLite, allowing for dependency injection and easier testing. It wraps the core functionality for loading classification models while maintaining the same API surface.
Purpose
The wrapper serves several important purposes:
- Testability: Enables mocking of PyTorch operations in unit tests
- Dependency Injection: Allows services to depend on an interface rather than static methods
- Future Extensibility: Provides a place to add logging, error handling, or caching
- API Consistency: Maintains a consistent interface even if underlying implementation changes
Usage Example
final wrapper = PytorchWrapper();
final model = await wrapper.loadClassificationModel(
'assets/models/mosquito_classifier.pt',
224,
224,
labelPath: 'assets/labels/mosquito_species.txt',
);
Testing
For unit tests, this class can be easily mocked:
class MockPytorchWrapper extends Mock implements PytorchWrapper {}
test('classification service loads model', () async {
final mockWrapper = MockPytorchWrapper();
final mockModel = MockClassificationModel();
when(mockWrapper.loadClassificationModel(any, any, any, labelPath: any))
.thenAnswer((_) async => mockModel);
final service = ClassificationService(pytorchWrapper: mockWrapper);
await service.loadModel();
expect(service.isModelLoaded, isTrue);
});
See also:
- PytorchLite for the underlying PyTorch Lite implementation
- ClassificationService which uses this wrapper
- ClassificationModel for the model interface
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
loadClassificationModel(
String pathImageModel, int imageWidth, int imageHeight, {String? labelPath}) → Future< ClassificationModel> - Loads a classification model from the specified asset path.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited