shunk031 commited on
Commit
492c23f
1 Parent(s): 69f1bab

download annotation if auth token exists (#11)

Browse files

* download annotation if auth token exists

* fix for CI

Files changed (2) hide show
  1. COCOA.py +28 -9
  2. tests/COCOA_test.py +13 -1
COCOA.py CHANGED
@@ -70,6 +70,10 @@ _URLS = {
70
  "BSDS": {
71
  "images": "http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/BSR/BSR_bsds500.tgz",
72
  },
 
 
 
 
73
  }
74
 
75
 
@@ -334,7 +338,7 @@ class CocoaDataset(ds.GeneratorBasedBuilder):
334
  return ann_json
335
 
336
  @property
337
- def manual_download_instructions(self) -> str:
338
  return (
339
  "To use COCOA, you need to download the annotations "
340
  "from the google drive in the official repositories "
@@ -489,10 +493,11 @@ class CocoaDataset(ds.GeneratorBasedBuilder):
489
  ),
490
  ]
491
 
492
- def _split_generators(self, dl_manager: ds.DownloadManager):
493
- file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
494
- image_dirs = file_paths["images"] # type: ignore
495
 
 
496
  assert dl_manager.manual_dir is not None, dl_manager.manual_dir
497
  data_path = os.path.expanduser(dl_manager.manual_dir)
498
 
@@ -501,7 +506,7 @@ class CocoaDataset(ds.GeneratorBasedBuilder):
501
  f"{data_path} does not exists. Make sure you insert a manual dir "
502
  'via `datasets.load_dataset("shunk031/COCOA", data_dir=...)` '
503
  "that includes tar/untar files from the COCOA annotation tar.gz. "
504
- f"Manual download instructions: {self.manual_download_instructions}"
505
  )
506
  else:
507
  data_path = (
@@ -509,16 +514,30 @@ class CocoaDataset(ds.GeneratorBasedBuilder):
509
  if not os.path.isdir(data_path)
510
  else data_path
511
  )
 
 
 
 
 
 
 
 
 
 
512
 
513
  assert isinstance(data_path, str)
514
  ann_dir = os.path.join(data_path, "annotations")
515
 
516
  if self.config.name == "COCO":
517
- return self._split_generators_coco(ann_dir=ann_dir, image_dirs=image_dirs)
518
-
 
 
519
  elif self.config.name == "BSDS":
520
- return self._split_generators_bsds(ann_dir=ann_dir, image_dir=image_dirs)
521
-
 
 
522
  else:
523
  raise ValueError(f"Invalid name: {self.config.name}")
524
 
 
70
  "BSDS": {
71
  "images": "http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/BSR/BSR_bsds500.tgz",
72
  },
73
+ # The author of this loading script has uploaded the annotation files to the HuggingFace's private repository to facilitate testing.
74
+ # If you are using this loading script, please download the annotations from the appropriate channels, such as the Google Drive link provided by the COCOA's author.
75
+ # (To the author of COCOA, if there are any issues regarding this matter, please contact us. We will address it promptly.)
76
+ "annotations": "https://huggingface.co/datasets/shunk031/COCOA-annotation/resolve/main/annotations.tar.gz",
77
  }
78
 
79
 
 
338
  return ann_json
339
 
340
  @property
341
+ def _manual_download_instructions(self) -> str:
342
  return (
343
  "To use COCOA, you need to download the annotations "
344
  "from the google drive in the official repositories "
 
493
  ),
494
  ]
495
 
496
+ def _download_annotation_from_hf(self, dl_manager: ds.DownloadManager) -> str:
497
+ data_path = dl_manager.download_and_extract(_URLS["annotations"])
498
+ return data_path # type: ignore
499
 
500
+ def _download_annotation_from_local(self, dl_manager: ds.DownloadManager) -> str:
501
  assert dl_manager.manual_dir is not None, dl_manager.manual_dir
502
  data_path = os.path.expanduser(dl_manager.manual_dir)
503
 
 
506
  f"{data_path} does not exists. Make sure you insert a manual dir "
507
  'via `datasets.load_dataset("shunk031/COCOA", data_dir=...)` '
508
  "that includes tar/untar files from the COCOA annotation tar.gz. "
509
+ f"Manual download instructions: {self._manual_download_instructions}"
510
  )
511
  else:
512
  data_path = (
 
514
  if not os.path.isdir(data_path)
515
  else data_path
516
  )
517
+ return data_path # type: ignore
518
+
519
+ def _split_generators(self, dl_manager: ds.DownloadManager):
520
+ urls = _URLS[self.config.name]
521
+ image_dirs = dl_manager.download_and_extract(urls["images"]) # type: ignore
522
+
523
+ if dl_manager.download_config.token:
524
+ data_path = self._download_annotation_from_hf(dl_manager)
525
+ else:
526
+ data_path = self._download_annotation_from_local(dl_manager)
527
 
528
  assert isinstance(data_path, str)
529
  ann_dir = os.path.join(data_path, "annotations")
530
 
531
  if self.config.name == "COCO":
532
+ return self._split_generators_coco(
533
+ ann_dir=ann_dir,
534
+ image_dirs=image_dirs, # type: ignore
535
+ )
536
  elif self.config.name == "BSDS":
537
+ return self._split_generators_bsds(
538
+ ann_dir=ann_dir,
539
+ image_dir=image_dirs, # type: ignore
540
+ )
541
  else:
542
  raise ValueError(f"Invalid name: {self.config.name}")
543
 
tests/COCOA_test.py CHANGED
@@ -18,6 +18,13 @@ def data_dir() -> str:
18
  return "annotations.tar.gz"
19
 
20
 
 
 
 
 
 
 
 
21
  @pytest.mark.parametrize(
22
  argnames="decode_rle,",
23
  argvalues=(False, True),
@@ -42,9 +49,14 @@ def test_load_dataset(
42
  expected_num_validation: int,
43
  expected_num_test: int,
44
  decode_rle: bool,
 
45
  ):
46
  dataset = ds.load_dataset(
47
- path=dataset_path, name=dataset_name, data_dir=data_dir, decode_rle=decode_rle
 
 
 
 
48
  )
49
 
50
  assert dataset["train"].num_rows == expected_num_train # type: ignore
 
18
  return "annotations.tar.gz"
19
 
20
 
21
+ @pytest.mark.parametrize(
22
+ argnames="use_auth_token",
23
+ argvalues=(
24
+ True,
25
+ False,
26
+ ),
27
+ )
28
  @pytest.mark.parametrize(
29
  argnames="decode_rle,",
30
  argvalues=(False, True),
 
49
  expected_num_validation: int,
50
  expected_num_test: int,
51
  decode_rle: bool,
52
+ use_auth_token: bool,
53
  ):
54
  dataset = ds.load_dataset(
55
+ path=dataset_path,
56
+ name=dataset_name,
57
+ data_dir=data_dir if not use_auth_token else None,
58
+ decode_rle=decode_rle,
59
+ token=use_auth_token,
60
  )
61
 
62
  assert dataset["train"].num_rows == expected_num_train # type: ignore