MELD_Audio_3Labels / MELD_Audio_3Labels.py
EdwardHayashi-2023
Create MELD_Audio_3Labels.py
ec03453
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 25 13:21:54 2023
@author: lin.kinwahedward
"""
#------------------------------------------------------------------------------
# Standard Libraries
import datasets
import csv
#------------------------------------------------------------------------------
"""The Audio, Speech, and Vision Processing Lab - Emotional Sound Database (ASVP - ESD)"""
_CITATION = """\
@article{poria2018meld,
title={Meld: A multimodal multi-party dataset for emotion recognition in conversations},
author={Poria, Soujanya and Hazarika, Devamanyu and Majumder, Navonil and Naik, Gautam and Cambria, Erik and Mihalcea, Rada},
journal={arXiv preprint arXiv:1810.02508},
year={2018}
}
@article{chen2018emotionlines,
title={Emotionlines: An emotion corpus of multi-party conversations},
author={Chen, Sheng-Yeh and Hsu, Chao-Chun and Kuo, Chuan-Chun and Ku, Lun-Wei and others},
journal={arXiv preprint arXiv:1802.08379},
year={2018}
}
"""
_DESCRIPTION = """\
Multimodal EmotionLines Dataset (MELD) has been created by enhancing and extending EmotionLines dataset.
MELD contains the same dialogue instances available in EmotionLines, but it also encompasses audio and
visual modality along with text. MELD has more than 1400 dialogues and 13000 utterances from Friends TV series.
Multiple speakers participated in the dialogues. Each utterance in a dialogue has been labeled by any of these
seven emotions -- Anger, Disgust, Sadness, Joy, Neutral, Surprise and Fear. MELD also has sentiment (positive,
negative and neutral) annotation for each utterance.
This dataset is slightly modified, so that it concentrates on Emotion recognition in audio input only.
"""
_HOMEPAGE = "https://affective-meld.github.io/"
_LICENSE = "CC BY 4.0"
# The actual place where the data is stored!
_DATA_URL = "https://drive.google.com/uc?export=download&id=1J8wBcuXD-E98k3Ls3oE59xT7Qd6m1qjY"
#------------------------------------------------------------------------------
# Define Dataset Configuration (e.g., subset of dataset, but it is not used here.)
class DS_Config(datasets.BuilderConfig):
#--------------------------------------------------------------------------
def __init__(self, name, description, homepage, data_url):
super(DS_Config, self).__init__(
name = self.name,
version = datasets.Version("1.0.0"),
description = self.description,
)
self.name = name
self.description = description
self.homepage = homepage
self.data_url = data_url
#------------------------------------------------------------------------------
# Define Dataset Class
class MELD_Audio_3Labels(datasets.GeneratorBasedBuilder):
#--------------------------------------------------------------------------
BUILDER_CONFIGS = [DS_Config(
name = "MELD_Audio_3Labels",
description = _DESCRIPTION,
homepage = _HOMEPAGE,
data_url = _DATA_URL
)]
#--------------------------------------------------------------------------
'''
Define the "column header" (feature) of a datum.
2 Features:
1) audio samples
2) emotion label
'''
def _info(self):
features = datasets.Features(
{
"audio": datasets.Audio(sampling_rate = 16000),
"label": datasets.ClassLabel(
names = [
"neutral",
"joy",
"anger"
])
}
)
# return dataset info and data feature info
return datasets.DatasetInfo(
description = _DESCRIPTION,
features = features,
homepage = _HOMEPAGE,
citation = _CITATION,
)
#--------------------------------------------------------------------------
def _split_generators(self, dl_manager):
'''
Split the dataset into datasets.Split.{"TRAIN", "VALIDATION", "TEST", "ALL"}
The dataset can be further modified, please see below link for details.
https://huggingface.co/docs/datasets/process
'''
# Get the dataset and store at the machine where this script is executed!
dataset_path = dl_manager.download_and_extract(self.config.data_url)
# "audio_path" and "csv_path" would be the parameters passed to def _generate_examples()
return [
datasets.SplitGenerator(
name = datasets.Split.TRAIN,
gen_kwargs = {"audio_path": dataset_path + "/MELD_Audio_3Labels/train/",
"csv_path": dataset_path + "/MELD_Audio_3Labels/train.csv"
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs = {"audio_path": dataset_path + "/MELD_Audio_3Labels/dev/",
"csv_path": dataset_path + "/MELD_Audio_3Labels/dev.csv"
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs = {"audio_path": dataset_path + "/MELD_Audio_3Labels/test/",
"csv_path": dataset_path + "/MELD_Audio_3Labels/test.csv"
},
),
]
#--------------------------------------------------------------------------
def _generate_examples(self, audio_path, csv_path):
'''
Get the audio file and set the corresponding labels
Must execute till yield, otherwise, error will occur!
'''
key = 0
with open(csv_path, encoding = "utf-8") as csv_file:
csv_reader = csv.reader(csv_file, delimiter = ",", skipinitialspace=True)
next(csv_reader)
for row in csv_reader:
_, _, _, emotion, _, dialogue_id, utterance_id, _, _, _, _ = row
filename = "dia" + dialogue_id + "_utt" + utterance_id + ".mp3"
yield key, {
# huggingface dataset's will use soundfile to read the audio file
"audio": audio_path + filename,
"label": emotion,
}
key += 1
#------------------------------------------------------------------------------