CoolFace
Apppublic

Rishabh-9090/Galaxy_Morphology_Classification_using_CNNs_and_ViT

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
App README

Galaxy Morphology Classification (PyTorch)


Live Demo

๐Ÿ”— Hugging Face Space: https://huggingface.co/spaces/Rishabh-9090/GalaxyMorphologyClassificationusingCNNsandViT

Overview

This repository contains a clean, reproducible, and research oriented implementation of galaxy morphology classification using PyTorch.

The project re implements and extends prior research work on galaxy morphology classification, originally conducted using TensorFlow, and transitions it into a PyTorch first, engineering grade pipeline. The focus is on correctness, reproducibility, scalability, and clean dataset handling rather than quick experimentation.

The project uses the Galaxy10 DECaLS dataset as the initial benchmark and is designed to be easily extensible to larger datasets such as Galaxy Zoo and DECaLS full releases.

<br>

Motivation

Galaxy morphology classification is a fundamental task in astrophysics, providing insight into galaxy formation and evolution. While many prior works rely on small in memory datasets and framework specific loaders, this project emphasizes:

  • โ€”Framework agnostic dataset handling
  • โ€”Disk backed datasets compatible with large scale training
  • โ€”Clean PyTorch data pipelines
  • โ€”Research reproducibility and clarity
  • โ€”Industry level project structure

The goal is to build a foundation suitable for:

  • โ€”Research extensions
  • โ€”Large scale experiments
  • โ€”Open source collaboration
  • โ€”Future deployment and demos

<br>

Dataset

Galaxy10 DECaLS

The Galaxy10 DECaLS dataset consists of 17,736 RGB galaxy images of resolution 256 ร— 256, categorized into 10 morphological classes.

The original dataset is distributed as a single HDF5 file via the astroNN library. In this project, the dataset is exported once into an ImageFolder compatible directory structure to enable PyTorch native workflows.

Class Labels

The dataset uses the following class mapping:

LabelClass Name
0Disturbed
1Merging
2Round Smooth
3Smooth, Cigar Shaped
4Cigar Shaped Smooth
5Barred Spiral
6Unbarred Tight Spiral
7Unbarred Loose Spiral
8Edge on without Bulge
9Edge on with Bulge

This class ordering matches the original Galaxy10 DECaLS specification.

<br>

Dataset Preparation Pipeline

The dataset preparation follows a one time extraction workflow:

  1. 1.Galaxy10 DECaLS is downloaded automatically via astroNN
  2. 2.The raw .h5 file is cached outside the repository
  3. 3.Images are exported into a disk based directory structure
  4. 4.Data is split into train, validation, and test sets
  5. 5.PyTorch ImageFolder is used for all experiments

After export, astroNN is no longer required.

Final Dataset Structure

data/
โ”œโ”€โ”€ Galaxy10_DECaLS/
โ”‚   โ”œโ”€โ”€ train/
โ”‚   โ”‚   โ”œโ”€โ”€ barred_spiral/
โ”‚   โ”‚   โ”œโ”€โ”€ merging/
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ val/
โ”‚   โ”‚   โ”œโ”€โ”€ barred_spiral/
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ””โ”€โ”€ test/
โ”‚       โ”œโ”€โ”€ barred_spiral/
โ”‚       โ””โ”€โ”€ ...
โ”‚
โ”œโ”€โ”€ Galaxy10_DECaLS_Balanced/
โ”‚   โ”œโ”€โ”€ train/
โ”‚   โ”‚   โ”œโ”€โ”€ barred_spiral/
โ”‚   โ”‚   โ”œโ”€โ”€ merging/
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ””โ”€โ”€ test/
โ”‚       โ”œโ”€โ”€ barred_spiral/
โ”‚       โ””โ”€โ”€ ...

The `data/` directory is git ignored and treated as immutable once generated.

<br>

Project Structure

