An implementation of a convolutional neuronal net with pytorch to classify weather conditions from pictures
This project delivers an endâtoâend image classification pipeline for detecting 11 distinct weather phenomena (dew, fog/smog, frost, glaze, hail, lightning, rain, rainbow, rime, sandstorm, snow) using a custom convolutional neural network in PyTorch. It provides:
src/training/train_model.py) implementing a 3âlayer ConvNet with
configurable image resizing and logging.src/training/optimize_hyperparameters.py) that tunes learning rate, weight decay, and more
via small/large modes and persists trials in db.sqlite3.src/evaluation/evaluation.py) that computes weighted F1,
precision, recall, prints a classification report, and saves a confusion matrix plot.src/prediction/predict.py) to batchâinfer on new images using the
trained model.src/utils/ for data loading/splitting (train/val/test),
classâdistribution analysis, and model architecture visualization (via torchview).experiments/ folder capturing loss curves, confusion matrices, and other artifacts
for reproducibility.torchvision.datasets.ImageFolder, custom WeatherDataset,
torch.utils.data.DataLoaderoptuna-dashboardtorchview for model graphsenvironment.yml), requierements.txt (pip lockfile)requierements.txt):
pip install -r requierements.txtTrain a new model (saves to models/weather-model.pth by default):
python src/training/train_model.py \
--data dataset \
--device cuda \
--epochs 28 \
--model_path models/my-weather-model.pth \
--verbose True
Run Optuna study (small or large mode):
python src/training/optimize_hyperparameters.py \
--data dataset \
--n_trials 50 \
--epochs 10 \
--device cuda \
--mode small \
--study_name my-weather-study
View live dashboard:
optuna-dashboard sqlite:///db.sqlite3
Evaluate a trained model on the test split and save confusion matrix:
python src/evaluation/evaluation.py \
--data dataset \
--model models/my-weather-model.pth \
--device cuda
Batchâpredict on a folder of images:
python src/prediction/predict.py \
--model models/my-weather-model.pth \
--images test_images/test_image_class \
--dataset dataset \
--device cuda
WeatherClassifier (train_model.py):
Convolutional blocks (3 layers): channels [3â64â512â1024], kernel_size=3, stride=2, padding=1
LeakyReLU activations + MaxPool2d, dynamic feature-size computation
Classifier head: dropout 0.3573 â linear (num_featuresâ1024) â LeakyReLU â dropout â linear (ânum_classes)
Training loop with CrossEntropyLoss + Adam, history of loss & accuracy plotted
Hyperparameter Optimization:
Small mode: tunes lr (1e-6â1e-1) & weight_decay (1e-10â1e-2), minimizes validation loss
Large mode: tunes lr, weight_decay, batch_size (2â32), activation [ReLU/SiLU/LeakyReLU], dropout (0.1â0.5), epochs (5â50), maximizes test
accuracy
DataLoader (utils/data_loader.py):
Splits into train/val/test (70/15/15) with seed reproducibility
custom_image_loader for single-image prediction
Class distribution analysis + pie-chart plotting
Evaluation (evaluation.py):
Computes weighted F1, precision, recall via scikit-learn
Plots & saves confusion matrix with class labels
Prediction (predict.py):
Core Modules
src/utils/data_loader.py: WeatherDataset wraps ImageFolder, applies
Resize/ToTensor, and provides train/val/test splits plus classâdistribution analysis.src/training/train_model.py: Defines WeatherClassifier (3 conv layers â
fullyâconnected head), training loop with loss/accuracy tracking, and modelâsize reporting.src/training/optimize_hyperparameters.py: Launches Optuna trials for LR, weight decay,
batch sizes, etc.src/evaluation/evaluation.py: Loads a saved model, computes F1/precision/recall, prints
tabulated report, and plots + saves confusion matrix.src/prediction/predict.py: Inference pipeline over arbitrary image folders, outputs a
CLI table of predicted labels.Supporting Artifacts
notebooks/data_analysis.ipynb: Exploratory analysis of class distributions and sample
images.src/utils/plot_model.py: Generates a visual graph of the CNN architecture via
torchview.experiments/: Stores training loss curves (.png), confusion matrices, and Optuna
trial logs.test_images/: Sample images for quick sanity checks in prediction.There is no formal unitâtest suite yet; instead, a small set of labeled images under
test_images/test_image_class can be used to verify endâtoâend inference correctness.
Implemented Solution
WeatherClassifier class: modular _initialize_layers & _setup_classifier methodsWhy This Approach
Future Improvements
torch.cuda.amp) to exploit RTX 4070âTi fullyImplemented Solution
db.sqlite3 and enabled interactive inspection via optuna-dashboardWhy This Approach
Future Improvements
