# m-hasan-code/pytorch-cnn-with-fastapi architecture

> Architecture diagram of the GitHub repository m-hasan-code/pytorch-cnn-with-fastapi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/m-hasan-code/pytorch-cnn-with-fastapi
- Repository: https://github.com/m-hasan-code/pytorch-cnn-with-fastapi

## Overview

This is a FastAPI inference service for classifying uploaded MRI-compatible images with a pretrained four-class PyTorch CNN. The workflow is: a client uploads an image, the prediction endpoint validates and reads it, preprocessing converts it to a grayscale tensor, the model produces class probabilities, and the endpoint returns the predicted class and confidence as JSON. The CNN and serving logic are in main.py. The README describes the pretrained model, but the referenced my_model.pth file is not present in the supplied tree.

Main entry points: [`main.py`](https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py) (Prediction endpoint)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_serving["API serving"]
  node_endpoint["Prediction endpoint<br/>[main.py]"]
  node_json["Prediction JSON<br/>[main.py]"]
end

subgraph group_inference["Model inference"]
  node_preprocess["Image preprocessing<br/>[main.py]"]
  node_cnn["CNN classifier<br/>[main.py]"]
  node_weights["Model weights"]
end

node_client(("Image client"))

node_client -->|"uploads image"| node_endpoint
node_endpoint -->|"preprocesses"| node_preprocess
node_endpoint -->|"runs inference"| node_cnn
node_weights -->|"loads weights"| node_cnn
node_endpoint -->|"returns result"| node_json

click node_endpoint "https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py"
click node_preprocess "https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py"
click node_cnn "https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py"
click node_json "https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_endpoint,node_json,node_client toneBlue
class node_preprocess,node_cnn,node_weights toneAmber
```

## Components

### API serving

- [Prediction endpoint](https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py): `main.py`
- [Prediction JSON](https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py): `main.py`

### Model inference

- [Image preprocessing](https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py): `main.py`
- [CNN classifier](https://github.com/m-hasan-code/pytorch-cnn-with-fastapi/blob/main/main.py): `main.py`
- **Model weights**

### Other components

- **Image client**

## Connections

- Image client → Prediction endpoint: uploads image
- Prediction endpoint → Image preprocessing: preprocesses
- Prediction endpoint → CNN classifier: runs inference
- Model weights → CNN classifier: loads weights
- Prediction endpoint → Prediction JSON: returns result
