-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIN_aug.py
300 lines (215 loc) · 8.88 KB
/
PIN_aug.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
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
import pickle
import os
import clip
import torch
import network
import torch.nn as nn
import torch.nn.functional as F
from utils.stats import calc_mean_std
import argparse
from main import get_dataset
from torch.utils import data
import numpy as np
import random
# +
# For target domain image feature
class PIN(nn.Module):
def __init__(self,shape,content_feat):
super(PIN,self).__init__()
self.shape = shape
self.content_feat = content_feat.clone().detach()
self.content_mean, self.content_std = calc_mean_std(self.content_feat)
self.size = self.content_feat.size()
self.content_feat_norm = (self.content_feat - self.content_mean.expand(
self.size)) / self.content_std.expand(self.size)
self.style_mean = self.content_mean.clone().detach()
self.style_std = self.content_std.clone().detach()
self.style_mean = nn.Parameter(self.style_mean, requires_grad = True)
self.style_std = nn.Parameter(self.style_std, requires_grad = True)
self.relu = nn.ReLU(inplace=True)
def forward(self):
self.style_std.data.clamp_(min=0)
target_feat = self.content_feat_norm \
* self.style_std.expand(self.size) \
+ self.style_mean.expand(self.size)
target_feat = self.relu(target_feat)
return target_feat
# -
available_models = sorted(name for name in network.modeling.__dict__ if name.islower() and not (name.startswith("__") or name.startswith('_')) and callable(network.modeling.__dict__[name]))
gpu_id = "0"
data_root = "/workspace/dataset/GTAV"
save_dir = "exp2"
dataset = "gta5"
crop_size = 768
batch_size = 80
model = "deeplabv3plus_resnet_clip"
BB = "RN50"
weight_decay = 1e-4
total_it = 100
resize_feat = True
random_seed = 123
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
np.random.seed(random_seed)
random.seed(random_seed)
train_dst, val_dst = get_dataset(dataset, data_root, crop_size, data_aug=False)
train_loader = data.DataLoader(train_dst, batch_size=batch_size, shuffle=True, num_workers=4, drop_last=False)
print("Dataset: %s, Train set: %d, Val set: %d" % (dataset, len(train_dst), len(val_dst)))
cur_itrs = 0
if not os.path.isdir(save_dir):
os.mkdir(save_dir)
if resize_feat:
t1 = nn.AdaptiveAvgPool2d((56, 56))
else:
t1 = lambda x: x
import clip
from clip.simple_tokenizer import *
class TextEncoder(nn.Module):
def __init__(self, clip_model):
super().__init__()
self.transformer = clip_model.transformer
self.positional_embedding = clip_model.positional_embedding
self.ln_final = clip_model.ln_final
self.text_projection = clip_model.text_projection
self.dtype = clip_model.dtype
def forward(self, prompts, tokenized_prompts):
x = prompts + self.positional_embedding.type(self.dtype)
x = x.permute(1, 0, 2)
x = self.transformer(x)
x = x.permute(1, 0, 2)
x = self.ln_final(x).type(self.dtype)
x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
return x
# +
device = 'cuda:0'
clip_model, preprocess = clip.load('RN50', device, jit=False)
text_encoder = TextEncoder(clip_model)
# tokenizer = SimpleTokenizer()
# -
def diversity_loss(embedding_list, embedding, idx):
Lstyle = 0.0
for i in range(idx):
prev_embedding = embedding_list[i]
prev_embedding = prev_embedding / prev_embedding.norm(dim=-1, keepdim=True)
embedding = embedding / embedding.norm(dim=-1, keepdim=True)
Lstyle = Lstyle + abs(embedding @ prev_embedding.T)
Lstyle = Lstyle / idx
return Lstyle
# +
# training iteration -> argument
L = 100
# number of style vectors -> argument
K = 80
# dimension
D = 512
style_feature_list =[]
n_ctx = 1
Lstyle = 0
# -
for j in range(K):
ctx_vectors = torch.empty(1, 1, D)
nn.init.normal_(ctx_vectors, std=0.2)
# ctx_vectors = ctx_vectors.repeat(7, 1, 1)
prompt_prefix = " ".join(["X"] * n_ctx)
# optimize vector
ctx = nn.Parameter(ctx_vectors)
# optimizer & scheduler
optimizer = torch.optim.SGD([ctx], lr=0.01, momentum=0.9)
ctx_half = 'A diverse driving conditions in'
# classnames = [name.replace("_", " ") for name in class_name]
for x in range(L):
prompts = [ctx_half + " " + prompt_prefix]
tokenized_prompts = clip.tokenize(prompts).to(device)
embedding = clip_model.token_embedding(tokenized_prompts)
prefix = embedding[:, :6, :].to(device)
suffix = embedding[:, 6+n_ctx :, :].to(device)
# "<SOS> A diverse driving conditions at X"라는 임베딩 값을 "<SOS> Driving conditions at S*"로 변경
ctx_i = ctx[:, :, :].to(device)
prefix_i = prefix[:, :, :].to(device)
suffix_i = suffix[:, :, :].to(device)
prompts = torch.cat((prefix_i, ctx_i, suffix_i), dim=1).half()
# style_feature : "Driving conditions at S*"
style_feature = text_encoder(prompts.half(), tokenized_prompts.long())
# style_feature = style_feature / style_feature.norm(dim=-1, keepdim=True)
if j == 0:
break
# Lstyle loss
Lstyle = diversity_loss(style_feature_list, style_feature, j)
optimizer.zero_grad()
Lstyle.backward()
optimizer.step()
if j != 0:
print(f"{j+1} : loss is {Lstyle.item():4f}")
style_feature_list.append(style_feature.detach())
style_feature_list = torch.stack(style_feature_list)
style_feature_list = style_feature_list.squeeze(1)
with open('style_feature.pkl', 'wb') as f:
pickle.dump(style_feature_list, f)
style_feature_list = torch.stack(style_feature_list)
style_feature_list = style_feature_list.squeeze(1)
model = network.modeling.__dict__["deeplabv3plus_resnet_clip"](num_classes=19,
BB=BB,
replace_stride_with_dilation=[False,False,False])
for p in model.backbone.parameters():
p.requires_grad = False
model.backbone.eval()
print('model.backbone.eval()')
# +
clip_model, preprocess = clip.load('RN50', device, jit=False)
cur_itrs = 0
# writer = SummaryWriter()
if not os.path.isdir(save_dir):
os.mkdir(save_dir)
if resize_feat:
t1 = nn.AdaptiveAvgPool2d((56, 56))
else:
t1 = lambda x: x
# -
model = model.to(device)
for i,(img_id, tar_id, images, labels) in enumerate(train_loader):
# print(images.shape)
# torch.Size([32, 3, 768, 768])
print(i)
f1 = model.backbone(images.to(device),
trunc1=False, trunc2=False, trunc3=False, trunc4=False,
get1=True, get2=False, get3=False, get4=False) # (B,C1,H1,W1)
# print(f"f1 shape : {f1.shape}")
#optimize mu and sigma of target features with CLIP
model_pin_1 = PIN([f1.shape[0],256,1,1], f1.to(device)) # mu_T (B,C1) sigma_T(B,C1)
model_pin_1.to(device)
optimizer_pin_1 = torch.optim.SGD(params=[
{'params': model_pin_1.parameters(), 'lr': 1},
], lr= 1, momentum=0.9, weight_decay=weight_decay)
if i == len(train_loader)-1 and f1.shape[0] < batch_size :
style_feature_list = style_feature_list[:f1.shape[0]]
# print(f"text_source shape : {text_source.shape}")
while cur_itrs< total_it:
cur_itrs += 1
optimizer_pin_1.zero_grad()
f1_hal = model_pin_1()
f1_hal_trans = t1(f1_hal)
# target_features (optimized)
target_features_from_f1 = model.backbone(f1_hal_trans,
trunc1=True, trunc2=False, trunc3=False, trunc4=False,
get1=False, get2=False, get3=False, get4=False)
target_features_from_f1 /= target_features_from_f1.norm(dim=-1, keepdim=True).clone().detach()
# print(f"target_features_from_f1 : {target_features_from_f1.shape}")
# loss
loss_CLIP1 = (1- torch.cosine_similarity(style_feature_list, target_features_from_f1, dim=1)).mean()
loss_CLIP1.backward(retain_graph=True)
optimizer_pin_1.step()
cur_itrs = 0
for name, param in model_pin_1.named_parameters():
if param.requires_grad and name == 'style_mean':
learnt_mu_f1 = param.data
elif param.requires_grad and name == 'style_std':
learnt_std_f1 = param.data
for k in range(learnt_mu_f1.shape[0]):
learnt_mu_f1_ = torch.from_numpy(learnt_mu_f1[k].detach().cpu().numpy())
learnt_std_f1_ = torch.from_numpy(learnt_std_f1[k].detach().cpu().numpy())
stats = {}
stats['mu_f1'] = learnt_mu_f1_
stats['std_f1'] = learnt_std_f1_
with open(save_dir+'/'+img_id[k].split('/')[-1]+'.pkl', 'wb') as f:
pickle.dump(stats, f)