def main(exp, frame_sizes, generate_from, **params): params = dict(default_params, exp=exp, frame_sizes=frame_sizes, generate_from=generate_from, **params) model = SampleRNN( frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], nb_classes=params['nb_classes'], weight_norm=params['weight_norm'], ) # model = SampleRNN([16, 4], 2, 1024, True, 256, True) print('Loading saved model' + params['generate_from']) checkpoint = torch.load(params['generate_from']) temporary_dict = {} for k, v in checkpoint.items(): temporary_dict[k[6:]] = v checkpoint = temporary_dict model.load_state_dict(checkpoint) if not os.path.exists(params['generate_to']): os.mkdir(params['generate_to']) print(params['cond']) generator = GeneratorPlugin(params['generate_to'], params['n_samples'], params['sample_length'], params['sample_rate'], params['nb_classes'], params['cond']) generator.register_generate(model.cuda(), params['cuda']) generator.epoch(exp)
def main(exp, frame_sizes, dataset, **params): params = dict( default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params ) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) model = SampleRNN( frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm'] ) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() optimizer = gradient_clipping(torch.optim.Adam(predictor.parameters())) test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] data_loader = make_data_loader(model.lookback, params) train_data_loader = data_loader(0, val_split, eval=False) val_data_loader = data_loader(val_split, test_split, eval=True) test_data_loader = data_loader(test_split, 1, eval=True)
def main(exp, frame_sizes, dataset, **params): params = dict(default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm']) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() optimizer = gradient_clipping(torch.optim.Adam(predictor.parameters())) data_loader = make_data_loader(model.lookback, params) test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] checkpoints_path = os.path.join(results_path, 'checkpoints') samples_path = os.path.join(results_path, 'samples') trainer = Trainer(predictor, sequence_nll_loss_bits, optimizer, data_loader(0, val_split, eval=False), checkpoints_path, samples_path, params['n_samples'], params['sample_length'], params['sample_rate'], data_loader(val_split, test_split, eval=True), data_loader(test_split, 1, eval=True), cuda=params['cuda']) checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path) if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data trainer.epochs = epoch trainer.iterations = iteration predictor.load_state_dict(state_dict) trainer.run(params['epoch_limit'])
def main(exp, frame_sizes, dataset, **params): params = dict(default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) # Save samplernn parameters in .json for future audio generation import json with open(os.path.join(results_path, 'sample_rnn_params.json'), 'w') as fp: json.dump(params, fp, sort_keys=True, indent=4) model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm']) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() optimizer = gradient_clipping(torch.optim.Adam(predictor.parameters())) data_loader = make_data_loader(model.lookback, params) test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] trainer = Trainer(predictor, sequence_nll_loss_bits, optimizer, data_loader(0, val_split, eval=False), cuda=params['cuda']) checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path) if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data trainer.epochs = epoch trainer.iterations = iteration predictor.load_state_dict(state_dict) else: trainer.epochs = 0 trainer.iterations = 0 torch.save(predictor, os.path.join(checkpoints_path, "pytorch_model.bin")) # else: # print("***** Saving fine-tuned model *****") # output_model_file = os.path.join(params['results_path'], "pytorch_model.bin") # if params['cuda']: # torch.save(predictor, output_model_file) # else: # torch.save(predictor, output_model_file) trainer.register_plugin( TrainingLossMonitor(smoothing=params['loss_smoothing'])) trainer.register_plugin( ValidationPlugin(data_loader(val_split, test_split, eval=True), data_loader(test_split, 1, eval=True))) trainer.register_plugin(AbsoluteTimeMonitor()) trainer.register_plugin( SaverPlugin(checkpoints_path, params['keep_old_checkpoints'])) trainer.register_plugin( GeneratorPlugin(os.path.join(results_path, 'samples'), params['n_samples'], params['sample_length'], params['sample_rate'], params['sampling_temperature'])) trainer.register_plugin( Logger(['training_loss', 'validation_loss', 'test_loss', 'time'])) trainer.register_plugin( StatsPlugin(results_path, iteration_fields=[ 'training_loss', ('training_loss', 'running_avg'), 'time' ], epoch_fields=['validation_loss', 'test_loss', 'time'], plots={ 'loss': { 'x': 'iteration', 'ys': [ 'training_loss', ('training_loss', 'running_avg'), 'validation_loss', 'test_loss', ], 'log_y': True } })) init_comet(params, trainer) trainer.run(params['epoch_limit'])
def main(exp, frame_sizes, dataset, **params): params = dict(default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params) os.environ['CUDA_VISIBLE_DEVICES'] = params['gpu'] results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm'], dropout=params['dropout']) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() optimizer = gradient_clipping( torch.optim.Adam(predictor.parameters(), lr=params['lr'])) data_loader = make_data_loader(model.lookback, params) test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] criterion = sequence_nll_loss_bits checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path, params) if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data start_epoch = int(epoch) global_step = iteration start_epoch = iteration predictor.load_state_dict(state_dict) else: start_epoch = 0 global_step = 0 #writer = SummaryWriter("runs/{}-{}".format(params['dataset'], str(datetime.datetime.now()).split('.')[0].replace(' ', '-'))) writer = SummaryWriter( os.path.join( results_path, "{}-{}".format( params['dataset'], str(datetime.datetime.now()).split('.')[0].replace(' ', '-')))) dataset_train = data_loader(0, val_split, eval=False) dataset_val = data_loader(val_split, test_split, eval=True) dataset_test = data_loader(test_split, 1, eval=True) generator = Generator(predictor.model, params['cuda']) best_val_loss = 10000000000000 for e in range(start_epoch, int(params['epoch_limit'])): for i, data in enumerate(dataset_train): batch_inputs = data[:-1] batch_target = data[-1] def wrap(input): if torch.is_tensor(input): input = torch.autograd.Variable(input) if params['cuda']: input = input.cuda() return input batch_inputs = list(map(wrap, batch_inputs)) batch_target = torch.autograd.Variable(batch_target) if params['cuda']: batch_target = batch_target.cuda() plugin_data = [None, None] def closure(): batch_output = predictor(*batch_inputs) loss = criterion(batch_output, batch_target) loss.backward() if plugin_data[0] is None: plugin_data[0] = batch_output.data plugin_data[1] = loss.data return loss optimizer.zero_grad() optimizer.step(closure) train_loss = plugin_data[1] # stats: iteration writer.add_scalar('train/train loss', train_loss, global_step) print("E:{:03d}-S{:05d}: Loss={}".format(e, i, train_loss)) global_step += 1 # validation: per epoch predictor.eval() with torch.no_grad(): loss_sum = 0 n_examples = 0 for data in dataset_val: batch_inputs = data[:-1] batch_target = data[-1] batch_size = batch_target.size()[0] def wrap(input): if torch.is_tensor(input): input = torch.autograd.Variable(input) if params['cuda']: input = input.cuda() return input batch_inputs = list(map(wrap, batch_inputs)) batch_target = torch.autograd.Variable(batch_target) if params['cuda']: batch_target = batch_target.cuda() batch_output = predictor(*batch_inputs) loss_sum += criterion(batch_output, batch_target).item() * batch_size n_examples += batch_size val_loss = loss_sum / n_examples writer.add_scalar('validation/validation loss', val_loss, global_step) print("== Validation Step E:{:03d}: Loss={} ==".format( e, val_loss)) predictor.train() # saver: epoch last_pattern = 'ep{}-it{}' best_pattern = 'best-ep{}-it{}' if not params['keep_old_checkpoints']: pattern = os.path.join(checkpoints_path, last_pattern.format('*', '*')) for file_name in glob(pattern): os.remove(file_name) torch.save( predictor.state_dict(), os.path.join(checkpoints_path, last_pattern.format(e, global_step))) cur_val_loss = val_loss if cur_val_loss < best_val_loss: pattern = os.path.join(checkpoints_path, last_pattern.format('*', '*')) for file_name in glob(pattern): os.remove(file_name) torch.save( predictor.state_dict(), os.path.join(checkpoints_path, best_pattern.format(e, global_step))) best_val_loss = cur_val_loss generate_sample(generator, params, writer, global_step, results_path, e) # generate final results generate_sample(generator, params, None, global_step, results_path, 0)
def main(exp, frame_sizes, dataset, **params): params = dict(default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm']) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() optimizer = gradient_clipping(torch.optim.Adam(predictor.parameters())) data_loader = make_data_loader(model.lookback, params) test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] trainer = Trainer(predictor, sequence_nll_loss_bits, optimizer, data_loader(0, val_split, eval=False), cuda=params['cuda']) checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path) if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data trainer.epochs = epoch trainer.iterations = iteration predictor.load_state_dict(state_dict) trainer.register_plugin( TrainingLossMonitor(smoothing=params['loss_smoothing'])) trainer.register_plugin( ValidationPlugin(data_loader(val_split, test_split, eval=True), data_loader(test_split, 1, eval=True))) trainer.register_plugin(AbsoluteTimeMonitor()) trainer.register_plugin( SaverPlugin(checkpoints_path, params['keep_old_checkpoints'])) trainer.register_plugin( GeneratorPlugin(os.path.join(results_path, 'samples'), params['n_samples'], params['sample_length'], params['sample_rate'])) trainer.register_plugin( Logger(['training_loss', 'validation_loss', 'test_loss', 'time'])) trainer.register_plugin( StatsPlugin(results_path, iteration_fields=[ 'training_loss', ('training_loss', 'running_avg'), 'time' ], epoch_fields=['validation_loss', 'test_loss', 'time'], plots={ 'loss': { 'x': 'iteration', 'ys': [ 'training_loss', ('training_loss', 'running_avg'), 'validation_loss', 'test_loss', ], 'log_y': True } })) init_comet(params, trainer) trainer.run(params['epoch_limit'])
def main(exp, dataset, **params): params = dict(default_params, exp=exp, dataset=dataset, **params) print(params) storage_client = None bucket = None path = os.path.join(params['datasets_path'], params['dataset']) if params['bucket']: storage_client = storage.Client() bucket = Bucket(storage_client, params['bucket']) preload_dataset(path, storage_client, bucket) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) (quantize, dequantize) = quantizer(params['q_method']) model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm']) predictor = Predictor(model, dequantize) if params['cuda'] is not False: print(params['cuda']) model = model.cuda() predictor = predictor.cuda() optimizer = gradient_clipping( torch.optim.Adam(predictor.parameters(), lr=params['learning_rate'])) data_loader = make_data_loader(path, model.lookback, quantize, params) test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] trainer = Trainer(predictor, sequence_nll_loss_bits, optimizer, data_loader(0, val_split, eval=False), cuda=params['cuda']) checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path, storage_client, bucket) if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data trainer.epochs = epoch trainer.iterations = iteration predictor.load_state_dict(state_dict) trainer.register_plugin( TrainingLossMonitor(smoothing=params['loss_smoothing'])) trainer.register_plugin( ValidationPlugin(data_loader(val_split, test_split, eval=True), data_loader(test_split, 1, eval=True))) trainer.register_plugin(SchedulerPlugin(params['lr_scheduler_step'])) def upload(file_path): if bucket is None: return name = file_path.replace(os.path.abspath(os.curdir) + '/', '') blob = Blob(name, bucket) try: blob.upload_from_filename(file_path, timeout=300) except Exception as e: print(str(e)) trainer.register_plugin(AbsoluteTimeMonitor()) samples_path = os.path.join(results_path, 'samples') trainer.register_plugin( SaverPlugin(checkpoints_path, params['keep_old_checkpoints'], upload)) trainer.register_plugin( GeneratorPlugin(samples_path, params['n_samples'], params['sample_length'], params['sample_rate'], params['q_levels'], dequantize, params['sampling_temperature'], upload=upload)) trainer.register_plugin( Logger(['training_loss', 'validation_loss', 'test_loss', 'time'])) trainer.register_plugin( StatsPlugin( results_path, iteration_fields=[ 'training_loss', #('training_loss', 'running_avg'), 'time' ], epoch_fields=[ 'training_loss', ('training_loss', 'running_avg'), 'validation_loss', 'test_loss', 'time' ], plots={ 'loss': { 'x': 'iteration', 'ys': [ 'training_loss', # ('training_loss', 'running_avg'), 'validation_loss', 'test_loss' ], 'log_y': True } })) init_comet(params, trainer, samples_path, params['n_samples'], params['sample_rate']) trainer.run(params['epoch_limit'])
def main(checkpoint, **args): task_id = setup_logging( 'gen', logging.NOTSET if args.get('debug', False) else logging.INFO) params = dict( { 'n_rnn': 3, 'dim': 1024, 'learn_h0': False, 'q_levels': 256, 'weight_norm': True, 'frame_sizes': [16, 16, 4], 'sample_rate': 16000, 'n_samples': 1, 'sample_length': 16000 * 60 * 4, 'sampling_temperature': 1, 'q_method': QMethod.LINEAR, }, exp=checkpoint, **args) logging.info(str(params)) logging.info('booting') # dataset = storage_client.list_blobs(bucket, prefix=path) # for blob in dataset: # blob.download_to_filename(blob.name) bucket = None if args['bucket']: logging.debug('setup google storage bucket {}'.format(args['bucket'])) storage_client = storage.Client() bucket = Bucket(storage_client, args['bucket']) preload_checkpoint(checkpoint, storage_client, bucket) results_path = os.path.abspath( os.path.join(checkpoint, os.pardir, os.pardir, task_id)) ensure_dir_exists(results_path) checkpoint = os.path.abspath(checkpoint) tmp_pretrained_state = torch.load( checkpoint, map_location=lambda storage, loc: storage.cuda(0) if args['cuda'] else storage) # Load all tensors onto GPU 1 # torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1)) pretrained_state = OrderedDict() for k, v in tmp_pretrained_state.items(): # Delete "model." from key names since loading the checkpoint automatically attaches it layer_name = k.replace("model.", "") pretrained_state[layer_name] = v # print("k: {}, layer_name: {}, v: {}".format(k, layer_name, np.shape(v))) # Create model with same parameters as used in training model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm']) if params['cuda']: model = model.cuda() # Load pretrained model model.load_state_dict(pretrained_state) def upload(file_path): if bucket is None: return # remove prefix /app name = file_path.replace(os.path.abspath(os.curdir) + '/', '') blob = Blob(name, bucket) logging.info('uploading {}'.format(name)) blob.upload_from_filename(file_path) (_, dequantize) = quantizer(params['q_method']) gen = Gen(Runner(model), params['cuda']) gen.register_plugin( GeneratorPlugin(results_path, params['n_samples'], params['sample_length'], params['sample_rate'], params['q_levels'], dequantize, params['sampling_temperature'], upload)) gen.run()
# Load pretrained model model.load_state_dict(new_pretrained_state) # Generate Plugin num_samples = 1 # params['n_samples'] sample_length = params['sample_length'] sample_rate = params['sample_rate'] sampling_temperature = params['sampling_temperature'] # Override from our options sample_length = sample_rate * int(options.length) print("Number samples: {}, sample_length: {}, sample_rate: {}".format(num_samples, sample_length, sample_rate)) print("Generating %d seconds of audio" % (sample_length / sample_rate)) generator = GeneratorPlugin(GENERATED_PATH, num_samples, sample_length, sample_rate, sampling_temperature) # Call new register function to accept the trained model and the cuda setting generator.register_generate(model.cuda(), params['cuda']) # Generate new audio # $$$ check if we already have generated audio and increment the file name generator.epoch(OUTPUT_NAME) GENERATED_FILEPATH = GENERATED_PATH + "ep" + OUTPUT_NAME + "-s1.wav" print("Saved audio to %s " % GENERATED_FILEPATH) if options.output: print("Moving to %s" % options.output) os.rename(GENERATED_FILEPATH, options.output)
def main(frame_sizes, **params): use_cuda = torch.cuda.is_available() params = dict(default_params, frame_sizes=frame_sizes, **params) # Redefine parameters listed in the experiment directory and separated with '~' for i in params['model'].split('/')[1].split('~'): param = i.split(':') if param[0] in params: params[param[0]] = as_type(param[1], type(params[param[0]])) # Define npy file names with maximum and minimum values of de-normalized conditioners npy_name_min_max_cond = 'npy_datasets/min_max' + params['norm_ind'] * '_ind' + (not params['norm_ind']) * '_joint' \ + params['static_spk'] * '_static' + '.npy' # Define npy file name with array of unique speakers in dataset npy_name_spk_id = 'npy_datasets/spk_id.npy' # Get file names from partition's list file_names = open( str(params['datasets_path']) + 'generate_cond_gina.list', 'r').read().splitlines() spk_names = open( str(params['datasets_path']) + 'generate_spk_gina.list', 'r').read().splitlines() datasets_path = os.path.join(params['datasets_path'], params['cond_set']) spk = np.load(npy_name_spk_id) if len(spk_names) != len(file_names): print( 'Length of speaker file do not match length of conditioner file.') quit() print('Generating', len(file_names), 'audio files') for i in range(len(file_names)): print('Generating Audio', i) print('Generating...', file_names[i]) # Load CC conditioner c = np.loadtxt(datasets_path + file_names[i] + '.cc') # Load LF0 conditioner f0file = np.loadtxt(datasets_path + file_names[i] + '.lf0') f0, _ = interpolation(f0file, -10000000000) f0 = f0.reshape(f0.shape[0], 1) # Load FV conditioner fvfile = np.loadtxt(datasets_path + file_names[i] + '.gv') fv, uv = interpolation(fvfile, 1e3) num_fv = fv.shape[0] uv = uv.reshape(num_fv, 1) fv = fv.reshape(num_fv, 1) # Load speaker conditioner speaker = np.where(spk == spk_names[i])[0][0] cond = np.concatenate((c, f0), axis=1) cond = np.concatenate((cond, fv), axis=1) cond = np.concatenate((cond, uv), axis=1) # Load maximum and minimum of de-normalized conditioners min_cond = np.load(npy_name_min_max_cond)[0] max_cond = np.load(npy_name_min_max_cond)[1] # Normalize conditioners with absolute maximum and minimum for each speaker of training partition if params['norm_ind']: print( 'Normalizing conditioners for each speaker of training dataset' ) cond = (cond - min_cond[speaker]) / (max_cond[speaker] - min_cond[speaker]) else: print('Normalizing conditioners jointly') cond = (cond - min_cond) / (max_cond - min_cond) print('Shape cond', cond.shape) if params['look_ahead']: delayed = np.copy(cond) delayed[:-1, :] = delayed[1:, :] cond = np.concatenate((cond, delayed), axis=1) print('Shape cond after look ahead', cond.shape) print(cond.shape) seed = params.get('seed') init_random_seed(seed, use_cuda) spk_dim = len([ i for i in os.listdir( os.path.join(params['datasets_path'], params['cond_set'])) if os.path.islink( os.path.join(params['datasets_path'], params['cond_set']) + '/' + i) ]) print('Start Generate SampleRNN') model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], ulaw=params['ulaw'], weight_norm=params['weight_norm'], cond_dim=params['cond_dim'] * (1 + params['look_ahead']), spk_dim=spk_dim, qrnn=params['qrnn']) print(model) if use_cuda: model = model.cuda() predictor = Predictor(model).cuda() else: predictor = Predictor(model) f_name = params['model'] model_data = load_model(f_name) if model_data is None: sys.exit('ERROR: Model not found in' + str(f_name)) (state_dict, epoch_index, iteration) = model_data print('OK: Read model', f_name, '(epoch:', epoch_index, ')') print(state_dict) predictor.load_state_dict(state_dict) original_name = file_names[i].split('/')[1] if original_name == "..": original_name = file_names[i].split('/')[3] generator = RunGenerator(model=model, sample_rate=params['sample_rate'], cuda=use_cuda, epoch=epoch_index, cond=cond, spk_list=spk, speaker=speaker, checkpoints_path=f_name, original_name=original_name) generator(params['n_samples'], params['sample_length'], cond, speaker)
def main(exp, frame_sizes, dataset, **params): print('main called') params = dict( default_params, # exp='TEST', frame_sizes=[16,4], dataset='piano', exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) model = SampleRNN(frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm']) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() optimizer = torch.optim.Adam(predictor.parameters()) # optimizer = gradient_clipping(torch.optim.Adam(predictor.parameters())) loss = sequence_nll_loss_bits test_split = 1 - params['test_frac'] val_split = test_split - params['val_frac'] data_loader = make_data_loader(model.lookback, params) train_loader = data_loader(0, val_split, eval=False) val_loader = data_loader(val_split, test_split, eval=True) test_loader = data_loader(test_split, 1, eval=True) trainer = create_supervised_trainer(predictor, optimizer, loss, params['cuda']) evaluator = create_supervised_evaluator(predictor, metrics={'nll': Loss(loss)}) # @trainer.on(Events.ITERATION_COMPLETED) # def log_training_loss(trainer): # print("Epoch[{}] Loss: {:.2f}".format( # trainer.state.epoch, trainer.state.output)) @trainer.on(Events.EPOCH_COMPLETED) def log_training_results(trainer): evaluator.run(train_loader) metrics = evaluator.state.metrics print("Training Results - Epoch: {} Avg loss: {:.2f}".format( trainer.state.epoch, metrics['nll'])) @trainer.on(Events.EPOCH_COMPLETED) def log_validation_results(trainer): evaluator.run(val_loader) metrics = evaluator.state.metrics print("Validation Results - Epoch: {} Avg loss: {:.2f}".format( trainer.state.epoch, metrics['nll'])) trainer.run(train_loader, max_epochs=2) print('train complete!')
if data_name == 'cognimuse': params['seq2seq_model_type'] = SEQ2SEQ_MODEL_TYPE # Delete "model." from key names since loading the checkpoint automatically attaches it to the key names print("Load model...") pretrained_state = torch.load(PRETRAINED_PATH) new_pretrained_state = OrderedDict() for k, v in pretrained_state.items(): layer_name = k.replace("model.", "") new_pretrained_state[layer_name] = v # print("k: {}, layer_name: {}, v: {}".format(k, layer_name, np.shape(v))) # Load pretrained model model.load_state_dict(new_pretrained_state) model = model.cuda() pretrained_state_cnnseq2sample = torch.load(PRETRAINED_PATH_CNNSEQ2SAMPLE) trim_model_name = False # True if num_batches_tracked issue -> means Pytorch version is not 0.4.1 model_cnnseq2sample = CNNSeq2SampleRNN(params, trim_model_name=trim_model_name).cuda() model_cnnseq2sample.load_state_dict(pretrained_state_cnnseq2sample) # Generate Plugin print("Generate Plugin...") num_samples = 42 #(max) # params['n_samples'] sample_length = params['sample_length'] sample_rate = params['sample_rate'] print("Number samples: {}, sample_length: {}, sample_rate: {}".format( num_samples, sample_length, sample_rate)) generator = GeneratorCNNSeq2SamplePlugin(GENERATED_PATH, num_samples,
def main(exp, frame_sizes, dataset, **params): params = dict( default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params ) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) # Save samplernn parameters in .json for future audio generation import json with open(os.path.join(results_path, 'sample_rnn_params.json'), 'w') as fp: json.dump(params, fp, sort_keys=True, indent=4) # Model model = SampleRNN( frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], weight_norm=params['weight_norm'], batch_size=params['batch_size'] ) print("CUDA num: {}".format(torch.cuda.device_count())) predictor = Predictor(model) if params['cuda']: model = model.cuda() predictor = predictor.cuda() model_cnnseq2sample = CNNSeq2SampleRNN(params).cuda() optimizer = gradient_clipping(torch.optim.Adam(predictor.parameters())) data_loader = make_data_loader(model.lookback, params) data_loader_test = make_data_loader(model.lookback, params, npz_filename=params['npz_filename_test']) # test_split = 1 - params['test_frac'] # val_split = test_split - params['val_frac'] trainer = Trainer( predictor, model_cnnseq2sample, sequence_nll_loss_bits, optimizer, # data_loader(0, val_split, eval=False), data_loader(0, 1, eval=False), cuda=params['cuda'] ) checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path) checkpoint_data_cnnseq2sample = load_last_checkpoint(checkpoints_path, model_type='cnnseq2sample') if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data (state_dict_cnnseq2sample, epoch, iteration) = checkpoint_data_cnnseq2sample trainer.epochs = epoch trainer.iterations = iteration predictor.load_state_dict(state_dict) model_cnnseq2sample.load_state_dict(state_dict_cnnseq2sample) trainer.register_plugin(TrainingLossMonitor( smoothing=params['loss_smoothing'] )) trainer.register_plugin(ValidationPlugin( # data_loader(val_split, test_split, eval=True), # data_loader_test(0, 1, eval=True) data_loader_test(0, params['val_frac'], eval=True), data_loader_test(params['val_frac'], 1, eval=True) # data_loader(test_split, 1, eval=True) )) trainer.register_plugin(AbsoluteTimeMonitor()) trainer.register_plugin(SaverPlugin( checkpoints_path, params['keep_old_checkpoints'] )) trainer.register_plugin(GeneratorPlugin( os.path.join(results_path, 'samples'), params['n_samples'], params['sample_length'], params['sample_rate'] )) trainer.register_plugin( Logger([ 'training_loss', 'validation_loss', 'test_loss', 'time' ]) ) trainer.register_plugin(StatsPlugin( results_path, iteration_fields=[ 'training_loss', ('training_loss', 'running_avg'), 'time' ], epoch_fields=[ 'validation_loss', 'test_loss', 'time' ], plots={ 'loss': { 'x': 'iteration', 'ys': [ 'training_loss', ('training_loss', 'running_avg'), 'validation_loss', 'test_loss', ], 'log_y': True } } )) trainer.run(params['epoch_limit'])
samples_path = os.path.join(modeldir, "gen_samples") os.makedirs(samples_path, exist_ok=True) # sys.stderr.write("available models are: {}".format(listdir(modeldir))) modelpath = os.path.join(modeldir, modelname) srnn_model1 = SampleRNN(frame_sizes=[4, 16], n_rnn=2, dim=1024, learn_h0=True, q_levels=256, weight_norm=True) if torch.cuda.is_available(): srnn_model1 = srnn_model1.cuda() predictor1 = Predictor(srnn_model1) if torch.cuda.is_available(): predictor1 = predictor1.cuda() if torch.cuda.is_available(): predictor1.load_state_dict(torch.load(modelpath)['model']) else: predictor1.load_state_dict( torch.load(modelpath, map_location='cpu')['model']) print("model loaded successfully!") generate = Generator(srnn_model1, True)
def main(exp, frame_sizes, dataset, **params): scheduler = True use_cuda = torch.cuda.is_available() print('Start Sample-RNN') params = dict( default_params, exp=exp, frame_sizes=frame_sizes, dataset=dataset, **params ) seed = params.get('seed') init_random_seed(seed, use_cuda) results_path = setup_results_dir(params) tee_stdout(os.path.join(results_path, 'log')) spk_dim = len([i for i in os.listdir(os.path.join(params['datasets_path'], params['dataset'])) if os.path.islink(os.path.join(params['datasets_path'], params['dataset']) + '/' + i)]) print('Create model') model = SampleRNN( frame_sizes=params['frame_sizes'], n_rnn=params['n_rnn'], dim=params['dim'], learn_h0=params['learn_h0'], q_levels=params['q_levels'], ulaw=params['ulaw'], weight_norm=params['weight_norm'], cond_dim=params['cond_dim']*(1+params['look_ahead']), spk_dim=spk_dim, qrnn=params['qrnn'] ) if use_cuda: model = model.cuda() predictor = Predictor(model).cuda() else: predictor = Predictor(model) print('Done!') f_name = params['model'] if f_name is not None: print('pre train with', f_name) model_data = load_model(f_name) if model_data is None: sys.exit('ERROR: Model not found in' + str(f_name)) (state_dict, epoch_index, iteration) = model_data print('OK: Read model', f_name, '(epoch:', epoch_index, ')') print(state_dict) predictor.load_state_dict(state_dict) print('predictor', predictor) for name, param in predictor.named_parameters(): print(name, param.size()) optimizer = torch.optim.Adam(predictor.parameters(), lr=params['learning_rate']) if params['scheduler']: scheduler = MultiStepLR(optimizer, milestones=[15, 35], gamma=0.1) optimizer = gradient_clipping(optimizer) print('Saving results in path', results_path) print('Read data') data_loader = make_data_loader(model.lookback, params) print('Done!') data_model = data_loader('train') show_dataset = False if show_dataset: for i, full in enumerate(data_model): print('Data Loader---------------------------------------') print('batch', i) (data, reset, target, cond) = full print('Data', data.size()) print('Target', target.size()) if not params['scheduler']: scheduler = None if use_cuda: cuda = True else: cuda = False writer = SummaryWriter(log_dir='sample_board') trainer = Trainer( predictor, sequence_nll_loss_bits, optimizer, data_model, cuda, writer, scheduler ) checkpoints_path = os.path.join(results_path, 'checkpoints') checkpoint_data = load_last_checkpoint(checkpoints_path) if checkpoint_data is not None: (state_dict, epoch, iteration) = checkpoint_data trainer.epochs = epoch trainer.iterations = iteration predictor.load_state_dict(state_dict) trainer.register_plugin(TrainingLossMonitor( smoothing=params['loss_smoothing'] )) trainer.register_plugin(ValidationPlugin( data_loader('validation'), data_loader('test'), writer )) trainer.register_plugin(AbsoluteTimeMonitor()) trainer.register_plugin(SaverPlugin( checkpoints_path, params['keep_old_checkpoints'] )) trainer.register_plugin( Logger([ 'training_loss', 'validation_loss', 'test_loss', 'time' ]) ) trainer.register_plugin(StatsPlugin( results_path, iteration_fields=[ 'training_loss', ('training_loss', 'running_avg'), 'time' ], epoch_fields=[ 'validation_loss', 'test_loss', 'time' ], plots={ 'loss': { 'x': 'iteration', 'ys': [ 'training_loss', ('training_loss', 'running_avg'), 'validation_loss', 'test_loss', ], 'log_y': True } } )) trainer.run(params['epoch_limit'])