Zihao commited on
Commit
383573d
1 Parent(s): 1baba94
Files changed (1) hide show
  1. zhlds.py +138 -30
zhlds.py CHANGED
@@ -27,6 +27,7 @@ from PIL import Image
27
 
28
  _VERSION = "1.0.0"
29
  _BASE_URL = "https://cvinthewildeus.blob.core.windows.net/datasets/"
 
30
 
31
  _ELEVATER_CITATION = """\
32
  @article{li2022elevater,
@@ -236,7 +237,8 @@ class ELEVATERConfig(datasets.BuilderConfig):
236
 
237
  """BuilderConfig for ELEVATER."""
238
  def __init__(self, name, description, contact, version, type_, format_,
239
- root_folder, labelmap, num_classes, train, val, test, citation, url, **kwargs):
 
240
  """BuilderConfig for ELEVATER.
241
  Args:
242
  features: `list[string]`, list of the features that will appear in the
@@ -262,8 +264,11 @@ class ELEVATERConfig(datasets.BuilderConfig):
262
  self.train = train
263
  self.val = val
264
  self.test = test
 
265
  self.citation = citation
266
  self.url = url
 
 
267
 
268
 
269
  class ELEVATER(datasets.GeneratorBasedBuilder):
@@ -292,6 +297,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
292
  },
293
  citation=_CIFAR_10_CITATION,
294
  url=_BASE_URL,
 
 
 
295
  ),
296
  ELEVATERConfig(
297
  name="voc-2007-classification",
@@ -320,6 +328,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
320
  num_classes=20,
321
  citation=_VOC_2007_CLASSIFICATION_CITATION,
322
  url=_BASE_URL,
 
 
 
323
  ),
324
  ELEVATERConfig(
325
  name="gtsrb",
@@ -348,6 +359,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
348
  num_classes=43,
349
  citation=_GTSRB_CITATION,
350
  url=_BASE_URL,
 
 
 
351
  ),
352
  ELEVATERConfig(
353
  name="country211",
@@ -376,6 +390,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
376
  num_classes=211,
377
  citation=_COUNTRY211_CITATION,
378
  url=_BASE_URL,
 
 
 
379
  ),
380
  ELEVATERConfig(
381
  name="rendered-sst2",
@@ -404,6 +421,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
404
  num_classes=2,
405
  citation=_RENDERED_SST2_CITATION,
406
  url=_BASE_URL,
 
 
 
407
  ),
408
  ELEVATERConfig(
409
  name="kitti-distance",
@@ -432,6 +452,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
432
  num_classes=4,
433
  citation=_KITTI_DISTANCE_CITATION,
434
  url=_BASE_URL,
 
 
 
435
  ),
436
  ELEVATERConfig(
437
  name="eurosat_clip",
@@ -460,6 +483,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
460
  num_classes=10,
461
  citation=_EOROSAT_CLIP_CITATION,
462
  url=_BASE_URL,
 
 
 
463
  ),
464
  ELEVATERConfig(
465
  name="resisc45_clip",
@@ -488,6 +514,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
488
  num_classes=45,
489
  citation=_RESISC45_CLIP_CITATION,
490
  url=_BASE_URL,
 
 
 
491
  ),
492
  ELEVATERConfig(
493
  name="caltech-101",
@@ -512,6 +541,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
512
  num_classes=45,
513
  citation=_CALTECH_101_CITATION,
514
  url=_BASE_URL,
 
 
 
515
  ),
516
  ELEVATERConfig(
517
  name="cifar-100",
@@ -536,6 +568,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
536
  num_classes=100,
537
  citation=_CIFAR_100_CITATION,
538
  url=_BASE_URL,
 
 
 
539
  ),
540
  ELEVATERConfig(
541
  name="dtd",
@@ -564,6 +599,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
564
  num_classes=47,
565
  citation=_DTD_CITATION,
566
  url=_BASE_URL,
 
 
 
567
  ),
568
  ELEVATERConfig(
569
  name="fgvc-aircraft-2013b-variants102",
@@ -592,6 +630,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
592
  num_classes=100,
593
  citation=_FGVC_AIRCRAFT_2013B_VARIANTS102_CITATION,
594
  url=_BASE_URL,
 
 
 
595
  ),
