-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·238 lines (202 loc) · 7.71 KB
/
main.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
#!/usr/bin/env python
import argparse
import os
import sys
from pathlib import Path
import numpy as np
import torch
from PIL import Image
from tqdm import tqdm
from sinder import (
get_neighbor_loss,
get_tokens,
load_data,
load_model,
load_visual_data,
pca_array,
replace_back,
replace_linear_addition_noqk,
)
os.environ['XFORMERS_DISABLED'] = '1'
torch.set_float32_matmul_precision('high')
def parse_args():
parser = argparse.ArgumentParser(description='Beautify network')
parser.add_argument(
'--model', type=str, default='dinov2_vitg14', help='config file'
)
parser.add_argument('--work_dir', type=str, default='results')
parser.add_argument('--resolution', type=int, default=518)
parser.add_argument('--lr', type=float, default=0.005)
parser.add_argument('--max_iter', type=int, default=30000)
parser.add_argument('--num_train_max', type=int, default=30000)
parser.add_argument('--mask_thr', type=float, default=4)
parser.add_argument('--skip_less_than', type=int, default=3)
parser.add_argument('--visual_size', type=int, default=448 * 2)
parser.add_argument('--kernel', type=int, default=3)
parser.add_argument('--save_at_skip', type=int, nargs='+', default=[75])
parser.add_argument('--limit_layers', type=int, default=10)
args = parser.parse_args()
return args
def prepare_train(args, model):
model.train()
all_params = []
for name, param in model.named_parameters():
param.requires_grad = False
replace_linear_addition_noqk(model, 'model')
for name, param in model.named_parameters():
if '.epsilon' in name and param.requires_grad is True:
all_params.append(param)
grad_params = []
for name, param in model.named_parameters():
if param.requires_grad:
grad_params.append(name)
assert len(grad_params) == len(all_params)
print(len(grad_params), grad_params)
print(len(all_params), all_params)
optimizer = torch.optim.SGD(
all_params,
lr=args.lr,
momentum=0.9,
)
return optimizer
def save_model(args, model):
print('save model')
model.eval()
replace_back(model, 'model')
torch.save(model.state_dict(), args.folder / 'model.pt')
def train(args, model, dataset, optimizer, visual_dataset):
print('training')
skip_history = [False] * 1000
model.train()
for global_iter in tqdm(range(args.max_iter)):
img = dataset[global_iter % len(dataset)]
H = img.shape[1] // model.patch_size
W = img.shape[2] // model.patch_size
density = np.array(skip_history[-1000:]).astype(float).mean()
print(f'{global_iter=} {W=} {H=} {density=:.2f}')
for percent in args.save_at_skip:
if percent / 100 <= density:
print(f'save checkpoint at {density=}')
args.save_at_skip.remove(percent)
torch.save(model, args.folder / f'checkpoint_p{percent}.pth')
if len(args.save_at_skip) == 0:
break
model.zero_grad()
model.train()
with torch.enable_grad():
image_batch = img.unsqueeze(0).cuda()
result = get_neighbor_loss(
model,
image_batch,
skip_less_than=args.skip_less_than,
mask_thr=args.mask_thr,
kernel=args.kernel,
)
if result is None:
skip_history.append(True)
print('no loss, skip')
else:
skip_history.append(False)
(
layer,
loss,
I,
J,
T,
alpha,
mask_angle,
x_token,
) = result
print(
f'{global_iter=}, {layer=}, {density=}, {alpha=:.2f}, {len(I)=}, '
f'{loss.item()=:.2f}'
)
if torch.isnan(loss).any():
print('nan loss, skip')
continue
loss.backward()
# set some grad to 0
if args.limit_layers:
with torch.no_grad():
for t in range(layer - args.limit_layers + 1):
for p in model.blocks[t].parameters():
p.grad = None
has_nan = False
for name, param in model.named_parameters():
if param.grad is not None and torch.isnan(param.grad).any():
print(f'nan grad at {name}, skip')
has_nan = True
if has_nan:
continue
optimizer.step()
# visualize
if global_iter % 100 == 0:
try:
print(f'visualization at {global_iter=}')
pca_img = pca_array(x_token)
pca_img.save(args.folder / 'pca.png')
mask_img = Image.fromarray(
(mask_angle * 255)
.detach()
.cpu()
.numpy()
.astype(np.uint8)
).resize((W * 7, H * 7), resample=Image.NEAREST)
mask_img.save(args.folder / 'mask.png')
Image.fromarray(
(
(
img.permute((1, 2, 0)).cpu().numpy() * 0.22
+ 0.45
)
* 255
)
.clip(0, 255)
.astype(np.uint8)
).save(args.folder / 'img.png')
if global_iter % 1000 == 0:
pca_img.save(args.folder / f'{global_iter:05}_pca.png')
mask_img.save(
args.folder / f'{global_iter:05}_mask.png'
)
Image.fromarray(
(
(
img.permute((1, 2, 0)).cpu().numpy() * 0.22
+ 0.45
)
* 255
)
.clip(0, 255)
.astype(np.uint8)
).save(args.folder / f'{global_iter:05}_img.png')
except Exception as e:
print(e)
for d in range(len(visual_dataset)):
visual_image = visual_dataset[d]
visual_tokens_all = get_tokens(model, visual_image)
visual_tokens, visual_tokens_cls = zip(*visual_tokens_all)
pca_img = pca_array(visual_tokens[-1])
pca_img.save(args.folder / f'{d}_pca.png')
if global_iter % 500 == 0:
pca_img.save(
args.folder / f'{global_iter:05}_{d}_pca.png'
)
torch.save(model, args.folder / 'checkpoint.pth')
def main():
print('Start beautify')
args = parse_args()
name = f'res{args.resolution}_lr{args.lr}_{args.num_train_max}_skipless{args.skip_less_than}_maskthr{args.mask_thr}_limit{args.limit_layers}_ker{args.kernel}'
args.folder = Path(args.work_dir) / name
os.makedirs(args.folder, exist_ok=True)
print(args)
print(' '.join(sys.argv))
print(f'work dir {args.folder}')
model = load_model(args.model)
dataset = load_data(args, model)
visual_dataset = load_visual_data(args, model)
optimizer = prepare_train(args, model)
train(args, model, dataset, optimizer, visual_dataset)
save_model(args, model)
if __name__ == '__main__':
main()