-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
165 lines (126 loc) · 6.02 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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__()
###########################################
## FILL THIS IN: CREATE ARCHITECTURE ##
###########################################
# self.deconv1 = deconv(...)
# self.deconv2 = deconv(...)
# self.deconv3 = deconv(...)
# self.deconv4 = deconv(...)
self.deconv1 = deconv(in_channels=100, out_channels=128, kernel_size=4, stride=2, padding=0, batch_norm=True)
self.deconv2 = deconv(in_channels=128, out_channels=64, kernel_size=4, stride=2, padding=1, batch_norm=True)
self.deconv3 = deconv(in_channels=64, out_channels=32, kernel_size=4, stride=2, padding=1, batch_norm=True)
self.deconv4 = deconv(in_channels=32, out_channels=3, kernel_size=4, stride=2, padding=1, 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__()
###########################################
## FILL THIS IN: CREATE ARCHITECTURE ##
###########################################
# 1. Define the encoder part of the generator (that extracts features from the input image)
# self.conv1 = conv(...)
# self.conv2 = conv(...)
# 2. Define the transformation part of the generator
# self.resnet_block = ...
# 3. Define the decoder part of the generator (that builds up the output image from features)
# self.deconv1 = deconv(...)
# self.deconv2 = deconv(...)
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__()
###########################################
## FILL THIS IN: CREATE ARCHITECTURE ##
###########################################
# self.conv1 = conv(...)
# self.conv2 = conv(...)
# self.conv3 = conv(...)
# self.conv4 = conv(...)
self.conv1 = conv(in_channels=3, out_channels=32, kernel_size=4, stride=2, padding=1, batch_norm=True, init_zero_weights=False)
self.conv2 = conv(in_channels=32, out_channels=64, kernel_size=4, stride=2, padding=1, batch_norm=True, init_zero_weights=False)
self.conv3 = conv(in_channels=64, out_channels=128, kernel_size=4, stride=2, padding=1, batch_norm=True, init_zero_weights=False)
self.conv4 = conv(in_channels=128, out_channels=1, kernel_size=4, stride=2, padding=0, batch_norm=False, init_zero_weights=False)
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