-
Notifications
You must be signed in to change notification settings - Fork 0
/
mygan.py
165 lines (150 loc) · 6.92 KB
/
mygan.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
import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import HTML
from torch.utils.data import Dataset
from PIL import Image
from numpy import asarray
class MyImageDataSet(Dataset):
"""A generic data loader where the images (no labels) are arranged in this way:
root/xxx.png
root/xxy.png
root/subfolder1/xxz.png
root/subfolder2/123.png
Args:
root (string): Root directory path.
extension (string): image extension.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
"""
def __init__(self, root, extension, transform):
self.root = root
self.extension = extension
self.transform = transform
if len([f for f in os.listdir(root) if f.endswith(extension)]) > 0:
self.all_imgs = [f for f in os.listdir(root) if f.endswith(extension)]
else:
subfolders = os.listdir(root)
self.all_imgs = []
for subf in subfolders:
self.all_imgs.extend([subf + '/' + f for f in os.listdir(root + '/' + subf) if f.endswith(extension)])
def __len__(self):
return len(self.all_imgs)
def __getitem__(self, idx):
img_loc = os.path.join(self.root, self.all_imgs[idx])
image = Image.open(img_loc)
if self.transform:
image = self.transform(image)
return image
# custom weights initialization called on netG and netD (From the DCGAN paper)
def weights_init(m):
if isinstance(m, nn.Conv2d) or isinstance(m,nn.ConvTranspose2d):
# Fills the input Tensor m.weight.data with values drawn from the normal distribution (with mean 0.0 and std 0.02)
nn.init.normal_(m.weight.data, 0.0, 0.02)
if isinstance(m, nn.BatchNorm2d):
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
class Generator(nn.Module):
'''
Generator Class
Values:
z_dim: the dimension of the noise vector, a scalar
im_chan: the number of channels in the images, fitted for the dataset used, a scalar
hidden_dim: the inner dimension, a scalar
'''
def __init__(self, ngpu, z_dim=100, im_chan=3, hidden_dim=64):
super(Generator, self).__init__()
self.ngpu = ngpu # Number of GPUs available. Use 0 for CPU mode.
self.z_dim = z_dim
# Build the neural network
self.gen = nn.Sequential(
self.make_gen_block(z_dim, hidden_dim * 8, kernel_size=4, stride=1, padding=0),
self.make_gen_block(hidden_dim * 8, hidden_dim * 4),
self.make_gen_block(hidden_dim * 4, hidden_dim * 2),
self.make_gen_block(hidden_dim * 2, hidden_dim),
self.make_gen_block(hidden_dim, im_chan, final_layer=True),
)
def make_gen_block(self, input_channels, output_channels, kernel_size=4, stride=2, padding=1, final_layer=False):
'''
Function to return a sequence of operations corresponding to a generator block of DCGAN,
corresponding to a transposed convolution, a batchnorm (except for in the last layer), and an activation.
Parameters:
input_channels: how many channels the input feature representation has
output_channels: how many channels the output feature representation should have
kernel_size: the size of each convolutional filter, equivalent to (kernel_size, kernel_size)
stride: the stride of the convolution
padding: zero-padding will be added to both sides of each dimension in the input
final_layer: a boolean, true if it is the final layer and false otherwise
(affects activation and batchnorm)
'''
# Build the neural block
if not final_layer:
return nn.Sequential(
nn.ConvTranspose2d(input_channels, output_channels, kernel_size=kernel_size, stride=stride, padding=padding),
nn.BatchNorm2d(output_channels),
nn.ReLU(inplace=True)
)
else: # Final Layer
return nn.Sequential(
nn.ConvTranspose2d(input_channels, output_channels, kernel_size=kernel_size, stride=stride, padding=padding),
nn.Tanh()
)
def forward(self, noise):
'''
Function for completing a forward pass of the generator.
'''
return self.gen(noise)
class Discriminator(nn.Module):
'''
Discriminator Class
Values:
im_chan: the number of channels in the images, fitted for the dataset used, a scalar
hidden_dim: the inner dimension, a scalar
'''
def __init__(self, ngpu, im_chan=3, hidden_dim=64):
super(Discriminator, self).__init__()
self.ngpu = ngpu
self.disc = nn.Sequential(
self.make_disc_block(im_chan, hidden_dim),
self.make_disc_block(hidden_dim, hidden_dim * 2),
self.make_disc_block(hidden_dim * 2, hidden_dim * 4),
self.make_disc_block(hidden_dim * 4, hidden_dim * 8),
self.make_disc_block(hidden_dim * 8, 1, kernel_size=4, stride=1, padding=0, final_layer=True),
)
def make_disc_block(self, input_channels, output_channels, kernel_size=4, stride=2, padding=1, final_layer=False):
'''
Function to return a sequence of operations corresponding to a discriminator block of DCGAN,
corresponding to a convolution, a batchnorm (except for in the last layer), and an activation.
Parameters:
input_channels: how many channels the input feature representation has
output_channels: how many channels the output feature representation should have
kernel_size: the size of each convolutional filter, equivalent to (kernel_size, kernel_size)
stride: the stride of the convolution
final_layer: a boolean, true if it is the final layer and false otherwise
(affects activation and batchnorm)
'''
# Build the neural block
if not final_layer:
return nn.Sequential(
nn.Conv2d(input_channels, output_channels, kernel_size=kernel_size, stride=stride, padding=padding),
nn.BatchNorm2d(output_channels),
nn.LeakyReLU(negative_slope=0.2, inplace=True)
)
else: # Final Layer
return nn.Sequential(
nn.Conv2d(input_channels, output_channels, kernel_size=kernel_size, stride=stride, padding=padding),
nn.Sigmoid()
)
def forward(self, image):
return self.disc(image)