-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
145 lines (100 loc) · 4.82 KB
/
train.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
############################# Imports ###############################
import os
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
from model import G, D
from dataset import Dataset
from IPython import embed
############################# Declarations ##########################
def save_intermediate_results(save_pth, data):
y_axis = list(np.array(data.view(-1)))
x_axis = [i for i in range(len(y_axis))]
plt.clf()
plt.plot(x_axis, y_axis, '.-', label='line 1', linewidth=2)
Path('results').mkdir(parents=True, exist_ok=True)
plt.savefig(save_pth)
def train(config):
################################### Model Initalization ####################################
dataset = Dataset(config['dir_csv']) # Class declaraction --> Runs ___init___ with Dataset
dataloader = DataLoader(dataset, batch_size=1) # Built-in class within Pytorch
'''
Save Ground Truth Plots
'''
if True:
Path('ground_truths').mkdir(parents=True, exist_ok=True)
for data in dataloader:
real_data = data['real_data']
basename = data['basename'][0]
save_intermediate_results(os.path.join('ground_truths', basename + '.jpg'), real_data)
gen = G().to(config['device'])
dsc = D().to(config['device'])
optimizer_G = torch.optim.Adam(gen.parameters(), lr=config['lr'])
optimizer_D = torch.optim.Adam(dsc.parameters(), lr=config['lr'])
real_label = torch.tensor(1.0).view(1, -1).to(config['device']) # Tensor of shape (1, 1)
fake_label = torch.tensor(0.0).view(1, -1).to(config['device']) # Tensor of shape (1, 1)
criterion = nn.BCELoss() # Binary Cross Entropy Loss
fixed_noise = torch.rand((1, 100)).to(config['device'])
for epoch in range(config['n_epoch']):
for data in dataloader:
real_data = data['real_data'].to(config['device'])
##################### Optimize for Generator ##########################
optimizer_G.zero_grad()
fake_data = gen(fixed_noise) # (1, 100) -> (1, 1, 800)
pred = dsc(fake_data) # (1, 1, 800) -> (1, 1)
G_loss = criterion(pred, real_label) # Train the generator to fool the discriminator
'''
Optimize
'''
G_loss.backward()
optimizer_G.step()
##################### Optimize for Discriminator ######################
optimizer_D.zero_grad()
'''
Real Input
'''
pred = dsc(real_data) # (1, 1, 800) -> (1, 1)
D_loss_real = criterion(pred, real_label) # Train the discriminator to distinguish between real and fake data
'''
Fake Input
'''
pred = dsc(fake_data.detach()) # (1, 1, 800) -> (1, 1)
D_loss_fake = criterion(pred, fake_label) # Train the discriminator to distinguish between real and fake data
'''
Optimize
'''
D_loss_total = (D_loss_real + D_loss_fake) / 2
D_loss_total.backward()
optimizer_D.step()
if (((epoch + 1) % config['val_epoch']) == 0):
Path('results').mkdir(parents=True, exist_ok=True)
save_intermediate_results(os.path.join('results', 'epoch_%d.jpg'%(epoch + 1)), fake_data.detach().cpu())
print('[Epoch] %d / %d'%(epoch + 1, config['n_epoch']), end='\r')
if __name__=='__main__':
'''
Fixed Seeds for Consistency
'''
torch.manual_seed(0)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
random.seed(0)
'''
Configs
'''
config = {
'device' : torch.device('cuda') if (torch.cuda.is_available()) else torch.device('cpu'), # Device to train with
'n_epoch' : 400, # Number of total epochs to run
'lr' : 0.0001, # Learning Rate
'dir_csv' : 'real_data', # Directory of samples
'val_epoch' : 20 # Interval to view results
}
'''
Enter Main Function
'''
train(config)