How astroshreyas1/lesioniq works
1. This is a split monorepo with two main runtime surfaces: a Python backend that performs dermoscopy inference/training, and a React + TypeScript frontend that acts as a clinical review workstation. The two communicate through one backend API contract centered on a typed CaseRecord.
2. The backend is the computational core. backend/api.py exposes a FastAPI endpoint (POST /cases/analyze) that receives an uploaded image plus patient metadata, then calls the inference pipeline. A standalone CLI wrapper also exists via backend/inference.py, so the same pipeline serves both API and command-line use.
3. The inference flow is sequential and artifact-producing: input image + metadata → preprocessing → hybrid classifier inference → post-processing/calibration → explainability generation → local SLM narration → diagnosis.json plus image artifacts. The output is stored as a per-case bundle under backend/output/inference/<case_id>/.
4. Image preprocessing is its own subsystem under backend/preprocessing/. It applies a fixed dermoscopy cleanup chain: hair removal (DullRazor), color normalization, CLAHE in LAB space, and circular border/vignette removal. These stages are saved as intermediate images and exposed to the frontend for provenance review.
5. The classifier subsystem under backend/classifier/ contains the model definition, training, evaluation, explainability, and inference code. The core model is a dual-backbone hybrid: EfficientNet-B4 + Swin Transformer V2, with an optional metadata MLP fused into the final classifier head for the full mode.
6. Training is separate from inference and is driven by ISIC 2019 data manifests in backend/data/. It includes weighted sampling, augmentation, CutMix, SWA, and ablation scripts for the different model modes. This keeps the offline model-development path isolated from the runtime API path.
7. Post-training calibration is another distinct layer. Files in backend/checkpoints/ hold the trained weights and learned runtime calibration parameters: threshold scales, temperature, and a MEL safety threshold. These are loaded during inference to adjust class scores and decision thresholds without retraining.
8. Explainability is first-class in the backend. The pipeline emits Grad-CAM++ overlays for the CNN branch, attention rollout for the transformer branch, and metadata attribution summaries. These are packaged into the case bundle and surfaced in the UI as separate review artifacts rather than being hidden behind one combined score.
9. There is also a local small-language-model explanation step. The backend passes diagnosis.json and visual evidence to an Ollama-hosted Gemma model inside Docker Compose, then validates or repairs the generated report before returning it. The architecture explicitly treats this as a downstream narrative layer, not the source of truth for diagnosis.
10. The frontend under frontend/src/ is a stateful single-page app that consumes the same typed case record across multiple screens: Case Review, Explainability, Preprocessing, History, Compare, and Settings. It is organized into layout, primitive UI, and domain-specific components, with the API adapter isolated in frontend/src/lib/lesioniqApi.ts.
11. The frontend’s main data boundary is the live analysis response. Uploaded images only run inference once required metadata is complete, and the UI expects the backend to return both structured predictions and artifact URLs. Demo cases exist only as optional seed data behind an environment flag and do not change the live API path.
12. Deployment is also split: the frontend is built as a static Vite app, while the backend runs on a local GPU host. Docker Compose brings up the backend plus Ollama, and the README also documents a Vercel + ngrok pattern for exposing the frontend and backend separately. External dependencies that materially affect architecture are ISIC 2019 for training and Ollama/Gemma for narrative generation.
13. In diagram form, the key boundary is between “analysis bundle generation” in Python and “case review/rendering” in React. Everything else—preprocessing, classification, calibration, explainability, and SLM narration—feeds that bundle, while the UI is a consumer of it rather than a participant in model logic.
Main entry points: App.tsx App shell · models.py Classifier · preprocessing Preprocess · api.py API
Components 29
Python backend
Inference, preprocessing, calibration, and training pipeline.
- API · Receives case analysis requests and returns bundled results.
backend/api.py - CLI · Command-line entrypoint for the same inference pipeline.
backend/inference.py - Preprocess · Dermoscopy cleanup chain: hair removal, normalization, contrast enhancement, and border cleanup.
backend/preprocessing - DullRazor · Hair removal stage.
backend/preprocessing/dull_razor.py - Normalize · Color normalization stage in the preprocessing chain.
backend/preprocessing/shades_of_grey.py - CLAHE · Contrast enhancement in LAB space.
backend/preprocessing/apply_clahe.py - Border removal · Circular border and vignette removal stage.
backend/preprocessing/remove_circular_border.py - Classifier · Dual-backbone dermoscopy model with optional metadata fusion.
backend/classifier/models.py - EfficientNet · CNN branch used for local lesion feature extraction.
backend/classifier/models.py - Swin V2 · Transformer branch used for global image context.
backend/classifier/models.py - Metadata MLP · Optional patient metadata encoder fused into the classifier head.
backend/classifier/models.py - Training · Weighted sampling, augmentation, CutMix, SWA, and ablations.
backend/classifier/train.py - Evaluation · Model evaluation and ablation workflows.
backend/classifier/evaluate.py - Calibration · Loads threshold scales, temperature, and safety thresholds for inference.
backend/checkpoints - Explainability · Produces Grad-CAM++, attention rollout, and metadata attribution summaries.
backend/classifier/explainability.py - Case bundle · Per-case diagnosis JSON plus image artifacts produced by inference.
- Narration · Generates and repairs natural-language report text from diagnosis and evidence.
backend/api.py
React frontend
Clinical review workstation and UI state.
- Case record · Shared typed case payload and artifact metadata consumed by the UI.
frontend/src/types/lesioniq.ts - API client · Typed adapter for backend case analysis and artifact retrieval.
frontend/src/lib/lesioniqApi.ts - App shell · Top-level navigation and screen coordination for the workstation.
frontend/src/app/App.tsx - Case review · Primary review surface for predictions, metadata, and uploaded cases.
frontend/src/screens/CaseReview.tsx - Explainability UI · Renders overlays, attribution summaries, and evidence panels.
frontend/src/screens/Explainability.tsx - Preprocessing UI · Shows preprocessing provenance and intermediate image artifacts.
frontend/src/screens/Preprocessing.tsx - History · Displays prior cases and review history.
frontend/src/screens/History.tsx - Compare · Side-by-side case comparison workspace.
frontend/src/screens/Compare.tsx - Settings · User preferences and application configuration.
frontend/src/screens/Settings.tsx
Deployment
Containerization and local service orchestration.
- Compose · Runs the backend alongside local Ollama for narration generation.
docker-compose.yml - Backend image · Backend runtime container definition.
backend/Dockerfile - Frontend build · Vite-based frontend build and deployment surface.
frontend/vite.config.ts
Connections 32
- API → Preprocess: preprocesses · API calls the image cleanup chain before inference.
- API → Classifier: runs · API invokes the hybrid classifier for a case.
- CLI → Preprocess: preprocesses · CLI uses the same preprocessing pipeline.
- CLI → Classifier: runs · CLI shares the inference core.
- Preprocess → DullRazor: stage · Hair removal stage.
- Preprocess → Normalize: stage · Color normalization stage.
- Preprocess → CLAHE: stage · Contrast enhancement stage.
- Preprocess → Border removal: stage · Final artifact cleanup stage.
- Classifier → EfficientNet: backbone · CNN branch used by the hybrid model.
- Classifier → Swin V2: backbone · Transformer branch used by the hybrid model.
- Classifier → Metadata MLP: optional fusion · Metadata path used in full model mode.
- Classifier → Calibration: applies · Inference loads calibration parameters before final decisions.
- Classifier → Explainability: emits · Model inference produces explainability artifacts.
- Training → Classifier: trains · Offline training produces model weights for runtime use.
- Training → Evaluation: measured by · Training is assessed through evaluation and ablation workflows.
- Calibration → Case bundle: writes · Calibrated scores and thresholds contribute to diagnosis output.
- Explainability → Case bundle: packages · Explainability outputs are stored in the per-case bundle.
- API → Narration: narrates via · API sends diagnosis evidence to the local language model step.
- Narration → Case bundle: repairs · Narrative output is validated and stored alongside the case bundle.
- Case bundle → Case record: exposes · The backend bundle is mapped into the typed case record.
- Case record → API client: consumed by · Frontend uses the typed contract through its API adapter.
- API client → App shell: feeds · Fetched case data drives the SPA.
- App shell → Case review: navigates · Primary review screen.
- App shell → Explainability UI: navigates · Explainability review screen.
- App shell → Preprocessing UI: navigates · Preprocessing provenance screen.
- App shell → History: navigates · Historical case screen.
- App shell → Compare: navigates · Comparison screen.
- App shell → Settings: navigates · Configuration screen.
- Compose → API: runs · Compose launches the backend service.
- Compose → Narration: runs · Compose also launches local Ollama for narration.
- Backend image → API: builds · Container image packages backend runtime.
- Frontend build → App shell: builds · Vite builds the frontend application.