-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpseudotrain_selection.py
229 lines (174 loc) · 10.8 KB
/
pseudotrain_selection.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
import torch
import torch.nn as nn
import numpy as np
import argparse
import random
import time
import open_clip
from open_clip import tokenizer, tokenize
from data import get_data, get_data_val
from text_preprocessing import text_preprocessing
def select_pseudotrain(args, target_model, selected_nt_txt, selected_nt_url, dataloader, train_threshold, preprocess_train, preprocess_val, device, length):
selected_t_txt = [] ## to save the train text information to exclude those data in evaluation section
selected_t_url = [] ## to save the train text information to exclude those data in evaluation section
selected_t_cs_lst_tar = []
selected_t_feat_lst_tar = []
### Train dataloader
start_time = time.time()
cnt_train = 0
for i, batch in enumerate( dataloader ): ## LAION
train_text = [text_preprocessing(q) for q in batch[1]]
train_url = [d['url'] for d in batch[2]]
common = np.intersect1d(np.array(train_text), np.array(selected_nt_txt))
x_ind = np.where(np.isin(np.array(train_text), common))[0]
common_2 = np.intersect1d(np.array(train_url), np.array(selected_nt_url))
x_ind_2nd = np.where(np.isin(np.array(train_url), common_2))[0]
combined_x_ind = np.union1d(x_ind, x_ind_2nd)
if len(combined_x_ind) > 0:
selected_ind = np.setdiff1d(np.arange(len(batch[0])), combined_x_ind)
else:
selected_ind = np.arange(len(batch[0]))
images, texts = batch[0][selected_ind], tokenize(batch[1])[selected_ind]
cnt_train += len(images)
images = images.to(device)
texts = texts.to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
image_features2, text_features2, logit_scale2 = target_model(images, texts)
cs_2 = torch.diagonal(image_features2@text_features2.T)
## Batch-wise approach
train_ind = torch.where(cs_2 >= train_threshold)[0] ## we do assume the knowledge for non-train data distribution, so we only sample train data samples here
train_ind = train_ind.detach().cpu()
selected_t_txt.extend( np.array(train_text)[selected_ind][train_ind.numpy()] )
selected_t_url.extend( np.array(train_url)[selected_ind][train_ind.numpy()] )
selected_t_cs_lst_tar.extend( cs_2[train_ind].detach().cpu().numpy() )
selected_t_feat_lst_tar.extend( torch.cat([image_features2, text_features2], dim=1)[train_ind].detach().cpu() )
if cnt_train >= length:
break
true_train = len(selected_t_cs_lst_tar)
##################################################
## nontrain data | collect the data from the second val loader : CC3M
args.val_data = args.val_data_train_1
args.val_num_samples = args.val_num_samples_train_1
valloader = get_data_val(args, (preprocess_val, preprocess_val))
valloader = valloader.dataloader
### Nontrain data from valloader
start_time = time.time()
CC3M_LAION_commonset = np.load('./CC3M_LAION_commonset.npy')
CC3M_LAION_unqiue_commonset = np.load('./CC3M_LAION_unqiue_commonset.npy')
CC3M_LAION_url_commonset = np.load('./CC3M_LAION_url_commonset.npy')
cnt_nontrain = 0
for i, batch in enumerate( valloader ):
train_text = [text_preprocessing(q) for q in batch[1]]
train_url = [d['url'] for d in batch[2]]
common= np.intersect1d(np.array(train_text), CC3M_LAION_commonset) ## to exclude the overlapped non-train pairs with train pairs
x_ind = np.where(np.isin(np.array(train_text), common))[0]
common2 = np.intersect1d(np.array(train_text), np.array(selected_nt_txt))
x_ind_2nd = np.where(np.isin(np.array(train_text), common2))[0]
common_3 = np.intersect1d(np.array(train_url), CC3M_LAION_url_commonset) ## to exclude the overlapped non-train pairs with train pairs [url-wise]
x_ind_3rd = np.where(np.isin(np.array(train_url), common_3))[0]
common_4 = np.intersect1d(np.array(train_url), np.array(selected_nt_url))
x_ind_4th = np.where(np.isin(np.array(train_url), common_4))[0]
combined_x_ind = np.union1d(np.union1d(np.union1d(x_ind, x_ind_2nd), x_ind_3rd), x_ind_4th)
if len(combined_x_ind) > 0:
selected_ind = np.setdiff1d(np.arange(len(batch[0])), combined_x_ind)
else:
selected_ind = np.arange(len(batch[0]))
images, texts = batch[0][selected_ind], tokenize(batch[1])[selected_ind]
cnt_nontrain += len(images)
images = images.to(device)
texts = texts.to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
image_features2, text_features2, logit_scale2 = target_model(images, texts)
cs_2 = torch.diagonal(image_features2@text_features2.T)
train_ind = torch.where(cs_2 >= train_threshold)[0]
train_ind = train_ind.detach().cpu()
selected_t_txt.extend( np.array(train_text)[selected_ind][train_ind.numpy()] )
selected_t_url.extend( np.array(train_url)[selected_ind][train_ind.numpy()] )
selected_t_cs_lst_tar.extend( cs_2[train_ind].detach().cpu().numpy() )
selected_t_feat_lst_tar.extend( torch.cat([image_features2, text_features2], dim=1)[train_ind].detach().cpu() )
if cnt_nontrain >= length:
break
##################################################
## nontrain data | collect the data from the second val loader : CC12M
args.val_data = args.val_data_train_2
args.val_num_samples = args.val_num_samples_train_2
cc12m_valoader = get_data_val(args, (preprocess_val, preprocess_val))
cc12m_valoader = cc12m_valoader.dataloader
start_time = time.time()
CC12M_LAION_commonset = np.load('./CC12M_LAION_commonset.npy')
CC12M_LAION_unqiue_commonset = np.load('./CC12M_LAION_unqiue_commonset.npy')
CC12M_LAION_url_commonset = np.load('./CC12M_LAION_url_commonset.npy')
cnt_nontrain = 0
for i, batch in enumerate( cc12m_valoader ):
train_text = [text_preprocessing(q) for q in batch[1]]
train_url = [d['url'] for d in batch[2]]
common= np.intersect1d(np.array(train_text), CC12M_LAION_commonset) ## to exclude the overlapped non-train pairs with train pairs
x_ind = np.where(np.isin(np.array(train_text), common))[0]
common2 = np.intersect1d(np.array(train_text), np.array(selected_nt_txt))
x_ind_2nd = np.where(np.isin(np.array(train_text), common2))[0]
common_3 = np.intersect1d(np.array(train_url), CC12M_LAION_url_commonset) ## to exclude the overlapped non-train pairs with train pairs [url-wise]
x_ind_3rd = np.where(np.isin(np.array(train_url), common_3))[0]
common_4 = np.intersect1d(np.array(train_url), np.array(selected_nt_url))
x_ind_4th = np.where(np.isin(np.array(train_url), common_4))[0]
combined_x_ind = np.union1d(np.union1d(np.union1d(x_ind, x_ind_2nd), x_ind_3rd), x_ind_4th)
if len(combined_x_ind) > 0:
selected_ind = np.setdiff1d(np.arange(len(batch[0])), combined_x_ind)
else:
selected_ind = np.arange(len(batch[0]))
images, texts = batch[0][selected_ind], tokenize(batch[1])[selected_ind]
cnt_nontrain += len(images)
images = images.to(device)
texts = texts.to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
image_features2, text_features2, logit_scale2 = target_model(images, texts)
cs_2 = torch.diagonal(image_features2@text_features2.T)
train_ind = torch.where(cs_2 >= train_threshold)[0]
train_ind = train_ind.detach().cpu()
selected_t_txt.extend( np.array(train_text)[selected_ind][train_ind.numpy()] )
selected_t_url.extend( np.array(train_url)[selected_ind][train_ind.numpy()] )
selected_t_cs_lst_tar.extend( cs_2[train_ind].detach().cpu().numpy() )
selected_t_feat_lst_tar.extend( torch.cat([image_features2, text_features2], dim=1)[train_ind].detach().cpu() )
if cnt_nontrain >= length:
break
##################################################
## nontrain data | collect the data from the second val loader : MSCOCO
args.val_data = args.val_data_train_3
args.val_num_samples = args.val_num_samples_train_3
mscoco_valoader = get_data_val(args, (preprocess_val, preprocess_val))
mscoco_valoader = mscoco_valoader.dataloader
start_time = time.time()
MSCOCO_LAION_commonset = np.load('./MSCOCO_LAION_commonset.npy')
MSCOCO_LAION_unqiue_commonset = np.load('./MSCOCO_LAION_unqiue_commonset.npy')
## nontrain data
cnt_nontrain = 0
for i, batch in enumerate( mscoco_valoader ):
train_text = [text_preprocessing(q) for q in batch[1]]
train_url = [d['url'] for d in batch[2]]
common= np.intersect1d(np.array(train_text), MSCOCO_LAION_commonset) ## to exclude the overlapped non-train pairs with train pairs
x_ind = np.where(np.isin(np.array(train_text), common))[0]
common2 = np.intersect1d(np.array(train_text), np.array(selected_nt_txt))
x_ind_2nd = np.where(np.isin(np.array(train_text), common2))[0]
common_3 = np.intersect1d(np.array(train_url), np.array(selected_nt_url))
x_ind_3rd = np.where(np.isin(np.array(train_url), common_3))[0]
combined_x_ind = np.union1d(np.union1d(x_ind, x_ind_2nd), x_ind_3rd)
if len(combined_x_ind) > 0:
selected_ind = np.setdiff1d(np.arange(len(batch[0])), combined_x_ind)
else:
selected_ind = np.arange(len(batch[0]))
images, texts = batch[0][selected_ind], tokenize(batch[1])[selected_ind]
cnt_nontrain += len(images)
images = images.to(device)
texts = texts.to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
image_features2, text_features2, logit_scale2 = target_model(images, texts)
cs_2 = torch.diagonal(image_features2@text_features2.T)
train_ind = torch.where(cs_2 >= train_threshold)[0]
train_ind = train_ind.detach().cpu()
selected_t_txt.extend( np.array(train_text)[selected_ind][train_ind.numpy()] )
selected_t_url.extend( np.array(train_url)[selected_ind][train_ind.numpy()] )
selected_t_cs_lst_tar.extend( cs_2[train_ind].detach().cpu().numpy() )
selected_t_feat_lst_tar.extend( torch.cat([image_features2, text_features2], dim=1)[train_ind].detach().cpu() )
if cnt_nontrain >= length:
break
pseudo_train = len(selected_t_cs_lst_tar) - true_train
return selected_t_txt, selected_t_url, selected_t_cs_lst_tar, selected_t_feat_lst_tar, true_train, pseudo_train