showgan's picture
Training in progress, step 1000
09b13b3 verified

Training CycleGAN on your own data

This folder contains a script to train CycleGAN, leveraging the Hugging Face ecosystem for processing data and pushing the model to the Hub.

drawing

Example applications of CycleGAN. Taken from this repo.

The script leverages 🤗 Datasets for loading and processing data, and 🤗 Accelerate for instantly running on CPU, single, multi-GPUs or TPU, also supporting mixed precision.

Launching the script

To train the model with the default parameters (200 epochs, 256x256 images, etc.) on huggan/facades on your environment, first run:

accelerate config

and answer the questions asked. Next, launch the script as follows:

accelerate launch train.py

This will create local "images" and "saved_models" directories, containing generated images and saved checkpoints over the course of the training.

To train on another dataset available on the hub, simply do:

accelerate launch train.py --dataset huggan/edges2shoes

Make sure to pick a dataset which has "imageA" and "imageB" columns defined. One can always tweak the script in case the column names are different.

Training on your own data

You can of course also train on your own images. For this, one can leverage Datasets' ImageFolder. Make sure to authenticate with the hub first, by running the huggingface-cli login command in a terminal, or the following in case you're working in a notebook:

from huggingface_hub import notebook_login

notebook_login()

Next, run the following in a notebook/script:

from datasets import load_dataset

# first: load dataset
# option 1: from local folder
dataset = load_dataset("imagefolder", data_dir="path_to_folder")
# option 2: from remote URL (e.g. a zip file)
dataset = load_dataset("imagefolder", data_files="URL to .zip file")

# next: push to the hub (assuming git-LFS is installed)
dataset.push_to_hub("huggan/my-awesome-dataset")

You can then simply pass the name of the dataset to the script:

accelerate launch train.py --dataset huggan/my-awesome-dataset

Pushing model to the Hub

You can push your trained generator to the hub after training by specifying the push_to_hub flag. Then, you can run the script as follows:

accelerate launch train.py --push_to_hub --model_name cyclegan-horse2zebra

This is made possible by making the generator inherit from PyTorchModelHubMixinavailable in the huggingface_hub library.

Citation

This repo is entirely based on Erik Linder-Norén's PyTorch-GAN repo, but with added HuggingFace goodies.