Dataset Viewer
Auto-converted to Parquet Duplicate
mel
array 2D
label
class label
10 classes
track_id
int64
1
999
artist_id
int64
1
999
genre
stringclasses
10 values
[[18.691486358642578,19.823274612426758,19.663578033447266,18.12021255493164,21.89167022705078,23.53(...TRUNCATED)
0blues
1
1
blues
[[22.1632137298584,23.166370391845703,21.60466766357422,21.952573776245117,23.056150436401367,23.044(...TRUNCATED)
0blues
1
1
blues
[[20.809675216674805,20.333791732788086,19.95160484313965,19.31183624267578,19.0899600982666,19.0928(...TRUNCATED)
0blues
1
1
blues
[[23.831939697265625,22.99634552001953,22.376882553100586,23.545141220092773,23.38983917236328,22.32(...TRUNCATED)
0blues
1
1
blues
[[21.01511573791504,21.587732315063477,22.193378448486328,21.72208023071289,21.962921142578125,21.74(...TRUNCATED)
0blues
1
1
blues
[[17.45304298400879,17.790063858032227,17.384002685546875,17.50863265991211,19.699169158935547,20.65(...TRUNCATED)
0blues
1
1
blues
[[19.863630294799805,20.53801918029785,19.212759017944336,19.360570907592773,19.128219604492188,16.7(...TRUNCATED)
0blues
1
1
blues
[[16.269180297851562,19.703779220581055,20.508909225463867,19.818984985351562,18.578777313232422,19.(...TRUNCATED)
0blues
1
1
blues
[[17.831260681152344,19.445446014404297,21.983598709106445,23.608417510986328,23.963531494140625,24.(...TRUNCATED)
0blues
1
1
blues
[[16.486827850341797,17.982919692993164,17.33139991760254,17.336341857910156,18.947126388549805,17.9(...TRUNCATED)
0blues
1
1
blues
End of preview. Expand in Data Studio

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, validationtest 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 = "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:

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).

# Đọ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
Downloads last month
32