modified dataset building script to load embeddings as List[Value(float)] -> ds.to_pandas() compatible
Browse files- dcase23-task2-enriched.py +5 -18
dcase23-task2-enriched.py
CHANGED
@@ -298,15 +298,6 @@ class DCASE2023Task2DatasetConfig(datasets.BuilderConfig):
|
|
298 |
raise NotImplementedError
|
299 |
|
300 |
if type(data) == datasets.Dataset:
|
301 |
-
# remove embedding columns first -> throws error in .to_pandas()
|
302 |
-
embeddings = {}
|
303 |
-
emb_features = [key for key, val in data.features.items() if type(val) == datasets.Array2D]
|
304 |
-
if len(emb_features) > 0:
|
305 |
-
embeddings = {
|
306 |
-
key: [np.asarray(emb).reshape(-1,) for emb in data[key].copy()] for key in emb_features
|
307 |
-
}
|
308 |
-
data = data.remove_columns(emb_features)
|
309 |
-
|
310 |
# retrieve split
|
311 |
df = data.to_pandas()
|
312 |
df["split"] = data.split._name if "+" not in data.split._name else df["path"].map(get_split)
|
@@ -315,10 +306,6 @@ class DCASE2023Task2DatasetConfig(datasets.BuilderConfig):
|
|
315 |
# get clearnames for classes
|
316 |
class_names = data.features["class"].names
|
317 |
df["class_name"] = df["class"].apply(lambda x: class_names[x])
|
318 |
-
|
319 |
-
# append embeddings
|
320 |
-
for emb_name, emb_list in embeddings.items():
|
321 |
-
df[emb_name] = emb_list
|
322 |
elif type(data) == pd.DataFrame:
|
323 |
df = data
|
324 |
else:
|
@@ -341,7 +328,7 @@ class DCASE2023Task2Dataset(datasets.GeneratorBasedBuilder):
|
|
341 |
"""Dataset for the DCASE 2023 Challenge Task 2 "First-Shot Unsupervised Anomalous Sound Detection
|
342 |
for Machine Condition Monitoring"."""
|
343 |
|
344 |
-
VERSION = datasets.Version("0.0.
|
345 |
|
346 |
DEFAULT_CONFIG_NAME = "dev"
|
347 |
|
@@ -365,7 +352,7 @@ class DCASE2023Task2Dataset(datasets.GeneratorBasedBuilder):
|
|
365 |
features = {
|
366 |
"audio": datasets.Audio(sampling_rate=16_000),
|
367 |
"path": datasets.Value("string"),
|
368 |
-
"section": datasets.Value("
|
369 |
"domain": datasets.ClassLabel(num_classes=2, names=["source", "target"]),
|
370 |
"label": datasets.ClassLabel(num_classes=_NUM_TARGETS, names=_TARGET_NAMES),
|
371 |
"class": datasets.ClassLabel(num_classes=_NUM_CLASSES, names=_CLASS_NAMES),
|
@@ -378,7 +365,7 @@ class DCASE2023Task2Dataset(datasets.GeneratorBasedBuilder):
|
|
378 |
}
|
379 |
if self.config.embeddings_urls is not None:
|
380 |
features.update({
|
381 |
-
emb_name: datasets.
|
382 |
})
|
383 |
features = datasets.Features(features)
|
384 |
|
@@ -408,7 +395,7 @@ class DCASE2023Task2Dataset(datasets.GeneratorBasedBuilder):
|
|
408 |
audio_path[split] = dl_manager.download(self.config.data_urls[split])
|
409 |
local_extracted_archive[split] = dl_manager.extract(
|
410 |
audio_path[split]) if not dl_manager.is_streaming else None
|
411 |
-
if self.config.embeddings_urls is not None
|
412 |
for emb_name, emb_data in self.config.embeddings_urls.items():
|
413 |
downloaded_embeddings = dl_manager.download(emb_data[split])
|
414 |
embeddings[split][emb_name] = np.load(downloaded_embeddings, allow_pickle=True)["arr_0"].item()
|
@@ -447,7 +434,7 @@ class DCASE2023Task2Dataset(datasets.GeneratorBasedBuilder):
|
|
447 |
result = {field: None for field in data_fields}
|
448 |
result.update(metadata[metadata["path"] == lookup].T.squeeze().to_dict())
|
449 |
for emb_key in embeddings.keys():
|
450 |
-
result[emb_key] = embeddings[emb_key][lookup]
|
451 |
result["path"] = path
|
452 |
yield id_, {**result, "audio": audio}
|
453 |
id_ += 1
|
|
|
298 |
raise NotImplementedError
|
299 |
|
300 |
if type(data) == datasets.Dataset:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
# retrieve split
|
302 |
df = data.to_pandas()
|
303 |
df["split"] = data.split._name if "+" not in data.split._name else df["path"].map(get_split)
|
|
|
306 |
# get clearnames for classes
|
307 |
class_names = data.features["class"].names
|
308 |
df["class_name"] = df["class"].apply(lambda x: class_names[x])
|
|
|
|
|
|
|
|
|
309 |
elif type(data) == pd.DataFrame:
|
310 |
df = data
|
311 |
else:
|
|
|
328 |
"""Dataset for the DCASE 2023 Challenge Task 2 "First-Shot Unsupervised Anomalous Sound Detection
|
329 |
for Machine Condition Monitoring"."""
|
330 |
|
331 |
+
VERSION = datasets.Version("0.0.4")
|
332 |
|
333 |
DEFAULT_CONFIG_NAME = "dev"
|
334 |
|
|
|
352 |
features = {
|
353 |
"audio": datasets.Audio(sampling_rate=16_000),
|
354 |
"path": datasets.Value("string"),
|
355 |
+
"section": datasets.Value("uint32"),
|
356 |
"domain": datasets.ClassLabel(num_classes=2, names=["source", "target"]),
|
357 |
"label": datasets.ClassLabel(num_classes=_NUM_TARGETS, names=_TARGET_NAMES),
|
358 |
"class": datasets.ClassLabel(num_classes=_NUM_CLASSES, names=_CLASS_NAMES),
|
|
|
365 |
}
|
366 |
if self.config.embeddings_urls is not None:
|
367 |
features.update({
|
368 |
+
emb_name: [datasets.Value(emb["dtype"])] for emb_name, emb in self.config.embeddings_urls.items()
|
369 |
})
|
370 |
features = datasets.Features(features)
|
371 |
|
|
|
395 |
audio_path[split] = dl_manager.download(self.config.data_urls[split])
|
396 |
local_extracted_archive[split] = dl_manager.extract(
|
397 |
audio_path[split]) if not dl_manager.is_streaming else None
|
398 |
+
if self.config.embeddings_urls is not None:
|
399 |
for emb_name, emb_data in self.config.embeddings_urls.items():
|
400 |
downloaded_embeddings = dl_manager.download(emb_data[split])
|
401 |
embeddings[split][emb_name] = np.load(downloaded_embeddings, allow_pickle=True)["arr_0"].item()
|
|
|
434 |
result = {field: None for field in data_fields}
|
435 |
result.update(metadata[metadata["path"] == lookup].T.squeeze().to_dict())
|
436 |
for emb_key in embeddings.keys():
|
437 |
+
result[emb_key] = np.asarray(embeddings[emb_key][lookup]).squeeze().tolist()
|
438 |
result["path"] = path
|
439 |
yield id_, {**result, "audio": audio}
|
440 |
id_ += 1
|