RSPRIMES1234 commited on
Commit
bf6ff20
·
verified ·
1 Parent(s): 581fb1a

Upload 4 files

Browse files
Files changed (4) hide show
  1. example_use.py +5 -0
  2. model.h5 +3 -0
  3. model.py +91 -0
  4. requirements.txt +3 -0
example_use.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from model import load_model
2
+
3
+ melody_generator = load_model("path/to/model.h5", "path/to/mapping.json")
4
+ seed = "60 _ 60 _ 67 _ 67 _ 69 _ 69 _ 67 _ _"
5
+ melody = melody_generator.generate_melody(seed, 500, 64, 0.3)
model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8e397acef2daf0f3f21fcc40297263423c5fa8148b324bdc985422ad293fb053
3
+ size 3802272
model.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow.keras as keras
2
+ import json
3
+ import numpy as np
4
+
5
+
6
+ class MelodyGenerator:
7
+ """
8
+ This class represents a melody generator. It uses a pre-trained model to generate new melodies based on a given seed.
9
+ """
10
+
11
+ def __init__(self, model_path="model.h5", mapping_path="mapping.json", sequence_length=64):
12
+ """
13
+ Initializes the MelodyGenerator object.
14
+
15
+ :param model_path: Path to the trained model (default: "model.h5").
16
+ :param mapping_path: Path to the mapping file for symbols to integers (default: "mapping.json").
17
+ :param sequence_length: The length of input sequences for the model (default: 64).
18
+ """
19
+ self.model = keras.models.load_model(model_path) # Load the pre-trained model
20
+ self.sequence_length = sequence_length # Store the sequence length
21
+
22
+ # Load the mappings from symbols (e.g., "60", "r", "_") to integers
23
+ with open(mapping_path, "r") as fp:
24
+ self._mappings = json.load(fp)
25
+
26
+ # Initialize the seed with the start symbol "/" repeated for the sequence length
27
+ self._start_symbols = ["/"] * sequence_length
28
+
29
+ def generate_melody(self, seed, num_steps, max_sequence_length, temperature):
30
+ """
31
+ Generates a melody based on the given seed.
32
+
33
+ :param seed: Initial sequence of musical symbols (e.g., "60 _ _ r").
34
+ :param num_steps: Number of steps (time units) to generate.
35
+ :param max_sequence_length: Maximum length of the input sequence for the model.
36
+ :param temperature: Controls the randomness of the generated melody. Higher temperature -> more random.
37
+ :return: The generated melody as a list of symbols.
38
+ """
39
+ seed = seed.split() # Split the seed into individual symbols
40
+ melody = seed # Initialize the melody with the seed
41
+ seed = self._start_symbols + seed # Prepend start symbols to the seed
42
+
43
+ # Convert seed symbols to their corresponding integer representation
44
+ seed = [self._mappings[symbol] for symbol in seed]
45
+
46
+ # Generate melody step by step
47
+ for _ in range(num_steps):
48
+ seed = seed[-max_sequence_length:] # Keep only the last max_sequence_length elements
49
+ onehot_seed = keras.utils.to_categorical(seed, num_classes=len(self._mappings)) # One-hot encode the seed
50
+ onehot_seed = onehot_seed[np.newaxis, ...] # Add a batch dimension
51
+
52
+ # Predict probabilities for the next symbol
53
+ probabilities = self.model.predict(onehot_seed)[0]
54
+
55
+ # Sample the next symbol based on temperature
56
+ output_int = self._sample_with_temperature(probabilities, temperature)
57
+ seed.append(output_int) # Add the new symbol to the seed
58
+
59
+ # Convert the integer back to its symbol representation
60
+ output_symbol = [k for k, v in self._mappings.items() if v == output_int][0]
61
+
62
+ # Check for end of sequence symbol
63
+ if output_symbol == "/":
64
+ break
65
+
66
+ melody.append(output_symbol)
67
+
68
+ return melody # Return the generated melody
69
+
70
+ def _sample_with_temperature(self, probabilities, temperature):
71
+ """
72
+ Samples an index from the given probabilities with temperature adjustment.
73
+
74
+ :param probabilities: List of probabilities for each symbol.
75
+ :param temperature: The temperature for sampling.
76
+ :return: The sampled index.
77
+ """
78
+ # Adjust probabilities with temperature
79
+ predictions = np.log(probabilities) / temperature
80
+ probabilities = np.exp(predictions) / np.sum(np.exp(predictions))
81
+
82
+ # Sample an index from the adjusted probabilities
83
+ choices = range(len(probabilities))
84
+ index = np.random.choice(choices, p=probabilities)
85
+
86
+ return index # Return the sampled index
87
+
88
+
89
+ # Helper function to load a MelodyGenerator instance
90
+ def load_model(model_path="model.h5", mapping_path="mapping.json"):
91
+ return MelodyGenerator(model_path, mapping_path)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ tensorflow==2.6.0
2
+ music21==7.1.0
3
+ numpy==1.19.5