-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain.py
More file actions
160 lines (128 loc) · 5.84 KB
/
train.py
File metadata and controls
160 lines (128 loc) · 5.84 KB
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
import os
import shutil
import argparse
import yaml
from tqdm import tqdm
import torch
import utils.misc as utils
import numpy as np
import wandb
from PIL import Image
from loss import TextureLoss
from models import NCA, NoiseNCA, PENCA
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='configs/Noise-NCA.yml', help="Path to the config file")
parser.add_argument('--data_dir', type=str, default='data/textures/', help="Texture images directory")
os.environ["WANDB_SILENT"] = "true"
def get_nca_model(config, texture_name):
model_type = config['model']['type']
if model_type == 'NCA':
return NCA(**config['model']['attr'])
elif model_type == 'NoiseNCA':
noise_levels = config['model']['noise_levels']
if texture_name in noise_levels:
noise_level = noise_levels[texture_name]
else:
noise_level = noise_levels['default']
return NoiseNCA(noise_level=noise_level, **config['model']['attr'])
elif model_type == 'PENCA':
return PENCA(**config['model']['attr'])
else:
raise ValueError(f"Unknown model type: {model_type}")
def main(config):
wandb_enabled = 'wandb' in config
if wandb_enabled:
wandb.login(key=config['wandb']['key'], relogin=True)
device = torch.device(config['device'])
config['loss']['attr']['device'] = device
config['model']['attr']['device'] = device
loss_fn = TextureLoss(**config['loss']['attr']).to(device)
data_dir = config['data_dir']
image_paths = [f"{data_dir}/{f}" for f in os.listdir(data_dir)]
for idx, url in enumerate(image_paths):
if "ipynb" in url:
continue
style_img = utils.imread(url, max_size=128)
target_image = torch.from_numpy(style_img).to(device)
target_image = target_image.permute(2, 0, 1)[None, ...]
texture_name = url.split("/")[-1].split(".")[0]
model_path = os.path.join(config['experiment_path'], f"{texture_name}")
log_path = os.path.join(model_path, "logs")
if not os.path.exists(model_path):
os.makedirs(log_path)
elif os.path.exists(os.path.join(model_path, "weights.pt")):
print(f"A trained model for {texture_name} exists.")
continue
else:
shutil.rmtree(model_path)
os.makedirs(log_path)
if wandb_enabled:
name = config['experiment_name'] + f"-{texture_name}"
wandb_run = wandb.init(project=config['wandb']['project'],
name=name, dir=log_path, config=config)
nca = get_nca_model(config, texture_name).to(device)
opt = torch.optim.Adam(nca.parameters(), config['training']['lr'], capturable=True)
lr_sched = None
if 'type' not in config['training']['scheduler'] or config['training']['scheduler']['type'] == 'MultiStep':
lr_sched = torch.optim.lr_scheduler.MultiStepLR(opt, **config['training']['scheduler']['attr'])
elif config['training']['scheduler']['type'] == 'Cyclic':
lr_sched = torch.optim.lr_scheduler.CyclicLR(opt, **config['training']['scheduler']['attr'])
batch_size = config['training']['batch_size']
iterations = config['training']['iterations']
alpha = config['training']['overflow_weight']
step_range = config['training']['nca']['step_range']
inject_seed_step = config['training']['nca']['inject_seed_step']
pool_size = config['training']['nca']['pool_size']
with torch.no_grad():
pool = nca.seed(pool_size).to(device)
pbar = tqdm(range(iterations), desc=f"Training {idx + 1}/{len(image_paths)} on {texture_name}")
for epoch in pbar:
with torch.no_grad():
batch_idx = np.random.choice(pool_size, batch_size, replace=False)
x = pool[batch_idx]
if epoch % inject_seed_step == 0:
x[:1] = nca.seed(1)
step_n = np.random.randint(step_range[0], step_range[1])
for _ in range(step_n):
x = nca(x)
overflow_loss = (x - x.clamp(-1.0, 1.0)).abs().sum()
texture_loss, texture_loss_per_img = loss_fn(target_image, nca.to_rgb(x))
loss = texture_loss + alpha * overflow_loss
with torch.no_grad():
loss.backward()
for p in nca.parameters():
p.grad /= (p.grad.norm() + 1e-8) # normalize gradients
opt.step()
opt.zero_grad()
lr_sched.step()
pool[batch_idx] = x
if (epoch + 1) % config['training']['log_interval'] == 0:
imgs = nca.to_rgb(x[:4]).permute([0, 2, 3, 1]).detach().cpu().numpy()
imgs = np.hstack((np.clip(imgs, 0, 1) * 255.0).astype(np.uint8))
if wandb_enabled:
wandb_run.log({'NCA Output': wandb.Image(imgs, caption='NCA Output')}, step=epoch)
else:
Image.fromarray(imgs).save(f'{log_path}/epoch-{epoch}.png')
if wandb_enabled:
loss_log = {
'total': loss.item(),
'overflow': overflow_loss.item(),
'texture': texture_loss.item()
}
wandb_run.log(loss_log, step=epoch)
torch.save(nca.state_dict(), f'{model_path}/weights.pt')
if wandb_enabled:
wandb_run.finish()
del nca
del opt
if __name__ == "__main__":
args = parser.parse_args()
with open(args.config, 'r') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
config['data_dir'] = args.data_dir
exp_name = config['experiment_name']
exp_path = f'results_new/{exp_name}/'
config['experiment_path'] = exp_path
if not os.path.exists(exp_path):
os.makedirs(exp_path)
main(config)