596
  ELEVATERConfig(
597
  name="food-101",
@@ -616,6 +657,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
616
  num_classes=101,
617
  citation=_FOOD_101_CITATION,
618
  url=_BASE_URL,
 
 
 
619
  ),
620
  ELEVATERConfig(
621
  name="mnist",
@@ -640,6 +684,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
640
  num_classes=10,
641
  citation=_MNIST_CITATION,
642
  url=_BASE_URL,
 
 
 
643
  ),
644
  ELEVATERConfig(
645
  name="oxford-flower-102",
@@ -668,6 +715,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
668
  num_classes=102,
669
  citation=_OXFORD_FLOWER_102_CITATION,
670
  url=_BASE_URL,
 
 
 
671
  ),
672
  ELEVATERConfig(
673
  name="oxford-iiit-pets",
@@ -692,6 +742,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
692
  num_classes=37,
693
  citation=_OXFORD_IIIT_PETS_CITATION,
694
  url=_BASE_URL,
 
 
 
695
  ),
696
  ELEVATERConfig(
697
  name="patch-camelyon",
@@ -720,6 +773,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
720
  num_classes=2,
721
  citation=_PATCH_CAMELYON_CITATION,
722
  url=_BASE_URL,
 
 
 
723
  ),
724
  ELEVATERConfig(
725
  name="stanford-cars",
@@ -744,6 +800,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
744
  num_classes=196,
745
  citation=_STANFORD_CARS_CITATION,
746
  url=_BASE_URL,
 
 
 
747
  ),
748
  ELEVATERConfig(
749
  name="fer-2013",
@@ -772,6 +831,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
772
  num_classes=7,
773
  citation=_FER_2013_CITATION,
774
  url=_BASE_URL,
 
 
 
775
  ),
776
  ELEVATERConfig(
777
  name="hateful-memes",
@@ -796,6 +858,9 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
796
  num_classes=2,
797
  citation=_HATEFUL_MEMES_CITATION,
798
  url=_BASE_URL,
 
 
 
799
  ),
800
  ]
801
 
@@ -834,13 +899,26 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
834
  "index": os.path.join(_URL, self.config.test['index_path']),
835
  }
836
  }
837
- if self.config.val is not None:
838
- urls_to_download['val'] = {
839
- "images": os.path.join(_URL, self.config.val['files_for_local_usage'][0]),
840
- "index": os.path.join(_URL, self.config.val['index_path']),
841
- }
 
 
 
 
 
 
 
 
 
842
 
843
  downloaded_files = dl_manager.download_and_extract(urls_to_download)
 
 
 
 
844
 
845
  SplitGenerator_list = [
846
  datasets.SplitGenerator(
@@ -848,6 +926,7 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
848
  gen_kwargs={
849
  "images": downloaded_files["train"]["images"],
850
  "index": downloaded_files["train"]["index"],
 
851
  "split": datasets.Split.TRAIN,
852
  },
853
  ),
@@ -856,35 +935,53 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
856
  gen_kwargs={
857
  "images": downloaded_files["test"]["images"],
858
  "index": downloaded_files["test"]["index"],
 
859
  "split": datasets.Split.TEST,
860
  },
861
  )]
862
 
863
- if self.config.val is not None:
864
- SplitGenerator_list.append(datasets.SplitGenerator(
865
- name=datasets.Split.VALIDATION,
866
- gen_kwargs={
867
- "images": downloaded_files["val"]["images"],
868
- "index": downloaded_files["val"]["index"],
869
- "split": datasets.Split.VALIDATION,
870
- },
871
- ))
872
 
873
  return SplitGenerator_list
874
 
875
- def _generate_examples(self, images, index, split):
 
 
 
 
 
 
 
876
 
877
  if self.config.name in ["kitti-distance", "resisc45_clip", "hateful-memes"]:
878
  with open(index, encoding="utf-8") as f:
879
  data = json.load(f)
880
  for i in range(len(data['images'])):
881
  label = data['annotations'][i]['category_id']
882
- path = images + '/' + data['images'][i]['file_name'].split('@')[1]
883
- yield i, {
884
- "image_file_path": path,
885
- "image": path,
886
- "labels": label,
887
- }
 
 
 
 
 
 
 
 
 
 
888
 
889
  else:
890
  with open(index, "r") as f:
@@ -894,10 +991,12 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
894
 
895
  if len(line_split) > 3:
