This repository contains a PyTorch-based pipeline for AI-image Detection tasks on a Kaggle competition dataset. It demonstrates how to train and evaluate popular segmentation models.

🏆 This project achieved a 97.2% score and secured 2nd place in the Aaltoes 2025 Computer Vision v1 Hackthon.
👥 Team: Ziyi Wang, Rijie Hao, Zhuchenyang Liu
🙏 Special thanks to the organizers at Aaltoes for hosting this amazing challenge.
- ✨ Features
- 💻 Requirements
- 🗂️ Dataset
- 🚀 Usage
- 🤝 Model Ensemble
- 📊 Results
- ⚙️ Arguments and Options
- 📁 Project Structure
- 🔗 Related Resources
- 📝 License
- Multiple models in one framework: Easily switch between U-Net, ResUNet, AttentionUNet, SegFormer, and Mask2Former.
- Flexible device setup: Runs on GPU if available; falls back to CPU automatically otherwise.
- Configurable hyperparameters: Adjust learning rate, batch size, epochs, threshold, etc.
- Run-Length Encoding (RLE): Automatically encodes predicted masks in RLE format for Kaggle submissions.
- Validation: Splits the training dataset into train/val sets for monitoring performance via Dice coefficient.
- Easy inference: Generate submission files for Kaggle evaluation.
- PyTorch (with GPU support if you want to train on CUDA)
- torchvision
- OpenCV
- scikit-learn
- pandas
- tqdm
You can install the required Python packages via:
pip install -r requirements.txtYou can download dataset via
kaggle competitions download -c aaltoes-2025-computer-vision-v-1Please place your data in Datasets folder.
This competition provides a dataset of 46836 images, each with its original version and corresponding binary mask indicating inpainted regions. All images are 256×256 pixels in size.
- train/ - Directory containing training data:
- images/ - Manipulated versions of the images
- masks/ - Binary masks indicating inpainted regions
- originals/ - Original versions of the images
- test/ - Directory containing test data:
- images/ - Manipulated versions of the images
- sample_submission.csv - Example submission file in the correct format
- Format: PNG
- Resolution: 256×256 pixels
- Color space: RGB (3 channels)
- Format: PNG
- Resolution: 256×256 pixels
- Values:
- 0: Original (non-inpainted) regions
- 1: Inpainted (manipulated) regions
To train a model, run the main script with appropriate arguments. You can also you the run.sh to train or inference.
python train.py \
--model "Mask2Fomer" \
--batch_size 16 \
--epochs 30 \
--lr 1e-4 \
--num_workers 4 \
--train_image_dir "../Dataset/train/train/images" \
--train_mask_dir "../Dataset/train/train/masks" \
--test_image_dir "../Dataset/test/test/images" \
--threshold 0.5For inference you need add model name and the path to the pretrained model.
python train.py \
--model "Mask2Former" \
--test_image_dir "../Dataset/test/test/images" \
--load_pretrain "/path/to/your/best_model.pth" \
--threshold 0.5First, generate the float mask predictions from your target model on the validation set:
python train.py \
--model "Mask2Former" \
--predict_validation True \
--load_pretrain "/path/to/your/best_model.pth" Next, you can optimize the threshold using Mask2Former/threshold_optimization.ipynb.
To adjust the search gap, modify the following line:
thresholds = np.linspace(0.4, 0.7, 20) Example output:
Optimal threshold: 0.637 with Dice coefficient: 0.9667
Threshold results:
Threshold Mean_Dice
0 0.400000 0.392585
1 0.415789 0.392585
2 0.431579 0.392585
3 0.447368 0.392585
...To further improve the prediction score, try the ensemble method.
After gathering multiple encoded submission.csv by different models, you can try the ensemble strategy using Mask2Former/ensemble.ipynb.
Adjust the combination of models and the voting threshold by modifying: VOTE_THRESHOLD.
The final private leaderboard result is based on the ensemble of three fine-tuned models with threshold optimization:
| Model | Threshold | Public Dice Coefficient | Private Dice Coefficient |
|---|---|---|---|
| mask2former-swin-small-ade-semantic | 0.61 | 0.9678 | 0.9651 |
| mask2former-swin-base-ade-semantic | 0.62 | 0.9686 | 0.9677 |
| mask2former-swin-large-ade-semantic | 0.61 | 0.9659 | 0.9639 |
| Ensemble | / | 0.9733 | 0.9713 |
Here are some prediction samples:
🎉 We achieved second place in the Aaltoes-CV-Hackathon competition!
This project is licensed under a MIT License.
We acknowledge all the open-source contributors for the following projects to make this work possible:


