-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdcgan_pytorch.py
More file actions
337 lines (261 loc) · 11.1 KB
/
Copy pathdcgan_pytorch.py
File metadata and controls
337 lines (261 loc) · 11.1 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import os
import numpy as np
import random
from PIL import Image
import matplotlib.pyplot as plt
from math import floor
import argparse
parser = argparse.ArgumentParser(description='Train Dataset on DCGAN',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--data_path', type=str,
default='./data/', help='Path of dataset for train ')
parser.add_argument('--dest_path', type=str,
default='./data/', help='Path of dataset for test')
args = parser.parse_args()
print(args)
def zero():
return np.random.uniform(0.0, 0.01, size = [1])
def one():
return np.random.uniform(0.99, 1.0, size = [1])
def noise(n):
return np.random.uniform(-1.0, 1.0, size = [n, 4096])
os.makedirs(args.dest_path, exist_ok=True)
os.makedirs(os.path.join(args.dest_path , "Models"), exist_ok=True)
os.makedirs(os.path.join(args.dest_path , "Results"), exist_ok=True)
print("Importing Images...")
Images = []
images_path = args.data_path #"/home/yasamin/Documents/Data_breast/Data_breast_splits/split_1/train/benign"
#os.getcwd() + '\images'
files = os.listdir(images_path)
for name in os.listdir(images_path):
path_img = os.path.join(images_path , name)
temp1 = Image.open(path_img)
temp1 = temp1.resize((256 , 256))
temp = np.array(temp1.convert('L'), dtype='float32')
Images.append(temp / 255)
Images.append(np.flip(Images[-1], 1))
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import random
import numpy as np
import os
from torchvision import transforms
# Helper functions for noise and labels
def zero():
return torch.rand(1) * 0.01
def one():
return torch.rand(1) * 0.01 + 0.99
def noise(n):
return torch.randn(n, 4096)
# GAN class implementation in PyTorch
class GAN(nn.Module):
def __init__(self):
super(GAN, self).__init__()
# Config
self.LR = 0.0001
self.steps = 1
# Define the models
self.D = self.discriminator()
self.G = self.generator()
# Define optimizers
self.optimizer_D = optim.Adam(self.D.parameters(), lr=self.LR)
self.optimizer_G = optim.Adam(self.G.parameters(), lr=self.LR)
def discriminator(self):
model = nn.Sequential(
nn.Conv2d(1, 8, kernel_size=3, padding = 'same'),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.AvgPool2d(2, 2),
nn.Conv2d(8, 16, kernel_size=3, padding = 'same'),
nn.BatchNorm2d(16, momentum=0.7),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.AvgPool2d(2, 2),
nn.Conv2d(16, 32, kernel_size=3, padding = 'same'),
nn.BatchNorm2d(32, momentum=0.7),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.AvgPool2d(2, 2),
nn.Conv2d(32, 64, kernel_size=3, padding = 'same'),
nn.BatchNorm2d(64, momentum=0.7),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.AvgPool2d(2, 2),
nn.Conv2d(64, 128, kernel_size=3, padding = 'same'),
nn.BatchNorm2d(128, momentum=0.7),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.AvgPool2d(2, 2),
nn.Conv2d(128, 256, kernel_size=3, padding = 'same'),
nn.BatchNorm2d(256, momentum=0.7),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.AvgPool2d(2, 2),
nn.Flatten(),
nn.Linear(4 * 4 * 256, 128), # Update input features here to match output of conv layers
nn.LeakyReLU(0.2),
nn.Linear(128, 1),
nn.Sigmoid()
)
return model
def generator(self):
model = nn.Sequential(
# 1x1x4096
nn.ConvTranspose2d(4096, 256, kernel_size=4),
nn.ReLU(),
# 4x4x256
nn.Conv2d(256, 256, kernel_size=4, padding='same'),
nn.BatchNorm2d(256, momentum=0.7),
nn.ReLU(),
nn.Upsample(scale_factor=2),
# 8x8x256
nn.Conv2d(256, 128, kernel_size=4, padding='same'),
nn.BatchNorm2d(128, momentum=0.7),
nn.ReLU(),
nn.Upsample(scale_factor=2),
# 16x16x128
nn.Conv2d(128, 64, kernel_size=3, padding='same'),
nn.BatchNorm2d(64, momentum=0.7),
nn.ReLU(),
nn.Upsample(scale_factor=2),
# 32x32x64
nn.Conv2d(64, 32, kernel_size=3, padding='same'),
nn.BatchNorm2d(32, momentum=0.7),
nn.ReLU(),
nn.Upsample(scale_factor=2),
# 64x64x32
nn.Conv2d(32, 16, kernel_size=3, padding='same'),
nn.BatchNorm2d(16, momentum=0.7),
nn.ReLU(),
nn.Upsample(scale_factor=2),
# 128x128x16
nn.Conv2d(16, 8, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Upsample(scale_factor=2),
# 256x256x8
nn.Conv2d(8, 1, kernel_size=3, padding='same'),
nn.Sigmoid()
)
return model
def forward_G(self, x):
x = x.view(x.size(0), 4096, 1, 1) # Reshape to [batch_size, 4096, 1, 1]
return self.G(x)
def forward_D(self, x):
return self.D(x)
def train_discriminator(self, real_data, fake_data):
self.optimizer_D.zero_grad()
# Train on real data
real_pred = self.forward_D(real_data)
real_loss = F.binary_cross_entropy(real_pred, torch.zeros_like(real_pred))
real_loss.backward()
# Train on fake data
fake_pred = self.forward_D(fake_data)
fake_loss = F.binary_cross_entropy(fake_pred, torch.ones_like(fake_pred))
fake_loss.backward()
self.optimizer_D.step()
return real_loss.item(), fake_loss.item()
def train_generator(self, batch_size):
self.optimizer_G.zero_grad()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
z = noise(batch_size).to(device)
fake_data = self.forward_G(z)
fake_pred = self.forward_D(fake_data)
loss = F.binary_cross_entropy(fake_pred, torch.zeros_like(fake_pred))
loss.backward()
self.optimizer_G.step()
return loss.item()
# Model GAN implementation in PyTorch
class Model_GAN:
def __init__(self):
self.GAN = GAN()
def train(self, images, batch_size=16, device='cpu'):
self.GAN.D.to(device) # Ensure the Discriminator is on the correct device
self.GAN.G.to(device) # Ensure the Generator is on the correct device
#for i in range(500000): # Adjust the range as needed
im_no = random.randint(0, len(images) - batch_size - 1)
# Convert the selected images to tensors, ensuring any negative strides are removed
real_data = torch.stack([transforms.ToTensor()(np.copy(img)) for img in images[im_no:im_no + int(batch_size / 2)]])
real_data = real_data.to(device) # Move data to the device
z = noise(int(batch_size / 2)).to(device) # Move noise to the device
fake_data = self.GAN.forward_G(z)
d_real, d_fake = self.GAN.train_discriminator(real_data, fake_data)
g_loss = self.GAN.train_generator(batch_size)
print(f"D Real: {d_real}, D Fake: {d_fake}, G All: {g_loss}")
if self.GAN.steps % 500 == 0:
self.save(floor(self.GAN.steps / 1000))
self.evaluate(device)
self.GAN.steps += 1
def evaluate(self, device='cpu'):
self.GAN.G.to(device) # Ensure the Generator is on the correct device
with torch.no_grad():
z = noise(48).to(device) # Move noise to the device
generated_images = self.GAN.forward_G(z).cpu()
# Visualization logic goes here using matplotlib or any other tool
# e.g., plt.imshow(generated_images[0].permute(1, 2, 0)) to show the first image
def save(self, num):
# Save the generator and discriminator state dictionaries
torch.save(self.GAN.G.state_dict(), os.path.join(args.dest_path , "Models/gen.pth"))
torch.save(self.GAN.D.state_dict(), os.path.join(args.dest_path ,"Models/dis.pth"))
print(f"Model number {str(num)} Saved!")
def load(self, num):
steps1 = self.GAN.steps
# Reinitialize the GAN model
self.GAN = GAN()
# Load the state dictionaries into the models
self.GAN.G.load_state_dict(torch.load(os.path.join(args.dest_path , "Models/gen.pth")))
self.GAN.D.load_state_dict(torch.load(os.path.join(args.dest_path ,"Models/dis.pth")))
# Ensure models are set to evaluation mode (if needed)
self.GAN.G.eval()
self.GAN.D.eval()
# Reinitialize the necessary components
self.generator = self.GAN.generator()
self.DisModel = self.GAN.DisModel()
self.AdModel = self.GAN.AdModel()
# Restore the step count
self.GAN.steps = steps1
def eval2(self, num=0, device='cpu'):
self.GAN.G.to(device) # Ensure the Generator is on the correct device
with torch.no_grad():
z = noise(48).to(device) # Move noise to the device
generated_images = self.GAN.forward_G(z).cpu() # Move the generated images to the CPU for saving
generated_images = np.squeeze(generated_images)
print(generated_images.shape)
generated_images = generated_images.permute(0, 1, 2).numpy()
print(generated_images.shape)
r1 = np.concatenate(generated_images[:8], axis=1)
r2 = np.concatenate(generated_images[8:16], axis=1)
r3 = np.concatenate(generated_images[16:24], axis=1)
r4 = np.concatenate(generated_images[24:32], axis=1)
r5 = np.concatenate(generated_images[32:40], axis=1)
r6 = np.concatenate(generated_images[40:48], axis=1)
c1 = np.concatenate([r1, r2, r3, r4, r5, r6], axis=0)
image = (c1 * 255).astype(np.uint8)
print(image.shape)
print(type(image))
Image.fromarray(image , mode = 'L').save(os.path.join( args.dest_path , "Results/i.png"))
# Check if CUDA is available, otherwise use CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# If training a new model:
model = Model_GAN()
model.GAN.D.to(device)
model.GAN.G.to(device)
# Print model summaries
print("Discriminator Summary:")
print(model.GAN.D)
print("\nGenerator Summary:")
print(model.GAN.G)
print("We're off! See you in a while!")
# Set the initial step count if needed
# model.GAN.steps = 165001
# Assuming 'Images' is a preloaded dataset of images for training
while model.GAN.steps < 500000:
# Train the model
model.train(Images, device=device) # Pass the device to the train method
if model.GAN.steps % 500 == 0:
print("\nRound: " + str(model.GAN.steps))
if model.GAN.steps % 1000 == 0:
print("\n\n\n\nRound " + str(model.GAN.steps) + ":")
model.eval2(int(model.GAN.steps / 1000), device=device) # Pass the device to eval2