CoolFace
Datasetpublic

Khahn-nh/GTZAN-Dataset-Music-Genre-Classification

Hướng dẫn sử dụng Dataset GTZAN cho Model Team 1. Thành phần bàn giao Dataset trên Hugging Face: Tài nguyên đi kèm: [stats.json] , [label_map.json] 2. Cách Load Dataset từ Hugging Face Dữ liệu đã chia sẵn thành 3 tập: train, validation và test theo tỷ lệ chuẩn, đảm bảo Zero-Leakage (các đoạn cắt từ cùng một bài hát gốc sẽ nằm chung trong một tập). from datasets import load_dataset # Thay token bằng Hugging Face Token của bạn HF_TOKEN =… See the full description on the dataset page: https://huggingface.co/datasets/Khahn-nh/GTZAN-Dataset-Music-Genre-Classification.

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes74downloads
Dataset Card

Hướng dẫn sử dụng Dataset GTZAN cho Model Team

1. Thành phần bàn giao

  • —Dataset trên Hugging Face:
  • —Tài nguyên đi kèm: [stats.json] , [label_map.json]

2. Cách Load Dataset từ Hugging Face

Dữ liệu đã chia sẵn thành 3 tập: train, validation và test theo tỷ lệ chuẩn, đảm bảo Zero-Leakage (các đoạn cắt từ cùng một bài hát gốc sẽ nằm chung trong một tập).

python
from datasets import load_dataset

# Thay token bằng Hugging Face Token của bạn
HF_TOKEN = "your_hf_token_here"
REPO_ID = "Khahn-nh/GTZAN-Dataset-Music-Genre-Classification"

# Load toàn bộ DatasetDict
dataset = load_dataset(REPO_ID, token=HF_TOKEN)

# Truy cập các tập
train_ds = dataset["train"]
val_ds = dataset["validation"]
test_ds = dataset["test"]

print(f"Train size: {len(train_ds)}") # ~12,794 samples

3. Cách Chuẩn Hóa (Normalization) bằng PyTorch DataLoader

Dữ liệu trên Hugging Face lưu Ma trận Log-Mel Spectrogram thô (chưa chuẩn hóa) với shape (128, 300). Cần dùng file [stats.json] để chuẩn hóa (Z-score Normalization) on-the-fly trong DataLoader. Dưới đây là code mẫu:

python
import json
import torch
from torch.utils.data import Dataset, DataLoader

class GTZANDataset(Dataset):
    def __init__(self, hf_dataset, stats_path="data/stats.json"):
        self.hf_dataset = hf_dataset
        
        # Load stats for Normalization
        with open(stats_path, "r") as f:
            stats = json.load(f)
            
        # Reshape to (128, 1) to broadcast across time dimension (300 frames)
        self.mean = torch.tensor(stats["mean"]).view(-1, 1)
        self.std = torch.tensor(stats["std"]).view(-1, 1)

    def __len__(self):
        return len(self.hf_dataset)

    def __getitem__(self, idx):
        item = self.hf_dataset[idx]
        
        # Lấy Mel-spectrogram tensor
        mel = torch.tensor(item["mel"], dtype=torch.float32)
        
        # Áp dụng chuẩn hóa Z-score dọc theo trục thời gian
        mel_normalized = (mel - self.mean) / self.std
        
        # Thêm channel dimension (1, 128, 300) vì hầu hết model CV (như CNN/ResNet) cần
        mel_normalized = mel_normalized.unsqueeze(0)
        
        label = torch.tensor(item["label"], dtype=torch.long)
        
        return mel_normalized, label

# Tạo DataLoader
train_loader = DataLoader(
    GTZANDataset(dataset["train"]), 
    batch_size=32, 
    shuffle=True, 
    num_workers=4
)

# Chạy thử
first_batch_mels, first_batch_labels = next(iter(train_loader))
print("Batch shape:", first_batch_mels.shape) # Output: torch.Size([32, 1, 128, 300])

4. Sử dụng Label Map

Dùng label_map.json khi cần in ra Tên thể loại (Ví dụ: để làm Confusion Matrix hoặc Inference Result).

python
# Đọc mapping
with open("data/label_map.json", "r") as f:
    label_map = json.load(f)

# Đảo ngược mapping: từ ID -> Name
id_to_genre = {v: k for k, v in label_map.items()}

# In thử
sample_id = 4
print(f"ID {sample_id} tương ứng với thể loại: {id_to_genre[sample_id]}")
# Output: ID 4 tương ứng với thể loại: hiphop