-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
143 lines (109 loc) · 4.71 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# CSC 321, Assignment 4
#
# This file contains the models used for both parts of the assignment:
#
# - DCGenerator --> Used in the vanilla GAN in Part 1
# - CycleGenerator --> Used in the CycleGAN in Part 2
# - DCDiscriminator --> Used in both the vanilla GAN and CycleGAN (Parts 1 and 2)
#
# For the assignment, you are asked to create the architectures of these three networks by
# filling in the __init__ methods in the DCGenerator, CycleGenerator, and DCDiscriminator classes.
# Note that the forward passes of these models are provided for you, so the only part you need to
# fill in is __init__.
import pdb
import torch
import torch.nn as nn
import torch.nn.functional as F
def deconv(in_channels, out_channels, kernel_size, stride=2, padding=1, batch_norm=True):
"""Creates a transposed-convolutional layer, with optional batch normalization.
"""
layers = []
layers.append(nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding, bias=False))
if batch_norm:
layers.append(nn.BatchNorm2d(out_channels))
return nn.Sequential(*layers)
def conv(in_channels, out_channels, kernel_size, stride=2, padding=1, batch_norm=True, init_zero_weights=False):
"""Creates a convolutional layer, with optional batch normalization.
"""
layers = []
conv_layer = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)
if init_zero_weights:
conv_layer.weight.data = torch.randn(out_channels, in_channels, kernel_size, kernel_size) * 0.001
layers.append(conv_layer)
if batch_norm:
layers.append(nn.BatchNorm2d(out_channels))
return nn.Sequential(*layers)
class DCGenerator(nn.Module):
def __init__(self, noise_size, conv_dim):
super(DCGenerator, self).__init__()
self.deconv1 = deconv(100,128,4,padding=0)
self.deconv2 = deconv(128,64,4)
self.deconv3 = deconv(64,32,4)
self.deconv4 = deconv(32,3,4,batch_norm=False)
def forward(self, z):
"""Generates an image given a sample of random noise.
Input
-----
z: BS x noise_size x 1 x 1 --> 16x100x1x1
Output
------
out: BS x channels x image_width x image_height --> 16x3x32x32
"""
out = F.relu(self.deconv1(z))
out = F.relu(self.deconv2(out))
out = F.relu(self.deconv3(out))
out = F.tanh(self.deconv4(out))
return out
class ResnetBlock(nn.Module):
def __init__(self, conv_dim):
super(ResnetBlock, self).__init__()
self.conv_layer = conv(in_channels=conv_dim, out_channels=conv_dim, kernel_size=3, stride=1, padding=1)
def forward(self, x):
out = x + self.conv_layer(x)
return out
class CycleGenerator(nn.Module):
"""Defines the architecture of the generator network.
Note: Both generators G_XtoY and G_YtoX have the same architecture in this assignment.
"""
def __init__(self, conv_dim=64, init_zero_weights=False):
super(CycleGenerator, self).__init__()
# 1. Define the encoder part of the generator (that extracts features from the input image)
self.conv1 = conv(3,32,4)
self.conv2 = conv(32,64,4,padding=0,batch_norm=False)
# 2. Define the transformation part of the generator
self.resnet_block = ResnetBlock(64)
# 3. Define the decoder part of the generator (that builds up the output image from features)
self.deconv1 = deconv(64,32,4,padding=0)
self.deconv2 = deconv(32,3,4,batch_norm=False)
def forward(self, x):
"""Generates an image conditioned on an input image.
Input
-----
x: BS x 3 x 32 x 32
Output
------
out: BS x 3 x 32 x 32
"""
out = F.relu(self.conv1(x))
out = F.relu(self.conv2(out))
out = F.relu(self.resnet_block(out))
out = F.relu(self.deconv1(out))
out = F.tanh(self.deconv2(out))
return out
class DCDiscriminator(nn.Module):
"""Defines the architecture of the discriminator network.
Note: Both discriminators D_X and D_Y have the same architecture in this assignment.
"""
def __init__(self, conv_dim=64):
super(DCDiscriminator, self).__init__()
self.conv1 = conv(3,32,4)
self.conv2 = conv(32,64,4)
self.conv3 = conv(64,128,4)
self.conv4 = conv(128,1,4,batch_norm=False,padding=0)
def forward(self, x):
out = F.relu(self.conv1(x))
out = F.relu(self.conv2(out))
out = F.relu(self.conv3(out))
out = self.conv4(out).squeeze()
out = F.sigmoid(out)
return out