-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
214 lines (173 loc) · 6.65 KB
/
utils.py
File metadata and controls
214 lines (173 loc) · 6.65 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
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
import os
from pathlib import Path
import copy
import yaml
from ast import literal_eval
from tqdm import tqdm
from typing import List
import torch
import numpy as np
import clip
def warp_tqdm(data_loader, disable_tqdm):
if disable_tqdm:
tqdm_loader = data_loader
else:
tqdm_loader = tqdm(data_loader, total=len(data_loader))
return tqdm_loader
def compute_confidence_interval(data, axis=0):
"""
Compute 95% confidence interval
:param data: An array of mean accuracy (or mAP) across a number of sampled episodes.
:return: the 95% confidence interval for this data.
"""
a = 1.0 * np.array(data)
m = np.mean(a, axis=axis)
std = np.std(a, axis=axis)
pm = 1.96 * (std / np.sqrt(a.shape[axis]))
return m, pm
def clip_classifier(classnames, template, clip_model):
with torch.no_grad():
clip_weights = []
clip_weights_before = []
texts_ = []
for classname in classnames:
# Tokenize the prompts
classname = classname.replace('_', ' ')
texts = [t.format(classname) for t in template]
texts = clip.tokenize(texts).cuda()
# prompt ensemble for ImageNet
x_before, class_embeddings = clip_model.encode_text(texts)
x_before = x_before.squeeze(dim=1)
class_embeddings /= class_embeddings.norm(dim=-1, keepdim=True)
class_embedding = class_embeddings.mean(dim=0)
class_embedding /= class_embedding.norm()
clip_weights.append(class_embedding)
clip_weights_before.append(x_before)
texts_.append(texts[0])
clip_weights_before = torch.stack(clip_weights_before, dim=1).cuda()
clip_weights = torch.stack(clip_weights, dim=1).cuda()
texts = torch.stack(texts_,dim=0)
return texts, clip_weights_before, clip_weights
def pre_load_features(cfg, split, clip_model, loader):
if cfg['load_pre_feat'] == False:
features, labels = [], []
with torch.no_grad():
for i, (images, target) in enumerate(tqdm(loader)):
images, target = images.cuda(), target.cuda()
image_features = clip_model.encode_image(images)
image_features /= image_features.norm(dim=-1, keepdim=True)
features.append(image_features)
labels.append(target)
features, labels = torch.cat(features), torch.cat(labels)
torch.save(features, cfg['cache_dir'] + "/" + split + "_f.pt")
torch.save(labels, cfg['cache_dir'] + "/" + split + "_l.pt")
else:
features = torch.load(cfg['cache_dir'] + "/" + split + "_f.pt")
labels = torch.load(cfg['cache_dir'] + "/" + split + "_l.pt")
return features, labels
class CfgNode(dict):
"""
CfgNode represents an internal node in the configuration tree. It's a simple
dict-like container that allows for attribute-based access to keys.
"""
def __init__(self, init_dict=None, key_list=None, new_allowed=False):
# Recursively convert nested dictionaries in init_dict into CfgNodes
init_dict = {} if init_dict is None else init_dict
key_list = [] if key_list is None else key_list
for k, v in init_dict.items():
if type(v) is dict:
# Convert dict to CfgNode
init_dict[k] = CfgNode(v, key_list=key_list + [k])
super(CfgNode, self).__init__(init_dict)
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError(name)
def __setattr__(self, name, value):
self[name] = value
def __str__(self):
def _indent(s_, num_spaces):
s = s_.split("\n")
if len(s) == 1:
return s_
first = s.pop(0)
s = [(num_spaces * " ") + line for line in s]
s = "\n".join(s)
s = first + "\n" + s
return s
r = ""
s = []
for k, v in sorted(self.items()):
seperator = "\n" if isinstance(v, CfgNode) else " "
attr_str = "{}:{}{}".format(str(k), seperator, str(v))
attr_str = _indent(attr_str, 2)
s.append(attr_str)
r += "\n".join(s)
return r
def __repr__(self):
return "{}({})".format(self.__class__.__name__, super(CfgNode, self).__repr__())
def _decode_cfg_value(v):
if not isinstance(v, str):
return v
try:
v = literal_eval(v)
except ValueError:
pass
except SyntaxError:
pass
return v
def _check_and_coerce_cfg_value_type(replacement, original, key, full_key):
original_type = type(original)
replacement_type = type(replacement)
# The types must match (with some exceptions)
if replacement_type == original_type:
return replacement
def conditional_cast(from_type, to_type):
if replacement_type == from_type and original_type == to_type:
return True, to_type(replacement)
else:
return False, None
casts = [(tuple, list), (list, tuple)]
try:
casts.append((str, unicode)) # noqa: F821
except Exception:
pass
for (from_type, to_type) in casts:
converted, converted_value = conditional_cast(from_type, to_type)
if converted:
return converted_value
raise ValueError(
"Type mismatch ({} vs. {}) with values ({} vs. {}) for config "
"key: {}".format(
original_type, replacement_type, original, replacement, full_key
)
)
def load_cfg_from_cfg_file(file: str):
cfg = {}
assert os.path.isfile(file) and file.endswith('.yaml'), \
'{} is not a yaml file'.format(file)
with open(file, 'r') as f:
cfg_from_file = yaml.safe_load(f)
for key in cfg_from_file:
# for k, v in cfg_from_file[key].items():
cfg[key] = cfg_from_file[key]
cfg = CfgNode(cfg)
return cfg
def merge_cfg_from_list(cfg: CfgNode,
cfg_list: List[str]):
new_cfg = copy.deepcopy(cfg)
assert len(cfg_list) % 2 == 0, cfg_list
for full_key, v in zip(cfg_list[0::2], cfg_list[1::2]):
subkey = full_key.split('.')[-1]
assert subkey in cfg, 'Non-existent key: {}'.format(full_key)
value = _decode_cfg_value(v)
value = _check_and_coerce_cfg_value_type(
value, cfg[subkey], subkey, full_key
)
setattr(new_cfg, subkey, value)
return new_cfg
def append_to_file(file_path: Path, text: str):
file_path.parent.mkdir(parents=True, exist_ok=True) # Ensure the directory exists
with file_path.open('a', encoding="utf-8") as file:
file.write(text + "\n")