yangwang825 commited on
Commit
84c78dc
·
verified ·
1 Parent(s): 845fdeb

Update audioset.py

Browse files
Files changed (1) hide show
  1. audioset.py +44 -6
audioset.py CHANGED
@@ -6,6 +6,7 @@
6
  import os
7
  import json
8
  import gzip
 
9
  import shutil
10
  import pathlib
11
  import logging
@@ -126,14 +127,25 @@ class AudioSet(datasets.GeneratorBasedBuilder):
126
  _output_file = os.path.join(HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, concat_zip_filename)
127
 
128
  if not os.path.exists(_output_file):
129
- for zip_type in ['zip', 'z01', 'z02']:
 
130
  _UNBALANCED_TRAIN_FILENAME = f'unbalanced_train_segments_{partxx}_partial.{zip_type}'
131
  zip_file_url = f'https://huggingface.co/datasets/confit/audioset/resolve/main/unbalanced/{_UNBALANCED_TRAIN_FILENAME}'
132
  _save_path = os.path.join(
133
- HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, _UNBALANCED_TRAIN_FILENAME
 
 
 
 
134
  )
135
- download_file(zip_file_url, _save_path)
136
  logger.info(f"`{_UNBALANCED_TRAIN_FILENAME}` is downloaded to {_save_path}")
 
 
 
 
 
 
 
137
  logger.info(f"Reassembling {_output_file}")
138
  os.system(f"zip -q -F {_input_file} --out {_output_file}")
139
  part_zip_files = os.path.join(
@@ -141,8 +153,9 @@ class AudioSet(datasets.GeneratorBasedBuilder):
141
  )
142
  os.system(f"rm -rf {part_zip_files}")
143
 
144
- train_archive_path = dl_manager.extract(_output_file)
145
- logger.info(f"`{concat_zip_filename}` is downloaded to {train_archive_path}")
 
146
 
147
  if self.config.name == 'balanced':
148
  zip_file_url = f'https://huggingface.co/datasets/confit/audioset/resolve/main/{_EVAL_FILENAME}'
@@ -310,7 +323,32 @@ def download_file(
310
  if unpack:
311
  if dest_unpack is None:
312
  dest_unpack = os.path.dirname(dest)
313
- logger.info(f"Extracting {dest} to {dest_unpack}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  # shutil unpack_archive does not work with tar.gz files
315
  if (
316
  source.endswith(".tar.gz")
 
6
  import os
7
  import json
8
  import gzip
9
+ import joblib
10
  import shutil
11
  import pathlib
12
  import logging
 
127
  _output_file = os.path.join(HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, concat_zip_filename)
128
 
129
  if not os.path.exists(_output_file):
130
+
131
+ def _download(zip_type):
132
  _UNBALANCED_TRAIN_FILENAME = f'unbalanced_train_segments_{partxx}_partial.{zip_type}'
133
  zip_file_url = f'https://huggingface.co/datasets/confit/audioset/resolve/main/unbalanced/{_UNBALANCED_TRAIN_FILENAME}'
134
  _save_path = os.path.join(
135
+ HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION
136
+ )
137
+ download_file(
138
+ source=zip_file_url,
139
+ dest=os.path.join(_save_path, _UNBALANCED_TRAIN_FILENAME)
140
  )
 
141
  logger.info(f"`{_UNBALANCED_TRAIN_FILENAME}` is downloaded to {_save_path}")
142
+
143
+ joblib.Parallel(n_jobs=4, verbose=10)(
144
+ joblib.delayed(_download)(
145
+ zip_type=zip_type
146
+ ) for zip_type in ['zip', 'z01', 'z02']
147
+ )
148
+
149
  logger.info(f"Reassembling {_output_file}")
150
  os.system(f"zip -q -F {_input_file} --out {_output_file}")
151
  part_zip_files = os.path.join(
 
153
  )
154
  os.system(f"rm -rf {part_zip_files}")
155
 
156
+ dest = os.path.join(HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, 'extracted')
157
+ unpack(_output_file, dest)
158
+ logger.info(f"`{concat_zip_filename}` is downloaded to {dest}")
159
 
160
  if self.config.name == 'balanced':
161
  zip_file_url = f'https://huggingface.co/datasets/confit/audioset/resolve/main/{_EVAL_FILENAME}'
 
323
  if unpack:
324
  if dest_unpack is None:
325
  dest_unpack = os.path.dirname(dest)
326
+ if os.path.exists(dest_unpack):
327
+ logger.info(f"{dest_unpack} already exists. Skipping extraction")
328
+ else:
329
+ logger.info(f"Extracting {dest} to {dest_unpack}")
330
+ # shutil unpack_archive does not work with tar.gz files
331
+ if (
332
+ source.endswith(".tar.gz")
333
+ or source.endswith(".tgz")
334
+ or source.endswith(".gz")
335
+ ):
336
+ out = dest.replace(".gz", "")
337
+ with gzip.open(dest, "rb") as f_in:
338
+ with open(out, "wb") as f_out:
339
+ shutil.copyfileobj(f_in, f_out)
340
+ else:
341
+ shutil.unpack_archive(dest, dest_unpack)
342
+ if write_permissions:
343
+ set_writing_permissions(dest_unpack)
344
+
345
+
346
+ def unpack(source, dest):
347
+ dest_unpack = os.path.dirname(dest)
348
+ if os.path.exists(dest_unpack):
349
+ logger.info(f"{dest_unpack} already exists. Skipping extraction")
350
+ else:
351
+ logger.info(f"Extracting {source} to {dest_unpack}")
352
  # shutil unpack_archive does not work with tar.gz files
353
  if (
354
  source.endswith(".tar.gz")