galaxy_morphology_classification/
โ”œโ”€โ”€ checkpoints/                     # (git-ignored) trained model weights
โ”‚   โ”œโ”€โ”€ custom_cnn/
โ”‚   โ”œโ”€โ”€ resnet18/
โ”‚   โ”œโ”€โ”€ resnet26/
โ”‚   โ”œโ”€โ”€ resnet50/
โ”‚   โ”œโ”€โ”€ vgg16/
โ”‚   โ”œโ”€โ”€ vgg19/
โ”‚   โ””โ”€โ”€ vit/
โ”‚
โ”œโ”€โ”€ data/                            # (git-ignored) processed datasets
โ”‚   โ”œโ”€โ”€ Galaxy10_DECaLS/             # original class-imbalanced dataset
โ”‚   โ”‚   โ”œโ”€โ”€ train/
โ”‚   โ”‚   โ”œโ”€โ”€ val/
โ”‚   โ”‚   โ””โ”€โ”€ test/
โ”‚   โ””โ”€โ”€ Galaxy10_DECaLS_Balanced/    # class-balanced variant
โ”‚       โ”œโ”€โ”€ train/
โ”‚       โ””โ”€โ”€ test/
โ”‚
โ”œโ”€โ”€ inference/
โ”‚   โ”œโ”€โ”€ app.py                      # Gradio + Hugging Face Spaces app
โ”‚   โ””โ”€โ”€ config.py                   # inference-specific configuration
โ”‚
โ”œโ”€โ”€ notebooks/
โ”‚   โ”œโ”€โ”€ eda.ipynb                   # exploratory data analysis
โ”‚   โ””โ”€โ”€ train_and_evaluate.ipynb    # experimentation & ablation studies
โ”‚
โ”œโ”€โ”€ results/                        # evaluation outputs (metrics, plots)
โ”‚
โ”œโ”€โ”€ src/                             # core library code
โ”‚   โ”œโ”€โ”€ data/
โ”‚   โ”‚   โ”œโ”€โ”€ dataloaders.py
โ”‚   โ”‚   โ””โ”€โ”€ transforms.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ custom_cnn.py
โ”‚   โ”‚   โ”œโ”€โ”€ resnet.py
โ”‚   โ”‚   โ”œโ”€โ”€ vgg.py
โ”‚   โ”‚   โ”œโ”€โ”€ vit.py
โ”‚   โ”‚   โ””โ”€โ”€ get_model.py
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ training/
โ”‚   โ”‚   โ””โ”€โ”€ engine.py               # unified training loop
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ evaluation/
โ”‚   โ”‚   โ””โ”€โ”€ predictions.py
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ”œโ”€โ”€ early_stopping.py
โ”‚       โ”œโ”€โ”€ save_model.py
โ”‚       โ”œโ”€โ”€ set_seed.py
โ”‚       โ””โ”€โ”€ visualisations.py
โ”‚
โ”œโ”€โ”€ pyproject.toml                  # packaging & tooling metadata
โ”œโ”€โ”€ requirements.txt                # runtime dependencies
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ .gitignore
โ””โ”€โ”€ README.md

<br>

Experimental Results

This section summarizes the test set performance of all evaluated models under different training and evaluation conditions. Two training regimes were considered:

  • โ€”Large unbalanced dataset (original class distribution)
  • โ€”Balanced dataset (class balanced via resampling)

Each balanced model was evaluated on both:

  • โ€”A balanced test set
  • โ€”The original unbalanced test set

All metrics reported below correspond to held out test data.

CNN and Transformer Performance Overview

