-
Notifications
You must be signed in to change notification settings - Fork 0
/
performative_util.py
1043 lines (914 loc) · 35.4 KB
/
performative_util.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import time
import functools
from collections import OrderedDict
from typing import List, Dict, Tuple, Optional
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import Subset
from zipfile import ZipFile
import tarfile
import gdown
import json
import shutil
import pandas as pd
import ast
from utils import (
ConcatDatasetWithDomainLabel,
SubsetDatasetWithSampleGroup,
count_significant_digits,
)
from tllib.utils.metric import accuracy
from tllib.utils.meter import AverageMeter, ProgressMeter
import tllib.vision.datasets as datasets
from tllib.vision.datasets.tabular_data import DATASET_NAMES as TABULAR_DATASET_NAMES
from wilds.datasets.wilds_dataset import WILDSSubset
import argparse
from timm.data.loader import MultiEpochsDataLoader
from torch.utils.data import DataLoader
import collections
def partition_subpopulation_shift_dataset(
dataset: ConcatDatasetWithDomainLabel,
subpopulation_ratios: dict,
random_state: np.random.RandomState,
shift_type: str = "domain_class",
base_size: int = -1,
is_train_set: bool = False,
full_covariate_shift: bool = False,
):
"""
obtain an index dictionary consisting subpopulations, with the index [domain_index, class_index].
we then select a subset of these indices (globally) to create a subset.
"""
domain_ids = None
if full_covariate_shift:
# only show half of the domains randomly
domain_ids = random_state.choice(
np.arange(dataset.num_domains()), size=2, replace=False
)
index_subpopulations = get_subpopulation_indices(
stratified_category=shift_type,
concatenated_dataset=dataset,
full_covariate_shift=full_covariate_shift,
domain_ids=domain_ids,
)
if base_size <= 0:
selected_population_index = next(
(key for key, value in subpopulation_ratios.items() if value == 1), None
)
base_size = (
len(index_subpopulations[selected_population_index])
if selected_population_index
else len(dataset)
)
cnt = 0
pdf = list(subpopulation_ratios.values())
pdf /= np.sum(pdf) # for numerical consistency
key_list = list(subpopulation_ratios.keys())
sampled_subpopulation_indices = OrderedDict(
{key: [] for key in index_subpopulations.keys()}
)
if not is_train_set:
# add 5 samples to each subpopulation
for i in range(len(key_list)):
sampled_subpopulation_indices[key_list[i]] = random_state.choice(
index_subpopulations[key_list[i]], size=5, replace=False
).tolist()
cnt += len(sampled_subpopulation_indices[key_list[i]])
while cnt < base_size:
sampled_subpopulation_key = random_state.choice(key_list, p=pdf)
if (
sampled_subpopulation_key not in index_subpopulations
or len(index_subpopulations[sampled_subpopulation_key]) == 0
):
continue
if len(index_subpopulations[sampled_subpopulation_key]) == len(
sampled_subpopulation_indices[sampled_subpopulation_key]
): # if we exhausted the subpopulation
# to optimize sampling process, we set the probability of exhausted subpopulation to be 1e-4
exhausted_id = int(
sampled_subpopulation_key[sampled_subpopulation_key.find("_") + 1 :]
)
pdf[exhausted_id] = 1e-4
pdf /= np.sum(pdf)
continue
sample_id = random_state.choice(
list(
set(index_subpopulations[sampled_subpopulation_key])
- set(sampled_subpopulation_indices[sampled_subpopulation_key])
)
)
sampled_subpopulation_indices[sampled_subpopulation_key].append(sample_id)
cnt += 1
print(
f"Subpopulation sizes: {[len(sub) for sub in sampled_subpopulation_indices.values()]}"
)
print(
"Sum is {}".format(
np.sum([len(sub) for sub in sampled_subpopulation_indices.values()])
)
)
return sampled_subpopulation_indices
def get_subpopulation_ratios(
subpopulation_accuracies: dict,
temperature: float,
positive_correlation: bool,
):
assert temperature > 0
temperature = temperature if positive_correlation else -temperature
subpopulation_accuracies = OrderedDict(subpopulation_accuracies)
subpopulation_ratios = softmax_with_temperature(
logits=torch.tensor(list(subpopulation_accuracies.values())),
temperature=temperature,
)
n_digits = count_significant_digits(1 / len(subpopulation_ratios)) + 2
return OrderedDict(
{
list(subpopulation_accuracies.keys())[index]: round(
subpopulation_ratio.item(), n_digits
)
for index, subpopulation_ratio in enumerate(subpopulation_ratios)
}
)
def softmax_with_temperature(logits, temperature=1.0):
"""
Compute the softmax of the input logits with a temperature parameter.
Args:
logits (torch.Tensor): Input logits.
temperature (float): Temperature parameter. Higher values (e.g., > 1.0) make the distribution
more uniform, while lower values (e.g., < 1.0) sharpen the distribution.
Returns:
torch.Tensor: Softmax probabilities.
"""
if abs(temperature) == 1.0:
# Standard softmax
return torch.nn.functional.softmax(logits / temperature, dim=-1)
else:
logits = logits / temperature
max_logits = torch.max(logits, dim=-1, keepdim=True)[0]
exp_logits = torch.exp(logits - max_logits)
softmax_probs = exp_logits / exp_logits.sum(dim=-1, keepdim=True)
return softmax_probs
def stratified_validation(
val_loader,
model,
device,
num_domains,
num_classes,
stratified_category,
print_freq,
oracle=False,
training_priors=None,
test_priors=None,
) -> float:
batch_time = AverageMeter("Time", ":6.3f")
losses = AverageMeter("Loss", ":.4e")
top1 = AverageMeter("Acc@1", ":6.2f")
progress = ProgressMeter(
len(val_loader), [batch_time, losses, top1], prefix="Test: "
)
assert stratified_category in ["class", "domain", "domain_class"]
if stratified_category == "domain_class":
subpopulation_records = OrderedDict(
{
f"domain_{domain_label}_class_{class_label}": []
for domain_label in range(num_domains)
for class_label in range(num_classes)
}
)
elif stratified_category == "domain":
subpopulation_records = OrderedDict(
{f"domain_{domain_label}": [] for domain_label in range(num_domains)}
)
else:
subpopulation_records = OrderedDict(
{f"class_{class_label}": [] for class_label in range(num_classes)}
)
# switch to evaluate mode
model.eval()
with torch.no_grad():
end = time.time()
for i, (images, target, domain_ids) in enumerate(val_loader):
images = images.to(device)
target = target.to(device)
# compute output
output = model(images)
loss = F.cross_entropy(output, target)
# measure accuracy and record loss
if oracle == True:
output = F.softmax(output, dim=1)
output = torch.divide(output, training_priors)
output = torch.multiply(output, test_priors)
acc1 = accuracy(output, target)[0]
losses.update(loss.item(), images.size(0))
top1.update(acc1.item(), images.size(0))
corrects = get_corrects(outputs=output, targets=target)
for index, correct in enumerate(corrects):
if stratified_category == "domain_class":
subpopulation_records[
f"domain_{domain_ids[index].item()}_class_{target[index].item()}"
].append(correct)
elif stratified_category == "domain":
subpopulation_records[f"domain_{domain_ids[index].item()}"].append(
correct
)
else:
subpopulation_records[f"class_{target[index].item()}"].append(
correct
)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % print_freq == 0:
progress.display(i)
print(" * Acc@1 {top1.avg:.3f} ".format(top1=top1))
subpopulation_accuracies = OrderedDict(
{
key: (sum(record) / len(record) if len(record) > 0 else 0)
for key, record in subpopulation_records.items()
}
)
return top1.avg, subpopulation_accuracies
def stratified_validation_prior_pred(
val_loader,
model,
device,
num_domains,
num_classes,
stratified_category,
print_freq,
training_priors,
eval_priors,
) -> float:
batch_time = AverageMeter("Time", ":6.3f")
losses = AverageMeter("Loss", ":.4e")
top1 = AverageMeter("Acc@1", ":6.2f")
progress = ProgressMeter(
len(val_loader), [batch_time, losses, top1], prefix="Test: "
)
assert stratified_category in ["class", "domain", "domain_class"]
if stratified_category == "domain_class":
subpopulation_records = OrderedDict(
{
f"domain_{domain_label}_class_{class_label}": []
for domain_label in range(num_domains)
for class_label in range(num_classes)
}
)
elif stratified_category == "domain":
subpopulation_records = OrderedDict(
{f"domain_{domain_label}": [] for domain_label in range(num_domains)}
)
else:
subpopulation_records = OrderedDict(
{f"class_{class_label}": [] for class_label in range(num_classes)}
)
# switch to evaluate mode
model.eval()
with torch.inference_mode():
end = time.time()
for i, (images, target, domain_ids) in enumerate(val_loader):
images = images.to(device)
target = target.to(device)
# compute output
output = model(images)
# measure accuracy and record loss
output = F.softmax(output, dim=1)
output = torch.divide(output, training_priors)
output = torch.multiply(output, eval_priors)
loss = F.cross_entropy(output, target)
acc1 = accuracy(output, target)[0]
losses.update(loss.item(), images.size(0))
top1.update(acc1.item(), images.size(0))
corrects = get_corrects(outputs=output, targets=target)
for index, correct in enumerate(corrects):
if stratified_category == "domain_class":
subpopulation_records[
f"domain_{domain_ids[index].item()}_class_{target[index].item()}"
].append(correct)
elif stratified_category == "domain":
subpopulation_records[f"domain_{domain_ids[index].item()}"].append(
correct
)
else:
subpopulation_records[f"class_{target[index].item()}"].append(
correct
)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % print_freq == 0:
progress.display(i)
print(" * Acc@1 {top1.avg:.3f} ".format(top1=top1))
subpopulation_accuracies = OrderedDict(
{
key: (sum(record) / len(record) if len(record) > 0 else 0)
for key, record in subpopulation_records.items()
}
)
return top1.avg, subpopulation_accuracies
def get_corrects(outputs, targets):
with torch.no_grad():
_, preds = outputs.topk(1, 1, True, True)
preds = preds.t()
corrects = preds.eq(targets[None])
return corrects.tolist()[0]
def get_priors(dataloader, num_classes, device):
labels = torch.cat([label for _, label, _ in dataloader]).tolist()
freqs = collections.Counter(labels)
priors = np.array([freqs[i] for i in range(num_classes)]).astype(float)
priors /= np.sum(priors)
priors = torch.from_numpy(priors).to(device)
return priors
def test_after_shift_pre_adapt(
round_id: int,
test_loader: torch.utils.data.DataLoader,
classifier: torch.nn.Module,
device: str,
num_classes: int,
num_domains: int,
args: argparse.Namespace,
sampled_subpopulation_indices: Dict[str, List[int]],
training_priors: Optional[torch.Tensor],
test_priors: Optional[torch.Tensor],
test_val_subpopulation_accuracies: Optional[Dict[str, float]],
prior_predictor: Optional[torch.nn.Module],
):
prev_round_accs = None
oracle_acc = None
if (args.prior_predictor or args.pretraining_for_predictors):
if test_val_subpopulation_accuracies != None:
prev_round_accs = torch.Tensor(
list(test_val_subpopulation_accuracies.values())
).to(device)
else:
prev_round_accs = test_val_subpopulation_accuracies
# acc_t-1 -> priors
if args.prior_predictor and round_id != 0:
with torch.inference_mode():
out = prior_predictor(prev_round_accs)
priors = F.softmax(out, dim=0)
test_acc, test_subpopulation_accuracies = stratified_validation_prior_pred(
test_loader,
classifier,
device,
num_classes=num_classes,
num_domains=num_domains,
stratified_category=args.shift_type,
print_freq=args.print_freq,
training_priors=training_priors,
eval_priors=priors,
)
else:
test_acc, test_subpopulation_accuracies = stratified_validation(
test_loader,
classifier,
device,
num_classes=num_classes,
num_domains=num_domains,
stratified_category=args.shift_type,
print_freq=args.print_freq,
oracle=False,
training_priors=None,
test_priors=None,
)
if args.oracle:
oracle_acc, _ = stratified_validation(
test_loader,
classifier,
device,
num_classes=num_classes,
num_domains=num_domains,
stratified_category=args.shift_type,
print_freq=args.print_freq,
oracle=args.oracle,
training_priors=training_priors,
test_priors=test_priors,
)
non_zero_values = [
value for value in test_subpopulation_accuracies.values() if value != 0
]
mean_non_zero = (
sum(non_zero_values) / len(non_zero_values) if non_zero_values else 0
)
test_subpopulation_accuracies = collections.OrderedDict(
(
k,
(
mean_non_zero
if v == 0
and len(sampled_subpopulation_indices["class_{}".format(i)]) == 0
else v
),
)
for i, (k, v) in enumerate(test_subpopulation_accuracies.items())
)
return test_acc, test_subpopulation_accuracies, oracle_acc, prev_round_accs
def get_performative_dataloaders(
train_dataset: SubsetDatasetWithSampleGroup,
val_dataset: SubsetDatasetWithSampleGroup,
test_dataset: SubsetDatasetWithSampleGroup,
args: argparse.Namespace,
) -> Tuple[
torch.utils.data.DataLoader,
torch.utils.data.DataLoader,
torch.utils.data.DataLoader,
]:
train_loader = MultiEpochsDataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.workers,
drop_last=True,
pin_memory=True,
)
test_loader = DataLoader(
test_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.val_workers,
pin_memory=True,
)
if not args.no_training:
val_loader = DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.val_workers,
pin_memory=True,
)
else:
val_loader = test_loader
return train_loader, val_loader, test_loader
def get_performative_datasets(
original_train_dataset: ConcatDatasetWithDomainLabel,
original_val_dataset: ConcatDatasetWithDomainLabel,
original_test_dataset: ConcatDatasetWithDomainLabel,
args: argparse.Namespace,
random_state: np.random.RandomState,
test_val_subpopulation_accuracies: Dict[str, float],
initial_subpopulation_ratios: Dict[str, float],
) -> Tuple[
SubsetDatasetWithSampleGroup,
SubsetDatasetWithSampleGroup,
SubsetDatasetWithSampleGroup,
List,
]:
datasets = {}
for original_dataset, key in zip(
[original_val_dataset, original_test_dataset, original_train_dataset],
["val", "test", "train"],
):
if args.no_training and key == "val":
datasets["val"] = []
continue
datasets[key], sampled_subpopulation_indices = performative_shift(
original_dataset,
temperature=args.performative_temperature,
positive_correlation=args.positive_correlation,
shift_type=args.shift_type,
random_state=random_state,
subpopulation_accuracies=test_val_subpopulation_accuracies,
subpopulation_ratios=initial_subpopulation_ratios,
base_size=(
args.base_size * (args.split_ratios[key] / args.split_ratios["train"])
if key != "test"
else args.test_base_size
),
is_train_set=(key == "train"),
full_covariate_shift=args.full_covariate_shift,
)
print("train_dataset_size: ", len(datasets["train"]))
print("val_dataset_size: ", len(datasets["val"]))
print("test_dataset_size: ", len(datasets["test"]))
return (
datasets["train"],
datasets["val"],
datasets["test"],
sampled_subpopulation_indices,
)
def performative_shift(
concatenated_datasets: ConcatDatasetWithDomainLabel,
random_state: np.random.RandomState,
shift_type: str = "domain",
subpopulation_ratios: Dict[str, float] = None,
subpopulation_accuracies: Dict = None,
temperature: float = 1,
positive_correlation: bool = False,
base_size: int = -1,
is_train_set: bool = False,
full_covariate_shift: bool = False,
) -> List[Subset]:
assert shift_type in ["domain", "class", "domain_class"]
# get the subpopulation ratios
if subpopulation_accuracies:
assert subpopulation_ratios is None
subpopulation_ratios = get_subpopulation_ratios(
subpopulation_accuracies=subpopulation_accuracies,
temperature=temperature,
positive_correlation=positive_correlation,
)
print(
f"We have a subpopulation with max {max(subpopulation_ratios.items(), key=lambda x: x[1])} and min {min(subpopulation_ratios.items(),key=lambda x: x[1])}."
)
# adjust the subpopulation dataset
sampled_subpopulation_indices = partition_subpopulation_shift_dataset(
dataset=concatenated_datasets,
subpopulation_ratios=subpopulation_ratios,
random_state=random_state,
shift_type=shift_type,
base_size=base_size,
is_train_set=is_train_set,
full_covariate_shift=full_covariate_shift,
)
adjusted_index_set = functools.reduce(
lambda a, b: a + b, sampled_subpopulation_indices.values()
)
return (
SubsetDatasetWithSampleGroup(
dataset=concatenated_datasets,
indices=adjusted_index_set,
subpopulation_indices=sampled_subpopulation_indices,
),
sampled_subpopulation_indices,
)
def stage_path(data_dir, name):
full_path = os.path.join(data_dir, name)
if not os.path.exists(full_path):
os.makedirs(full_path)
return full_path
def download_and_extract(url, dst, remove=True):
gdown.download(url, dst, quiet=False)
if dst.endswith(".tar.gz"):
tar = tarfile.open(dst, "r:gz")
tar.extractall(os.path.dirname(dst))
tar.close()
if dst.endswith(".tar"):
tar = tarfile.open(dst, "r:")
tar.extractall(os.path.dirname(dst))
tar.close()
if dst.endswith(".zip"):
zf = ZipFile(dst, "r")
zf.extractall(os.path.dirname(dst))
zf.close()
if remove:
os.remove(dst)
def download_pacs(data_dir):
# Original URL: http://www.eecs.qmul.ac.uk/~dl307/project_iccv2017
full_path = stage_path(data_dir, "PACS")
download_and_extract(
"https://drive.google.com/uc?id=1JFr8f805nMUelQWWmfnJR3y4_SYoN5Pd",
os.path.join(data_dir, "PACS.zip"),
)
os.rename(os.path.join(data_dir, "kfold"), full_path)
def download_terra(data_dir):
# Original URL: https://beerys.github.io/CaltechCameraTraps/
# New URL: http://lila.science/datasets/caltech-camera-traps
full_path = stage_path(data_dir, "TerraIncognita")
download_and_extract(
"https://lilablobssc.blob.core.windows.net/caltechcameratraps/eccv_18_all_images_sm.tar.gz",
os.path.join(full_path, "terra_incognita_images.tar.gz"),
)
download_and_extract(
"https://lilablobssc.blob.core.windows.net/caltechcameratraps/labels/caltech_camera_traps.json.zip",
os.path.join(full_path, "caltech_camera_traps.json.zip"),
)
include_locations = ["38", "46", "100", "43"]
include_categories = [
"bird",
"bobcat",
"cat",
"coyote",
"dog",
"empty",
"opossum",
"rabbit",
"raccoon",
"squirrel",
]
images_folder = os.path.join(full_path, "eccv_18_all_images_sm/")
annotations_file = os.path.join(full_path, "caltech_images_20210113.json")
destination_folder = full_path
stats = {}
if not os.path.exists(destination_folder):
os.mkdir(destination_folder)
with open(annotations_file, "r") as f:
data = json.load(f)
category_dict = {}
for item in data["categories"]:
category_dict[item["id"]] = item["name"]
for image in data["images"]:
image_location = image["location"]
if image_location not in include_locations:
continue
loc_folder = os.path.join(
destination_folder, "location_" + str(image_location) + "/"
)
if not os.path.exists(loc_folder):
os.mkdir(loc_folder)
image_id = image["id"]
image_fname = image["file_name"]
for annotation in data["annotations"]:
if annotation["image_id"] == image_id:
if image_location not in stats:
stats[image_location] = {}
category = category_dict[annotation["category_id"]]
if category not in include_categories:
continue
if category not in stats[image_location]:
stats[image_location][category] = 0
else:
stats[image_location][category] += 1
loc_cat_folder = os.path.join(loc_folder, category + "/")
if not os.path.exists(loc_cat_folder):
os.mkdir(loc_cat_folder)
dst_path = os.path.join(loc_cat_folder, image_fname)
src_path = os.path.join(images_folder, image_fname)
shutil.copyfile(src_path, dst_path)
shutil.rmtree(images_folder)
os.remove(annotations_file)
def create_image_list(data_dir, dataset_name):
if dataset_name == "DollarStreet":
dir_name = os.path.join(data_dir, "image_list")
if os.path.isdir(dir_name) and ("image_list" in os.listdir(data_dir)):
return
root = data_dir
train_pth = "images_v2_imagenet_train.csv"
test_pth = "images_v2_imagenet_test.csv"
df_train = pd.read_csv(os.path.join(root, train_pth))
df_test = pd.read_csv(os.path.join(root, test_pth))
df = pd.concat([df_train, df_test])
regions = np.array(df["region.id"])
dct = {}
for r in regions:
if r not in dct:
dct[r] = 1
else:
dct[r] += 1
image_pth = "assets/"
image_dir = os.path.join(root, image_pth)
image_list = os.path.join(root, "image_list")
os.makedirs(image_list, exist_ok=True)
num = 0
imagenet_to_num = {}
for rg in dct.keys():
print(rg)
f_pth = os.path.join(image_list, rg + "_all.txt")
sub_df = df[df["region.id"] == rg]
paths = np.array(sub_df["imageRelPath"])
labels = np.array(sub_df["imagenet_sysnet_id"])
with open(f_pth, "w") as file:
for i in range(len(paths)):
str_label = np.array(ast.literal_eval(labels[i]))[0]
if str_label not in imagenet_to_num:
imagenet_to_num[str_label] = num
num += 1
txt = paths[i] + " " + str(imagenet_to_num[str_label]) + "\n"
file.write(txt)
else:
dir_name = os.path.join(data_dir, "image_list")
if os.path.isdir(dir_name) and ("image_list" in os.listdir(data_dir)):
return
domain_list = os.listdir(data_dir)
os.makedirs(dir_name, exist_ok=True)
for dom in domain_list:
dom_path = os.path.join(dir_name, dom + "_all.txt")
with open(dom_path, "w") as file:
read_domain = os.path.join(data_dir, dom)
for i, label in enumerate(sorted(os.listdir(read_domain))):
read_class_dom = os.path.join(read_domain, label)
for img_pth in os.listdir(read_class_dom):
txt = os.path.join(dom, label, img_pth) + " " + str(i) + "\n"
file.write(txt)
def get_subpopulation_shift_dataset(
dataset_name,
root,
split_ratios: Dict[str, float],
download=True,
train_transform=None,
val_transform=None,
seed=0,
):
assert set(split_ratios.keys()) == {"train", "val", "test"}
# load datasets from tllib.vision.datasets
supported_dataset = [
"PACS",
"OfficeHome",
"DomainNet",
"TerraIncognita",
"DollarStreet",
"CivilComments",
"Amazon",
"CIFAR10",
"CIFAR100",
"ImageNet100",
"AGNews",
] + TABULAR_DATASET_NAMES
assert dataset_name in supported_dataset
train_split_list = []
val_split_list = []
test_split_list = []
if dataset_name in TABULAR_DATASET_NAMES:
dataset = datasets.__dict__["TabularHFDataset"]
domains = ["none"]
else:
dataset = datasets.__dict__[dataset_name]
domains = dataset.domains()
for domain_index, task in enumerate(domains):
if dataset_name == "PACS":
try:
all_split = dataset(
root=root, task=task, split="all", download=download
)
num_classes = all_split.num_classes
except:
print("Resolving...")
# download
if not os.path.isdir(root) or len(os.listdir(root)) == 0:
download_pacs(root[: root.rfind("/")])
create_image_list(root, dataset_name)
all_split = dataset(
root=root, task=task, split="all", download=download
)
num_classes = all_split.num_classes
elif dataset_name == "OfficeHome":
all_split = dataset(root=root, task=task, download=download)
num_classes = all_split.num_classes
elif dataset_name == "DomainNet":
train_split = dataset(
root=root, task=task, split="train", download=download
)
test_split = dataset(root=root, task=task, split="test", download=download)
train_split.samples += test_split.samples
train_split.targets += test_split.targets
all_split = train_split
num_classes = all_split.num_classes
elif dataset_name == "TerraIncognita":
try:
all_split = dataset(
root=root, task=task, split="all", download=download
)
num_classes = all_split.num_classes
except:
print("Resolving...")
# download
if not os.path.isdir(root) or len(os.listdir(root)) == 0:
download_terra(root[: root.rfind("/")])
create_image_list(root, dataset_name)
all_split = dataset(root=root, task=task, split="all", download=True)
num_classes = all_split.num_classes
elif dataset_name == "DollarStreet":
"""
download the dataset from https://www.kaggle.com/datasets/mlcommons/the-dollar-street-dataset/data
put it on the data directory with name DollarStreet
"""
try:
all_split = dataset(
root=root,
task=task,
split="all",
download=download,
init_preprocessing=False,
use_preprocessed_data=True,
)
num_classes = all_split.num_classes
except:
print("Resolving...")
create_image_list(root, dataset_name)
all_split = dataset(root=root, task=task, split="all", download=True)
num_classes = all_split.num_classes
elif dataset_name == "CivilComments":
all_split = dataset(download=False, root_dir=root)
num_classes = 2
all_split.CLASSES = ["positive", "negative"]
# mask out the bad samples (235182, 245642)
mask = torch.ones(len(all_split), dtype=torch.bool)
mask[235182] = False
mask[245642] = False
all_split._y_array = all_split._y_array[mask]
del all_split._text_array[235182] # since we deleted 235182th
del all_split._text_array[
245641
] # to delete 245642th, decrement index by one
elif dataset_name == "Amazon":
all_split = dataset(download=False, root_dir=root)
num_classes = 5
all_split.CLASSES = ["1", "2", "3", "4", "5"]
elif dataset_name in ["CIFAR10", "CIFAR100"]:
train_split = dataset(root=root, split="train", download=download)
test_split = dataset(root=root, split="test", download=download)
train_split.data = np.concatenate(
(train_split.data, test_split.data), axis=0
)
train_split.targets += test_split.targets
all_split = train_split
num_classes = all_split.num_classes
elif dataset_name == "ImageNet100":
all_split = dataset(root=os.path.join(root, "train"))
num_classes = 100
elif dataset_name == "AGNews":
all_split = dataset()
num_classes = 4
elif dataset_name in TABULAR_DATASET_NAMES:
all_split = dataset(hf_dataset_name=dataset_name)
num_classes = all_split.num_classes
else:
raise NotImplementedError
train_split, val_split, test_split = split_dataset_multiple(
all_split,
[
int(len(all_split) * split_ratios["train"]),
int(len(all_split) * split_ratios["val"]),
],
seed,
)
train_split_list.append(train_split)
val_split_list.append(val_split)
test_split_list.append(test_split)
train_dataset = ConcatDatasetWithDomainLabel(
train_split_list, transform=train_transform
)
val_dataset = ConcatDatasetWithDomainLabel(val_split_list, transform=val_transform)
test_dataset = ConcatDatasetWithDomainLabel(
test_split_list, transform=val_transform
)
dataset_dict = {"train": train_dataset, "val": val_dataset, "test": test_dataset}
return dataset_dict["train"], dataset_dict["val"], dataset_dict["test"], num_classes
def get_subpopulation_indices(
stratified_category: str,
concatenated_dataset: ConcatDatasetWithDomainLabel,
full_covariate_shift: bool = False,
domain_ids: Optional[int] = None,
):
class_labels = []
for dataset in concatenated_dataset.datasets:
if isinstance(dataset.dataset, WILDSSubset):
class_labels.append(
dataset.dataset.dataset._y_array.numpy()[dataset.indices]
)
else:
class_labels.append(np.array(dataset.dataset.targets)[dataset.indices])
class_labels = np.concatenate(class_labels)
domain_labels = np.array(list(concatenated_dataset.index_to_domain_id.values()))
assert len(class_labels) == len(domain_labels)
assert min(class_labels) == min(domain_labels) == 0
num_classes = max(class_labels) + 1
num_domains = max(domain_labels) + 1
index_subpopulations = OrderedDict()
if stratified_category == "domain_class":
for domain_label in range(num_domains):
for class_label in range(num_classes):
domain_indices = np.where(domain_labels == domain_label)[0]
class_indices = np.where(class_labels == class_label)[0]
subpopulation_indices = np.intersect1d(
domain_indices, class_indices
).tolist()
index_subpopulations[f"domain_{domain_label}_class_{class_label}"] = (
subpopulation_indices
)
elif stratified_category == "domain":
for domain_label in range(num_domains):
domain_indices = np.where(domain_labels == domain_label)[0].tolist()
index_subpopulations[f"domain_{domain_label}"] = domain_indices
elif stratified_category == "class":
for class_label in range(num_classes):
if full_covariate_shift:
class_indices_with_domain = np.where(
(class_labels == class_label)