-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
255 lines (210 loc) · 10.7 KB
/
run.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
import os
from typing import List, Union, Tuple
import argparse
import torch
import cv2
from tqdm import tqdm
from tae.ellipse import MultiRotElpse
from tae.loss import SimMaxLoss
from tae.utils import (draw_ellipse_on_hd_image,
MultiCLIPGradCAM, MultiCLIPModel,
fix_seed, prepare_workspace, MultiImage)
from tae.initialize import EllipseInitializer
from tae.trainer import initialize_model, tune_one_step, initialize_ellipse_binary_cam
def save_hd_images(save_dir, img_path,
init_elp_params, pred_ellipse_list,
gt_box=None, steps=None):
"""
保存tunning初始化和过程中椭圆图像
"""
if not os.path.exists(save_dir):
os.makedirs(save_dir)
hd_elp_img = draw_ellipse_on_hd_image(img_path, init_elp_params, gt_box)
target_path = os.path.join(save_dir,
f'init_'
f'({init_elp_params[0]:.3f}_{init_elp_params[1]:.3f}_{init_elp_params[2]:.3f}_{init_elp_params[3]:.3f}_{init_elp_params[4]:.3f}).png')
cv2.imwrite(target_path, hd_elp_img)
if steps is None:
steps = [1, 5, 10, 20, 40, 80, 120, 160, 200, len(pred_ellipse_list)]
steps = list(set(steps))
steps = [step for step in steps if step <= len(pred_ellipse_list)]
steps.sort()
for step_i in steps:
step_i = step_i - 1
elp = pred_ellipse_list[step_i]
hd_elp_img = draw_ellipse_on_hd_image(img_path, elp, gt_box)
target_path = os.path.join(save_dir,
f'step{step_i + 1}_({elp[0]:.3f}_{elp[1]:.3f}_{elp[2]:.3f}_{elp[3]:.3f}_{elp[4]:.3f}).png')
cv2.imwrite(target_path, hd_elp_img)
def initialize_ellipse(args, Imgs: MultiImage, clipModel: MultiCLIPModel,
SimRotElpses: MultiRotElpse, CamRotElpses: MultiRotElpse,
GradCAMs: MultiCLIPGradCAM,
device) \
-> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, dict]:
"""
get the initial ellipse
"""
ellipse_initializer = EllipseInitializer(Imgs, clipModel,
SimRotElpses, CamRotElpses, GradCAMs)
init_score, init_ellipse, chose_grid_size, grid_idx, init_similarity = (
ellipse_initializer.anchor_topk_similarity_and_highest_mean_activation(
args.topk,
args.anchor_ratios,
args.anchor_scales,
base_circle_r=args.anchor_size,
grid_size=args.anchor_grid_size,
)
)
center_x, center_y, major_axis, minor_axis, angle = torch.tensor(init_ellipse, device=device)
return (center_x, center_y, major_axis, minor_axis, angle,
{'score': init_score, 'similarity': init_similarity, 'grid_size': chose_grid_size, 'grid_idx': grid_idx})
def tuning_an_ellipse(
args,
clipModel: MultiCLIPModel,
GradCAM: MultiCLIPGradCAM,
Img: MultiImage,
text_for_sim: str,
text_for_cam: str,
device: str,
show_pbar: bool = True,
):
# ===================== Update Text =====================
clipModel.update_text(text_for_sim)
GradCAM.update_text_features(text_for_cam)
# ===================== Prepare RotElpse =====================
SimRotElpses = MultiRotElpse([224, 336, 384], sigma=args.sim_sigma, eta=args.eta)
CamRotElpses = MultiRotElpse([224, 336, 384], sigma=args.cam_sigma, eta=args.eta)
# ===================== Initialize Ellipse =====================
center_x, center_y, major_axis, minor_axis, angle, init_grid_info = (
initialize_ellipse(args, Img, clipModel,
SimRotElpses, CamRotElpses, GradCAM, device))
predict_ellipse = (center_x, center_y, major_axis, minor_axis, angle)
similarity = init_grid_info['similarity']
init_grid_info['init_elpse'] = predict_ellipse
# ===================== Prepare Init Grad-CAM =====================
ellipse_mask, ellipse_img, grad_cam_highres = (
initialize_ellipse_binary_cam(center_x, center_y, major_axis, minor_axis, angle,
Img, GradCAM, CamRotElpses))
# ===================== Model & Optimizer =====================
model, optimizer, scheduler = initialize_model(args.num_step, args.lr,
(center_x, center_y, major_axis, minor_axis, angle),
224, 224,
args.sim_sigma, args.eta, device)
# ===================== Loss =====================
L_INF = SimMaxLoss(margin=0)
L_SQZ = SimMaxLoss(margin=0)
input_params = torch.zeros(1, 64).cuda()
pred_ellipse_list = []
# ===================== Differentiable Visual Prompting =====================
import copy
clipModel = copy.deepcopy(clipModel)
del clipModel.models['ViT-L/14']
if show_pbar:
pbar = tqdm(total=args.num_step, desc=f"[LR=0.0000][Loss=0.000] Process", dynamic_ncols=False, ascii=True)
for i in range(args.num_step):
textFeats = {}
for clip_name in args.sim_clip:
textFeats[clip_name] = torch.cat([clipModel.text_feat(clip_name),
*clipModel.background_feat(clip_name)], dim=0)
(ellipse_img, ellipse_mask, predict_ellipse, similarity,
m_act, m_act2, loss, l_sim, l_inf, l_sqz) = (
tune_one_step(model, input_params, optimizer,
scheduler, clipModel, Img, textFeats, grad_cam_highres,
L_INF, L_SQZ))
(cx, cy, a, b, t) = predict_ellipse
pred_ellipse_list.append([cx.item(), cy.item(), a.item(), b.item(), t.item()])
if show_pbar:
pbar.update(1)
pbar.set_description(f"[LR={optimizer.param_groups[0]['lr']:.4f}][Loss={loss.item():.4f}] Process")
if show_pbar:
pbar.close()
(cx, cy, a, b, t) = (_p.detach().cpu().numpy() for _p in predict_ellipse)
return (cx, cy, a, b, t, similarity, model,
ellipse_img, ellipse_mask, grad_cam_highres,
init_grid_info, pred_ellipse_list)
def train_ellipse(
args,
img_path: str,
img_text: str,
):
device = "cuda:0" if torch.cuda.is_available() else "cpu"
img_name = img_path.split('/')[-1].split('.')[0]
process_name = f"{img_name}_({img_text.replace('/', '-')})"
# ===================== CLIP Model =====================
clipModel = MultiCLIPModel(args.sim_clip, device=device)
# ===================== Prepare Text features =====================
img_text_for_similarity = args.sim_text_prompt.format(img_text)
img_text_for_cam = args.cam_text_prompt.format(img_text)
# ===================== Prepare Img & RotElpse =====================
Img = MultiImage(img_path, [224, 336, 384], device)
GradCAM = MultiCLIPGradCAM(args.cam_clip, img_text=img_text_for_cam, device=device)
# ===================== Tuning =====================
(center_x, center_y, major_axis, minor_axis, angle, similarity, model,
ellipse_img, ellipse_mask, grad_cam_highres,
init_grid_info, pred_ellipse_list) = (
tuning_an_ellipse(args,
clipModel,
GradCAM, Img,
img_text_for_similarity, img_text_for_cam,
device))
# ===================== Save Tuning images =====================
init_elp_params = init_grid_info['init_elpse']
hd_tunning_dir = os.path.join(args.workspace, 'hd_tune', process_name)
save_hd_images(hd_tunning_dir, img_path,
init_elp_params,
pred_ellipse_list, init_grid_info['similarity'])
# ===================== Save Tuning Elps =====================
result_file_path = os.path.join(args.workspace, 'result.txt')
with open(result_file_path, 'a') as f:
f.write(f"{process_name}_Init {init_elp_params}\n")
for i, ellipse in enumerate(pred_ellipse_list):
f.write(f"{process_name}_{i} {ellipse}\n")
# ===================== Save Model =====================
torch.save(model.state_dict(), os.path.join(args.workspace, "model", f"{process_name}.pth"))
def ellipse_parser():
parser = argparse.ArgumentParser(description='Tuning-An-Ellipse')
parser.add_argument('--workspace', type=str, default='workspace/test')
parser.add_argument('--img_path', type=str, default='source/frog.png')
parser.add_argument('--caption', type=str, default='the frog in the middle')
# Rotated Ellipse
parser.add_argument('--sim_sigma', type=float, default=0.05)
parser.add_argument('--cam_sigma', type=float, default=0.05)
parser.add_argument('--eta', type=float, default=50)
# CLIP model
parser.add_argument('--sim_clip', type=str, default="ViT-B/16,ViT-L/14",
help="CLIP model for calculating similarity")
parser.add_argument('--cam_clip', type=str, default="ViT-B/16,ViT-L/14@336px",
help="CLIP model for calculating CAM")
parser.add_argument('--sim_text_prompt', type=str, default="{}")
parser.add_argument('--cam_text_prompt', type=str, default="a clean origami {}")
# Anchor initialization
parser.add_argument('--anchor_ratios', type=str, default='0.5,1,2')
parser.add_argument('--anchor_scales', type=str, default='1,2')
parser.add_argument('--anchor_size', type=float, default=0.1)
parser.add_argument('--anchor_grid_size', type=int, default=9)
parser.add_argument('--topk', type=float, default=10,
help="Anchor-Ellipse in top-k similarity and highest mean activation value will be chosen")
# Tuning
parser.add_argument('--num_step', type=int, default=200)
parser.add_argument('--lr', type=float, default=0.001)
def _args_postprocess(_args):
_args.anchor_ratios = [float(x) for x in _args.anchor_ratios.split(',')]
_args.anchor_scales = [float(x) for x in _args.anchor_scales.split(',')]
_args.cam_clip = _args.cam_clip.split(',')
_args.sim_clip = _args.sim_clip.split(',')
return _args
return parser, _args_postprocess
def arg_parser():
parser, args_postprocess = ellipse_parser()
args = parser.parse_args()
args = args_postprocess(args)
return args
if __name__ == '__main__':
fix_seed(0)
args = arg_parser()
prepare_workspace(args.workspace)
train_ellipse(
args,
img_path=args.img_path,
img_text=args.caption,
)