Professional Documents
Culture Documents
Redes Neuronales Convoluciones Gan
Redes Neuronales Convoluciones Gan
François Fleuret
http://fleuret.org/amld/
February 10, 2018
ÉCOLE POLYTECHNIQUE
FÉDÉRALE DE LAUSANNE
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 2 / 56
A simple approach to analyzing “what a network is doing” is to compute the
derivative of the output wrt the input.
Springenberg et al. (2014) improved upon the deconvolution with the guided
back-propagation, which aims at the best of both worlds: Discarding structures
which would not contribute positively to the final response, and discarding
structures which are not already present.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 3 / 56
∂l
1{s>0} ,
∂x
∂l
1{ ∂l >0} ,
∂x ∂x
∂l
1{s>0} 1{ ∂l >0} .
∂x ∂x
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 4 / 56
These procedures can be implemented simply in PyTorch by changing the
nn.ReLU ’s backward pass.
The class nn.Module provides methods to register “hook” functions that are
called during the forward or the backward pass, and can implement a different
computation for the latter.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 5 / 56
For instance
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 6 / 56
Using hooks, we can implement the deconvolution as follows:
def e q u i p _ m o d e l _ d e c o n v ( model ) :
for m in model . modules () :
if isinstance (m , nn . ReLU ) :
m. register_backward_hook ( relu_backward_deconv_hook )
input = Variable ( img . view (1 , img . size (0) , img . size (1) , img . size (2) ) , \
requires_grad = True )
return result
model = models . vgg16 ( pretrained = True )
model . eval ()
model = model . features
e q u i p _ m o d e l _ d e c o n v ( model )
result = grad_view ( model , ’ blacklab . jpg ’)
utils . save_image ( result , ’ blacklab - vgg16 - deconv . png ’)
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 7 / 56
The code is the same for the guided back-propagation, except the hooks
themselves:
def e q u i p _ m o d e l _ g b a c k p r o p ( model ) :
for m in model . modules () :
if isinstance (m , nn . ReLU ) :
m. register_forward_hook ( relu_forward_gbackprop_hook )
m. register_backward_hook ( relu_backward_gbackprop_hook )
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 8 / 56
Original
Gradient
Deconvolution
Guided-backprop
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 9 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 11 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 12 / 56
Another approach to get an intuition of the information actually encoded in the
weights of a convnet consists of optimizing from scratch a sample to maximize
the activation f of a chosen unit, or the sum over an activation map.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 13 / 56
Doing so generates images with high frequencies, which tend to activate units a
lot. For instance these images maximize the responses of the units “bathtub”
and “lipstick” respectively (yes, this is strange, we will come back to it).
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 14 / 56
To prevent this from happening, we add a penalty over the edge amplitude at
several scales, of the form
X
h(x) = kδ s (x) − g ~ δ s (x)k2
s>0
class M u l t i S c a l e E d g e E n e r g y ( nn . Module ) :
def __init__ ( self ) :
super ( MultiScaleEdgeEnergy , self ) . __init__ ()
k = Tensor ([[1 , 4 , 6 , 4 , 1]])
k _ 5 x 5 _ p s e u d o _ g a u s s i a n = k . t () . mm ( k ) . view (1 , 1 , 5 , 5)
k _ 5 x 5 _ p s e u d o _ g a u s s i a n /= k _ 5 x 5 _ p s e u d o _ g a u s s i a n . sum ()
self . k _ 5 x 5 _ p s e u d o _ g a u s s i a n = Parameter ( k _ 5 x 5 _ p s e u d o _ g a u s s i a n )
k_2x2_uniform = Tensor ([[0.25 , 0.25] , [0.25 , 0.25]]) . view (1 , 1 , 2 , 2)
self . k_2x2_uniform = Parameter ( k_2x2_uniform )
return result
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 15 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 16 / 56
VGG16, maximizing a channel of the 4th convolution layer
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 17 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 18 / 56
VGG16, maximizing a unit of the 10th convolution layer
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 19 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 20 / 56
VGG16, maximizing a unit of the output layer
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 21 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 22 / 56
VGG16, maximizing a unit of the output layer
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 23 / 56
“Sunglass” “Geyser”
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 24 / 56
These results show that the parameters of a network trained for classification
carry enough information to generate identifiable large-scale structures.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 25 / 56
Adversarial examples
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 26 / 56
In spite of their very good predictive capabilities, deep neural networks are quite
sensitive to adversarial inputs, that is to inputs crafted to make them behave
incorrectly (Szegedy et al., 2014).
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 27 / 56
Let x be an image, y its proper label, f (x; w ) the network’s prediction, and L
the cross-entropy loss. We can construct an adversarial example by maximizing
the loss. To do so, we iterate a “gradient ascent” step:
After a few iterations, this procedure will reach a sample x̌ whose class is not y .
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 28 / 56
input = Variable ( input , requires_grad = True )
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 29 / 56
Original
Adversarial
Differences
kx−x̌k
kxk
1.02% 0.27%
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 30 / 56
Predicted classes
Nb. iterations Image #1 Image #2
0 Weimaraner desktop computer
1 Weimaraner desktop computer
2 Labrador retriever desktop computer
3 Labrador retriever desktop computer
4 Labrador retriever desktop computer
5 brush kangaroo desktop computer
6 brush kangaroo desktop computer
7 sundial desktop computer
8 sundial desktop computer
9 sundial desktop computer
10 sundial desktop computer
11 sundial desktop computer
12 sundial desktop computer
13 sundial desktop computer
14 sundial desk
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 31 / 56
Autoencoders
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 32 / 56
Many applications such as image synthesis, denoising, super-resolution, speech
synthesis, compression, etc. require to go beyond classification and regression,
and model explicitly a high dimension signal.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 33 / 56
Latent space F
Original space X
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 34 / 56
Deep Autoencoders
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 35 / 56
AutoEncoder (
( encoder ) : Sequential (
(0) : Conv2d (1 , 32 , kernel_size =(5 , 5) , stride =(1 , 1) )
(1) : ReLU ( inplace )
(2) : Conv2d (32 , 32 , kernel_size =(5 , 5) , stride =(1 , 1) )
(3) : ReLU ( inplace )
(4) : Conv2d (32 , 32 , kernel_size =(4 , 4) , stride =(2 , 2) )
(5) : ReLU ( inplace )
(6) : Conv2d (32 , 32 , kernel_size =(3 , 3) , stride =(2 , 2) )
(7) : ReLU ( inplace )
(8) : Conv2d (32 , 8 , kernel_size =(4 , 4) , stride =(1 , 1) )
)
( decoder ) : Sequential (
(0) : C o nv Tr an s po se 2 d (8 , 32 , kernel_size =(4 , 4) , stride =(1 , 1) )
(1) : ReLU ( inplace )
(2) : C o nv Tr an s po se 2 d (32 , 32 , kernel_size =(3 , 3) , stride =(2 , 2) )
(3) : ReLU ( inplace )
(4) : C o nv Tr an s po se 2 d (32 , 32 , kernel_size =(4 , 4) , stride =(2 , 2) )
(5) : ReLU ( inplace )
(6) : C o nv Tr an s po se 2 d (32 , 32 , kernel_size =(5 , 5) , stride =(1 , 1) )
(7) : ReLU ( inplace )
(8) : C o nv Tr an s po se 2 d (32 , 1 , kernel_size =(5 , 5) , stride =(1 , 1) )
)
)
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 36 / 56
Encoder
1×28×28
28
nn.Conv2d(1, 32, kernel size=5, stride=1)
32×24×24 ×24
24
nn.Conv2d(32, 32, kernel size=5, stride=1)
32×20×20 ×20
20
nn.Conv2d(32, 32, kernel size=4, stride=2)
32×9×9 ×9
9
nn.Conv2d(32, 32, kernel size=3, stride=2)
32×4×4 ×4
4
nn.Conv2d(32, 8, kernel size=4, stride=1)
8×1×1 ×1
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 37 / 56
Decoder
8×1×1
×1
nn.ConvTranspose2d(32, 8, kernel size=4, stride=1)
32×4×4 4
×4
nn.ConvTranspose2d(32, 32, kernel size=3, stride=2)
32×9×9 3
×9
nn.ConvTranspose2d(32, 32, kernel size=4, stride=2)
32×20×20 4
×20
nn.ConvTranspose2d(32, 32, kernel size=5, stride=1)
32×24×24 5
×24
nn.ConvTranspose2d(1, 32, kernel size=5, stride=1)
1×28×28 5
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 38 / 56
Training is achieved with MSE and Adam
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 39 / 56
X (original samples)
g ◦ f (X ) (CNN, d = 8)
g ◦ f (X ) (PCA, d = 8)
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 40 / 56
To get an intuition of the latent representation, we can pick two samples x and
x 0 at random and interpolate samples along the line in the latent space
f
f (x)
x x0
f (x 0 )
Latent space F
Original space X
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 41 / 56
Autoencoder interpolation (d = 8)
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 42 / 56
And we can assess the generative capabilities of the decoder g by introducing a
[simple] density model q Z over the latent space F, sample there, and map the
samples into the image space X with g .
We can for instance use a Gaussian model with diagonal covariance matrix.
ˆ
f (X ) ∼ N(m̂, ∆)
Latent space F
Original space X
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 43 / 56
Autoencoder sampling (d = 8)
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 44 / 56
These results are unsatisfying, because the density model used on the latent
space F is too simple and inadequate.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 45 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 46 / 56
A different approach to learn high-dimension generative models are the
Generative Adversarial Networks proposed by Goodfellow et al. (2014).
The approach is adversarial since the two networks have antagonistic objectives.
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 47 / 56
Training GANs is difficult and involve a lot of technical details regarding the
network structure and the optimization procedure.
Under review as a conference paper at ICLR 2016
Radford et al. (2015) proposed a simple convolutional architecture for
generating small images.
Figure 1: DCGAN generator used for LSUN scene modeling. A 100 dimensional uniform distribu-
tion Z is projected to a small spatial extent convolutional representation with many feature maps.
A series of four fractionally-strided convolutions (in some recent papers, these are wrongly called
deconvolutions) then convert this high level representation into a 64 × 64 pixel image. Notably, no
fully connected or pooling layers are used.
suggested value of 0.9 resulted in training oscillation and instability while reducing it to 0.5 helped
(Radford et al., 2015)
stabilize training.
4.1 LSUN
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 48 / 56
As visual quality of samples from generative image models has improved, concerns of over-fitting
We can have a look at the reference implementation provided in
https://github.com/pytorch/examples.git
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 49 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 50 / 56
# custom weights initia lizatio n called on netG and netD
def weights_init ( m ) :
classname = m . __class__ . __name__
if classname . find ( ’ Conv ’) != -1:
m . weight . data . normal_ (0.0 , 0.02)
elif classname . find ( ’ BatchNorm ’) != -1:
m . weight . data . normal_ (1.0 , 0.02)
m . bias . data . fill_ (0)
criterion = nn . BCELoss ()
# setup optimizer
optimizerD = optim . Adam ( netD . parameters () , lr = opt . lr , betas =( opt . beta1 , 0.999) )
optimizerG = optim . Adam ( netG . parameters () , lr = opt . lr , betas =( opt . beta1 , 0.999) )
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 51 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 52 / 56
# (2) Update G network : maximize log ( D ( G ( z ) ) )
netG . zero_grad ()
# fake labels are real for generator cost
labelv = Variable ( label . fill_ ( real_label ) )
output = netD ( fake )
errG = criterion ( output , labelv )
errG . backward ()
D_G_z2 = output . data . mean ()
optimizerG . step ()
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 53 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 54 / 56
Fake images after 1 epoch (3M images)
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 55 / 56
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 56 / 56
References
François Fleuret AMLD – Deep Learning in PyTorch / 6. Visualizations, auto-encoders and GANs 56 / 56