-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_decoder.py
181 lines (162 loc) · 5.92 KB
/
main_decoder.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
"""
@author: Gaetan Hadjeres
"""
import importlib
import os
import shutil
from datetime import datetime
import click
import torch
from VQCPCB.data_processor.data_processor import DataProcessor
from VQCPCB.getters import get_dataloader_generator, get_encoder, get_data_processor, get_decoder
@click.command()
@click.option('-t', '--train', is_flag=True)
@click.option('-l', '--load', is_flag=True)
@click.option('-o', '--overfitted', is_flag=True,
help='Load over-fitted weights for the decoder instead of early-stopped.'
'Only used with -l')
@click.option('-c', '--config', type=click.Path(exists=True))
@click.option('-r', '--reharmonization', is_flag=True)
@click.option('--code_juxtaposition', is_flag=True)
@click.option('-n', '--num_workers', type=int, default=0)
def main(train,
load,
overfitted,
config,
reharmonization,
code_juxtaposition,
num_workers
):
# Use all gpus available
gpu_ids = [int(gpu) for gpu in range(torch.cuda.device_count())]
print(f'Using GPUs {gpu_ids}')
if len(gpu_ids) == 0:
device = 'cpu'
else:
device = 'cuda'
# Load config
config_path = config
config_module_name = os.path.splitext(config)[0].replace('/', '.')
config = importlib.import_module(config_module_name).config
# compute time stamp
if config['timestamp'] is not None:
timestamp = config['timestamp']
else:
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
config['timestamp'] = timestamp
if load:
model_dir = os.path.dirname(config_path)
else:
model_dir = f'models/{config["savename"]}_{timestamp}'
# ==== Load encoders ====
# load stack of encoders from top-most encoder (lastly trained)
config_encoder_path = config['config_encoder']
if config_encoder_path is None:
# Load any encoder w/ 16 code
config_encoder_path = 'VQCPCB/configs/encoder_random_16C.py'
config_encoder_module_name = os.path.splitext(config_encoder_path)[0].replace('/', '.')
config_encoder = importlib.import_module(config_encoder_module_name).config
config_encoder['quantizer_kwargs']['initialize'] = False
if config['config_encoder'] is None:
model_dir_encoder = None
else:
model_dir_encoder = os.path.dirname(config_encoder_path)
dataloader_generator = get_dataloader_generator(
dataset=config_encoder['dataset'],
training_method=config_encoder['training_method'],
dataloader_generator_kwargs=config_encoder['dataloader_generator_kwargs'],
)
encoder = get_encoder(model_dir=model_dir_encoder,
dataloader_generator=dataloader_generator,
config=config_encoder
)
if config['config_encoder'] is not None:
encoder.load(early_stopped=False, device=device)
# === Decoder ====
dataloader_generator = get_dataloader_generator(
dataset=config['dataset'],
training_method=config['training_method'],
dataloader_generator_kwargs=config['dataloader_generator_kwargs']
)
data_processor: DataProcessor = get_data_processor(
dataloader_generator=dataloader_generator,
data_processor_type=config['data_processor_type'],
data_processor_kwargs=config['data_processor_kwargs']
)
decoder = get_decoder(
model_dir=model_dir,
dataloader_generator=dataloader_generator,
data_processor=data_processor,
encoder=encoder,
decoder_type=config['decoder_type'],
decoder_kwargs=config['decoder_kwargs']
)
if load:
if overfitted:
decoder.load(early_stopped=False, device=device)
else:
decoder.load(early_stopped=True, device=device)
decoder.to(device)
if train:
# Copy .py config file in the save directory before training
if not load:
if not os.path.exists(model_dir):
os.makedirs(model_dir)
shutil.copy(config_path, f'{model_dir}/config.py')
decoder.to(device)
decoder.train_model(
batch_size=config['batch_size'],
num_batches=config['num_batches'],
num_epochs=config['num_epochs'],
lr=config['lr'],
schedule_lr=config['schedule_lr'],
plot=True,
num_workers=num_workers
)
num_examples = 0
for _ in range(num_examples):
if code_juxtaposition:
scores = decoder.generate(
temperature=1.0,
top_p=0.9,
top_k=0,
batch_size=3,
seed_set='val',
plot_attentions=False,
code_juxtaposition=True
)
scores = decoder.generate(temperature=0.95,
top_p=0.8,
top_k=0,
batch_size=3,
seed_set='val',
plot_attentions=False,
code_juxtaposition=False)
# for score in scores:
# score.show()
if reharmonization:
scores = decoder.generate_reharmonisation(
temperature=0.9,
top_p=0.8,
top_k=0,
num_reharmonisations=3)
# for score in scores:
# score.show()
# # Body code: need do check cluster before adding values
# start_cluster = 7
# end_cluster = 21
# pad_cluster = 12
#
# start_codes = [pad_cluster] * 5 + [start_cluster]
# end_codes = [end_cluster] + [pad_cluster] * 5
# body_codes = [1] * 16 # put what u want here
# scores = decoder.generate_alla_mano(
# start_codes=start_codes,
# end_codes=end_codes,
# body_codes=body_codes,
# temperature=1.2,
# )
# for score in scores:
# score.show()
if __name__ == '__main__':
main()