Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.learn.pytorch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
bob
bob.learn.pytorch
Merge requests
!4
Resolve "Add GANs"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Resolve "Add GANs"
4-add-gans
into
master
Overview
0
Commits
26
Pipelines
8
Changes
23
Merged
Resolve "Add GANs"
Guillaume HEUSCH
requested to merge
4-add-gans
into
master
Jul 23, 2018
Overview
0
Commits
26
Pipelines
8
Changes
23
Closes
#4 (closed)
Edited
Jul 24, 2018
by
Guillaume HEUSCH
0
0
Merge request reports
Compare
master
version 6
78a92c7e
Jul 24, 2018
version 5
e6a05829
Jul 24, 2018
version 4
b3319f64
Jul 23, 2018
version 3
2221e9af
Jul 23, 2018
version 2
cac9487c
Jul 23, 2018
version 1
dab01b02
Jul 23, 2018
master (base)
and
latest version
latest version
a8c468d6
26 commits,
Jul 24, 2018
version 6
78a92c7e
24 commits,
Jul 24, 2018
version 5
e6a05829
23 commits,
Jul 24, 2018
version 4
b3319f64
15 commits,
Jul 23, 2018
version 3
2221e9af
14 commits,
Jul 23, 2018
version 2
cac9487c
11 commits,
Jul 23, 2018
version 1
dab01b02
9 commits,
Jul 23, 2018
23 files
+
1273
−
49
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
23
bob/learn/pytorch/architectures/ConditionalGAN.py
0 → 100644
+
166
−
0
View file @ a8c468d6
Edit in single-file editor
Open in Web IDE
#!/usr/bin/env python
# encoding: utf-8
import
torch
import
torch.nn
as
nn
class
ConditionalGAN_generator
(
nn
.
Module
):
"""
Class implementating the conditional GAN generator
This network is introduced in the following publication:
Mehdi Mirza, Simon Osindero:
"
Conditional Generative Adversarial Nets
"
Attributes
----------
ngpu : int
The number of available GPU devices
main : :py:class:`torch.nn.Sequential`
The sequential container
"""
def
__init__
(
self
,
noise_dim
,
conditional_dim
,
channels
=
3
,
ngpu
=
1
):
"""
Init function
Parameters
----------
noise_dim : int
The dimension of the noise
conditional_dim : int
The dimension of the conditioning variable
channels : int
The number of channels in the image
ngpu : int
The number of available GPU devices
"""
super
(
ConditionalGAN_generator
,
self
).
__init__
()
self
.
ngpu
=
ngpu
self
.
conditional_dim
=
conditional_dim
# output dimension
ngf
=
64
self
.
main
=
nn
.
Sequential
(
# input is Z, going into a convolution
nn
.
ConvTranspose2d
((
noise_dim
+
conditional_dim
),
ngf
*
8
,
4
,
1
,
0
,
bias
=
False
),
nn
.
BatchNorm2d
(
ngf
*
8
),
nn
.
ReLU
(
True
),
# state size. (ngf*8) x 4 x 4
nn
.
ConvTranspose2d
(
ngf
*
8
,
ngf
*
4
,
4
,
2
,
1
,
bias
=
False
),
nn
.
BatchNorm2d
(
ngf
*
4
),
nn
.
ReLU
(
True
),
# state size. (ngf*4) x 8 x 8
nn
.
ConvTranspose2d
(
ngf
*
4
,
ngf
*
2
,
4
,
2
,
1
,
bias
=
False
),
nn
.
BatchNorm2d
(
ngf
*
2
),
nn
.
ReLU
(
True
),
# state size. (ngf*2) x 16 x 16
nn
.
ConvTranspose2d
(
ngf
*
2
,
ngf
,
4
,
2
,
1
,
bias
=
False
),
nn
.
BatchNorm2d
(
ngf
),
nn
.
ReLU
(
True
),
# state size. (ngf) x 32 x 32
nn
.
ConvTranspose2d
(
ngf
,
channels
,
4
,
2
,
1
,
bias
=
False
),
nn
.
Tanh
()
# state size. (nc) x 64 x 64
)
def
forward
(
self
,
z
,
y
):
"""
Forward function
Parameters
----------
z : :py:class: `torch.autograd.Variable`
The minibatch of noise.
y : :py:class: `torch.autograd.Variable`
The conditional one hot encoded vector for the minibatch.
Returns
-------
:py:class:`torch.Tensor`
the output of the generator (i.e. an image)
"""
generator_input
=
torch
.
cat
((
z
,
y
),
1
)
if
isinstance
(
generator_input
.
data
,
torch
.
cuda
.
FloatTensor
)
and
self
.
ngpu
>
1
:
output
=
nn
.
parallel
.
data_parallel
(
self
.
main
,
generator_input
,
range
(
self
.
ngpu
))
else
:
output
=
self
.
main
(
generator_input
)
return
output
class
ConditionalGAN_discriminator
(
nn
.
Module
):
"""
Class implementating the conditional GAN discriminator
Attributes
----------
conditional_dim: int
The dimension of the conditioning variable.
channels: int
The number of channels in the input image (default: 3).
ngpu : int
The number of available GPU devices
main : :py:class:`torch.nn.Sequential`
The sequential container
"""
def
__init__
(
self
,
conditional_dim
,
channels
=
3
,
ngpu
=
1
):
"""
Init function
Parameters
----------
conditional_dim: int
The dimension of the conditioning variable.
channels: int
The number of channels in the input image (default: 3).
ngpu : int
The number of available GPU devices
"""
super
(
ConditionalGAN_discriminator
,
self
).
__init__
()
self
.
conditional_dim
=
conditional_dim
self
.
ngpu
=
ngpu
# input dimension
ndf
=
64
self
.
main
=
nn
.
Sequential
(
# input is (nc) x 64 x 64
nn
.
Conv2d
((
channels
+
conditional_dim
),
ndf
,
4
,
2
,
1
,
bias
=
False
),
nn
.
LeakyReLU
(
0.2
,
inplace
=
True
),
# state size. (ndf) x 32 x 32
nn
.
Conv2d
(
ndf
,
ndf
*
2
,
4
,
2
,
1
,
bias
=
False
),
nn
.
BatchNorm2d
(
ndf
*
2
),
nn
.
LeakyReLU
(
0.2
,
inplace
=
True
),
# state size. (ndf*2) x 16 x 16
nn
.
Conv2d
(
ndf
*
2
,
ndf
*
4
,
4
,
2
,
1
,
bias
=
False
),
nn
.
BatchNorm2d
(
ndf
*
4
),
nn
.
LeakyReLU
(
0.2
,
inplace
=
True
),
# state size. (ndf*4) x 8 x 8
nn
.
Conv2d
(
ndf
*
4
,
ndf
*
8
,
4
,
2
,
1
,
bias
=
False
),
nn
.
BatchNorm2d
(
ndf
*
8
),
nn
.
LeakyReLU
(
0.2
,
inplace
=
True
),
# state size. (ndf*8) x 4 x 4
nn
.
Conv2d
(
ndf
*
8
,
1
,
4
,
1
,
0
,
bias
=
False
),
nn
.
Sigmoid
()
)
def
forward
(
self
,
images
,
y
):
"""
Forward function
Parameters
----------
images : :py:class: `torch.autograd.Variable`
The minibatch of input images.
y : :py:class: `torch.autograd.Variable`
The corresponding conditional feature maps.
Returns
-------
:py:class:`torch.Tensor`
the output of the discriminator
"""
input_discriminator
=
torch
.
cat
((
images
,
y
),
1
)
if
isinstance
(
input_discriminator
.
data
,
torch
.
cuda
.
FloatTensor
)
and
self
.
ngpu
>
1
:
output
=
nn
.
parallel
.
data_parallel
(
self
.
main
,
input_discriminator
,
range
(
self
.
ngpu
))
else
:
output
=
self
.
main
(
input_discriminator
)
return
output
.
view
(
-
1
,
1
).
squeeze
(
1
)
Loading