KalineZephyr commited on
Commit
04dcc8a
Β·
verified Β·
1 Parent(s): 431db93

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +196 -0
README.md CHANGED
@@ -1,3 +1,199 @@
1
  ---
2
  license: mit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ language:
4
+ - en
5
+ tags:
6
+ - music-generation
7
+ - midi
8
+ - lstm
9
+ - attention
10
+ - pytorch
11
+ - piano
12
+ - symbolic-music
13
+ - codealpha
14
+ datasets:
15
+ - ktonal/maestro-v3
16
+ - pictureinthenoise/music-generation-with-giantmidi-piano
17
  ---
18
+
19
+ # 🎡 Music LSTM β€” Symbolic Piano Generation
20
+
21
+ A **Stacked LSTM + Attention** model for symbolic piano music generation, trained on 46 million musical events from ~12,000 MIDI files.
22
+
23
+ Built from scratch as part of the **CodeAlpha AI Internship (Task 3)** β€” no pre-trained model, no external API, just raw deep learning on MIDI data.
24
+
25
+ ---
26
+
27
+ ## Model Description
28
+
29
+ The model treats music generation as a **next-token prediction** problem β€” the same principle behind language models like GPT, applied to piano music.
30
+
31
+ Each musical event is encoded as a single token representing three attributes simultaneously:
32
+ - **Pitch** β€” MIDI note value (0–127)
33
+ - **Duration** β€” discretized into 10 musical buckets (thirty-second β†’ whole note)
34
+ - **Velocity** β€” discretized into 8 classical nuance buckets (ppp β†’ fff)
35
+
36
+ The model learns to predict the next token given a sequence of 64 past tokens, then generates music autoregressively.
37
+
38
+ ### Architecture
39
+
40
+ ```
41
+ Token sequence (64 tokens)
42
+ β”‚
43
+ β–Ό
44
+ Embedding (vocab_size=6543, dim=256)
45
+ β”‚
46
+ β–Ό
47
+ LSTM Layer 1 (hidden=1024) ← local patterns: intervals, rhythm
48
+ β”‚
49
+ β–Ό
50
+ LSTM Layer 2 (hidden=1024) ← higher-level patterns: phrases, motifs
51
+ β”‚
52
+ β–Ό
53
+ Attention (additive, Bahdanau-style)
54
+ β”‚
55
+ β–Ό
56
+ Linear β†’ Softmax (6543 classes)
57
+ β”‚
58
+ β–Ό
59
+ Next token prediction
60
+ ```
61
+
62
+ | Parameter | Value |
63
+ |---|---|
64
+ | Total parameters | 22,030,480 |
65
+ | Vocabulary size | 6,543 tokens |
66
+ | Sequence length | 64 tokens |
67
+ | Hidden size | 1,024 |
68
+ | LSTM layers | 2 |
69
+ | Embedding dim | 256 |
70
+ | Dropout | 0.3 |
71
+
72
+ ---
73
+
74
+ ## Training Data
75
+
76
+ | Dataset | Files | Events |
77
+ |---|---|---|
78
+ | [Maestro v3.0.0](https://magenta.tensorflow.org/datasets/maestro) | 1,276 | ~6M |
79
+ | [GiantMIDI-Piano v1.21](https://github.com/bytedance/GiantMIDI-Piano) | 10,841 | ~41M |
80
+ | **Total** | **12,109** | **~46M** |
81
+
82
+ Both datasets consist of professional and semi-professional **solo piano** recordings in classical style, ensuring a consistent musical domain.
83
+
84
+ Preprocessing used **symusic** (C++ MIDI parser) for fast extraction of pitch, duration, and velocity attributes from raw MIDI files.
85
+
86
+ ---
87
+
88
+ ## Training
89
+
90
+ Trained on **Kaggle 2Γ—T4 GPUs** (30GB VRAM total) using `torch.nn.DataParallel`.
91
+
92
+ | Hyperparameter | Value |
93
+ |---|---|
94
+ | Optimizer | Adam (lr=0.001) |
95
+ | Scheduler | ReduceLROnPlateau (factor=0.5, patience=2) |
96
+ | Batch size | 256 |
97
+ | Gradient clipping | 5.0 |
98
+ | Early stopping patience | 5 |
99
+
100
+ ### Results
101
+
102
+ | Epoch | Train Loss | Val Loss |
103
+ |---|---|---|
104
+ | 1 | 6.834 | 6.407 |
105
+ | 4 | 5.906 | 5.900 |
106
+ | **8** | **5.505** | **5.806** βœ“ best |
107
+ | 13 | 5.041 | 5.857 β€” early stop |
108
+
109
+ **Best checkpoint: epoch 8, val_loss = 5.805**
110
+
111
+ Random baseline: `ln(6543) β‰ˆ 8.78` β€” the model significantly outperforms random prediction.
112
+
113
+ ---
114
+
115
+ ## Usage
116
+
117
+ ### Quick start
118
+
119
+ ```bash
120
+ git clone https://github.com/Tahsine/CodeAlpha_Music_Generation
121
+ cd CodeAlpha_Music_Generation
122
+ pip install -r requirements.txt
123
+ ```
124
+
125
+ The model weights are downloaded automatically from this repo on first run:
126
+
127
+ ```bash
128
+ # Generate MIDI (256 notes, temperature=0.9)
129
+ python generate.py
130
+
131
+ # Full options
132
+ python generate.py \
133
+ --n_tokens 512 \
134
+ --temperature 0.9 \
135
+ --bpm 120 \
136
+ --output artifacts/my_music.mid \
137
+ --device cpu
138
+ ```
139
+
140
+ ### Generate MIDI + WAV
141
+
142
+ ```bash
143
+ # Install FluidSynth
144
+ sudo apt-get install fluidsynth # Linux
145
+ brew install fluidsynth # macOS
146
+
147
+ # Generate audio β€” soundfont (~30MB) downloads automatically
148
+ python generate.py --n_tokens 512 --temperature 0.9 --audio
149
+ ```
150
+
151
+ ### Temperature guide
152
+
153
+ | Temperature | Effect |
154
+ |---|---|
155
+ | `0.7` | Conservative β€” coherent but repetitive |
156
+ | `0.9` | Balanced β€” musical and varied *(recommended)* |
157
+ | `1.1` | Creative β€” surprising but less coherent |
158
+
159
+ ### Load model directly in Python
160
+
161
+ ```python
162
+ import torch
163
+ from huggingface_hub import hf_hub_download
164
+
165
+ # Download weights
166
+ model_path = hf_hub_download(
167
+ repo_id="KalineZephyr/music-lstm-midi-codealpha",
168
+ filename="best_model.pt",
169
+ )
170
+
171
+ # Load checkpoint
172
+ ckpt = torch.load(model_path, map_location="cpu")
173
+ print(f"Best epoch : {ckpt['epoch']}")
174
+ print(f"Val loss : {ckpt['val_loss']:.4f}")
175
+ print(f"Config : {ckpt['config']}")
176
+ ```
177
+
178
+ ---
179
+
180
+ ## Limitations
181
+
182
+ - **Short-range coherence only** β€” LSTM memory is limited to ~50–100 tokens. The model generates locally coherent phrases but lacks long-range structure (no recurring themes, no global form).
183
+ - **Piano only** β€” trained exclusively on solo piano data. Other instruments will produce poor results.
184
+ - **Classical/romantic style** β€” dataset bias toward Western classical music.
185
+ - **No rhythm quantization** β€” generated durations are discretized into 10 buckets, which may sound mechanical compared to human performance.
186
+
187
+ These limitations are inherent to LSTM-based sequence models. A Transformer architecture with full self-attention would address the long-range coherence issue.
188
+
189
+ ---
190
+
191
+ ## Repository
192
+
193
+ **GitHub:** [Tahsine/CodeAlpha_Music_Generation](https://github.com/Tahsine/CodeAlpha_Music_Generation)
194
+
195
+ ---
196
+
197
+ ## License
198
+
199
+ MIT