-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_training.py
120 lines (89 loc) · 3.86 KB
/
run_training.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
import time
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
from graphing_class import CreateGraph
from models import Generator, Discriminator
from dataset_loader import ImagesLoader
start_time = time.time()
plt.rcParams['image.cmap'] = 'gray'
training_paths = []
#Append all training folders
for i in range(10):
training_paths.append("C:/Users/Samuel/PycharmProjects/pythonProject/MNIST/MNIST - JPG - training/{}/".format(i))
if __name__ == '__main__':
#Method for showing images from model
def show_images(images):
sqrtn = int(np.ceil(np.sqrt(images.shape[0])))
for index, image in enumerate(images):
plt.subplot(sqrtn, sqrtn, index+1)
plt.imshow(image.reshape(28, 28))
# Discriminator and generator loss, we use BCEWithLogitsLoss instead of BCELoss for better stability
# Do not use sigmoid activation at the end of discriminator
d_loss_function = nn.BCEWithLogitsLoss()
g_loss_function = nn.BCEWithLogitsLoss()
# Set device
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
print('GPU State:', device)
# Set training parameters
epochs = 200
lr = 0.0002
batch_size = 512
channels_noise = 100
# Create models
G = Generator(channels_noise= channels_noise, channels_img=1).to(device)
D = Discriminator(input_shape= (1, 28, 28)).to(device)
# Load dataset
loader = ImagesLoader(batch_size)
dataset = loader.get_dataset(training_paths, training=True)
# Create optimizers for models
g_optimizer = optim.Adam(G.parameters(), lr=lr, betas=(0.5, 0.999))
d_optimizer = optim.Adam(D.parameters(), lr=lr, betas=(0.5, 0.999))
# Create graphing class
Loss_chart = CreateGraph(118, "Generator and discriminator loss")
# Train
for epoch in range(epochs):
#Batch loop
for images, labels in (zip(*dataset)):
real_inputs = images.to(device)
real_outputs = D(real_inputs)
real_label = torch.ones(real_inputs.shape[0], 1).to(device)
noise = torch.randn(batch_size, channels_noise, 1, 1).to(device)
fake_inputs = G(noise)
fake_outputs = D(fake_inputs)
fake_label = torch.zeros(fake_inputs.shape[0], 1).to(device)
outputs = torch.cat((real_outputs, fake_outputs), 0)
targets = torch.cat((real_label, fake_label), 0)
# Backward propagation
d_loss = d_loss_function(outputs, targets)
Loss_chart.num_for_D += float(d_loss.item())
d_optimizer.zero_grad()
d_loss.backward()
d_optimizer.step()
# Generator
noise = torch.randn(batch_size, channels_noise, 1, 1).to(device)
fake_inputs = G(noise)
fake_outputs = D(fake_inputs)
# Backward propagation
targets = torch.ones([fake_outputs.shape[0], 1])
targets = targets.to(device)
g_loss = g_loss_function(fake_outputs, targets)
Loss_chart.num_for_G += float(g_loss.item())
g_optimizer.zero_grad()
g_loss.backward()
g_optimizer.step()
print('[{}/{}] Average G loss: {:.3f} Average D loss: {:.3f} '.format(epoch + 1, epochs, Loss_chart.num_for_G / 118, Loss_chart.num_for_D / 118))
# Show images
imgs_numpy = ((fake_inputs.view(-1, 784)).data.cpu().numpy()+1.0)/2.0
show_images(imgs_numpy[:16])
plt.show()
# Create graph of loss
Loss_chart.count(epoch + 1)
# Save model every 50th epoch
if epoch % 50 == 0:
torch.save(G, '0.0002_-1-1_Generator_epoch_{}.pth'.format(epoch + 1))
print('Model saved at {} epoch'.format(epoch + 1))
print('Training finished sucessfully.')
print('Training took: {} seconds'.format(time.time()-start_time))