896
  image_path_temp = " ".join(line_split[:-1])
897
- path = images + '/' + image_path_temp.split('@')[1] # image_path_temp[0]
898
  else:
899
- path = images + '/' + line_split[0].split('@')[1]
900
 
 
 
901
  if self.config.type == "classification_multilabel":
902
  label = [int(x) for x in line_split[-1].split(',')]
903
  else:
@@ -906,9 +1005,18 @@ class ELEVATER(datasets.GeneratorBasedBuilder):
906
  except:
907
  if self.config.name == "eurosat_clip" and split == 'test':
908
  label = 9
909
-
910
- yield i, {
911
- "image_file_path": path,
912
- "image": path,
913
- "labels": label,
914
- }
 
 
 
 
 
 
 
 
 
27
 
28
  _VERSION = "1.0.0"
29
  _BASE_URL = "https://cvinthewildeus.blob.core.windows.net/datasets/"
30
+ _FEW_SHOTS_FILE_PATH="subidx/id_label/data_train_#shot/"
31
 
32
  _ELEVATER_CITATION = """\
33
  @article{li2022elevater,
237
 
238
  """BuilderConfig for ELEVATER."""
239
  def __init__(self, name, description, contact, version, type_, format_,
240
+ root_folder, labelmap, num_classes, train, val, test, few_shots_file_path,
241
+ citation, url, num_shots, random_seed, **kwargs):
242
  """BuilderConfig for ELEVATER.
243
  Args:
244
  features: `list[string]`, list of the features that will appear in the
264
  self.train = train
265
  self.val = val
266
  self.test = test
267
+ self.few_shots_file_path = few_shots_file_path
268
  self.citation = citation
269
  self.url = url
270
+ self.num_shots = num_shots
271
+ self.random_seed = random_seed
272
 
273
 
274
  class ELEVATER(datasets.GeneratorBasedBuilder):
297
  },
298
  citation=_CIFAR_10_CITATION,
299
  url=_BASE_URL,
300
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
301
+ num_shots=-1, # 5, 20, 50
302
+ random_seed=-1, # 0, 1, 2
303
  ),
304
  ELEVATERConfig(
305
  name="voc-2007-classification",
328
  num_classes=20,
329
  citation=_VOC_2007_CLASSIFICATION_CITATION,
330
  url=_BASE_URL,
331
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
332
+ num_shots=-1, # 5, 20, 50
333
+ random_seed=-1, # 0, 1, 2
334
  ),
335
  ELEVATERConfig(
336
  name="gtsrb",
359
  num_classes=43,
360
  citation=_GTSRB_CITATION,
361
  url=_BASE_URL,
362
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
363
+ num_shots=-1, # 5, 20, 50
364
+ random_seed=-1, # 0, 1, 2
365
  ),
366
  ELEVATERConfig(
367
  name="country211",
390
  num_classes=211,
391
  citation=_COUNTRY211_CITATION,
392
  url=_BASE_URL,
393
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
394
+ num_shots=-1, # 5, 20, 50
395
+ random_seed=-1, # 0, 1, 2
396
  ),
397
  ELEVATERConfig(
398
  name="rendered-sst2",
421
  num_classes=2,
422
  citation=_RENDERED_SST2_CITATION,
423
  url=_BASE_URL,
424
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
425
+ num_shots=-1, # 5, 20, 50
426
+ random_seed=-1, # 0, 1, 2
427
  ),
428
  ELEVATERConfig(
429
  name="kitti-distance",
452
  num_classes=4,
453
  citation=_KITTI_DISTANCE_CITATION,
454
  url=_BASE_URL,
455
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
456
+ num_shots=-1, # 5, 20, 50
457
+ random_seed=-1, # 0, 1, 2
458
  ),
459
  ELEVATERConfig(
460
  name="eurosat_clip",
483
  num_classes=10,
484
  citation=_EOROSAT_CLIP_CITATION,
485
  url=_BASE_URL,
486
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
487
+ num_shots=-1, # 5, 20, 50
488
+ random_seed=-1, # 0, 1, 2
489
  ),
490
  ELEVATERConfig(
491
  name="resisc45_clip",
514
  num_classes=45,
515
  citation=_RESISC45_CLIP_CITATION,
516
  url=_BASE_URL,
517
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
518
+ num_shots=-1, # 5, 20, 50
519
+ random_seed=-1, # 0, 1, 2
520
  ),
