-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcgan.py
199 lines (151 loc) · 6.95 KB
/
dcgan.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import torch as t
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torch.optim import Adam
import numpy as np
import cv2
import os
'''
Deep convolutional generative adversarial network to produce artificial bubble images to imitate
32x32 pixel preprocessed images of bubbles in PICO-60. The training images can be produces with
PICO60_image_analysis.py.
'''
batch_size = 16
img_size = 32
latent_dim = 20 # Dimension of the latent space
channels = 1
lr = 0.00008 #0.0001
b1, b2 = 0.7, 0.999 # Beta parameters for Adam optimizer
n_epochs = 60
n_ch = 128 # Number of channels in initial convolution layers
sample_interval = 100 # Save a generated image every sample_interval number of batches
save_progress_samples = False # Save images every sample_interval number of batches to check progress
write_dir = './dcgan_gen_images/' # Directory to save images to if save_progress_samples is True
data_dir = './bub_single_24/' # Directory containing training images
model_path = './dcgan_model.pt' # File to save the model to once training is complete
if save_progress_samples:
os.makedirs(write_dir, exist_ok=True)
class Data(Dataset):
def __init__(self):
self.data = [cv2.imread(data_dir + im, cv2.IMREAD_GRAYSCALE)//255 for im in os.listdir(data_dir) if '-0.png' in im]
self.data = t.Tensor([im for im in self.data if im.shape == (img_size, img_size)])
self.len = self.data.shape[0]
def __len__(self):
return self.len
def __getitem__(self, index):
return self.data[index]
class Generator(nn.Module):
# Takes as input a random tensor of size (batch_size, latent_dim) generated by noise()
def __init__(self):
super(Generator, self).__init__()
self.init_size = img_size//4
self.l1 = nn.Sequential(nn.Linear(latent_dim, n_ch * self.init_size**2))
self.conv_blocks = nn.Sequential(
nn.BatchNorm2d(n_ch),
nn.Upsample(scale_factor=2),
nn.Conv2d(n_ch, n_ch, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(n_ch, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Upsample(scale_factor=2),
nn.Conv2d(n_ch, n_ch//2, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(n_ch//2, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(n_ch//2, channels, kernel_size=3, stride=1, padding=1),
nn.Sigmoid()
#nn.Tanh()
)
def forward(self, z):
out = self.l1(z)
out = out.view(out.shape[0], n_ch, self.init_size, self.init_size)
img = self.conv_blocks(out)
return img
class Discriminator(nn.Module):
# Takes as input a data image or image generated by Generator
def __init__(self):
super(Discriminator, self).__init__()
def discriminator_block(in_filters, out_filters, bn=True):
block = [nn.Conv2d(in_filters, out_filters, 3, 2, 1), nn.LeakyReLU(0.2, inplace=True), nn.Dropout2d(0.25)]
if bn:
block.append(nn.BatchNorm2d(out_filters, 0.8))
return block
self.model = nn.Sequential(
*discriminator_block(channels, n_ch//8, bn=False),
*discriminator_block(n_ch//8, n_ch//4),
*discriminator_block(n_ch//4, n_ch//2),
*discriminator_block(n_ch//2, n_ch),
)
# The height and width of downsampled image
ds_size = img_size // 2 ** 4
self.adv_layer = nn.Sequential(nn.Linear(n_ch * ds_size ** 2, 1), nn.Sigmoid())
def forward(self, img):
out = self.model(img)
out = out.view(out.shape[0], -1)
validity = self.adv_layer(out)
return validity
def generate_noise(name):
if name == 'Gaussian':
return t.normal(mean=0, std=1, size=(batch_size, latent_dim))
return t.rand((batch_size, latent_dim))
def save_model():
print('Saving Saving generator model to ' + model_path)
t.save(G, model_path)
def main():
dataset = Data()
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
# Loss function
adversarial_loss = nn.BCELoss()
G = Generator()
D = Discriminator()
# Optimizers
optimizer_G = Adam(G.parameters(), lr=lr, betas=(b1, b2))
optimizer_D = Adam(D.parameters(), lr=lr, betas=(b1, b2))
try:
for epoch in range(n_epochs):
for i, data_imgs in enumerate(dataloader):
# Configure generator input
data_shape = data_imgs.shape
data_imgs = data_imgs.view(data_imgs.shape[0], channels, data_imgs.shape[1], data_imgs.shape[2])
# Adversarial ground truths
valid = t.ones((data_shape[0], 1), requires_grad=False)
fake = t.zeros((data_shape[0], 1), requires_grad=False)
# ----------------
# Train Generator
# ----------------
optimizer_G.zero_grad()
# Sample noise as generator input
noise = generate_noise('Gaussian')
# Generate a batch of images
gen_imgs = G(noise)
gen_imgs = gen_imgs[:data_shape[0]]
# Loss measures generator's ability to fool the discriminator
G_loss = adversarial_loss(D(gen_imgs), valid)
G_loss.backward()
optimizer_G.step()
# --------------------
# Train Discriminator
# --------------------
optimizer_D.zero_grad()
# Measure discriminator's ability to classify real from generated samples
real_loss = adversarial_loss(D(data_imgs), valid)
fake_loss = adversarial_loss(D(gen_imgs.detach()), fake)
D_loss = (real_loss + fake_loss) / 2
D_loss.backward()
optimizer_D.step()
# ----------------
# Log Progress
# ----------------
print(
"[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
% (epoch, n_epochs, i, len(dataloader), D_loss.item(), G_loss.item())
)
if save_progress_samples:
batches_done = epoch*len(dataloader) + i
if batches_done % sample_interval == 0:
os.makedirs(write_dir + str(batches_done), exist_ok=True)
for j, gen_im in enumerate(gen_imgs):
cv2.imwrite('%s%d/%d.png' % (write_dir, batches_done, j), gen_im[0].detach().numpy()*255)
save_model()
except KeyboardInterrupt:
save_model()
if __name__ == '__main__':
main()