-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
184 lines (155 loc) · 5.56 KB
/
train.py
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
# melodia/scripts/train.py
import argparse
import logging
from pathlib import Path
import tensorflow as tf
from typing import Optional, Tuple
from melodia.config import TrainingConfig, ModelConfig, DataConfig
from melodia.data.loader import MIDILoader
from melodia.data.processor import DataProcessor
from melodia.model.architecture import MelodiaModel
from melodia.training.trainer import Trainer
from melodia.generation.generator import MusicGenerator
from melodia.evaluation.metrics import MusicEvaluator
logger = logging.getLogger(__name__)
def setup_logging(log_dir: Path):
"""Set up logging configuration"""
log_dir.mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_dir / 'training.log'),
logging.StreamHandler()
]
)
def parse_args():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(description='Train Melodia model')
parser.add_argument('--data_dir', type=str, required=True,
help='Directory containing training MIDI files')
parser.add_argument('--model_dir', type=str, required=True,
help='Directory to save model checkpoints')
parser.add_argument('--config', type=str,
help='Path to configuration file')
# Training parameters
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--learning_rate', type=float, default=0.001)
parser.add_argument('--validation_split', type=float, default=0.1)
# Model parameters
parser.add_argument('--embedding_dim', type=int, default=512)
parser.add_argument('--num_layers', type=int, default=6)
parser.add_argument('--num_heads', type=int, default=8)
# Generation parameters
parser.add_argument('--generate_samples', action='store_true',
help='Generate samples during training')
parser.add_argument('--num_samples', type=int, default=5)
return parser.parse_args()
def prepare_data(
data_dir: Path,
config: TrainingConfig
) -> Tuple[tf.data.Dataset, Optional[tf.data.Dataset]]:
"""Prepare training and validation datasets"""
logger.info("Preparing datasets...")
# Create data config
data_config = DataConfig(
max_sequence_length=512,
min_sequence_length=64,
valid_time_signatures=[(4, 4), (3, 4), (6, 8)]
)
# Load MIDI files
loader = MIDILoader(data_config)
processor = DataProcessor(config)
midi_files = list(data_dir.glob('**/*.mid'))
if not midi_files:
raise ValueError(f"No MIDI files found in {data_dir}")
# Process all files
all_events = []
for midi_file in midi_files:
try:
events = loader.read_midi(midi_file)
if events:
all_events.extend(events)
except Exception as e:
logger.warning(f"Error processing {midi_file}: {str(e)}")
# Create datasets
if config.validation_split > 0:
split_idx = int(len(all_events) * (1 - config.validation_split))
train_events = all_events[:split_idx]
val_events = all_events[split_idx:]
train_dataset = processor.prepare_dataset(
train_events,
batch_size=config.batch_size,
shuffle=True
)
val_dataset = processor.prepare_dataset(
val_events,
batch_size=config.batch_size,
shuffle=False
)
return train_dataset, val_dataset
train_dataset = processor.prepare_dataset(
all_events,
batch_size=config.batch_size,
shuffle=True
)
return train_dataset, None
def main():
"""Main training function"""
args = parse_args()
# Create directories
model_dir = Path(args.model_dir)
model_dir.mkdir(parents=True, exist_ok=True)
# Set up logging
setup_logging(model_dir / 'logs')
# Create configurations
model_config = ModelConfig(
embedding_dim=args.embedding_dim,
num_layers=args.num_layers,
num_heads=args.num_heads
)
training_config = TrainingConfig(
batch_size=args.batch_size,
learning_rate=args.learning_rate,
max_epochs=args.epochs,
validation_split=args.validation_split,
checkpoint_dir=str(model_dir / 'checkpoints')
)
# Prepare data
train_dataset, val_dataset = prepare_data(
Path(args.data_dir),
training_config
)
# Create model
model = MelodiaModel(model_config)
# Create generator and evaluator if needed
generator = None
evaluator = None
if args.generate_samples:
generator = MusicGenerator(model, training_config)
evaluator = MusicEvaluator()
# Create trainer
trainer = Trainer(
model=model,
config=training_config,
generator=generator,
evaluator=evaluator
)
# Train model
logger.info("Starting training...")
try:
trainer.train(
train_dataset=train_dataset,
validation_dataset=val_dataset
)
logger.info("Training completed successfully")
except KeyboardInterrupt:
logger.info("Training interrupted by user")
except Exception as e:
logger.error(f"Training failed: {str(e)}")
finally:
# Save final model state
trainer.save_training_state()
if __name__ == '__main__':
main()