-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
165 lines (130 loc) · 6.58 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import torch
from torch.utils.data import Dataset, DataLoader
import cv2
from PIL import Image
import numpy as np
from tqdm import tqdm
from dataset import ImageDataset
from model import FCNet
import utils
from torch.utils.tensorboard import SummaryWriter
from accelerate.utils import set_seed
from optim_adahessian import Adahessian
from seng import SENG
import argparse
class Trainer:
def __init__(self, image_path, res, use_pe=True, device='cuda', batch_size = 4096,
nepochs = 200, model = None, out_dir = 'output', optimizer = 'adam', lr = 1e-3):
self.dataset = ImageDataset(image_path, res, device)
self.res = res
self.dataloader = DataLoader(self.dataset, batch_size=batch_size, shuffle=True)
out_dir = os.path.join(out_dir, image_path.split('/')[-1].split('.')[0])
os.makedirs(out_dir, exist_ok=True)
self.out_dir = out_dir
if model is not None:
self.model = model
else:
self.model = FCNet(use_pe, num_res = 10, num_layers = 2, width=256).to(device)
if optimizer == 'adam':
self.optimizer = torch.optim.Adam(self.model.parameters(), lr= lr)
self.scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=100, gamma=0.5)
elif optimizer == 'adahessian':
self.optimizer = Adahessian(self.model.parameters())
elif optimizer == 'lbfgs':
self.optimizer = torch.optim.LBFGS(self.model.parameters(), lr=lr)
elif optimizer == 'seng':
self.optimizer = SENG(self.model, 1.2, update_freq=200) # TODO not sure about the params
self.criterion = torch.nn.MSELoss()
self.nepochs = nepochs
def run(self):
pbar = tqdm(range(self.nepochs))
for epoch in pbar:
self.model.train()
for coords, rgb_vals in self.dataloader:
self.optimizer.zero_grad()
out = self.model(coords)
loss = self.criterion(out, rgb_vals)
loss.backward(create_graph=True)
self.optimizer.step()
if (epoch + 1) % 10 == 0:
self.model.eval()
with torch.no_grad():
coords = self.dataset.coords
pred = self.model(coords)
gt = self.dataset.rgb_vals
psnr = utils.get_psnr(pred, gt)
pbar.set_description(f'Epoch:: {epoch}, PSNR: {psnr.item()}')
pred = pred.cpu().numpy().reshape(*self.dataset.image.size[::-1], 3)
pred = (pred * 255).astype(np.uint8)
gt = self.dataset.rgb_vals.cpu().numpy().reshape(*self.dataset.image.size[::-1], 3)
gt = (gt * 255).astype(np.uint8)
save_image = np.hstack([gt, pred])
self.visualize(np.array(save_image), text='PSNR: {:.2f}'.format(psnr), epoch = epoch)
return self.model, psnr
def visualize(self, image, text, epoch):
save_image = np.ones((self.res + 50, 2 * self.res, 3), dtype=np.uint8) * 255
img_start = 50
save_image[img_start:img_start + self.res, :, :] = image
save_image = cv2.cvtColor(save_image, cv2.COLOR_RGB2BGR)
position = (100, 20)
font = cv2.FONT_HERSHEY_SIMPLEX
scale = 0.6
color = (255, 0, 0)
thickness = 2
cv2.putText(save_image, text, position, font, scale, color, thickness)
cv2.imwrite(os.path.join(self.out_dir, f'output_{epoch}.png'), save_image)
if __name__ == '__main__':
# arguments
parser = argparse.ArgumentParser(description='Continual Learning on the Circles')
parser.add_argument('-optimizer',
choices=['adahessian', 'adam', 'lbfgs', 'seng'],
help='optimizer for training the model',
default= 'adam') # TODO implement others
parser.add_argument('-seed', type = int , default= 42 , help = 'our random seed')
# parser.add_argument('-batch_size', type = int , default= 4096 , help = 'batch_size')
parser.add_argument('-image_size', type = int , default= 256 , help = 'input image size')
parser.add_argument('-lr', type = float , default= 1e-3 , help = 'learning_rate')
parser.add_argument('-nepochs', type = int , default= 500 , help = 'epochs')
parser.add_argument('-training_mode', choices=['continual', 'scratch'],
help='training_mode',
default= 'scratch')
args = parser.parse_args()
set_seed(args.seed)
writer = SummaryWriter(f'./runs_{args.seed}_{args.optimizer}_{args.training_mode}')
image_dir = 'circles4'
image_paths = sorted(os.listdir('circles4'))
model = None
print(f'Model is being trained in {args.training_mode} mode')
print(f'The input image dir is {image_dir}')
print(f'The optimizer is {args.optimizer}')
print(f'The learning rate is {args.lr}')
print(f'The number of epochs is {args.nepochs}')
print(f'The image size is {args.image_size}')
for counter, image_path in enumerate(image_paths):
print(image_path)
if args.training_mode == 'scratch':
trainer = Trainer(os.path.join(image_dir, image_path), args.image_size, batch_size= args.image_size* args.image_size,
nepochs= args.nepochs, optimizer= args.optimizer , lr= args.lr,
model = None, out_dir='output4')
model, psnr = trainer.run()
else:
trainer = Trainer(os.path.join(image_dir, image_path), args.image_size, batch_size= args.image_size* args.image_size,
nepochs= args.nepochs, optimizer= args.optimizer , lr= args.lr,
model = model, out_dir='output4')
model, psnr = trainer.run()
#trainer = Trainer(os.path.join(image_dir, image_path), 256, batch_size=256*256, nepochs=500, model = None, out_dir=f'output{5}')
#model_scratch, psnr_scratch = trainer.run()
writer.add_scalar('PSNR', psnr, counter)
#writer.add_scalar('PSNR_scratch', psnr_scratch, counter)
#writer.add_scalar('PSNR_diff', psnr - psnr_scratch, counter)
# for training adahessian continually
# python train.py -optimizer adahessian -training_mode continual
# for training lbfgs continually
# python train.py -optimizer lbfgs -training_mode continual
# for training seng continually
# python train.py -optimizer seng -training_mode continual
# for training adam continually
# python train.py -training_mode continual
# for training adam from scratch
# python train.py -training_mode scratch