In this code, we classify whether a patient has pneumonia or not based on chest X-ray images. However, the same approach can be extended to multi-class classification tasks, as long as your input dataset contains labeled images.
For our case, the dataset contains chest X-ray images categorized into two folders: NORMAL and PNEUMONIA. The dataset structure looks like this:
dataset/ ├── NORMAL/ │ ├── img1.jpg │ ├── img2.jpg │ └── ... ├── PNEUMONIA/ │ ├── img1.jpg │ └── ...
If you have more classes, simply add more folders under the dataset/ directory, each representing a separate class with its own set of images.
In the code, images are converted into high-dimensional tensors using PyTorch. A Convolutional Neural Network (CNN) — such as ResNet — is used to extract features and perform classification.
The images are normalized to have zero mean and unit variance, which improves training stability and model performance.
The model uses:
- CrossEntropyLoss — a standard loss function for multi-class classification that compares predicted class probabilities (via
softmax) with the true labels. - Adam optimizer — an efficient optimization algorithm that adapts learning rates for each parameter during training.