<jupyter_start><jupyter_text>Dataset<jupyter_code>le = LabelEncoder()
le.fit(np.unique(train_df.label))
train_df['label_idx'] = le.transform(train_df['label'])
train_df.head()
le.classes_
num_classes = len(le.classes_)
print(num_classes)
np.save('labels.npy', le.classes_)
labels = np.load('labels.npy')
test_df = pd.read_csv('../data/sample_submission.csv')
test_df.head()
def random_crop(y, max_length=176400):
    """音声波形を固定長にそろえる
    
    max_lengthより長かったらランダムに切り取る
    max_lengthより短かったらランダムにパディングする
    """
    if len(y) > max_length:
        max_offset = len(y) - max_length
        offset = np.random.randint(max_offset)
        y = y[offset:max_length + offset]
    else:
        if max_length > len(y):
            max_offset = max_length - len(y)
            offset = np.random.randint(max_offset)
        else:
            offset = 0
        y = np.pad(y, (offset, max_length - len(y) - offset), 'constant')
    return y


class AudioDataset(torch.utils.data.Dataset):
    
    def __init__(self, df, wav_dir, sr=None, max_length=4.0, window_size=0.02, hop_size=0.01, n_mels=64):
        if not os.path.exists(wav_dir):
            print('ERROR: not found %s' % wav_dir)
            exit(1)
        self.df = df
        self.wav_dir = wav_dir
        self.sr = sr
        self.max_length = max_length     # sec
        self.window_size = window_size   # sec
        self.hop_size = hop_size         # sec
        self.n_mels = n_mels

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

    def __getitem__(self, index):
        fpath = os.path.join(self.wav_dir, self.df.fname[index])
        y, sr = librosa.load(fpath, sr=self.sr)
        y = random_crop(y, int(self.max_length * sr))
        
        # feature
        n_fft = int(self.window_size * sr)
        hop_length = int(self.hop_size * sr)
        mel = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mels=self.n_mels)
        # (channel, features, frames)
        mel = np.resize(mel, (1, mel.shape[0], mel.shape[1]))
        tensor = torch.from_numpy(mel).float()

        # label
        label = self.df.label_idx[index]
        
        return tensor, label<jupyter_output><empty_output><jupyter_text>- メルスペクトログラムの標準化はどうする？- ファイルごとに平均0、標準偏差1でよいか？ -> GCommandsPytorch<jupyter_code>train_dataset = AudioDataset(train_df, '../data/audio_train')
print(len(train_dataset))
data, target = train_dataset[0]
print(data.size(), target, le.classes_[target])<jupyter_output>9473
torch.Size([1, 64, 401]) 23 Hi-hat<jupyter_text>DataLoader<jupyter_code>train_dataset = AudioDataset(train_df, '../data/audio_train')
seed = 0
valid_size = 0.1
batch_size = 128
num_workers= 0

np.random.seed(0)

# 訓練データとバリデーションデータに分割
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(valid_size * num_train)
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]

train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)

train_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size,
    sampler=train_sampler,
    drop_last=True,
    num_workers=num_workers
)

valid_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size,
    sampler=valid_sampler,
    drop_last=True,
    num_workers=num_workers
)
len(train_loader), len(valid_loader)
data, target = iter(train_loader).next()
print(data.size())
print(target.size())<jupyter_output>torch.Size([128, 1, 64, 401])
torch.Size([128])<jupyter_text>Model<jupyter_code>class LeNet2D(nn.Module):
    def __init__(self):
        super(LeNet2D, self).__init__()
        self.block1 = nn.Sequential(
            nn.Conv2d(1, 20, kernel_size=5),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )
        
        self.block2 = nn.Sequential(
            nn.Conv2d(20, 20, kernel_size=5),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )
        self.fc1 = nn.Linear(25220, 1000)
        self.fc2 = nn.Linear(1000, num_classes)

    def forward(self, x):
        print(x.size())
        x = self.block1(x)
        print(x.size())
        x = self.block2(x)
        print(x.size())
        x = x.view(x.size(0), -1)
        print(x.size())
        x = F.relu(self.fc1(x))
        print(x.size())
        x = self.fc2(x)
        print(x.size())
        return x
model = LeNet2D()
model(data)<jupyter_output>torch.Size([128, 1, 64, 401])
torch.Size([128, 20, 30, 198])
torch.Size([128, 20, 13, 97])
torch.Size([128, 25220])
torch.Size([128, 1000])
torch.Size([128, 41])
{'fraction_non_alphanumeric': 0.1385186803583133, 'fraction_numerical': 0.032117107275507976, 'mean_word_length': 2.99825327510917, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '258137', 'license': 'MIT', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1792, 'n_words': 278, 'path': 'notebooks/train.ipynb', 'repo_name': 'aidiary/freesound-audio-tagging', 'size': '4577'}