thomasw21 commited on
Commit
59af408
·
1 Parent(s): 3802b20

Add support for multiprocessing for visual genome

Browse files
Files changed (1) hide show
  1. pmd.py +86 -46
pmd.py CHANGED
@@ -17,7 +17,9 @@ import json
17
  import re
18
  import sqlite3
19
  from abc import abstractmethod
 
20
  from hashlib import md5
 
21
  from pathlib import Path
22
  from typing import Any, Dict, List, Optional
23
 
@@ -26,7 +28,7 @@ from urllib.parse import unquote_plus
26
 
27
  # TODO: @thomasw21
28
  import datasets
29
- from datasets import load_dataset
30
  from langdetect import detect
31
 
32
  _CITATION = """"""
@@ -84,11 +86,13 @@ class DatasetsLoader(BaseLoader):
84
  dataset_name: str,
85
  config_name: Optional[str],
86
  split: str,
 
87
  batch_size: int = 1000,
88
  ):
89
  super(DatasetsLoader, self).__init__(source=dataset_name, split=split)
90
  self.dataset_name = dataset_name
91
  self.config_name = config_name
 
92
  self.batch_size = batch_size
93
 
94
  @abstractmethod
@@ -105,16 +109,38 @@ class DatasetsLoader(BaseLoader):
105
  for i in range(batch_size)
106
  ]
107
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  def _generate_examples(self):
109
  dataset = load_dataset(self.dataset_name, self.config_name, split=self.split)
110
  dataset_size = len(dataset)
 
111
  # load batches and yield individual rows
112
- for batch_start in range(0, dataset_size, self.batch_size):
113
- batch_end = min(batch_start + self.batch_size, dataset_size)
114
- batch = dataset[batch_start:batch_end]
115
- rows = self.convert_batch_to_list_of_rows(batch)
116
- for row in rows:
117
- rows_casted_pmd_features = self.cast_to_pmd_features(row)
 
 
 
 
 
 
 
 
 
118
  for row_casted_pmd_features in rows_casted_pmd_features:
119
  yield row_casted_pmd_features
120
 
@@ -211,6 +237,7 @@ class SBULoader(DatasetsLoader):
211
  config_name=None,
212
  split=split,
213
  batch_size=batch_size,
 
214
  )
215
 
216
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
@@ -426,12 +453,13 @@ class LocalizedNarrativesADE20kLoader(BaseLoaderWithDLManager):
426
 
427
 
428
  class VisualGenomeLoader(DatasetsLoader):
429
- def __init__(self, split: str, batch_size: int = 1000):
430
  super(VisualGenomeLoader, self).__init__(
431
  dataset_name="visual_genome",
432
  config_name="region_descriptions_v1.2.0",
433
  split=split,
434
  batch_size=batch_size,
 
435
  )
436
 
437
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
@@ -468,6 +496,7 @@ class WITLoader(DatasetsLoader):
468
  config_name=None,
469
  split=split,
470
  batch_size=batch_size,
 
471
  )
472
 
473
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
@@ -502,6 +531,7 @@ class ConceptualCaptions(DatasetsLoader):
502
  config_name="unlabeled",
503
  split=split,
504
  batch_size=batch_size,
 
505
  )
506
 
507
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
@@ -528,6 +558,7 @@ class Conceptual12MLoader(DatasetsLoader):
528
  config_name=None,
529
  split=split,
530
  batch_size=batch_size,
 
531
  )
532
 
533
  def cast_to_pmd_features(self, row):
@@ -554,6 +585,7 @@ class RedCapsLoader(DatasetsLoader):
554
  config_name="all",
555
  split=split,
556
  batch_size=batch_size,
 
557
  )
558
 
559
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
@@ -732,15 +764,22 @@ class PMDConfig(datasets.BuilderConfig):
732
 
733
  def __init__(
734
  self,
 
735
  datasets_batch_size: int = 1000,
736
  sqlite3_batch_size: int = 10_000,
737
  **kwargs,
738
  ):
 
 
 
739
  super(PMDConfig, self).__init__(**kwargs)
740
  # determines how much we can load
741
  self.datasets_batch_size = datasets_batch_size
742
  self.sqlite3_batch_size = sqlite3_batch_size
743
 
 
 
 
744
 