ModelTraining DataTest DataTest AccuracyTest Loss
ResNet50UnbalancedUnbalanced0.52591.3268
ResNet50BalancedBalanced0.48331.4588
ResNet50BalancedUnbalanced0.47521.4446
ResNet26UnbalancedUnbalanced0.51691.3493
ResNet26BalancedBalanced0.49241.4766
ResNet26BalancedUnbalanced0.48991.4552
ResNet18UnbalancedUnbalanced0.47071.4653
ResNet18BalancedBalanced0.46671.5569
ResNet18BalancedUnbalanced0.45041.5166
VGG16UnbalancedUnbalanced0.60711.1234
VGG16BalancedBalanced0.53941.2986
VGG16BalancedUnbalanced0.53491.3320
VGG19UnbalancedUnbalanced0.56881.2092
VGG19BalancedBalanced0.51061.3947
VGG19BalancedUnbalanced0.51181.3717
Custom CNNUnbalancedUnbalanced0.75140.8144
Custom CNNBalancedBalanced0.59241.1362
Custom CNNBalancedUnbalanced0.62401.0804
ViTUnbalancedUnbalanced0.76380.7343
ViTBalancedBalanced0.75000.7966
ViTBalancedUnbalanced0.76940.7158

Key Observations

  1. 1.Vision Transformer achieves the best overall performance

The Vision Transformer consistently outperforms all CNN based architectures across both balanced and unbalanced evaluation settings. Its performance remains stable even when trained on balanced data and evaluated on the original unbalanced distribution, indicating strong generalization.

This suggests that global self attention is particularly effective for galaxy morphology classification, especially for complex and irregular structures.

  1. 1.Custom CNN performs competitively with ViT

The custom CNN achieves strong performance, closely approaching the Vision Transformer on the unbalanced dataset. This indicates that with appropriate architectural design, convolutional models can still be highly effective for this task.

However, the drop in performance when trained on balanced data highlights the sensitivity of CNNs to changes in data distribution.

  1. 1.VGG models outperform ResNet variants

Among CNN based architectures, VGG16 and VGG19 consistently outperform ResNet18, ResNet26, and ResNet50. In particular, VGG19 trained on balanced data demonstrates improved generalization across both balanced and unbalanced test sets.

This may be attributed to the deeper sequential convolutional structure of VGG models, which appears better suited to capturing fine grained morphological features.

  1. 1.ResNet depth does not correlate with better performance

Increasing ResNet depth does not lead to improved accuracy in this task. ResNet50 performs only marginally better than ResNet26 and ResNet18, and in some cases performs worse when trained on balanced data.

This suggests that residual depth alone is insufficient for modeling the structural complexity present in galaxy morphology images.

  1. 1.Consistent failure of CNNs on disturbed galaxies

Across all convolutional architectures evaluated, the disturbed galaxy class is consistently the most poorly classified. This behavior persists regardless of model depth, architecture type, or dataset balancing strategy.

In contrast, the Vision Transformer does not exhibit the same degradation on this class, indicating a stronger ability to model global irregularities, asymmetries, and long range spatial dependencies that characterize disturbed morphologies.

  1. 1.Impact of dataset balancing

Training on a balanced dataset generally improves class level fairness but often leads to reduced overall accuracy on the original unbalanced distribution. This trade off is particularly evident in CNN based models.

The Vision Transformer is notably less affected by this trade off, maintaining high performance across both evaluation settings.

Summary

Overall, the results demonstrate that:

  • โ€”Vision Transformers provide the most robust and generalizable performance for galaxy morphology classification
  • โ€”Carefully designed CNNs can still achieve competitive results
  • โ€”Dataset balancing introduces important trade offs that must be evaluated in context
  • โ€”Disturbed and irregular morphologies remain challenging for convolutional architectures
  • โ€”These findings support the use of transformer based models for future large scale galaxy morphology studies. <br>

Future Work

Planned extensions include:

  • โ€”Vision Transformers provide the most robust and generalizable performance for galaxy morphology classification
  • โ€”Carefully designed CNNs can still achieve competitive results
  • โ€”VGG style architectures outperform residual networks in this task
  • โ€”Dataset balancing introduces important trade offs that must be evaluated in context
  • โ€”Disturbed and highly irregular morphologies remain challenging for convolutional architectures
  • โ€”Transformer based models offer a clear advantage for capturing global galaxy structure

This structure is intentionally modular and scalable.