521
  ELEVATERConfig(
522
  name="caltech-101",
541
  num_classes=45,
542
  citation=_CALTECH_101_CITATION,
543
  url=_BASE_URL,
544
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
545
+ num_shots=-1, # 5, 20, 50
546
+ random_seed=-1, # 0, 1, 2
547
  ),
548
  ELEVATERConfig(
549
  name="cifar-100",
568
  num_classes=100,
569
  citation=_CIFAR_100_CITATION,
570
  url=_BASE_URL,
571
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
572
+ num_shots=-1, # 5, 20, 50
573
+ random_seed=-1, # 0, 1, 2
574
  ),
575
  ELEVATERConfig(
576
  name="dtd",
599
  num_classes=47,
600
  citation=_DTD_CITATION,
601
  url=_BASE_URL,
602
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
603
+ num_shots=-1, # 5, 20, 50
604
+ random_seed=-1, # 0, 1, 2
605
  ),
606
  ELEVATERConfig(
607
  name="fgvc-aircraft-2013b-variants102",
630
  num_classes=100,
631
  citation=_FGVC_AIRCRAFT_2013B_VARIANTS102_CITATION,
632
  url=_BASE_URL,
633
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
634
+ num_shots=-1, # 5, 20, 50
635
+ random_seed=-1, # 0, 1, 2
636
  ),
637
  ELEVATERConfig(
638
  name="food-101",
657
  num_classes=101,
658
  citation=_FOOD_101_CITATION,
659
  url=_BASE_URL,
660
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
661
+ num_shots=-1, # 5, 20, 50
662
+ random_seed=-1, # 0, 1, 2
663
  ),
664
  ELEVATERConfig(
665
  name="mnist",
684
  num_classes=10,
685
  citation=_MNIST_CITATION,
686
  url=_BASE_URL,
687
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
688
+ num_shots=-1, # 5, 20, 50
689
+ random_seed=-1, # 0, 1, 2
690
  ),
691
  ELEVATERConfig(
692
  name="oxford-flower-102",
715
  num_classes=102,
716
  citation=_OXFORD_FLOWER_102_CITATION,
717
  url=_BASE_URL,
718
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
719
+ num_shots=-1, # 5, 20, 50
720
+ random_seed=-1, # 0, 1, 2
721
  ),
722
  ELEVATERConfig(
723
  name="oxford-iiit-pets",
742
  num_classes=37,
743
  citation=_OXFORD_IIIT_PETS_CITATION,
744
  url=_BASE_URL,
745
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
746
+ num_shots=-1, # 5, 20, 50
747
+ random_seed=-1, # 0, 1, 2
748
  ),
749
  ELEVATERConfig(
750
  name="patch-camelyon",
773
  num_classes=2,
774
  citation=_PATCH_CAMELYON_CITATION,
775
  url=_BASE_URL,
776
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
777
+ num_shots=-1, # 5, 20, 50
778
+ random_seed=-1, # 0, 1, 2
779
  ),
780
  ELEVATERConfig(
781
  name="stanford-cars",
800
  num_classes=196,
801
  citation=_STANFORD_CARS_CITATION,
802
  url=_BASE_URL,
803
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
804
+ num_shots=-1, # 5, 20, 50
805
+ random_seed=-1, # 0, 1, 2
806
  ),
807
  ELEVATERConfig(
808
  name="fer-2013",
831
  num_classes=7,
832
  citation=_FER_2013_CITATION,
833
  url=_BASE_URL,
834
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
835
+ num_shots=-1, # 5, 20, 50
836
+ random_seed=-1, # 0, 1, 2
837
  ),
838
  ELEVATERConfig(
839
  name="hateful-memes",
858
  num_classes=2,
859
  citation=_HATEFUL_MEMES_CITATION,
860
  url=_BASE_URL,
861
+ few_shots_file_path=_FEW_SHOTS_FILE_PATH,
862
+ num_shots=-1, # 5, 20, 50
863
+ random_seed=-1, # 0, 1, 2
864
  ),
865
  ]
866
 
899
  "index": os.path.join(_URL, self.config.test['index_path']),
900
  }
901
  }
