File size: 7,131 Bytes
9c7e38e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
from typing import Tuple

import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset, random_split

import config
from model import build_model


def load_dataset_npz(path: str) -> Tuple[torch.Tensor, torch.Tensor]:
    data = np.load(path)
    X = data["X"].astype(np.float32)
    Y = data["Y"].astype(np.float32)
    return torch.from_numpy(X), torch.from_numpy(Y)


def load_dataset_pt(path: str) -> Tuple[torch.Tensor, torch.Tensor]:
    data = torch.load(path)
    return data["X"].float(), data["Y"].float()


def train(
    dataset_path: str,
    use_npz: bool = True,
    batch_size: int = 32,
    num_epochs: int = 20,
    val_split: float = 0.1,
    learning_rate: float = 1e-3,
    device: str = None,
    save_dir: str = "models",
    best_name: str = "transformer_music_best.pt",
    last_name: str = "transformer_music_last.pt",
    checkpoint_name: str = "transformer_music_checkpoint.pt",
    extra_epochs: int = None,
) -> None:
    """
    Train MusicTransformer with resume & best-model saving.

    If extra_epochs is not None, the function will:
      - load checkpoint (if any) and read checkpoint["epoch"] as last_epoch,
      - start from last_epoch + 1,
      - run for exactly extra_epochs epochs (end_epoch = last_epoch + extra_epochs).

    If extra_epochs is None, num_epochs is treated as a global target:
      - start from start_epoch (1 or checkpoint_epoch+1),
      - run until epoch == num_epochs.
    """

    if device is None:
        device = "cuda" if torch.cuda.is_available() else "cpu"

    # Load dataset
    if use_npz:
        X, Y = load_dataset_npz(dataset_path)
    else:
        X, Y = load_dataset_pt(dataset_path)

    print(f"Loaded dataset: X {X.shape}, Y {Y.shape}")

    dataset = TensorDataset(X, Y)

    # Train/val split
    n_total = len(dataset)
    n_val = int(n_total * val_split)
    n_train = n_total - n_val

    train_ds, val_ds = random_split(dataset, [n_train, n_val])

    train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
    val_loader = DataLoader(val_ds, batch_size=batch_size, shuffle=False)

    # Build model, optimizer, loss
    model = build_model(config).to(device)
    bce = nn.BCEWithLogitsLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    os.makedirs(save_dir, exist_ok=True)
    best_model_path = os.path.join(save_dir, best_name)
    last_model_path = os.path.join(save_dir, last_name)
    checkpoint_path = os.path.join(save_dir, checkpoint_name)

    # ===== Resume from checkpoint if present =====
    start_epoch = 1
    best_val_loss = float("inf")

    if os.path.exists(checkpoint_path):
        print(f"Found checkpoint at {checkpoint_path}, loading...")
        checkpoint = torch.load(checkpoint_path, map_location=device)
        model.load_state_dict(checkpoint["model_state_dict"])
        optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
        start_epoch = checkpoint["epoch"] + 1
        best_val_loss = checkpoint["best_val_loss"]
        print(f"Resuming from epoch {start_epoch}, best_val_loss={best_val_loss:.4f}")

    # Determine end_epoch based on extra_epochs / num_epochs
    if extra_epochs is not None:
        # Run exactly extra_epochs from the current checkpoint point
        end_epoch = start_epoch + extra_epochs - 1
    else:
        # Run until global num_epochs
        end_epoch = num_epochs

    if start_epoch > end_epoch:
        print(f"No epochs to run (start_epoch={start_epoch}, end_epoch={end_epoch}).")
        print("Training complete.")
        print(f"Best model: {best_model_path}")
        print(f"Last model: {last_model_path}")
        return

    # ===== Training loop =====
    for epoch in range(start_epoch, end_epoch + 1):
        model.train()
        train_loss_sum = 0.0
        train_batches = 0

        for X_batch, Y_batch in train_loader:
            X_batch = X_batch.to(device)  # [B, T, 39]
            Y_batch = Y_batch.to(device)  # [B, T, 39]

            optimizer.zero_grad()

            outputs = model(X_batch)

            # Slice per-instrument targets
            strings_target = Y_batch[:, :, 0:12]
            woodwinds_target = Y_batch[:, :, 12:24]
            brass_target = Y_batch[:, :, 24:36]
            perc_target = Y_batch[:, :, 36:39]

            loss_strings = bce(outputs["strings"], strings_target)
            loss_wood = bce(outputs["woodwinds"], woodwinds_target)
            loss_brass = bce(outputs["brass"], brass_target)
            loss_perc = bce(outputs["perc"], perc_target)

            loss = loss_strings + loss_wood + loss_brass + loss_perc

            loss.backward()
            optimizer.step()

            train_loss_sum += loss.item()
            train_batches += 1

        avg_train_loss = train_loss_sum / max(train_batches, 1)

        # Validation
        model.eval()
        val_loss_sum = 0.0
        val_batches = 0
        with torch.no_grad():
            for X_batch, Y_batch in val_loader:
                X_batch = X_batch.to(device)
                Y_batch = Y_batch.to(device)

                outputs = model(X_batch)

                strings_target = Y_batch[:, :, 0:12]
                woodwinds_target = Y_batch[:, :, 12:24]
                brass_target = Y_batch[:, :, 24:36]
                perc_target = Y_batch[:, :, 36:39]

                loss_strings = bce(outputs["strings"], strings_target)
                loss_wood = bce(outputs["woodwinds"], woodwinds_target)
                loss_brass = bce(outputs["brass"], brass_target)
                loss_perc = bce(outputs["perc"], perc_target)

                loss = loss_strings + loss_wood + loss_perc + loss_brass

                val_loss_sum += loss.item()
                val_batches += 1

        avg_val_loss = val_loss_sum / max(val_batches, 1)

        print(
            f"Epoch {epoch:03d} | "
            f"Train loss: {avg_train_loss:.4f} | "
            f"Val loss: {avg_val_loss:.4f}"
        )

        # ===== Save checkpoint (for resume) =====
        checkpoint = {
            "epoch": epoch,
            "model_state_dict": model.state_dict(),
            "optimizer_state_dict": optimizer.state_dict(),
            "best_val_loss": best_val_loss,
        }
        torch.save(checkpoint, checkpoint_path)
        print(f"  ↳ Checkpoint saved to {checkpoint_path}")

        # ===== Best model logic =====
        if avg_val_loss < best_val_loss:
            best_val_loss = avg_val_loss
            torch.save(model.state_dict(), best_model_path)
            print(f"  ↳ New best model saved to {best_model_path}")

        # Always save "last" model as well (optional)
        torch.save(model.state_dict(), last_model_path)

    print("Training complete.")
    print(f"Best model: {best_model_path}")
    print(f"Last model: {last_model_path}")


if __name__ == "__main__":
    dataset_path = "musicnet_grid_dataset.npz"
    train(
        dataset_path=dataset_path,
        use_npz=True,
        batch_size=32,
        num_epochs=20,
        val_split=0.1,
        learning_rate=1e-3,
    )