{"id":19535479,"url":"https://github.com/gordicaleksa/pytorch-gans","last_synced_at":"2025-04-07T12:08:30.945Z","repository":{"id":38392082,"uuid":"291928204","full_name":"gordicaleksa/pytorch-GANs","owner":"gordicaleksa","description":"My implementation of various GAN (generative adversarial networks) architectures like vanilla GAN (Goodfellow et al.), cGAN (Mirza et al.), DCGAN (Radford et al.), etc.","archived":false,"fork":false,"pushed_at":"2020-12-07T16:01:10.000Z","size":78032,"stargazers_count":380,"open_issues_count":0,"forks_count":57,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-06T10:38:41.058Z","etag":null,"topics":["conditional-gan","dc-gan","deep-learning","deep-learning-tutorial","gan-tutorial","gans","generative-adversarial-nets","generative-adversarial-networks","machine-learning","python","pytorch","vanilla-gan"],"latest_commit_sha":null,"homepage":"https://youtube.com/c/TheAIEpiphany","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gordicaleksa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"patreon":"theaiepiphany"}},"created_at":"2020-09-01T07:29:57.000Z","updated_at":"2025-03-16T08:11:41.000Z","dependencies_parsed_at":"2022-08-18T08:21:04.954Z","dependency_job_id":null,"html_url":"https://github.com/gordicaleksa/pytorch-GANs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gordicaleksa%2Fpytorch-GANs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gordicaleksa%2Fpytorch-GANs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gordicaleksa%2Fpytorch-GANs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gordicaleksa%2Fpytorch-GANs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gordicaleksa","download_url":"https://codeload.github.com/gordicaleksa/pytorch-GANs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648978,"owners_count":20972945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditional-gan","dc-gan","deep-learning","deep-learning-tutorial","gan-tutorial","gans","generative-adversarial-nets","generative-adversarial-networks","machine-learning","python","pytorch","vanilla-gan"],"created_at":"2024-11-11T02:18:50.660Z","updated_at":"2025-04-07T12:08:30.925Z","avatar_url":"https://github.com/gordicaleksa.png","language":"Python","readme":"## PyTorch GANs :computer: vs :computer: = :heart:\nThis repo contains PyTorch implementation of various GAN architectures. \u003cbr/\u003e\nIt's aimed at making it **easy for beginners** to start playing and learning about GANs. \u003cbr/\u003e\n\nAll of the repos I found do obscure things like setting bias in some network layer to `False` without explaining \u003cbr/\u003e\nwhy certain design decisions were made. This repo makes **every design decision transparent.**\n\n## Table of Contents\n  * [What are GANs?](#what-are-gans)\n  * [Setup](#setup)\n  * [Implementations](#implementations)\n    + [Vanilla GAN](#vanilla-gan)\n    + [Conditional GAN](#conditional-gan)\n    + [DCGAN](#dcgan)\n\n## What are GANs?\n\nGANs were originally proposed by Ian Goodfellow et al. in a seminal paper called [Generative Adversarial Nets](https://papers.nips.cc/paper/5423-generative-adversarial-nets.pdf).\n\nGANs are a framework where 2 models (usually neural networks), called generator (G) and discriminator (D), play a **minimax game** against each other.\nThe generator is trying to **learn the distribution of real data** and is the network which we're usually interested in.\nDuring the game the goal of the generator is to trick the discriminator into \"thinking\" that the data it generates is real.\nThe goal of the discriminator, on the other hand, is to correctly discriminate between the generated (fake) images and real images coming from some dataset (e.g. MNIST).\n\n## Setup\n\n1. `git clone https://github.com/gordicaleksa/pytorch-gans`\n2. Open Anaconda console and navigate into project directory `cd path_to_repo`\n3. Run `conda env create` from project directory (this will create a brand new conda environment).\n4. Run `activate pytorch-gans` (for running scripts from your console or set the interpreter in your IDE)\n\nThat's it! It should work out-of-the-box executing environment.yml file which deals with dependencies.\n\n-----\n\nPyTorch package will pull some version of CUDA with it, but it is highly recommended that you install system-wide CUDA beforehand, mostly because of GPU drivers. I also recommend using Miniconda installer as a way to get conda on your system. \n\nFollow through points 1 and 2 of [this setup](https://github.com/Petlja/PSIML/blob/master/docs/MachineSetup.md) and use the most up-to-date versions of Miniconda and CUDA/cuDNN.\n\n## Implementations\n\nImportant note: you don't need to train the GANs to use this project I've checked-in pre-trained models. \u003cbr/\u003e\nYou can just use the `generate_imagery.py` script to play with the models.\n\n## Vanilla GAN\n\nVanilla GAN is my implementation of the [original GAN paper (Goodfellow et al.)](https://papers.nips.cc/paper/5423-generative-adversarial-nets.pdf) with certain modifications mostly in the model architecture,\nlike the usage of LeakyReLU and 1D batch normalization (it didn't even exist back then) instead of the maxout activation and dropout.\n\n### Examples\n\nGAN was trained on data from MNIST dataset. Here is how the digits from the dataset look like:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/real_samples/mnist.jpg\" width=\"850\"/\u003e\n\u003c/p\u003e\n\nYou can see how the network is slowly learning to capture the data distribution during training:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/training_progress/training_progress_vgan.gif\" /\u003e\n\u003c/p\u003e\n\nAfter the generator is trained we can use it to generate all 10 digits! Looks like it's coming directly from MNIST, right!?\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/generated_samples/generated_vgan.jpg\" width=\"850\"/\u003e\n\u003c/p\u003e\n\nWe can also pick 2 generated numbers that we like, save their latent vectors, and subsequently [linearly](https://en.wikipedia.org/wiki/Linear_interpolation) or [spherically](https://en.wikipedia.org/wiki/Slerp)\u003cbr/\u003e\ninterpolate between them to generate new images and understand how the latent space (z-space) is structured:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/interpolation/vgan_interpolated.jpg\" width=\"850\"/\u003e\n\u003c/p\u003e\n\nWe can see how the number 4 is slowly morphing into 9 and then into the number 3. \u003cbr/\u003e\n\nThe idea behind spherical interpolation is super easy - instead of moving over the shortest possible path\u003cbr/\u003e\n(line i.e. linear interpolation) from the first vector (p0) to the second (p1), you take the sphere's arc path: \n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/interpolation/slerp.png\" width=\"330\"/\u003e\n\u003c/p\u003e\n\n### Usage\n\n#### Option 1: Jupyter Notebook\n\nJust run `jupyter notebook` from you Anaconda console and it will open the session in your default browser. \u003cbr/\u003e\nOpen `Vanilla GAN (PyTorch).py` and you're ready to play! \u003cbr/\u003e\n\nIf you created the env before I added jupyter just do `pip install jupyter==1.0.0` and you're ready.\n\n---\n\n**Note:** if you get `DLL load failed while importing win32api: The specified module could not be found` \u003cbr/\u003e\nJust do `pip uninstall pywin32` and then either `pip install pywin32` or `conda install pywin32` [should fix it](https://github.com/jupyter/notebook/issues/4980)!\n\n#### Option 2: Use your IDE of choice\n\n#### Training\n\nIt's really easy to kick-off new training just run this: \u003cbr/\u003e\n`python train_vanilla_gan.py --batch_size \u003cnumber which won't break your GPU's VRAM\u003e`\n\nThe code is well commented so you can exactly understand how the training itself works. \u003cbr/\u003e\n\nThe script will:\n* Dump checkpoint *.pth models into `models/checkpoints/`\n* Dump the final *.pth model into `models/binaries/`\n* Dump intermediate generated imagery into `data/debug_imagery/`\n* Download MNIST (~100 MB) the first time you run it and place it into `data/MNIST/`\n* Dump tensorboard data into `runs/`, just run `tensorboard --logdir=runs` from your Anaconda\n\nAnd that's it you can track the training both visually (dumped imagery) and through G's and D's loss progress.\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/intermediate_imagery.PNG\" height=\"250\"/\u003e\n\u003cimg src=\"data/examples/losses.PNG\" height=\"250\"/\u003e\n\u003c/p\u003e\n\nTracking loss can be helpful but I mostly relied on visually analyzing intermediate imagery. \u003cbr/\u003e\n\nNote1: also make sure to check out **playground.py** file if you're having problems understanding adversarial loss.\u003cbr/\u003e\nNote2: Images are dumped both to the file system `data/debug_imagery/` but also to tensorboard.\n\n#### Generating imagery and interpolating\n\nTo generate a single image just run the script with defaults: \u003cbr/\u003e\n`python generate_imagery.py`\n\nIt will display and dump the generated image into `data/generated_imagery/` using checked-in generator model. \u003cbr/\u003e\n\nMake sure to change the `--model_name` param to your model's name (once you train your own model). \u003cbr/\u003e\n\n-----\n\nIf you want to play with interpolation, just set the `--generation_mode` to `GenerationMode.INTERPOLATION`. \u003cbr/\u003e\nAnd optionally set `--slerp` to true if you want to use spherical interpolation.\n\nThe first time you run it in this mode the script will start generating images, \u003cbr/\u003e\nand ask you to pick 2 images you like by entering `'y'` into the console.\n\nFinally it will start displaying interpolated imagery and dump the results to `data/interpolated_imagery`.\n\n## Conditional GAN\n\nConditional GAN (cGAN) is my implementation of the [cGAN paper (Mehdi et al.)](https://arxiv.org/pdf/1411.1784.pdf).\u003cbr/\u003e\nIt basically just adds conditioning vectors (one hot encoding of digit labels) to the vanilla GAN above.\n\n### Examples\n\nIn addition to everything that we could do with the original GAN, here we can exactly control which digit we want to generate!\nWe make it dump 10x10 grid where each column is a single digit and this is how the learning proceeds:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/training_progress/training_progress_cgan.gif\" /\u003e\n\u003c/p\u003e\n\n### Usage\n\nFor training just check out [vanilla GAN](#training) (just make sure to use `train_cgan.py` instead).\n\n#### Generating imagery\n\nSame as for [vanilla GAN](#generating-imagery-and-interpolating) but you can additionally set `cgan_digit` to a number between 0 and 9 to generate that exact digit!\nThere is no interpolation support for cGAN, it's the same as for vanilla GAN feel free to use that.\n\nNote: make sure to set `--model_name` to either `CGAN_000000.pth` (pre-trained and checked-in) or your own model.\n\n## DCGAN\n\nDCGAN is my implementation of the [DCGAN paper (Radford et al.)](https://arxiv.org/pdf/1511.06434.pdf).\u003cbr/\u003e\nThe main contribution of the paper was that they were the first who made CNNs successfully work in the GAN setup.\u003cbr/\u003e\nBatch normalization was invented in the meanwhile and that's what got CNNs to work basically.\n\n### Examples\n\nI trained DCGAN on preprocessed [CelebA dataset](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html). Here are some samples from the dataset:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/real_samples/celeba.jpg\" width=\"850\"/\u003e\n\u003c/p\u003e\n\nAgain, you can see how the network is slowly learning to capture the data distribution during training:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/training_progress/training_progress_dcgan.gif\" /\u003e\n\u003c/p\u003e\n\nAfter the generator is trained we can use it to generate new faces! This problem is much harder than generating MNIST digits,\nso generated faces are not indistinguishable from the real ones.\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/generated_samples/generated_dcgan.jpg\" width=\"850\"/\u003e\n\u003c/p\u003e\n\nSome SOTA GAN papers did a much better job at generating faces, currently the best model is [StyleGAN2](https://github.com/NVlabs/stylegan2).\n\nSimilarly we can explore the structure of the latent space via interpolations:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/interpolation/dcgan_interpolated.jpg\" width=\"850\"/\u003e\n\u003c/p\u003e\n\nWe can see how the man's face is slowly morphing into woman's face and also the skin tan is changing gradually.\n\nFinally, because the latent space has some nice properties (linear structure) we can do some interesting things.\u003cbr/\u003e\nSubtracting neutral woman's latent vector from smiling woman's latent vector gives us the \"smile vector\". \u003cbr/\u003e\nAdding that vector to neutral man's latent vector, we hopefully get smiling man's latent vector. And so it is!\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"data/examples/vector_arithmetic/vector_arithmetic.jpg\" /\u003e\n\u003c/p\u003e\n\nYou can also create the \"sunglasses vector\" and use it to add sunglasses to other faces, etc.\n\n*Note: I've created an interactive script so you can play with this check out `GenerationMode.VECTOR_ARITHMETIC`.*\n\n### Usage\n\nFor training just check out [vanilla GAN](#training) (just make sure to use `train_dcgan.py` instead). \u003cbr/\u003e\nThe only difference is that this script will download [pre-processed CelebA dataset](https://s3.amazonaws.com/video.udacity-data.com/topher/2018/November/5be7eb6f_processed-celeba-small/processed-celeba-small.zip) instead of MNIST.\n\n#### Generating imagery\n\nAgain just use the `generate_imagery.py` script.\n\nYou have 3 options you can set the `generation_mode` to:\n* `GenerationMode.SINGLE_IMAGE` \u003c- generate a single face image\n* `GenerationMode.INTERPOLATION` \u003c- pick 2 face images you like and script will interpolate between them\n* `GenerationMode.VECTOR_ARITHMETIC` \u003c- pick 9 images and script will do vector arithmetic\n\nGenerationMode.VECTOR_ARITHMETIC will give you an **interactive matplotlib plot** to pick 9 images.\n\nNote: make sure to set `--model_name` to either `DCGAN_000000.pth` (pre-trained and checked-in) or your own model.\n\n## Acknowledgements\n\nI found these repos useful (while developing this one):\n* [gans](https://github.com/diegoalejogm/gans) (PyTorch \u0026 TensorFlow)\n* [PyTorch-GAN](https://github.com/eriklindernoren/PyTorch-GAN) (PyTorch)\n\n## Citation\n\nIf you find this code useful for your research, please cite the following:\n\n```\n@misc{Gordić2020PyTorchGANs,\n  author = {Gordić, Aleksa},\n  title = {pytorch-gans},\n  year = {2020},\n  publisher = {GitHub},\n  journal = {GitHub repository},\n  howpublished = {\\url{https://github.com/gordicaleksa/pytorch-gans}},\n}\n```\n\n## Connect with me\n\nIf you'd love to have some more AI-related content in your life :nerd_face:, consider:\n* Subscribing to my YouTube channel [The AI Epiphany](https://www.youtube.com/c/TheAiEpiphany) :bell:\n* Follow me on [LinkedIn](https://www.linkedin.com/in/aleksagordic/) and [Twitter](https://twitter.com/gordic_aleksa) :bulb:\n* Follow me on [Medium](https://gordicaleksa.medium.com/) :books: :heart:\n\n## Licence\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/gordicaleksa/pytorch-gans/blob/master/LICENCE)","funding_links":["https://patreon.com/theaiepiphany"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgordicaleksa%2Fpytorch-gans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgordicaleksa%2Fpytorch-gans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgordicaleksa%2Fpytorch-gans/lists"}