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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
***
|
| 24 |
|
| 25 |
### Project Los Angeles
|
|
|
|
| 20 |
# midisim
|
| 21 |
## Similarity search output samples
|
| 22 |
|
| 23 |
+

|
| 24 |
+
|
| 25 |
+
***
|
| 26 |
+
|
| 27 |
+
## Main features
|
| 28 |
+
|
| 29 |
+
* Ultra-fast and flexible GPU/CPU MIDI-to-MIDI similarity calculation, search and analysis
|
| 30 |
+
* Quality pre-trained models and comprehensive pre-computed embeddings sets
|
| 31 |
+
* Stand-alone, versatile, and extensive codebase for general or custom MIDI-to-MIDI similarity tasks
|
| 32 |
+
* Full cross-platform compatibility and support
|
| 33 |
+
|
| 34 |
+
***
|
| 35 |
+
|
| 36 |
+
## [Pre-trained models](https://huggingface.co/projectlosangeles/midisim)
|
| 37 |
+
|
| 38 |
+
* ```midisim_small_pre_trained_model_2_epochs_43117_steps_0.3148_loss_0.9229_acc.pth``` - Very fast and accurate small model, suitable for all tasks. This model is included in PyPI package or it can be downloaded from Hugging Face
|
| 39 |
+
* ```midisim_large_pre_trained_model_2_epochs_86275_steps_0.2054_loss_0.9385_acc.pth``` - Fast large model for more nuanced embeddings generation. Download checkpoint from Hugging Face
|
| 40 |
+
|
| 41 |
+
#### Both pre-trained models were trained on full [Godzilla Piano](https://huggingface.co/datasets/asigalov61/Godzilla-Piano) dataset for 2 complete epochs
|
| 42 |
+
|
| 43 |
+
***
|
| 44 |
+
|
| 45 |
+
## [Pre-computed embeddings sets](https://huggingface.co/datasets/projectlosangeles/midisim-embeddings)
|
| 46 |
+
|
| 47 |
+
### For small pre-trained model
|
| 48 |
+
|
| 49 |
+
```discover_midi_dataset_37292_genres_midis_embeddings_cc_by_nc_sa.npy``` - 37292 genre MIDIs embeddings for genre (artist and song) identification tasks
|
| 50 |
+
|
| 51 |
+
```discover_midi_dataset_202400_identified_midis_embeddings_cc_by_nc_sa.npy``` - 202400 identified MIDIs embeddings for MIDI identification tasks
|
| 52 |
+
|
| 53 |
+
```discover_midi_dataset_3480123_clean_midis_embeddings_cc_by_nc_sa.npy``` - 3480123 select clean MIDIs embeddings for large scale similarity search and analysis tasks
|
| 54 |
+
|
| 55 |
+
### For large pre-trained model
|
| 56 |
+
|
| 57 |
+
```discover_midi_dataset_37303_genres_midis_embeddings_large_cc_by_nc_sa.npy``` - 37303 genre MIDIs embeddings for genre (artist and song) identification tasks
|
| 58 |
+
|
| 59 |
+
```discover_midi_dataset_202400_identified_midis_embeddings_large_cc_by_nc_sa.npy``` - 202400 identified MIDIs embeddings for MIDI identification tasks
|
| 60 |
+
|
| 61 |
+
```discover_midi_dataset_3480123_clean_midis_embeddings_large_cc_by_nc_sa.npy``` - 3480123 select clean MIDIs embeddings for large scale similarity search and analysis tasks
|
| 62 |
+
|
| 63 |
+
#### Source MIDI dataset: [Discover MIDI Dataset](https://huggingface.co/datasets/projectlosangeles/Discover-MIDI-Dataset)
|
| 64 |
+
|
| 65 |
+
***
|
| 66 |
+
|
| 67 |
+
### [Similarity search output samples](https://huggingface.co/datasets/projectlosangeles/midisim-samples)
|
| 68 |
+
|
| 69 |
+
```midisim-similarity-search-output-samples-CC-BY-NC-SA.zip``` - ~300000 MIDIs indentified with midisim music discovery pipeline with both pre-trained models
|
| 70 |
+
|
| 71 |
+
#### Source MIDI dataset: [Discover MIDI Dataset](https://huggingface.co/datasets/projectlosangeles/Discover-MIDI-Dataset)
|
| 72 |
+
|
| 73 |
+
***
|
| 74 |
+
|
| 75 |
+
## Installation
|
| 76 |
+
|
| 77 |
+
### midisim PyPI package (for general use)
|
| 78 |
+
|
| 79 |
+
```sh
|
| 80 |
+
!pip install -U midisim
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### x-transformers 2.3.1 (for raw/custom tasks)
|
| 84 |
+
|
| 85 |
+
```sh
|
| 86 |
+
!pip install x-transformers==2.3.1
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
***
|
| 90 |
+
|
| 91 |
+
## Basic use guide
|
| 92 |
+
|
| 93 |
+
### General use example
|
| 94 |
+
|
| 95 |
+
```python
|
| 96 |
+
# ================================================================================================
|
| 97 |
+
# Initalize midisim
|
| 98 |
+
# ================================================================================================
|
| 99 |
+
|
| 100 |
+
# Import main midisim module
|
| 101 |
+
import midisim
|
| 102 |
+
|
| 103 |
+
# ================================================================================================
|
| 104 |
+
# Prepare midisim embeddings
|
| 105 |
+
# ================================================================================================
|
| 106 |
+
|
| 107 |
+
# Option 1: Download sample pre-computed embeddings corpus from Hugging Face
|
| 108 |
+
emb_path = midisim.download_embeddings()
|
| 109 |
+
|
| 110 |
+
# Option 2: use custom pre-computed embeddings corpus
|
| 111 |
+
# See custom embeddings generation section of this README for details
|
| 112 |
+
# emb_path = './custom_midis_embeddings_corpus.npy'
|
| 113 |
+
|
| 114 |
+
# Load downloaded embeddings corpus
|
| 115 |
+
corpus_midi_names, corpus_emb = midisim.load_embeddings(emb_path)
|
| 116 |
+
|
| 117 |
+
# ================================================================================================
|
| 118 |
+
# Prepare midisim model
|
| 119 |
+
# ================================================================================================
|
| 120 |
+
|
| 121 |
+
# Option 1: Download main pre-trained midisim model from Hugging Face
|
| 122 |
+
model_path = midisim.download_model()
|
| 123 |
+
|
| 124 |
+
# Option 2: Use main pre-trained midisim model included in midisim PyPI package
|
| 125 |
+
# model_path = get_package_models()[0]['path']
|
| 126 |
+
|
| 127 |
+
# Load midisim model
|
| 128 |
+
model, ctx, dtype = midisim.load_model(model_path)
|
| 129 |
+
|
| 130 |
+
# ================================================================================================
|
| 131 |
+
# Prepare source MIDI
|
| 132 |
+
# ================================================================================================
|
| 133 |
+
|
| 134 |
+
# Load source MIDI
|
| 135 |
+
input_toks_seqs = midisim.midi_to_tokens('Come To My Window.mid')
|
| 136 |
+
|
| 137 |
+
# ================================================================================================
|
| 138 |
+
# Calculate and analyze embeddings
|
| 139 |
+
# ================================================================================================
|
| 140 |
+
|
| 141 |
+
# Compute source/query embeddings
|
| 142 |
+
query_emb = midisim.get_embeddings_bf16(model, input_toks_seqs)
|
| 143 |
+
|
| 144 |
+
# Calculate cosine similarity between source/query MIDI embeddings and embeddings corpus
|
| 145 |
+
idxs, sims = midisim.cosine_similarity_topk(query_emb, corpus_emb)
|
| 146 |
+
|
| 147 |
+
# ================================================================================================
|
| 148 |
+
# Processs, print and save results
|
| 149 |
+
# ================================================================================================
|
| 150 |
+
|
| 151 |
+
# Convert the results to sorted list with transpose values
|
| 152 |
+
idxs_sims_tvs_list = midisim.idxs_sims_to_sorted_list(idxs, sims)
|
| 153 |
+
|
| 154 |
+
# Print corpus matches (and optionally) convert the final result to a handy list for further processing
|
| 155 |
+
corpus_matches_list midisim.print_sorted_idxs_sims_list(idxs_sims_tvs_list, corpus_midi_names, return_as_list=True)
|
| 156 |
+
|
| 157 |
+
# ================================================================================================
|
| 158 |
+
# Copy matched MIDIs from the MIDI corpus for listening and further evaluation and analysis
|
| 159 |
+
# ================================================================================================
|
| 160 |
+
|
| 161 |
+
# Copy matched corpus MIDI to a desired directory for easy evaluation and analysis
|
| 162 |
+
out_dir_path = midisim.copy_corpus_files(corpus_matches_list)
|
| 163 |
+
|
| 164 |
+
# ================================================================================================
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
### Raw/custom use example
|
| 168 |
+
|
| 169 |
+
#### Small model (2 epochs)
|
| 170 |
+
|
| 171 |
+
```python
|
| 172 |
+
import torch
|
| 173 |
+
from x_transformers import TransformerWrapper, Encoder
|
| 174 |
+
|
| 175 |
+
# Original model hyperparameters
|
| 176 |
+
SEQ_LEN = 3072
|
| 177 |
+
|
| 178 |
+
MASK_IDX = 384 # Use this value for masked modelling
|
| 179 |
+
PAD_IDX = 385 # Model pad index
|
| 180 |
+
VOCAB_SIZE = 386 # Total vocab size
|
| 181 |
+
|
| 182 |
+
MASK_PROB = 0.15 # Original training mask probability value (use for masked modelling)
|
| 183 |
+
|
| 184 |
+
DEVICE = 'cuda' # You can use any compatible device or CPU
|
| 185 |
+
DTYPE = torch.bfloat16 # Original training dtype
|
| 186 |
+
|
| 187 |
+
# Official main midisim model checkpoint name
|
| 188 |
+
MODEL_CKPT = 'midisim_small_pre_trained_model_2_epochs_43117_steps_0.3148_loss_0.9229_acc.pth'
|
| 189 |
+
|
| 190 |
+
# Model architecture using x-transformers
|
| 191 |
+
model = TransformerWrapper(
|
| 192 |
+
num_tokens = VOCAB_SIZE,
|
| 193 |
+
max_seq_len = SEQ_LEN,
|
| 194 |
+
attn_layers = Encoder(
|
| 195 |
+
dim = 512,
|
| 196 |
+
depth = 8,
|
| 197 |
+
heads = 8,
|
| 198 |
+
rotary_pos_emb = True,
|
| 199 |
+
attn_flash = True,
|
| 200 |
+
),
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
model.load_state_dict(torch.load(MODEL_CKPT, map_location=DEVICE))
|
| 204 |
+
|
| 205 |
+
model.to(DEVICE)
|
| 206 |
+
model.eval()
|
| 207 |
+
|
| 208 |
+
# Original training autoxast setup
|
| 209 |
+
autocast_ctx = torch.amp.autocast(device_type=DEVICE, dtype=DTYPE)
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
#### Large model (2 epochs)
|
| 213 |
+
|
| 214 |
+
```python
|
| 215 |
+
import torch
|
| 216 |
+
from x_transformers import TransformerWrapper, Encoder
|
| 217 |
+
|
| 218 |
+
# Original model hyperparameters
|
| 219 |
+
SEQ_LEN = 3072
|
| 220 |
+
|
| 221 |
+
MASK_IDX = 384 # Use this value for masked modelling
|
| 222 |
+
PAD_IDX = 385 # Model pad index
|
| 223 |
+
VOCAB_SIZE = 386 # Total vocab size
|
| 224 |
+
|
| 225 |
+
MASK_PROB = 0.15 # Original training mask probability value (use for masked modelling)
|
| 226 |
+
|
| 227 |
+
DEVICE = 'cuda' # You can use any compatible device or CPU
|
| 228 |
+
DTYPE = torch.bfloat16 # Original training dtype
|
| 229 |
+
|
| 230 |
+
# Official main midisim model checkpoint name
|
| 231 |
+
MODEL_CKPT = 'midisim_large_pre_trained_model_2_epochs_86275_steps_0.2054_loss_0.9385_acc.pth'
|
| 232 |
+
|
| 233 |
+
# Model architecture using x-transformers
|
| 234 |
+
model = TransformerWrapper(
|
| 235 |
+
num_tokens = VOCAB_SIZE,
|
| 236 |
+
max_seq_len = SEQ_LEN,
|
| 237 |
+
attn_layers = Encoder(
|
| 238 |
+
dim = 512,
|
| 239 |
+
depth = 16,
|
| 240 |
+
heads = 8,
|
| 241 |
+
rotary_pos_emb = True,
|
| 242 |
+
attn_flash = True,
|
| 243 |
+
),
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
model.load_state_dict(torch.load(MODEL_CKPT, map_location=DEVICE))
|
| 247 |
+
|
| 248 |
+
model.to(DEVICE)
|
| 249 |
+
model.eval()
|
| 250 |
+
|
| 251 |
+
# Original training autoxast setup
|
| 252 |
+
autocast_ctx = torch.amp.autocast(device_type=DEVICE, dtype=DTYPE)
|
| 253 |
+
```
|
| 254 |
+
|
| 255 |
+
***
|
| 256 |
+
|
| 257 |
+
## Creating custom MIDI corpus embeddings
|
| 258 |
+
|
| 259 |
+
```python
|
| 260 |
+
# ================================================================================================
|
| 261 |
+
|
| 262 |
+
# Load main midisim module
|
| 263 |
+
import midisim
|
| 264 |
+
|
| 265 |
+
# Import helper modules
|
| 266 |
+
import os
|
| 267 |
+
import tqdm
|
| 268 |
+
|
| 269 |
+
# ================================================================================================
|
| 270 |
+
|
| 271 |
+
# Call included TMIDIX module through midisim to create MIDI files list
|
| 272 |
+
custom_midi_corpus_file_names = midisim.TMIDIX.create_files_list(['./custom_midi_corpus_dir/'])
|
| 273 |
+
|
| 274 |
+
# ================================================================================================
|
| 275 |
+
|
| 276 |
+
# Create two lists: one with MIDI corpus file names
|
| 277 |
+
# and another with MIDI corpus tokens representations suitable for embeddings generation
|
| 278 |
+
midi_corpus_file_names = []
|
| 279 |
+
midi_corpus_tokens = []
|
| 280 |
+
|
| 281 |
+
for midi_file in tqdm.tqdm(custom_midi_corpus_file_names):
|
| 282 |
+
midi_corpus_file_names.append(os.path.splitext(os.path.basename(midi_file))[0])
|
| 283 |
+
|
| 284 |
+
midi_tokens = midisim.midi_to_tokens(midi_file, transpose_factor=0, verbose=False)[0]
|
| 285 |
+
midi_corpus_tokens.append(midi_tokens)
|
| 286 |
+
|
| 287 |
+
# It is highly recommended to sort the resulting corpus by tokens sequence length
|
| 288 |
+
# This greatly speeds up embeddings calculations
|
| 289 |
+
sorted_midi_corpus = sorted(zip(midi_corpus_file_names, midi_corpus_tokens), key=lambda x: len(x[1]))
|
| 290 |
+
midi_corpus_file_names, midi_corpus_tokens = map(list, zip(*sorted_midi_corpus))
|
| 291 |
+
|
| 292 |
+
# ================================================================================================
|
| 293 |
+
# Now you are ready to generate embeddings as follows:
|
| 294 |
+
# ================================================================================================
|
| 295 |
+
|
| 296 |
+
# Load main midisim model
|
| 297 |
+
model, ctx, dtype = midisim.load_model(verbose=False)
|
| 298 |
+
|
| 299 |
+
# Generate MIDI corpus embeddings
|
| 300 |
+
midi_corpus_embeddings = midisim.get_embeddings_bf16(model, midi_corpus_tokens)
|
| 301 |
+
|
| 302 |
+
# ================================================================================================
|
| 303 |
+
|
| 304 |
+
# Save generated MIDI corpus embeddings and MIDI corpus file names in one handy NumPy file
|
| 305 |
+
midisim.save_embeddings(midi_corpus_file_names,
|
| 306 |
+
midi_corpus_embeddings,
|
| 307 |
+
verbose=False
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
# ================================================================================================
|
| 311 |
+
|
| 312 |
+
# You now can use this saved custom MIDI corpus NumPy file with midisim.load_embeddings()
|
| 313 |
+
# and the rest of the pipeline outlined in the general use section above
|
| 314 |
+
```
|
| 315 |
+
|
| 316 |
+
***
|
| 317 |
+
|
| 318 |
+
## Music discovery pipeline
|
| 319 |
+
Here is a complete MIDI music discovery pipeline example using midisim and [Discover MIDI Dataset](https://huggingface.co/datasets/projectlosangeles/Discover-MIDI-Dataset)
|
| 320 |
+
|
| 321 |
+
### Install midisim and discovermidi PyPI packages
|
| 322 |
+
|
| 323 |
+
```sh
|
| 324 |
+
!pip install -U midisim
|
| 325 |
+
```
|
| 326 |
+
|
| 327 |
+
```sh
|
| 328 |
+
!pip install -U discovermidi
|
| 329 |
+
```
|
| 330 |
+
|
| 331 |
+
### Download and unzip Discover MIDI Dataset
|
| 332 |
+
|
| 333 |
+
```python
|
| 334 |
+
import discovermidi
|
| 335 |
+
from discovermidi import fast_parallel_extract
|
| 336 |
+
|
| 337 |
+
discovermidi.download_dataset()
|
| 338 |
+
|
| 339 |
+
fast_parallel_extract.fast_parallel_extract()
|
| 340 |
+
```
|
| 341 |
+
|
| 342 |
+
### Choose and prepare one midisim model and corresponding embeddings set
|
| 343 |
+
|
| 344 |
+
#### Small model
|
| 345 |
+
|
| 346 |
+
```python
|
| 347 |
+
model_ckpt = 'midisim_small_pre_trained_model_2_epochs_43117_steps_0.3148_loss_0.9229_acc.pth'
|
| 348 |
+
model_depth = 8
|
| 349 |
+
|
| 350 |
+
embeddings_file = 'discover_midi_dataset_3480123_clean_midis_embeddings_cc_by_nc_sa.npy'
|
| 351 |
+
```
|
| 352 |
+
|
| 353 |
+
#### Large model
|
| 354 |
+
|
| 355 |
+
```python
|
| 356 |
+
model_ckpt = 'midisim_large_pre_trained_model_2_epochs_86275_steps_0.2054_loss_0.9385_acc.pth'
|
| 357 |
+
model_depth = 16
|
| 358 |
+
|
| 359 |
+
embeddings_file = 'discover_midi_dataset_3480123_clean_midis_embeddings_large_cc_by_nc_sa.npy'
|
| 360 |
+
```
|
| 361 |
+
|
| 362 |
+
### Create Master MIDI dataset directory and upload your source/master MIDIs in it
|
| 363 |
+
|
| 364 |
+
```python
|
| 365 |
+
import os
|
| 366 |
+
|
| 367 |
+
os.makedirs('./Master-MIDI-Dataset/', exist_ok=True)
|
| 368 |
+
```
|
| 369 |
+
|
| 370 |
+
### Initialize midisim, download and load chosen midisim model and embeddings set
|
| 371 |
+
|
| 372 |
+
```python
|
| 373 |
+
# Import main midisim module
|
| 374 |
+
import midisim
|
| 375 |
+
|
| 376 |
+
# Download embeddings from Hugging Face
|
| 377 |
+
emb_path = midisim.download_embeddings(filename=embeddings_file)
|
| 378 |
+
|
| 379 |
+
# Load downloaded embeddings corpus
|
| 380 |
+
corpus_midi_names, corpus_emb = midisim.load_embeddings(embeddings_path=emb_path)
|
| 381 |
+
|
| 382 |
+
# Download midisim model from Hugging Face
|
| 383 |
+
model_path = midisim.download_model(filename=model_ckpt)
|
| 384 |
+
|
| 385 |
+
# Load midisim model
|
| 386 |
+
model, ctx, dtype = midisim.load_model(model_path,
|
| 387 |
+
depth=model_depth
|
| 388 |
+
)
|
| 389 |
+
```
|
| 390 |
+
|
| 391 |
+
### Create Master MIDI dataset files list
|
| 392 |
+
|
| 393 |
+
```python
|
| 394 |
+
filez = midisim.TMIDIX.create_files_list(['./Master-MIDI-Dataset/'])
|
| 395 |
+
```
|
| 396 |
+
|
| 397 |
+
### Launch the search
|
| 398 |
+
|
| 399 |
+
```python
|
| 400 |
+
import os
|
| 401 |
+
import tqdm
|
| 402 |
+
|
| 403 |
+
for fa in tqdm.tqdm(filez):
|
| 404 |
+
|
| 405 |
+
# Load source MIDI
|
| 406 |
+
input_toks_seqs = midisim.midi_to_tokens(fa, verbose=False)
|
| 407 |
+
|
| 408 |
+
if input_toks_seqs:
|
| 409 |
+
|
| 410 |
+
# ================================================================================================
|
| 411 |
+
# Calculate and analyze embeddings
|
| 412 |
+
# ================================================================================================
|
| 413 |
+
|
| 414 |
+
# Compute source/query embeddings
|
| 415 |
+
query_emb = midisim.get_embeddings_bf16(model, input_toks_seqs, verbose=False)
|
| 416 |
+
|
| 417 |
+
# Calculate cosine similarity between source/query MIDI embeddings and embeddings corpus
|
| 418 |
+
idxs, sims = midisim.cosine_similarity_topk(query_emb, corpus_emb, verbose=False)
|
| 419 |
+
|
| 420 |
+
# ================================================================================================
|
| 421 |
+
# Processs, print and save results
|
| 422 |
+
# ================================================================================================
|
| 423 |
+
|
| 424 |
+
# Convert the results to sorted list with transpose values
|
| 425 |
+
idxs_sims_tvs_list = midisim.idxs_sims_to_sorted_list(idxs, sims)
|
| 426 |
+
|
| 427 |
+
# Print corpus matches (and optionally) convert the final result to a handy list for further processing
|
| 428 |
+
corpus_matches_list = midisim.print_sorted_idxs_sims_list(idxs_sims_tvs_list,
|
| 429 |
+
corpus_midi_names,
|
| 430 |
+
return_as_list=True
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
# ================================================================================================
|
| 434 |
+
# Copy matched MIDIs from the MIDI corpus for listening and further evaluation and analysis
|
| 435 |
+
# ================================================================================================
|
| 436 |
+
|
| 437 |
+
# Copy matched corpus MIDI to a desired directory for easy evaluation and analysis
|
| 438 |
+
out_dir_path = midisim.copy_corpus_files(corpus_matches_list,
|
| 439 |
+
corpus_midis_dirs=['./Discover-MIDI-Dataset/MIDIs/'],
|
| 440 |
+
main_output_dir='Output-MIDI-Dataset',
|
| 441 |
+
sub_output_dir=os.path.splitext(os.path.basename(fa))[0],
|
| 442 |
+
verbose=False
|
| 443 |
+
)
|
| 444 |
+
# ================================================================================================
|
| 445 |
+
```
|
| 446 |
+
|
| 447 |
+
***
|
| 448 |
+
|
| 449 |
+
## midisim functions reference lists
|
| 450 |
+
|
| 451 |
+
### Main functions
|
| 452 |
+
|
| 453 |
+
- ```midisim.copy_corpus_files``` — *Copy or synchronize MIDI corpus files from a source directory to a target corpus location.*
|
| 454 |
+
- ```midisim.cosine_similarity_topk``` — *Compute cosine similarities between a query embedding and a set of embeddings and return the top‑K matches.*
|
| 455 |
+
- ```midisim.download_all_embeddings``` — *Download an entire embeddings dataset snapshot from a Hugging Face dataset repository to a local directory.*
|
| 456 |
+
- ```midisim.download_embeddings``` — *Download a single precomputed embeddings `.npy` file from a Hugging Face dataset repository.*
|
| 457 |
+
- ```midisim.download_model``` — *Download a pre-trained model checkpoint file from a Hugging Face model repository to a local directory.*
|
| 458 |
+
- ```midisim.get_embeddings_bf16``` — *Load or convert embeddings into bfloat16 format for memory-efficient inference on supported hardware.*
|
| 459 |
+
- ```midisim.idxs_sims_to_sorted_list``` — *Convert parallel index and similarity arrays into a single sorted list of (index, similarity) pairs ordered by similarity.*
|
| 460 |
+
- ```midisim.load_embeddings``` — *Load a saved NumPy embeddings file and return the arrays of MIDI names and corresponding embedding vectors.*
|
| 461 |
+
- ```midisim.load_model``` — *Construct a Transformer model, load weights from a checkpoint, move it to the requested device, and return the model with an AMP autocast context and dtype.*
|
| 462 |
+
- ```midisim.masked_mean_pool``` — *Compute a masked mean pooling over sequence embeddings, ignoring padded positions via a boolean or numeric mask.*
|
| 463 |
+
- ```midisim.midi_to_tokens``` — *Convert a single-track MIDI file into one or more compact integer token sequences (with optional transpositions) suitable for model input.*
|
| 464 |
+
- ```midisim.pad_and_mask``` — *Pad a batch of variable-length token sequences to a common length and produce an attention/mask tensor indicating real tokens vs padding.*
|
| 465 |
+
- ```midisim.print_sorted_idxs_sims_list``` — *Pretty-print a sorted list of (index, similarity) pairs, optionally annotating entries with filenames or metadata.*
|
| 466 |
+
- ```midisim.save_embeddings``` — *Save a list of name strings and their corresponding embedding vectors into a structured NumPy array and optionally persist it to disk.*
|
| 467 |
+
|
| 468 |
+
### Helper functions
|
| 469 |
+
|
| 470 |
+
- ```midisim.helpers.get_package_models``` — *Return a sorted list of packaged model files and their paths.*
|
| 471 |
+
- ```midisim.helpers.get_package_embeddings``` — *Return a sorted list of packaged embedding files and their paths.*
|
| 472 |
+
- ```midisim.helpers.get_normalized_midi_md5_hash``` — *Compute original and normalized MD5 hashes for a MIDI file.*
|
| 473 |
+
- ```midisim.helpers.normalize_midi_file``` — *Normalize a MIDI file and write the result to disk.*
|
| 474 |
+
- ```midisim.helpers.install_apt_package``` — *Idempotently install an apt package with retries and optional python‑apt.*
|
| 475 |
+
|
| 476 |
+
***
|
| 477 |
+
|
| 478 |
+
## Limitations
|
| 479 |
+
|
| 480 |
+
* Current code and models support only MIDI music elements similarity (start-times, durations and pitches)
|
| 481 |
+
* MIDI channels, instruments, velocities and drums similarites are not currently supported due to complexity and practicality considerations
|
| 482 |
+
* Current pre-trained models are limited by 3k sequence length (~1000 MIDI music notes) so long running MIDIs can only be analyzed in chunks
|
| 483 |
+
* Solo drum track MIDIs are not currently supported and can't be analyzed
|
| 484 |
+
|
| 485 |
+
***
|
| 486 |
+
|
| 487 |
+
## Citations
|
| 488 |
+
|
| 489 |
+
```bibtex
|
| 490 |
+
@misc{project_los_angeles_2025,
|
| 491 |
+
author = { Project Los Angeles },
|
| 492 |
+
title = { midisim (Revision 707e311) },
|
| 493 |
+
year = 2025,
|
| 494 |
+
url = { https://huggingface.co/projectlosangeles/midisim },
|
| 495 |
+
doi = { 10.57967/hf/7383 },
|
| 496 |
+
publisher = { Hugging Face }
|
| 497 |
+
}
|
| 498 |
+
```
|
| 499 |
+
|
| 500 |
+
```bibtex
|
| 501 |
+
@misc{project_los_angeles_2025,
|
| 502 |
+
author = { Project Los Angeles },
|
| 503 |
+
title = { midisim-embeddings (Revision 8ebb453) },
|
| 504 |
+
year = 2025,
|
| 505 |
+
url = { https://huggingface.co/datasets/projectlosangeles/midisim-embeddings },
|
| 506 |
+
doi = { 10.57967/hf/7382 },
|
| 507 |
+
publisher = { Hugging Face }
|
| 508 |
+
}
|
| 509 |
+
```
|
| 510 |
+
|
| 511 |
+
```bibtex
|
| 512 |
+
@misc{project_los_angeles_2025,
|
| 513 |
+
author = { Project Los Angeles },
|
| 514 |
+
title = { midisim-samples (Revision 6394ee9) },
|
| 515 |
+
year = 2025,
|
| 516 |
+
url = { https://huggingface.co/datasets/projectlosangeles/midisim-samples },
|
| 517 |
+
doi = { 10.57967/hf/7387 },
|
| 518 |
+
publisher = { Hugging Face }
|
| 519 |
+
}
|
| 520 |
+
```
|
| 521 |
+
|
| 522 |
+
```bibtex
|
| 523 |
+
@misc{project_los_angeles_2025,
|
| 524 |
+
author = { Project Los Angeles },
|
| 525 |
+
title = { Discover-MIDI-Dataset (Revision 0eaecb5) },
|
| 526 |
+
year = 2025,
|
| 527 |
+
url = { https://huggingface.co/datasets/projectlosangeles/Discover-MIDI-Dataset },
|
| 528 |
+
doi = { 10.57967/hf/7361 },
|
| 529 |
+
publisher = { Hugging Face }
|
| 530 |
+
}
|
| 531 |
+
```
|
| 532 |
+
|
| 533 |
***
|
| 534 |
|
| 535 |
### Project Los Angeles
|