Skip to content

Commit

Permalink
Some small details of the repair
Browse files Browse the repository at this point in the history
  • Loading branch information
Lornatang committed Jun 1, 2021
1 parent f17ce76 commit b6f8b41
Show file tree
Hide file tree
Showing 19 changed files with 1,110 additions and 722 deletions.
40 changes: 25 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ coverage.xml
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
Expand Down Expand Up @@ -113,7 +114,6 @@ venv.bak/

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
Expand All @@ -122,24 +122,34 @@ dmypy.json
# Pyre type checker
.pyre/

# IDE file
# custom
.idea
.vscode

# Mac file
# Mac configure file.
.DS_Store

# custom
# Dataset
data/MNIST
data/TFD
data/cifar-10-python
data/cifar-10-python.tar.gz
data/cifar-10-batches-py

output
test
*.jpg
*.jpeg
*.png
data/Camelyon16
data/DF2K
data/DIV2K
data/DF2K+OST
data/Digital-pathology
data/ImageNet2012
data/Paper

# Program run create directory.
benchmarks
tests
videos
runs
weights

# Program run create file.
*.bmp
*.pth
*.png
*.mp4
*.zip
*.csv
*.json
135 changes: 54 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

### Overview

This repository contains an op-for-op PyTorch reimplementation
of [Conditional Generative Adversarial Networks](http://xxx.itp.ac.cn/pdf/1411.1784).
This repository contains an op-for-op PyTorch reimplementation of [Conditional Generative Adversarial Networks](http://arxiv.org/pdf/1411.1784).

### Table of contents

Expand All @@ -23,19 +22,17 @@ of [Conditional Generative Adversarial Networks](http://xxx.itp.ac.cn/pdf/1411.1

If you're new to CGANs, here's an abstract straight from the paper:

Generative Adversarial Nets were recently introduced as a novel way to train generative models. In this work we
introduce the conditional version of generative adversarial nets, which can be constructed by simply feeding the data,
y, we wish to condition on to both the generator and discriminator. We show that this model can generate MNIST digits
conditioned on class labels. We also illustrate how this model could be used to learn a multi-modal model, and provide
preliminary examples of an application to image tagging in which we demonstrate how this approach can generate
descriptive tags which are not part of training labels.
Generative Adversarial Nets were recently introduced as a novel way to train generative models. In this work we introduce the conditional version of
generative adversarial nets, which can be constructed by simply feeding the data, y, we wish to condition on to both the generator and discriminator.
We show that this model can generate MNIST digits conditioned on class labels. We also illustrate how this model could be used to learn a multi-modal
model, and provide preliminary examples of an application to image tagging in which we demonstrate how this approach can generate descriptive tags
which are not part of training labels.

### Model Description

We have two networks, G (Generator) and D (Discriminator).The Generator is a network for generating images. It receives
a random noise z and generates images from this noise, which is called G(z).Discriminator is a discriminant network that
discriminates whether an image is real. The input is x, x is a picture, and the output is D of x is the probability that
x is a real picture, and if it's 1, it's 100% real, and if it's 0, it's not real.
We have two networks, G (Generator) and D (Discriminator).The Generator is a network for generating images. It receives a random noise z and generates
images from this noise, which is called G(z).Discriminator is a discriminant network that discriminates whether an image is real. The input is x, x is
a picture, and the output is D of x is the probability that x is a real picture, and if it's 1, it's 100% real, and if it's 0, it's not real.

### Installation

Expand Down Expand Up @@ -67,15 +64,14 @@ import torchvision.utils as vutils
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

# Load the model into the specified device.
model = torch.hub.load("Lornatang/CGAN-PyTorch", "mnist", pretrained=True, progress=True, verbose=False)
model = torch.hub.load("Lornatang/CGAN-PyTorch", "cgan", pretrained=True, progress=True, verbose=False)
model.eval()
model = model.to(device)

# Create random noise image.
num_images = 64
number = 1
conditional = torch.randint(number, number + 1, (num_images,), device=device)
noise = torch.randn(num_images, 100, device=device)
noise = torch.randn([num_images, 100], device=device)
conditional = torch.randint(1, 2, (num_images,), device=device)

# The noise is input into the generator model to generate the image.
with torch.no_grad():
Expand All @@ -90,30 +86,22 @@ vutils.save_image(generated_images, "mnist.png", normalize=True)
Using pre training model to generate pictures.

```text
usage: test.py [-h] [-a ARCH] [--num-classes NUM_CLASSES]
[--number {0,1,2,3,4,5,6,7,8,9}] [-n NUM_IMAGES] [--outf PATH]
[--device DEVICE]
An implementation of CGAN algorithm using PyTorch framework.
usage: test.py [-h] [-a ARCH] [--number {0,1,2,3,4,5,6,7,8,9}] [--num-images NUM_IMAGES] [--model-path PATH] [--pretrained] [--seed SEED] [--gpu GPU]
optional arguments:
-h, --help show this help message and exit
-a ARCH, --arch ARCH model architecture: _gan | discriminator |
load_state_dict_from_url | mnist (default: mnist)
--num-classes NUM_CLASSES
Number of classes for dataset. (default: 10).
-a ARCH, --arch ARCH model architecture: cgan. (Default: `cgan`)
--number {0,1,2,3,4,5,6,7,8,9}
Specifies the generated number. (default: 1).
-n NUM_IMAGES, --num-images NUM_IMAGES
How many samples are generated at one time. (default:
64).
--outf PATH The location of the image in the evaluation process.
(default: ``test``).
--device DEVICE device id i.e. `0` or `0,1` or `cpu`. (default:
``cpu``).
Specifies the generated number. (Default: 1)
--num-images NUM_IMAGES
How many samples are generated at one time. (Default: 64)
--model-path PATH Path to latest checkpoint for model.
--pretrained Use pre-trained model.
--seed SEED Seed for initializing training. (Default: 666)
--gpu GPU GPU id to use.
# Example (e.g. MNIST)
$ python3 test.py -a mnist --number 1
$ python3 test.py -a cgan --number 1 --gpu 0
```

<span align="center"><img src="assets/mnist.gif" alt="">
Expand All @@ -122,67 +110,53 @@ $ python3 test.py -a mnist --number 1
### Train (e.g. MNIST)

```text
usage: train.py [-h] [-a ARCH] [-j N] [--start-iter N] [--iters N] [-b N]
[--lr LR] [--image-size IMAGE_SIZE] [--channels CHANNELS]
[--num-classes NUM_CLASSES] [--pretrained] [--netD PATH]
[--netG PATH] [--manualSeed MANUALSEED] [--device DEVICE]
usage: train.py [-h] [-a ARCH] [-j N] [--epochs N] [--start-epoch N] [-b N] [--lr LR] [--image-size IMAGE_SIZE] [--channels CHANNELS] [--num-classes NUM_CLASSES] [--netD PATH] [--netG PATH] [--pretrained] [--world-size WORLD_SIZE] [--rank RANK]
[--dist-url DIST_URL] [--dist-backend DIST_BACKEND] [--seed SEED] [--gpu GPU] [--multiprocessing-distributed]
DIR
An implementation of GAN algorithm using PyTorch framework.
positional arguments:
DIR path to dataset
DIR Path to dataset.
optional arguments:
-h, --help show this help message and exit
-a ARCH, --arch ARCH model architecture: _gan | discriminator |
load_state_dict_from_url | mnist (default: mnist)
-j N, --workers N Number of data loading workers. (default:8)
--start-iter N manual iter number (useful on restarts)
--iters N The number of iterations is needed in the training of
model. (default: 50000)
-b N, --batch-size N mini-batch size (default: 64), this is the total batch
size of all GPUs on the current node when using Data
Parallel or Distributed Data Parallel.
--lr LR Learning rate. (default:0.0002)
-a ARCH, --arch ARCH Model architecture: cgan. (Default: cgan)
-j N, --workers N Number of data loading workers. (Default: 4)
--epochs N Number of total epochs to run. (Default: 128)
--start-epoch N Manual epoch number (useful on restarts). (Default: 0)
-b N, --batch-size N Mini-batch size (default: 64), this is the total batch size of all GPUs on the current node when using Data Parallel or Distributed Data Parallel.
--lr LR Learning rate. (Default: 0.0002)
--image-size IMAGE_SIZE
The height / width of the input image to network.
(default: 28).
--channels CHANNELS The number of channels of the image. (default: 1).
Image size of high resolution image. (Default: 28)
--channels CHANNELS The number of channels of the image. (Default: 1)
--num-classes NUM_CLASSES
Number of classes for dataset. (default: 10).
Number of classes for dataset. (Default: 10)
--netD PATH Path to Discriminator checkpoint.
--netG PATH Path to Generator checkpoint.
--pretrained Use pre-trained model.
--netD PATH Path to latest discriminator checkpoint. (default:
````).
--netG PATH Path to latest generator checkpoint. (default: ````).
--manualSeed MANUALSEED
Seed for initializing training. (default:1111)
--device DEVICE device id i.e. `0` or `0,1` or `cpu`. (default:
``0``).
--world-size WORLD_SIZE
Number of nodes for distributed training.
--rank RANK Node rank for distributed training. (Default: -1)
--dist-url DIST_URL url used to set up distributed training. (Default: `tcp://59.110.31.55:12345`)
--dist-backend DIST_BACKEND
Distributed backend. (Default: `nccl`)
--seed SEED Seed for initializing training.
--gpu GPU GPU id to use.
--multiprocessing-distributed
Use multi-processing distributed training to launch N processes per node, which has N GPUs. This is the fastest way to use PyTorch for either single node or multi node data parallel training.
# Example (e.g. MNIST)
$ python3 train.py data -a mnist --image-size 28 --channels 1 --num-classes 10 --pretrained --device 0
$ python3 train.py -a cgan --gpu 0 data
```

If you want to load weights that you've trained before, run the following command.

```bash
$ python3 train.py data \
--dataset mnist \
-a mnist \
--image-size 28 \
--channels 1 \
--num-classes 10 \
--start-iter 10000 \
--netG weights/mnist_G_iter_10000.pth \
--netD weights/mnist_D_iter_10000.pth \
--device 0
$ python3 train.py -a cgan data --netD weights/Discriminator_epoch8.pth --netG weights/Generator_epoch8.pth --start-epoch 8 --gpu 0 data
```

### Contributing

If you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions,
simply post them as GitHub issues.
If you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions, simply post them as GitHub issues.

I look forward to seeing what the community does with these models!

Expand All @@ -194,12 +168,11 @@ I look forward to seeing what the community does with these models!

**Abstract**

Generative Adversarial Nets were recently introduced as a novel way to train generative models. In this work we
introduce the conditional version of generative adversarial nets, which can be constructed by simply feeding the data,
y, we wish to condition on to both the generator and discriminator. We show that this model can generate MNIST digits
conditioned on class labels. We also illustrate how this model could be used to learn a multi-modal model, and provide
preliminary examples of an application to image tagging in which we demonstrate how this approach can generate
descriptive tags which are not part of training labels.
Generative Adversarial Nets were recently introduced as a novel way to train generative models. In this work we introduce the conditional version of
generative adversarial nets, which can be constructed by simply feeding the data, y, we wish to condition on to both the generator and discriminator.
We show that this model can generate MNIST digits conditioned on class labels. We also illustrate how this model could be used to learn a multi-modal
model, and provide preliminary examples of an application to image tagging in which we demonstrate how this approach can generate descriptive tags
which are not part of training labels.

[[Paper]](http://xxx.itp.ac.cn/pdf/1411.1784)

Expand Down
5 changes: 3 additions & 2 deletions cgan_pytorch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Dakewe Biotech Corporation. All Rights Reserved.
# Copyright 2021 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -11,7 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from .activation import *
from .models import *
from .utils import *

__version__ = "0.1.0"
__version__ = "0.2.0"
Loading

0 comments on commit b6f8b41

Please sign in to comment.