A deep learning semantic segmentation project for detecting solar filaments in full-disk H-alpha images of the Sun, built with TensorFlow / Keras. The goal is to predict a pixel-wise binary mask marking filaments, comparing a U-Net trained from scratch against EfficientNetB0 transfer-learning variants before selecting a final model.
Solar filaments are dark, elongated structures of cool plasma suspended in the solar atmosphere. They are closely linked to solar eruptions, so detecting them automatically is useful for space-weather research.
The workflow progresses from raw COCO annotations to a tuned transfer-learning U-Net:
- Explore the dataset and its COCO-style annotations
- Convert polygon annotations into binary masks on the fly
- Train a baseline U-Net from scratch
- Train a U-Net with a frozen pretrained EfficientNetB0 encoder
- Fine-tune the top encoder layers and tune loss weights and the prediction threshold
- Evaluate with the Dice coefficient and compare all approaches
-
- Build a Kaggle submission pipeline at higher resolution (1024 × 1024) that splits the semantic mask into per-filament instances and exports them as RLE
- Name: MAGFiLO 1.0 (Kaggle 2026 edition)
- Source: Kaggle competition
filament-segmentation-2026 - Images: full-disk H-alpha solar images, 2048 × 2048 px (707 unique image files in the training folder)
- Annotations: COCO-format JSON (
images,annotations,categories), with a polygonsegmentation,bbox,areaand aspineline for every filament - Test set: 180 unlabeled images; the submission requires one RLE-encoded mask per filament (
filament_id,segmentation_rle) - Target: binary mask (1 = filament, 0 = background)
- Extremely imbalanced classes: filaments cover only a tiny fraction of each image, so plain pixel accuracy is misleading (a model that predicts "all background" already scores above 99%)
- Thin, elongated, irregular shapes with a variable number of filaments per image
- Some frames appear more than once in the annotation file under different image IDs
Note: The dataset is not included in this repository. It is downloaded automatically through
kagglehub(you must accept the competition rules on Kaggle first).
Data Analysis/Data_file.ipynb
- Downloaded the data and inspected the image folder, image sizes and COCO JSON structure
- Converted
imagesandannotationsinto DataFrames to inspect their columns - Counted filaments per image and visualized sample images
Shared across all notebooks and Final Model.py
generate_mask_for_image()rasterizes the polygon annotations into a binary mask withcv2.fillPoly- A custom Keras
Sequence(FilamentDataGenerator) reads, resizes and batches data on the fly:- Images resized to 512 × 512 (bilinear), masks resized with nearest neighbour to keep them binary
- Batch size 8
- 75 / 25 train / validation split (
random_state=19) - Images are fed as raw 0–255 values, since the Keras EfficientNet already includes its own rescaling layer (dividing by 255 manually was tried first)
models/*.ipynb
| Notebook | Architecture | Encoder | Loss |
|---|---|---|---|
Base CNN Model.ipynb |
U-Net from scratch (~1.37 M params) | Trained from scratch | BCE + Dice (1 : 1) |
Transform Model.ipynb |
U-Net with EfficientNetB0 encoder | ImageNet weights, fully frozen | BCE + Dice (1 : 1) |
Fine_Tuning Model.ipynb |
U-Net with EfficientNetB0 encoder | ImageNet weights, last 40 layers unfrozen | 0.3 · BCE + 0.7 · Dice |
Final Model.py
- Encoder: EfficientNetB0 (ImageNet), only the last 40 layers trainable
- Skip connections:
block2a,block3a,block4aandblock6aexpand activations - Decoder: 3 up-sampling blocks (
Conv2DTranspose→ skip concatenation → 2 ×Conv2D+BatchNormalization) with 256 / 128 / 64 filters, followed by a final up-sampling to full resolution and a 1 × 1 sigmoid output - Loss:
0.3 · BCE + 0.7 · (1 − Dice) - Optimizer: Adam,
learning_rate = 1e-4 - Callbacks:
ReduceLROnPlateauandModelCheckpoint, both monitoringval_dice_coef - Training: 20 epochs; the best checkpoint is saved as
best_filament_model.keras - Inference: sigmoid output thresholded at 0.45
submission/submission.ipynb
The submission notebook uses the same overall idea but with a different mindset: instead of only maximizing a semantic mask, it produces individual filament instances, which is what the competition evaluates.
| Aspect | Research models (models/, Final Model.py) |
Submission (submission/) |
|---|---|---|
| Input size | 512 × 512 | 1024 × 1024 |
| Train / val split | 75 / 25 | 80 / 20 |
| Skip connections | 4 (block2a–block6a) |
5 (adds top_activation) |
| Decoder | 3 blocks (256 / 128 / 64), 2 convs each | 4 blocks (512 / 256 / 128 / 64), 3 convs each |
| Learning rate | 1e-4 | 2e-4 (ReduceLROnPlateau, min 1e-6) |
| Callbacks | ReduceLROnPlateau, ModelCheckpoint |
+ EarlyStopping (restore best weights) |
| Output | Binary mask | Instance masks in RLE |
Inference & post-processing
- Resize each test image to 1024 × 1024 and predict the probability map
- Resize the probability map back to the original resolution
- Threshold at 0.45
- Split the mask into instances with
cv2.connectedComponentsWithStats(8-connectivity) - Discard components smaller than 50 pixels
- Encode each instance as RLE with
pycocotoolsand savesubmission.csv
The final file contains 1,705 filament instances predicted over the 180 test images.
Solar-Filament-Segmentation/
├── README.md
├── LICENSE
├── images/
│ ├── sample_image_and_mask.png
│ ├── architecture.png
│ ├── loss_curve.png
│ ├── dice_curve.png
│ └── predictions.png
├── Data Analysis/
│ └── Data_file.ipynb # Dataset exploration
├── models/
│ ├── Base CNN Model.ipynb # U-Net from scratch
│ ├── Transform Model.ipynb # Frozen EfficientNetB0 encoder
│ └── Fine_Tuning Model.ipynb # Partially unfrozen encoder
├── submission/
│ ├── submission.ipynb # 1024 px model + instance extraction + RLE export
│ └── submission.csv # Final Kaggle submission file
└── Final Model.py # Final training + evaluation script
git clone https://github.com/ali-119/Solar-Filament-Segmentation.git
cd Solar-Filament-Segmentationpip install tensorflow opencv-python numpy pandas matplotlib plotly scikit-learn kagglehubA GPU is strongly recommended. The notebooks were run on Google Colab.
- Join the
filament-segmentation-2026competition on Kaggle and accept its rules. - Add your Kaggle API credentials when
kagglehub.login()prompts for them. - If your download location differs, update
BASE_DIRat the top ofFinal Model.pyand the notebooks. The default is/root/.cache/kagglehub/competitions/filament-segmentation-2026/MAGFiLO_1.0_Kaggle_2026.
jupyter notebook "Data Analysis/Data_file.ipynb"Run any notebook inside models/ to reproduce that experiment.
python "Final Model.py"This builds the EfficientNetB0 U-Net, trains it for 20 epochs, plots the loss and Dice curves, and prints the final Dice score on a random validation sample.
Run submission/submission.ipynb end to end. It trains the 1024 × 1024 model, predicts the test set, and writes submission.csv.
| Model | Best Val Dice (Keras metric) | Dice @ threshold 0.45 | Epochs |
|---|---|---|---|
| U-Net from scratch | 0.627 | not measured | 10 |
| EfficientNetB0 (frozen encoder) | 0.659 | 66.48 (10 val. images) | 20 |
| EfficientNetB0 (fine-tuned) — Final | 0.662 | 68.34 (200 val. images) | 20 |
The thresholded Dice scores were computed on random validation subsets of different sizes, so they are indicative rather than strictly comparable. The Keras
val_dice_coefcolumn uses the full validation set and is the fairer comparison.
| Model | Best Val Dice (Keras metric) | Best Epoch | Test instances |
|---|---|---|---|
| EfficientNetB0 U-Net, 1024 px, deeper decoder | 0.688 | 14 / 20 | 1,705 |
This model uses an 80 / 20 split, so its validation Dice is not strictly comparable to the table above. Validation Dice is a pixel-wise semantic metric, while the competition scores individual filaments.
Many configurations were tried during development, and the results varied noticeably between them. The most important variations were:
- Training from scratch vs. transfer learning: pretrained ImageNet features gave a clear boost and much faster convergence
- Frozen vs. fine-tuned encoder: unfreezing the last 40 encoder layers gave the best validation loss and Dice
- Loss weighting: a plain
BCE + Diceloss was compared with0.3 · BCE + 0.7 · Dice, which emphasizes overlap on such a sparse target - Learning rate: a lower learning rate caused underfitting;
1e-4withReduceLROnPlateauworked best - Input scaling: raw 0–255 inputs (the EfficientNet default) were compared with 0–1 normalization
- Prediction threshold: several thresholds were tested and 0.45 gave the best Dice
- Input resolution: moving from 512 to 1024 px (with a deeper decoder) raised the best validation Dice from about 0.66 to 0.69
- Instance extraction: connected components with a minimum area of 50 px were used to turn the semantic mask into per-filament predictions
The final model is the configuration in Final Model.py.
- Pretrained encoders helped a lot on a small, highly imbalanced segmentation dataset
- A Dice-weighted loss is essential here, since plain accuracy is dominated by the background class
- Fine-tuning only the top encoder layers gave the best balance between adapting to solar images and keeping the pretrained features
- Training Dice kept rising to about 0.78 while validation Dice plateaued around 0.66, so the models show some overfitting
- Tuning the output threshold gave a small but real gain at inference time
- Higher input resolution helps thin structures like filaments, at a large cost in training time (~410 s per epoch at 1024 px)
- The annotation file lists some frames more than once under different IDs, and the split is made per entry, so the same frame can appear in both train and validation. This may make validation scores optimistic
- Images were downscaled from 2048 × 2048 (to 512 × 512 in the research models, 1024 × 1024 in the submission), which can erase thin filaments
- No data augmentation was used
- Thresholded Dice was measured on random validation subsets of different sizes, and the from-scratch U-Net has no thresholded score
- Local results are validation-only; the test set has no public labels, and instances are extracted with connected components, so touching filaments are merged into one
- Only EfficientNetB0 was explored as an encoder
- Group-aware train/validation splitting by image file
- Data augmentation (flips, rotations, brightness) and higher input resolution or patch-based training
- Other encoders (ResNet, EfficientNetV2) and attention-based or transformer-based decoders
- Post-processing of masks (e.g. removing small blobs)
tensorflow/kerasopencv-pythonnumpy/pandasscikit-learnmatplotlib/plotlykagglehubtqdmpycocotools
This project is released under the MIT License.
Author: Ali
Field: Data Science & Machine Learning Student
Email: ali.hz87980@gmail.com
GitHub: ali-119


