Skip to content

[ML/Backend] Integrate MobileNetV3 Species Classifier into /api/v1/scan Endpoint #172

Description

@jpdevhub

Problem

The MobileNetV3-Small species model (Models/species_mobilenetv3.pth) was merged in but is not yet wired into the FastAPI scan pipeline. The /api/v1/scan endpoint currently returns freshness data only; it does not run species classification.

Goal

Connect the pre-trained .pth model to the scan endpoint so the API response includes species classification alongside freshness scores.

Acceptance Criteria

  • backend/species_classifier.py created — loads Models/species_mobilenetv3.pth at startup (not on every request) using torch.load with map_location='cpu'
  • /api/v1/scan response extended with a new species object:
    {
      "species": {
        "name": "Rohu Carp",
        "confidence": 0.945,
        "top3": [
          { "name": "Rohu Carp", "confidence": 0.945 },
          { "name": "Catla Carp", "confidence": 0.038 },
          { "name": "Mrigal Carp", "confidence": 0.017 }
        ]
      }
    }
  • Model inference runs with torch.no_grad() for performance
  • Image pre-processing matches the training pipeline: resize to 224×224, ImageNet normalization
  • If species model fails for any reason, the scan still succeeds (species is null, not a 500 error)
  • Unit test added: backend/tests/test_species_classifier.py

Supported Species (10 classes)

Rohu Carp, Catla Carp, Mrigal Carp, Pangas, Basa, Tilapia, Pomfret, Kingfish, Mackerel, Sardine

Notes

  • Model is already in Models/species_mobilenetv3.pth (6.0 MB, MobileNetV3-Small, 94.5% val accuracy)
  • Do NOT reload the model on every request — load once at module import time using a module-level variable
  • This task is a prerequisite for the frontend UI issue (species display on ResultsPage)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions