-
Notifications
You must be signed in to change notification settings - Fork 2
/
03_eval.py
298 lines (260 loc) · 13.9 KB
/
03_eval.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
import numpy as np
import os
import sys
from time import time
from label_the_sky.config import config
from label_the_sky.training.trainer import Trainer, set_random_seeds
from label_the_sky.training.trainer import MAG_MAX
from label_the_sky.postprocessing import plot as p
from label_the_sky.utils import glob_re, get_dataset_label, get_channels_label, get_finetuning_suffix
def predict_unlabeled(base_dir, timestamp, backbone, n_channels, pretraining_dataset, split, dataset='unlabeled'):
model_name = f'{timestamp}_{backbone}_{n_channels}_{pretraining_dataset}'
weights_file = 'imagenet' if pretraining_dataset=='imagenet' else os.path.join(base_dir, 'trained_models', model_name+'.h5')
print(weights_file)
trainer = Trainer(
backbone=backbone,
n_channels=n_channels,
output_type='magnitudes',
base_dir=base_dir,
model_name=model_name,
weights=weights_file
)
X = trainer.load_data(dataset=dataset, split=split, return_y=False)
y_hat, X_features = trainer.extract_features_and_predict(X)
suffix = 'u' if dataset=='unlabeled' else ''
output_name = f'{split}_{timestamp}_{backbone}_{str(n_channels).zfill(2)}_{pretraining_dataset}'
np.save(os.path.join(base_dir, 'npy', f'y{suffix}hat_{output_name}.npy'), y_hat)
np.save(os.path.join(base_dir, 'npy', f'X{suffix}f_{output_name}.npy'), X_features)
def predict_clf(base_dir, timestamp, backbone, n_channels, pretraining_dataset, finetune, split, dataset='clf'):
model_name = f'{timestamp}_{backbone}_{n_channels}_{pretraining_dataset}_clf_ft{int(finetune)}'
weights_file = os.path.join(base_dir, 'trained_models', model_name+'.h5')
print(weights_file)
trainer = Trainer(
backbone=backbone,
n_channels=n_channels,
output_type='class',
base_dir=base_dir,
model_name=model_name,
weights=weights_file
)
X, y = trainer.load_data(dataset=dataset, split=split)
y_hat, X_features = trainer.extract_features_and_predict(X)
output_name = f'{split}_{timestamp}_{backbone}_{str(n_channels).zfill(2)}_{pretraining_dataset}_clf_ft{int(finetune)}'
suffix = 'u' if dataset=='unlabeled' else ''
np.save(os.path.join(base_dir, 'npy', f'y{suffix}hat_{output_name}.npy'), y_hat)
np.save(os.path.join(base_dir, 'npy', f'X{suffix}f_{output_name}.npy'), X_features)
# trainer.evaluate(X, y)
def concat_vectors(base_dir, timestamp, backbone, n_channels, pretraining_dataset, finetune, split):
output_name = f'{split}_{timestamp}_{backbone}_{str(n_channels).zfill(2)}_{pretraining_dataset}'#_clf_ft{int(finetune)}'
X = np.load(os.path.join(base_dir, 'npy', f'Xf_{output_name}.npy'))
Xu = np.load(os.path.join(base_dir, 'npy', f'Xuf_{output_name}.npy'))
X_concat = np.concatenate((X, Xu))
np.save(os.path.join(base_dir, 'npy', f'Xf-Xuf_{output_name}.npy'), X_concat)
if __name__ == '__main__':
set_random_seeds()
skip_predictions = bool(int(sys.argv[1])) if len(sys.argv)>1 else True
base_dir = os.environ['HOME']
data_dir = os.environ['DATA_PATH']
timestamp = config['timestamp']
backbone_lst = config['backbones']
n_channels_lst =config['n_channels']
pretraining_dataset_lst = config['pretraining_datasets']
finetune_lst = config['finetune']
split = config['eval_split']
projection_algo = config['projection_algorithm']
umap__n_neighbors = config['umap']['n_neighbors']
if not skip_predictions:
print('computing feature vectors and predictions')
for backbone in backbone_lst:
for n_channels in n_channels_lst:
for pretraining_dataset in pretraining_dataset_lst:
for finetune in finetune_lst:
if (not pretraining_dataset and finetune) or (pretraining_dataset=='imagenet' and n_channels!=3):
continue
predict_unlabeled(base_dir, timestamp, backbone, n_channels, pretraining_dataset, split)
for dataset in ['unlabeled', 'clf']:
predict_unlabeled(base_dir, timestamp, backbone, n_channels, pretraining_dataset, split)
concat_vectors(base_dir, timestamp, backbone, n_channels, pretraining_dataset, finetune, split)
backbone = backbone_lst[0]
p.set_plt_style()
cnt_iterator = iter(range(100))
try:
print(f'{str(next(cnt_iterator)).zfill(2)} plotting nr of missing values vs r-magnitude, dr1')
p.attributes_scatter(
attribute_x='r_auto', label_x='r',
attribute_y='nDet_auto', label_y='# missing magnitudes',
transform_fn=lambda y: 12 - y,
output_file=f'figures/exp_missingdata-r_dr1.pdf',
dataset_file=os.path.join(data_dir, 'dr1/dr1_master.csv'))
except FileNotFoundError as e:
print(e)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting r-magnitude distributions, unlabeled')
p.hist(
output_file=f'figures/exp_dist_r_unlabeled.pdf',
dataset_file='datasets/unlabeled.csv',
attribute='r',
color_pos=3)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting r-magnitude distributions, clf')
p.hist(
output_file=f'figures/exp_dist_r_clf.pdf',
dataset_file='datasets/clf.csv',
attribute='r',
color_attribute='class')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting fwhm distributions, unlabeled vs clf')
file_list = glob_re(os.path.join(base_dir, 'mnt/label-the-sky/datasets'), f'(unlabeled|clf).csv')
p.hist_datasets(
output_file=f'figures/exp_dist_fwhm.pdf',
dataset_files=file_list,
plt_labels=['$X_{u}$', '$X$'],
attribute='fwhm')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting pretraining loss curves')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_(12|05|03)_unlabeled.json')
p.metric_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) for f in file_list],
output_file='figures/exp_pretraining_loss.pdf',
metric='val_loss',
metric_scaling_factor=MAG_MAX,
legend_location='upper right')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting pretraining times curves')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_(12|05|03)_unlabeled.json')
p.metric_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) for f in file_list],
output_file='figures/exp_pretraining_times.pdf',
metric='times',
legend_location='upper right')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting RGB vs imagenet clf')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_03_(unlabeled|imagenet)_clf_ft(1|0).json')
p.metric_curve(
file_list=file_list,
plt_labels=[get_dataset_label(f) + ' ' + get_finetuning_suffix(f) for f in file_list],
output_file='figures/exp_clf_rgb-imagenet.pdf',
metric='val_accuracy',
paired=True,
color_pos=2)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting RGB vs imagenet clf lowdata')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_03_(unlabeled|imagenet)_clf_ft(1|0)_lowdata.json')
p.lowdata_curve(
file_list=file_list,
plt_labels=[get_dataset_label(f) + ' ' + get_finetuning_suffix(f) for f in file_list],
output_file='figures/exp_clf_rgb-imagenet_lowdata.pdf',
metric='val_accuracy',
paired=True,
color_pos=2)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels clf')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_(12|05|03)_unlabeled_clf_ft(1|0).json')
p.metric_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) + ' ' + get_finetuning_suffix(f) for f in file_list],
output_file='figures/exp_clf_channels.pdf',
metric='val_accuracy',
paired=True)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels clf lowdata')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_(12|05|03)_unlabeled_clf_ft(1|0)_lowdata.json')
p.lowdata_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) + ' ' + get_finetuning_suffix(f) for f in file_list],
output_file='figures/exp_clf_channels_lowdata.pdf',
metric='val_accuracy',
paired=True)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels ft vs scratch')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_(12|05|03)_(unlabeled_clf_ft1|None_clf_ft0).json')
p.metric_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) + '; ' + get_dataset_label(f) for f in file_list],
output_file='figures/exp_clf_channels_scratch.pdf',
metric='val_accuracy',
paired=True)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels clf ft vs scratch lowdata')
file_list = glob_re(os.path.join(base_dir, 'mnt/history'), f'{timestamp}_{backbone}_(12|05|03)_(unlabeled_clf_ft1|None_clf_ft0)_lowdata.json')
p.lowdata_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) + '; ' + get_dataset_label(f) for f in file_list],
output_file='figures/exp_clf_channels_scratch_lowdata.pdf',
metric='val_accuracy',
paired=True)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels clf accuracy vs r-magnitude')
file_list = glob_re(os.path.join(base_dir, 'npy'), f'yhat_{split}_{timestamp}_{backbone}_(12|05|03)_unlabeled_clf_ft1.npy')
p.acc_attribute_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) for f in file_list],
output_file=f'figures/exp_clf_channels_acc-r_{split}.pdf',
dataset_file='datasets/clf.csv',
split=split,
attribute='r',
legend_location='lower left')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels clf accuracy vs r-magnitude error')
file_list = glob_re(os.path.join(base_dir, 'npy'), f'yhat_{split}_{timestamp}_{backbone}_(12|05|03)_unlabeled_clf_ft1.npy')
p.acc_attribute_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) for f in file_list],
output_file=f'figures/exp_clf_channels_acc-r-err_{split}.pdf',
dataset_file='datasets/clf.csv',
split=split,
attribute='r',
legend_location='lower left')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting channels clf accuracy vs fwhm')
file_list = glob_re(os.path.join(base_dir, 'npy'), f'yhat_{split}_{timestamp}_{backbone}_(12|05|03)_unlabeled_clf_ft1.npy')
p.acc_attribute_curve(
file_list=file_list,
plt_labels=[get_channels_label(f) for f in file_list],
output_file=f'figures/exp_clf_channels_acc-fwhm_{split}.pdf',
dataset_file='datasets/clf.csv',
split=split,
attribute='fwhm',
legend_location='lower right')
print(f'{str(next(cnt_iterator)).zfill(2)} plotting projections colored by r-magnitude, from pretext model features')
file_list = glob_re(os.path.join(base_dir, 'npy'), f'Xf_unlabeled-{split}_{timestamp}_{backbone}_(12|05|03)_unlabeled.npy')
p.projection_scatter(
file_list=file_list,
plt_labels=[get_dataset_label(f) + '; ' + get_channels_label(f) for f in file_list],
output_file=f'figures/exp_{projection_algo}_{split}_pretraining_magnitudes.pdf',
dataset_file='datasets/unlabeled.csv',
algorithm=projection_algo,
split=split,
color_attribute='r',
n_cols=3,
n_neighbors=umap__n_neighbors)
print(f'{str(next(cnt_iterator)).zfill(2)} plotting projections colored by class, from clf features')
file_list = glob_re(os.path.join(base_dir, 'npy'), f'Xf_{split}_{timestamp}_{backbone}_(12|05|03)_(imagenet|unlabeled)_clf_ft1.npy')
p.projection_scatter(
file_list=file_list,
plt_labels=[get_dataset_label(f) + '; ' + get_channels_label(f) for f in file_list],
output_file=f'figures/exp_{projection_algo}_{split}_clf_classes.pdf',
dataset_file='datasets/clf.csv',
algorithm=projection_algo,
split=split,
color_attribute='class',
n_neighbors=umap__n_neighbors)
# print(f'{str(next(cnt_iterator)).zfill(2)} plotting projection colored by r-magnitude')
# file_list = glob_re(os.path.join(base_dir, 'npy'), f'Xf_{split}_{timestamp}_{backbone}_12_unlabeled(_clf_ft1.npy|.npy)')
# p.projection_scatter(
# file_list=file_list,
# plt_labels=['classifier', 'magnitudes regression model'],
# output_file=f'figures/exp_{projection_algo}_{split}_clf_magnitudes.pdf',
# dataset_file='datasets/clf.csv',
# algorithm=projection_algo,
# split=split,
# color_attribute='r',
# n_neighbors=umap__n_neighbors)
# print(f'{str(next(cnt_iterator)).zfill(2)} plotting X + Xu projection (extracted from classifier) colored by class')
# file_list = [os.path.join(base_dir, 'npy', f'Xf-Xuf_{split}_{timestamp}_{backbone}_12_unlabeled_clf_ft1.npy')]
# p.projection_scatter(
# file_list=file_list,
# plt_labels=['X + Xu features'],
# output_file=f'figures/exp_{projection_algo}_{split}_clf_X-Xu.pdf',
# dataset_file='datasets/clf.csv',
# algorithm=projection_algo,
# split=split,
# color_attribute='class')
# print(f'{str(next(cnt_iterator)).zfill(2)} plotting X + Xu projection (extracted from regression model) colored by class')
# file_list = [os.path.join(base_dir, 'npy', f'Xf-Xuf_{split}_{timestamp}_{backbone}_12_unlabeled.npy')]
# p.projection_scatter(
# file_list=file_list,
# plt_labels=['X + Xu features'],
# output_file=f'figures/exp_{projection_algo}_{split}_clf_X-Xu_regression-model.pdf',
# dataset_file='datasets/clf.csv',
# algorithm=projection_algo,
# split=split,
# color_attribute='class')