902
+ if self.config.num_shots in [5, 20, 50]:
903
+ assert self.config.random_seed in [0, 1, 2]
904
+ few_shots_file_path_temp = _FEW_SHOTS_FILE_PATH.replace('#', str(self.config.num_shots))
905
+ file_name = 'shot' + str(self.config.num_shots) + '_seed' + str(self.config.random_seed) + '.json'
906
+ few_shot_path = os.path.join(_BASE_URL, few_shots_file_path_temp, self.config.name, file_name)
907
+ urls_to_download["train"]["few_shot"] = few_shot_path
908
+ else:
909
+ pass
910
+
911
+ # if self.config.val is not None:
912
+ # urls_to_download['val'] = {
913
+ # "images": os.path.join(_URL, self.config.val['files_for_local_usage'][0]),
914
+ # "index": os.path.join(_URL, self.config.val['index_path']),
915
+ # }
916
 
917
  downloaded_files = dl_manager.download_and_extract(urls_to_download)
918
+ try:
919
+ few_shot_train_file = downloaded_files["train"]["few_shot"]
920
+ except:
921
+ few_shot_train_file = None
922
 
923
  SplitGenerator_list = [
924
  datasets.SplitGenerator(
926
  gen_kwargs={
927
  "images": downloaded_files["train"]["images"],
928
  "index": downloaded_files["train"]["index"],
929
+ "few_shot": few_shot_train_file,
930
  "split": datasets.Split.TRAIN,
931
  },
932
  ),
935
  gen_kwargs={
936
  "images": downloaded_files["test"]["images"],
937
  "index": downloaded_files["test"]["index"],
938
+ "few_shot": None,
939
  "split": datasets.Split.TEST,
940
  },
941
  )]
942
 
943
+ # if self.config.val is not None:
944
+ # SplitGenerator_list.append(datasets.SplitGenerator(
945
+ # name=datasets.Split.VALIDATION,
946
+ # gen_kwargs={
947
+ # "images": downloaded_files["val"]["images"],
948
+ # "index": downloaded_files["val"]["index"],
949
+ # "split": datasets.Split.VALIDATION,
950
+ # },
951
+ # ))
952
 
953
  return SplitGenerator_list
954
 
955
+ def _generate_examples(self, images, index, few_shot, split):
956
+
957
+ if few_shot is not None:
958
+ few_shot_images = []
959
+ with open(few_shot, encoding="utf-8") as f:
960
+ data = json.load(f)
961
+ for item in data:
962
+ few_shot_images.append(item['id'].split('@')[-1])
963
 
964
  if self.config.name in ["kitti-distance", "resisc45_clip", "hateful-memes"]:
965
  with open(index, encoding="utf-8") as f:
966
  data = json.load(f)
967
  for i in range(len(data['images'])):
968
  label = data['annotations'][i]['category_id']
969
+ path_temp = data['images'][i]['file_name'].split('@')[1]
970
+ path = os.path.join(images, path_temp)
971
+
972
+ if few_shot is not None:
973
+ if path_temp in few_shot_images:
974
+ yield i, {
975
+ "image_file_path": path,
976
+ "image": path,
977
+ "labels": label,
978
+ }
979
+ else:
980
+ yield i, {
981
+ "image_file_path": path,
982
+ "image": path,
983
+ "labels": label,
984
+ }
985
 
986
  else:
987
  with open(index, "r") as f:
991
 
992
  if len(line_split) > 3:
993
  image_path_temp = " ".join(line_split[:-1])
994
+ path_temp = image_path_temp.split('@')[1]
995
  else:
996
+ path_temp = line_split[0].split('@')[1]
997
 
998
+ path = os.path.join(images, path_temp)
999
+
1000
  if self.config.type == "classification_multilabel":
1001
  label = [int(x) for x in line_split[-1].split(',')]
1002
  else:
1005
  except:
1006
  if self.config.name == "eurosat_clip" and split == 'test':
1007
  label = 9
1008
+ if few_shot is not None:
1009
+ if path_temp in few_shot_images:
1010
+ yield i, {
1011
+ "image_file_path": path,
1012
+ "image": path,
1013
+ "labels": label,
1014
+ }
1015
+ else:
1016
+ pass
1017
+ else:
1018
+ yield i, {
1019
+ "image_file_path": path,
1020
+ "image": path,
1021
+ "labels": label,
1022
+ }