|
1 | | -# image-colorizer |
2 | | -A PyTorch framework to train models to restore colour to black-and-white images. |
| 1 | +# Image Colourisation with PyTorch |
| 2 | + |
| 3 | +This project contains a PyTorch framework to train deep learning models from scratch to automatically add colour to |
| 4 | +black-and-white (greyscale) images. |
| 5 | + |
| 6 | +## Repository Structure |
| 7 | + |
| 8 | +### Core |
| 9 | + |
| 10 | +``` |
| 11 | +src/ |
| 12 | + ├── api/ # Utilities to interact with data sources |
| 13 | + │ └── lorem_picsum.py |
| 14 | + ├── data/ # Dataset and data loading utilities |
| 15 | + │ ├── stock_image_dataset.py |
| 16 | + │ └── utils.py |
| 17 | + ├── infer/ # Inference pipeline |
| 18 | + │ ├── config.yaml |
| 19 | + │ └── inference.py |
| 20 | + ├── models/ # Model architectures |
| 21 | + │ └── unet.py |
| 22 | + ├── train/ # Training pipeline scripts and utilities |
| 23 | + │ ├── config.yaml # Training parameters |
| 24 | + │ ├── train.py # Entry point for training |
| 25 | + │ └── trainer.py # Epoch training and validation logic |
| 26 | + └── utils/ # General utilities |
| 27 | + ├── checkpoint.py # Model saving/loading |
| 28 | + ├── config.py # Config loading logic |
| 29 | + └── device.py # Device selection (CPU/GPU) |
| 30 | +``` |
| 31 | + |
| 32 | +### Other Components |
| 33 | + |
| 34 | +#### Notebooks |
| 35 | + |
| 36 | +Used for prototyping, experimentation, or showcasing model predictions. |
| 37 | + |
| 38 | +* `demo.ipynb` — A sample notebook to train a model from scratch and visualise outputs interactively. |
| 39 | + |
| 40 | +#### Scripts |
| 41 | + |
| 42 | +Standalone utility scripts. |
| 43 | + |
| 44 | +* `generate_dataset.py` — Script to generate an example image dataset. |
| 45 | + |
| 46 | +## Setup and Installation |
| 47 | + |
| 48 | +1. Clone this repository: |
| 49 | + |
| 50 | + ```bash |
| 51 | + git clone https://github.com/antonhosgood/image-colorizer.git |
| 52 | + cd image-colorizer |
| 53 | + ``` |
| 54 | + |
| 55 | +2. Create a **Python 3.13 or higher** virtual environment (optional but recommended): |
| 56 | + |
| 57 | + ```bash |
| 58 | + python3 --version # Ensure you have Python 3.13+ installed |
| 59 | + python3 -m venv .venv |
| 60 | + source .venv/bin/activate |
| 61 | + ``` |
| 62 | + |
| 63 | +3. Install dependencies: |
| 64 | + |
| 65 | + ```bash |
| 66 | + pip3 install -r requirements.txt |
| 67 | + ``` |
| 68 | + |
| 69 | +## Training |
| 70 | + |
| 71 | +Train the model using the provided training script and configuration file: |
| 72 | + |
| 73 | +```bash |
| 74 | +python3 -m src.train.train src/train/config.yaml |
| 75 | +``` |
| 76 | + |
| 77 | +The configuration file defines parameters like data directories, batch size, learning rate, number of epochs, etc. |
| 78 | + |
| 79 | +Model checkpoints will be saved at intervals defined in the config. |
| 80 | + |
| 81 | +## Inference |
| 82 | + |
| 83 | +Colorise a greyscale image using a trained model checkpoint: |
| 84 | + |
| 85 | +```bash |
| 86 | +python3 -m src.infer.inference src/infer/config.yaml path/to/grayscale_image.png path/to/checkpoint.pth --output path/to/save_colorized.png |
| 87 | +``` |
| 88 | + |
| 89 | +## Generate Sample Dataset |
| 90 | + |
| 91 | +[Lorem Picsum](https://picsum.photos) is an API to get random images. Although by default the API returns a random |
| 92 | +image, an identifier can be provided to request a specific image. |
| 93 | + |
| 94 | +`generate_dataset.py` creates a dataset of colour and greyscale image pairs by first obtaining a list of every image ID |
| 95 | +and downloading every image into a `color` and `grayscale` folder. An image width and height must be provided. Altering |
| 96 | +the width and/or height does not stretch or shrink the images. Instead, the original source images are cropped |
| 97 | +appropriately. |
| 98 | + |
| 99 | +```bash |
| 100 | +python3 -m scripts.generate_dataset data <WIDTH> <HEIGHT> |
| 101 | +``` |
| 102 | + |
| 103 | +## Future Improvements |
| 104 | + |
| 105 | +* Create unit tests for dataset, model, and training components |
| 106 | +* Add TensorBoard, Weights & Biases or MLFlow integration for better training monitoring |
| 107 | +* Support batch inference on directories of images |
| 108 | +* Add perceptual and adversarial loss functions for better colourisation quality |
0 commit comments