745
  class PMD(datasets.GeneratorBasedBuilder):
746
  """Builder for Open Images subset of PMD."""
@@ -762,48 +801,49 @@ class PMD(datasets.GeneratorBasedBuilder):
762
  name=split_name,
763
  gen_kwargs={
764
  "loaders": [
765
- COCOloader(dl_manager=dl_manager, split=split_name),
766
- SBULoader(
767
- split=split_name,
768
- batch_size=self.config.datasets_batch_size,
769
- ),
770
- LocalizedNarrativesOpenImagesLoader(
771
- dl_manager=dl_manager, split=split_name
772
- ),
773
- LocalizedNarrativesCOCOLoader(
774
- dl_manager=dl_manager, split=split_name
775
- ),
776
- LocalizedNarrativesFlickr30kLoader(
777
- dl_manager=dl_manager, split=split_name
778
- ),
779
- LocalizedNarrativesADE20kLoader(
780
- dl_manager=dl_manager, split=split_name
781
- ),
782
- ConceptualCaptions(
783
- split=split_name,
784
- batch_size=self.config.datasets_batch_size,
785
- ),
786
  VisualGenomeLoader(
787
  split=split_name,
788
  batch_size=self.config.datasets_batch_size,
 
789
  ),
790
- WITLoader(
791
- split=split_name,
792
- batch_size=self.config.datasets_batch_size,
793
- ),
794
- Conceptual12MLoader(
795
- split=split_name,
796
- batch_size=self.config.datasets_batch_size,
797
- ),
798
- RedCapsLoader(
799
- split=split_name,
800
- batch_size=self.config.datasets_batch_size,
801
- ),
802
- YFCC100MLoader(
803
- dl_manager=dl_manager,
804
- split=split_name,
805
- batch_size=self.config.sqlite3_batch_size,
806
- ),
807
  ]
808
  },
809
  )
 
17
  import re
18
  import sqlite3
19
  from abc import abstractmethod
20
+ from functools import partial
21
  from hashlib import md5
22
+ from multiprocessing import Pool
23
  from pathlib import Path
24
  from typing import Any, Dict, List, Optional
25
 
 
28
 
29
  # TODO: @thomasw21
30
  import datasets
31
+ from datasets import load_dataset, Dataset
32
  from langdetect import detect
33
 
34
  _CITATION = """"""
 
86
  dataset_name: str,
87
  config_name: Optional[str],
88
  split: str,
89
+ num_proc: int,
90
  batch_size: int = 1000,
91
  ):
92
  super(DatasetsLoader, self).__init__(source=dataset_name, split=split)
93
  self.dataset_name = dataset_name
94
  self.config_name = config_name
95
+ self.num_proc = num_proc
96
  self.batch_size = batch_size
97
 
98
  @abstractmethod
 
109
  for i in range(batch_size)
110
  ]
111
 
112
+ def _generate_rows_casted_pmd_features(self, dset: Dataset, batch_start: int):
113
+ dataset_size = len(dset)
114
+ batch_end = min(batch_start + self.batch_size, dataset_size)
115
+ batch = dset[batch_start:batch_end]
116
+ rows = self.convert_batch_to_list_of_rows(batch)
117
+
118
+ rows_casted_pmd_features = []
119
+ for row in rows:
120
+ rows_casted_pmd_features += self.cast_to_pmd_features(row)
121
+ return rows_casted_pmd_features
122
+
123
+
124
  def _generate_examples(self):
125
  dataset = load_dataset(self.dataset_name, self.config_name, split=self.split)
126
  dataset_size = len(dataset)
127
+
128
  # load batches and yield individual rows
129
+ if self.num_proc == 1:
130
+ for batch_start in range(0, dataset_size, self.batch_size):
131
+ rows_casted_pmd_features = self._generate_rows_casted_pmd_features(dset=dataset, batch_start=batch_start)
132
+ for row_casted_pmd_features in rows_casted_pmd_features:
133
+ yield row_casted_pmd_features
134
+
135
+ # Parallel version
136
+ else:
137
+ assert self.num_proc > 1
138
+ with Pool(self.num_proc) as pool:
139
+ rows_casted_pmd_features = pool.imap(
140
+ partial(self._generate_rows_casted_pmd_features, dset=dataset),
141
+ range(0, dataset_size, self.batch_size),
142
+ chunksize=10
143
+ )
144
  for row_casted_pmd_features in rows_casted_pmd_features:
145
  yield row_casted_pmd_features
146
 
 
237
  config_name=None,
238
  split=split,
239
  batch_size=batch_size,
240
+ num_proc=1
241
  )
242
 
243
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
 
453
 
454
 
455
  class VisualGenomeLoader(DatasetsLoader):
456
+ def __init__(self, split: str, num_proc: int, batch_size: int = 1000):
457
  super(VisualGenomeLoader, self).__init__(
458
  dataset_name="visual_genome",
459
  config_name="region_descriptions_v1.2.0",
460
  split=split,
461
  batch_size=batch_size,
462
+ num_proc=num_proc
463
  )
464
 
465
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
 
496
  config_name=None,
497
  split=split,
498
  batch_size=batch_size,
499
+ num_proc=1
500
  )
501
 
502
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
 
531
  config_name="unlabeled",
532
  split=split,
533
  batch_size=batch_size,
534
+ num_proc=1
535
  )
536
 
537
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
 
558
  config_name=None,
559
  split=split,
560
  batch_size=batch_size,
561
+ num_proc=1
562
  )
563
 
564
  def cast_to_pmd_features(self, row):
 
585
  config_name="all",
586
  split=split,
587
  batch_size=batch_size,
588
+ num_proc=1
589
  )
590
 
591
  def cast_to_pmd_features(self, row: Dict) -> List[Dict[str, Any]]:
 
764
 
765
  def __init__(
766
  self,
767
+ num_proc: Optional[int] = None,
768
  datasets_batch_size: int = 1000,
769
  sqlite3_batch_size: int = 10_000,
770
  **kwargs,
771
  ):
772
+ if num_proc is None:
773
+ # We disable multiprocessing.
774
+ num_proc = 1
775
  super(PMDConfig, self).__init__(**kwargs)
776
  # determines how much we can load
777
  self.datasets_batch_size = datasets_batch_size
778
  self.sqlite3_batch_size = sqlite3_batch_size
779
 
780
+ # Some datasets should be loaded via multiprocessing.
781
+ self.num_proc = num_proc
782
+
783
 
784
  class PMD(datasets.GeneratorBasedBuilder):
785
  """Builder for Open Images subset of PMD."""
 
801
  name=split_name,
802
  gen_kwargs={
803
  "loaders": [
804
+ # COCOloader(dl_manager=dl_manager, split=split_name),
805
+ # SBULoader(
806
+ # split=split_name,
807
+ # batch_size=self.config.datasets_batch_size,
808
+ # ),
809
+ # LocalizedNarrativesOpenImagesLoader(
810
+ # dl_manager=dl_manager, split=split_name
811
+ # ),
812
+ # LocalizedNarrativesCOCOLoader(
813
+ # dl_manager=dl_manager, split=split_name
814
+ # ),
815
+ # LocalizedNarrativesFlickr30kLoader(
816
+ # dl_manager=dl_manager, split=split_name
817
+ # ),
818
+ # LocalizedNarrativesADE20kLoader(
819
+ # dl_manager=dl_manager, split=split_name
820
+ # ),
821
+ # ConceptualCaptions(
822
+ # split=split_name,
823
+ # batch_size=self.config.datasets_batch_size,
824
+ # ),
825
  VisualGenomeLoader(
826
  split=split_name,
827
  batch_size=self.config.datasets_batch_size,
828
+ num_proc=self.config.num_proc
829
  ),
830
+ # WITLoader(
831
+ # split=split_name,
832
+ # batch_size=self.config.datasets_batch_size,
833
+ # ),
834
+ # Conceptual12MLoader(
835
+ # split=split_name,
836
+ # batch_size=self.config.datasets_batch_size,
837
+ # ),
838
+ # RedCapsLoader(
839
+ # split=split_name,
840
+ # batch_size=self.config.datasets_batch_size,
841
+ # ),
842
+ # YFCC100MLoader(
843
+ # dl_manager=dl_manager,
844
+ # split=split_name,
845
+ # batch_size=self.config.sqlite3_batch_size,
846
+ # ),
847
  ]
848
  },
849
  )