forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update install instruction (PaddlePaddle#88)
* docs: update install instruction 1. paddlepaddle2.0.0rc instruction can be found from main page 2. only two steps, install paddlepaddle and paddlegan, other than 3step * docs: update en install * feat: add dcgan * reform: debug with mnist datset validate performance with different options, and original structure and setting
- Loading branch information
Showing
9 changed files
with
445 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
epochs: 200 | ||
output_dir: output_dir | ||
|
||
model: | ||
name: DCGANModel | ||
generator: | ||
name: DCGenerator | ||
norm_type: batch | ||
input_nz: 100 | ||
input_nc: 1 | ||
output_nc: 1 | ||
ngf: 64 | ||
discriminator: | ||
name: DCDiscriminator | ||
norm_type: batch | ||
ndf: 64 | ||
input_nc: 1 | ||
gan_mode: vanilla #wgangp | ||
|
||
dataset: | ||
train: | ||
name: SingleDataset | ||
dataroot: data/mnist/train | ||
phase: train | ||
max_dataset_size: inf | ||
direction: AtoB | ||
input_nc: 1 | ||
output_nc: 1 | ||
batch_size: 128 | ||
serial_batches: False | ||
transforms: | ||
- name: Resize | ||
size: [64, 64] | ||
interpolation: 'bicubic' #cv2.INTER_CUBIC | ||
- name: Transpose | ||
- name: Normalize | ||
mean: [127.5, 127.5, 127.5] | ||
std: [127.5, 127.5, 127.5] | ||
test: | ||
name: SingleDataset | ||
dataroot: data/mnist/test | ||
max_dataset_size: inf | ||
input_nc: 1 | ||
output_nc: 1 | ||
serial_batches: False | ||
transforms: | ||
- name: Resize | ||
size: [64, 64] | ||
interpolation: 'bicubic' #cv2.INTER_CUBIC | ||
- name: Transpose | ||
- name: Normalize | ||
mean: [127.5, 127.5, 127.5] | ||
std: [127.5, 127.5, 127.5] | ||
|
||
optimizer: | ||
name: Adam | ||
beta1: 0.5 | ||
|
||
lr_scheduler: | ||
name: linear | ||
learning_rate: 0.00002 | ||
start_epoch: 100 | ||
decay_epochs: 100 | ||
|
||
log_config: | ||
interval: 100 | ||
visiual_interval: 500 | ||
|
||
snapshot_config: | ||
interval: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. | ||
# | ||
# 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 | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import paddle | ||
from .base_model import BaseModel | ||
|
||
from .builder import MODELS | ||
from .generators.builder import build_generator | ||
from .discriminators.builder import build_discriminator | ||
from .losses import GANLoss | ||
|
||
from ..solver import build_optimizer | ||
from ..modules.init import init_weights | ||
|
||
|
||
@MODELS.register() | ||
class DCGANModel(BaseModel): | ||
""" This class implements the DCGAN model, for learning a distribution from input images. | ||
The model training requires dataset. | ||
By default, it uses a '--netG DCGenerator' generator, | ||
a '--netD DCDiscriminator' discriminator, | ||
and a vanilla GAN loss (the cross-entropy objective used in the orignal GAN paper). | ||
DCGAN paper: https://arxiv.org/pdf/1511.06434 | ||
""" | ||
def __init__(self, cfg): | ||
"""Initialize the DCGAN class. | ||
Parameters: | ||
opt (config dict)-- stores all the experiment flags; needs to be a subclass of Dict | ||
""" | ||
super(DCGANModel, self).__init__(cfg) | ||
# define networks (both generator and discriminator) | ||
self.nets['netG'] = build_generator(cfg.model.generator) | ||
init_weights(self.nets['netG']) | ||
self.cfg = cfg | ||
if self.is_train: | ||
self.nets['netD'] = build_discriminator(cfg.model.discriminator) | ||
init_weights(self.nets['netD']) | ||
|
||
if self.is_train: | ||
self.losses = {} | ||
# define loss functions | ||
self.criterionGAN = GANLoss(cfg.model.gan_mode) | ||
|
||
# build optimizers | ||
self.build_lr_scheduler() | ||
self.optimizers['optimizer_G'] = build_optimizer( | ||
cfg.optimizer, | ||
self.lr_scheduler, | ||
parameter_list=self.nets['netG'].parameters()) | ||
self.optimizers['optimizer_D'] = build_optimizer( | ||
cfg.optimizer, | ||
self.lr_scheduler, | ||
parameter_list=self.nets['netD'].parameters()) | ||
|
||
def set_input(self, input): | ||
"""Unpack input data from the dataloader and perform necessary pre-processing steps. | ||
Parameters: | ||
input (dict): include the data itself and its metadata information. | ||
""" | ||
# get 1-channel gray image, or 3-channel color image | ||
self.real = paddle.to_tensor(input['A'][:,0:self.cfg.model.generator.input_nc,:,:]) | ||
self.image_paths = input['A_paths'] | ||
|
||
def forward(self): | ||
"""Run forward pass; called by both functions <optimize_parameters> and <test>.""" | ||
|
||
# generate random noise and fake image | ||
self.z = paddle.rand(shape=(self.real.shape[0],self.cfg.model.generator.input_nz,1,1)) | ||
self.fake = self.nets['netG'](self.z) | ||
|
||
# put items to visual dict | ||
self.visual_items['real'] = self.real | ||
self.visual_items['fake'] = self.fake | ||
|
||
def backward_D(self): | ||
"""Calculate GAN loss for the discriminator""" | ||
# Fake; stop backprop to the generator by detaching fake | ||
pred_fake = self.nets['netD'](self.fake.detach()) | ||
self.loss_D_fake = self.criterionGAN(pred_fake, False) | ||
|
||
pred_real = self.nets['netD'](self.real) | ||
self.loss_D_real = self.criterionGAN(pred_real, True) | ||
|
||
# combine loss and calculate gradients | ||
self.loss_D = (self.loss_D_fake + self.loss_D_real) * 0.5 | ||
|
||
self.loss_D.backward() | ||
|
||
self.losses['D_fake_loss'] = self.loss_D_fake | ||
self.losses['D_real_loss'] = self.loss_D_real | ||
|
||
def backward_G(self): | ||
"""Calculate GAN loss for the generator""" | ||
# G(A) should fake the discriminator | ||
pred_fake = self.nets['netD'](self.fake) | ||
self.loss_G_GAN = self.criterionGAN(pred_fake, True) | ||
|
||
# combine loss and calculate gradients | ||
self.loss_G = self.loss_G_GAN | ||
|
||
self.loss_G.backward() | ||
|
||
self.losses['G_adv_loss'] = self.loss_G_GAN | ||
|
||
def optimize_parameters(self): | ||
# compute fake images: G(A) | ||
self.forward() | ||
|
||
#update D | ||
self.set_requires_grad(self.nets['netD'], True) | ||
self.set_requires_grad(self.nets['netG'], False) | ||
self.optimizers['optimizer_D'].clear_grad() | ||
self.backward_D() | ||
self.optimizers['optimizer_D'].step() | ||
|
||
# update G | ||
self.set_requires_grad(self.nets['netD'], False) | ||
self.set_requires_grad(self.nets['netG'], True) | ||
self.optimizers['optimizer_G'].clear_grad() | ||
self.backward_G() | ||
self.optimizers['optimizer_G'].step() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. | ||
# | ||
# 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 | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import paddle | ||
import functools | ||
import numpy as np | ||
import paddle.nn as nn | ||
import paddle.nn.functional as F | ||
|
||
from paddle.nn import BatchNorm2D | ||
from ...modules.norm import build_norm_layer | ||
|
||
from .builder import DISCRIMINATORS | ||
|
||
|
||
@DISCRIMINATORS.register() | ||
class DCDiscriminator(nn.Layer): | ||
"""Defines a DCGAN discriminator""" | ||
def __init__(self, input_nc, ndf=64, norm_type='instance'): | ||
"""Construct a DCGAN discriminator | ||
Parameters: | ||
input_nc (int) -- the number of channels in input images | ||
ndf (int) -- the number of filters in the last conv layer | ||
norm_type (str) -- normalization layer type | ||
""" | ||
super(DCDiscriminator, self).__init__() | ||
norm_layer = build_norm_layer(norm_type) | ||
if type( | ||
norm_layer | ||
) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters | ||
use_bias = norm_layer.func == nn.BatchNorm2D | ||
else: | ||
use_bias = norm_layer == nn.BatchNorm2D | ||
|
||
kw = 4 | ||
padw = 1 | ||
|
||
sequence = [ | ||
nn.Conv2D(input_nc, | ||
ndf, | ||
kernel_size=kw, | ||
stride=2, | ||
padding=padw, | ||
bias_attr=use_bias), | ||
nn.LeakyReLU(0.2) | ||
] | ||
|
||
nf_mult = 1 | ||
nf_mult_prev = 1 | ||
n_downsampling = 4 | ||
|
||
for n in range(1, n_downsampling): # gradually increase the number of filters | ||
nf_mult_prev = nf_mult | ||
nf_mult = min(2**n, 8) | ||
if norm_type == 'batch': | ||
sequence += [ | ||
nn.Conv2D(ndf * nf_mult_prev, | ||
ndf * nf_mult, | ||
kernel_size=kw, | ||
stride=2, | ||
padding=padw), | ||
BatchNorm2D(ndf * nf_mult), | ||
nn.LeakyReLU(0.2) | ||
] | ||
else: | ||
sequence += [ | ||
nn.Conv2D(ndf * nf_mult_prev, | ||
ndf * nf_mult, | ||
kernel_size=kw, | ||
stride=2, | ||
padding=padw, | ||
bias_attr=use_bias), | ||
norm_layer(ndf * nf_mult), | ||
nn.LeakyReLU(0.2) | ||
] | ||
|
||
nf_mult_prev = nf_mult | ||
|
||
sequence += [ | ||
nn.Conv2D(ndf * nf_mult_prev, | ||
1, | ||
kernel_size=kw, | ||
stride=1, | ||
padding=0) | ||
] # output 1 channel prediction map | ||
|
||
self.model = nn.Sequential(*sequence) | ||
|
||
def forward(self, input): | ||
"""Standard forward.""" | ||
return self.model(input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.