-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-functa.py
292 lines (236 loc) · 9.13 KB
/
main-functa.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
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
'''
A file to train the Functa network.
It uses typer options for better compability with HPC systems.
'''
import os
import typer
from copy import deepcopy
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import numpy as np
import random
import matplotlib.pyplot as plt
from PIL import Image
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
from torchvision.io import read_image, ImageReadMode
from torch.utils.data import Dataset, DataLoader, random_split
from torchvision import datasets
from torchvision.transforms import Resize, Compose, ToTensor, Normalize
from models import ModulatedSineLayer, ModulatedSiren, ModulatedGaborR, ModulatedWIRE
from datautils import get_mgrid, INR_Dataset
from logs import Logger, present_time
# Training code
def train_functa(
model,
train_ds,
valid_ds,
num_iter,
bs,
N_inner,
lr_outer,
lr_inner,
lr_meta_decay = 0.9,
ep_start = None,
log_period = 1,
verbose = True,
args = None,
loss_func = F.mse_loss,
pretrained_path = None,
device = 'cuda'
):
print ('=== Training started ===\n')
logger = Logger(log_period=log_period, verbose=verbose, args=args)
if pretrained_path != None:
logger.load_from_path(path = pretrained_path, device = device)
model = deepcopy(logger.best_model_valid)
#meta_optimizer = deepcopy(logger.best_optim_valid)
#scheduler = torch.optim.lr_scheduler.StepLR(meta_optimizer, 5000, lr_meta_decay)
#else:
meta_optimizer = torch.optim.Adam(lr=lr_outer, params=model.parameters())
scheduler = torch.optim.lr_scheduler.StepLR(meta_optimizer, 5000, lr_meta_decay)
if ep_start == None:
ep_start = 1
model.train()
meta_grad_init = [0 for _ in range(len(model.state_dict()))] # starting point for meta-gradients
train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True) # === Check CAVIA for pin_memory
valid_dl = DataLoader(valid_ds, batch_size=bs, shuffle=True) # === Check CAVIA for pin_memory
iter = ep_start
while iter <= num_iter:
for counter, (xb,yb,imgs) in enumerate(train_dl):
if xb.shape[0] != bs:
continue
meta_grad = deepcopy(meta_grad_init)
logger.prepare_inner_loop(iter)
for j in range(bs):
model.reset_modulation()
logger.log_pre_update(iter, xb[j], yb[j], model)
# --- inner update
for i in range(N_inner):
# prediction
pred_train = model(xb[j])
# loss
loss_train = loss_func(pred_train, yb[j])
# grad, There are some note in the CAVIA's supplemetary matrial
grad_train = torch.autograd.grad(loss_train, model.modulation, create_graph=True)[0]
# update modulations
model.modulation = model.modulation - lr_inner * grad_train
# --- meta-gradients
pred_test = model(xb[j])
loss_test = loss_func(pred_test, yb[j])
grad_test = torch.autograd.grad(loss_test, model.parameters())
for i in range(len(grad_test)):
meta_grad[i] += grad_test[i].detach()
# print(f'loss test for meta-training: {loss_test}')
logger.log_post_update(iter, xb[j], yb[j], model)
model.reset_modulation()
logger.summarise_inner_loop(iter, mode='train')
if iter % log_period == 0:
evaluate(iter, model, logger, valid_dl, N_inner, lr_inner)
logger.update_best_model(iter, logger, model, meta_optimizer)
logger.print_logs(iter, grad_train, meta_grad)
# === save checkpoints ===
# --- Meta-update
meta_optimizer.zero_grad()
# setting gradients
for c, param in enumerate(model.parameters()):
param.grad = meta_grad[c] / float(bs)
param.grad.data.clamp_(-10, 10) # based on CAVIA
meta_optimizer.step()
scheduler.step()
iter += 1
if iter > num_iter:
logger.save_stats()
break
model.reset_modulation()
return logger, model
# Evaluation function
def evaluate(
iter,
model,
logger,
dataloader,
N_inner,
lr_inner,
loss_func = F.mse_loss
):
logger.prepare_inner_loop(iter, mode='valid')
for counter, (xb,yb,_) in enumerate(dataloader):
for j in range(xb.shape[0]):
model.reset_modulation()
# --- inner update
logger.log_pre_update(iter, xb[j], yb[j], model, mode='valid')
for _ in range(N_inner):
# prediction
pred_train = model(xb[j])
# loss
loss_train = loss_func(pred_train, yb[j])
# grad, There are some note in the CAVIA's supplemetary matrial
grad_train = torch.autograd.grad(loss_train, model.modulation, create_graph=True)[0]
# update modulations
model.modulation = model.modulation - lr_inner * grad_train
logger.log_post_update(iter, xb[j], yb[j], model, mode='valid')
# reset context parameters
model.reset_modulation()
# this will take the mean over the batches
logger.summarise_inner_loop(iter, mode='valid')
def main_process(
seed: int = 216,
data_path: str = './Data/Images/', #Now, datapath should directly point to the Images folder.
table_path: str = './Data/data_table.csv',
image_len: int = 128,
valid_split: float = 0.2,
method: str = 'siren', # 'siren', 'realgabor'
in_features: int = 2,
hidden_features: int = 128,
hidden_layers: int = 10,
num_modulations: int = 256,
out_features: int = 1,
last_linear: bool = True,
omega_0: int = 100,
scale_0: int = 10,
num_iter: int = 10000,
batch_size: int = 8,
N_inner: int = 2,
lr_outer: float = 5e-5,
lr_inner: float = 0.01,
ep_start: int = 1,
log_period: int = 20,
pretrained_path: str = None
):
args_str = '''
seed: {},
data_path: {},
table_path: {},
image_len: {},
valid_split: {},
method: {},
in_features: {},
hidden_features: {},
hidden_layers: {},
num_modulations: {},
out_features: {},
last_linear: {},
omega_0: {},
scale_0: {},
num_iter: {},
batch_size: {},
N_inner: {},
lr_outer: {},
lr_inner: {},
ep_start: {},
log_period: {},
pretrained_path: {}
'''.format(
seed,data_path,table_path,image_len,valid_split,method,in_features,hidden_features,
hidden_layers,num_modulations,out_features,last_linear,
omega_0,scale_0,num_iter,batch_size,N_inner,
lr_outer,lr_inner,ep_start,log_period,pretrained_path
)
print (args_str)
#os.chdir(data_path)
print ('Current working directory: ' + os.getcwd() + '\n')
# === Reading the data table; data_table.csv is the metadata file which is not created yet.
data_table = pd.read_csv(table_path, sep=';')
print (data_table.tail(10))
# === Data prepration
device = 'cuda' if torch.cuda.is_available() else 'cpu'
ds = INR_Dataset(data_table, data_path, image_len, device=device)
# === Data Split
generator = torch.Generator().manual_seed(seed)
train_ds, valid_ds = random_split(ds, [1-valid_split, valid_split], generator=generator)
print ('\nTrain size: ',train_ds.__len__(), '--- Valid size: ', valid_ds.__len__())
# === Model setup
assert method == 'siren' or method == 'realgabor', 'Method can be siren or realgabor'
if method == 'siren':
model = ModulatedSiren(
in_features=in_features,
hidden_features=[hidden_features]*hidden_layers,
num_modulations=num_modulations,
out_features=out_features,
last_linear=last_linear,
device = device,
first_omega_0=omega_0,
hidden_omega_0=omega_0
).to(device)
elif method == 'realgabor':
model = ModulatedWIRE(
in_features=in_features,
hidden_features=[hidden_features]*hidden_layers,
num_modulations=num_modulations,
out_features=out_features,
last_linear=last_linear,
omega_0 = omega_0,
scale_0 = scale_0,
wavelet_type = 'real',
device = device
).to(device)
summary(model, (in_features,), device = device)
# === Model training
logger, model = train_functa(model, train_ds, valid_ds, num_iter, batch_size, N_inner, lr_outer, lr_inner, ep_start = ep_start, log_period = log_period, args = args_str, device = device)
return
# Running the main process
if __name__ == '__main__':
typer.run(main_process)