-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrunTrain.py
176 lines (139 loc) · 5.38 KB
/
runTrain.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
################
#
# Deep Flow Prediction - N. Thuerey, K. Weissenov, H. Mehrotra, N. Mainali, L. Prantl, X. Hu (TUM)
#
# Main training script
#
################
import os, sys, random
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torch.optim as optim
import matplotlib.pyplot as plt
from DfpNet import TurbNetG, weights_init
import dataset
import utils
######## Settings ########
# number of training iterations
iterations = 400000
# batch size
batch_size = 5
# learning rate, generator
lrG = 0.0006
# decay learning rate?
decayLr = True
# channel exponent to control network size
expo = 7
# data set config
prop=None # by default, use all from "../data/train"
# save txt files with per epoch loss?
saveL1 = True
saveModel = True
n_save_model = 100
##########################
prefix = ""
if len(sys.argv)>1:
prefix = sys.argv[1]
print("Output prefix: {}".format(prefix))
dropout = 0. # note, the original runs from https://arxiv.org/abs/1810.08217 used slight dropout, but the effect is minimal; conv layers "shouldn't need" dropout, hence set to 0 here.
doLoad = "" # optional, path to pre-trained model
print("LR: {}".format(lrG))
print("LR decay: {}".format(decayLr))
print("Iterations: {}".format(iterations))
print("Dropout: {}".format(dropout))
##########################
seed = random.randint(0, 2**32 - 1)
print("Random seed: {}".format(seed))
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
#torch.backends.cudnn.deterministic=True # warning, slower
# create pytorch data object with dfp dataset
data = dataset.TurbDataset(prop, dataDir="./BASIC_data_coordinates_final_metricsAll_1940/train_avg/", dataDirTest="./BASIC_data_coordinates_final_metricsAll_1940/test_avg/", shuffle=1)
trainLoader = DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True)
print("Training batches: {}".format(len(trainLoader)))
dataValidation = dataset.ValiDataset(data)
valiLoader = DataLoader(dataValidation, batch_size=batch_size, shuffle=True, drop_last=True)
print("Validation batches: {}".format(len(valiLoader)))
# setup training
epochs = int(iterations/len(trainLoader) + 0.5)
netG = TurbNetG(channelExponent=expo, dropout=dropout)
print(netG) # print full net
model_parameters = filter(lambda p: p.requires_grad, netG.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
print("Initialized TurbNet with {} trainable params ".format(params))
netG.apply(weights_init)
if len(doLoad)>0:
netG.load_state_dict(torch.load(doLoad))
print("Loaded model "+doLoad)
netG.cuda()
criterionL1 = nn.L1Loss()
criterionL1.cuda()
optimizerG = optim.Adam(netG.parameters(), lr=lrG, betas=(0.5, 0.999), weight_decay=0.0)
targets = Variable(torch.FloatTensor(batch_size, 4, 128, 128))
inputs = Variable(torch.FloatTensor(batch_size, 12, 128, 128))
targets = targets.cuda()
inputs = inputs.cuda()
##########################
for epoch in range(epochs):
print("Starting epoch {} / {}".format((epoch+1),epochs))
netG.train()
L1_accum = 0.0
samples_accum = 0
for i, traindata in enumerate(trainLoader, 0):
inputs_cpu, targets_cpu = traindata
current_batch_size = targets_cpu.size(0)
inputs_cpu = inputs_cpu.float().cuda()
targets_cpu = targets_cpu.float().cuda()
inputs.data.resize_as_(inputs_cpu).copy_(inputs_cpu)
targets.data.resize_as_(targets_cpu).copy_(targets_cpu)
# compute LR decay
if decayLr:
currLr = utils.computeLR(epoch, epochs, lrG*0.1, lrG)
if currLr < lrG:
for g in optimizerG.param_groups:
g['lr'] = currLr
netG.zero_grad()
gen_out = netG(inputs)
############################################################# ---- work here!
lossL1 = criterionL1(gen_out, targets)
lossL1.backward()
optimizerG.step()
lossL1viz = lossL1.item()
L1_accum += lossL1viz
samples_accum += current_batch_size
if i==len(trainLoader)-1:
logline = "Epoch: {}, batch-idx: {}, L1: {}\n".format(epoch, i, lossL1viz)
print(logline)
# validation
netG.eval()
L1val_accum = 0.0
for i, validata in enumerate(valiLoader, 0):
inputs_cpu, targets_cpu = validata
current_batch_size = targets_cpu.size(0)
targets_cpu = targets_cpu.float().cuda()
inputs_cpu = inputs_cpu.float().cuda()
inputs.data.resize_as_(inputs_cpu).copy_(inputs_cpu)
targets.data.resize_as_(targets_cpu).copy_(targets_cpu)
outputs = netG(inputs)
############################################################# ---- work here!
lossL1 = criterionL1(outputs, targets)
L1val_accum += lossL1.item()
# data for graph plotting
L1_accum /= len(trainLoader)
L1val_accum /= len(valiLoader)
if saveL1:
if epoch==0:
utils.resetLog(prefix + "L1.txt" )
utils.resetLog(prefix + "L1val.txt")
utils.log(prefix + "L1.txt" , "{} ".format(L1_accum), False)
utils.log(prefix + "L1val.txt", "{} ".format(L1val_accum), False)
if saveModel:
if epoch % n_save_model == 0:
print("++++++++++ Save model... modelG... ++++++++++")
torch.save(netG.state_dict(), prefix + "modelG" )
torch.save(netG.state_dict(), prefix + "modelG" )