def generate_run(param): idx = param[0] class_label = param[1] dataset = param[2] wave_model = param[3] args = param[4] wave_generator = Generator(wave_model, dataset) start_idx = 0 seeds = [] for i, track in enumerate(dataset.tracks[start_idx:start_idx + 5]): print('Start generate sample {} in class {}'.format( i + 1, class_label)) start = 0 for start in range(0, 32000, wave_model.receptive_field): seed = track['audio'][start:start + wave_model.receptive_field] seed = torch.from_numpy(seed).to(wave_model.device) seeds.append(seed) seed = torch.stack(seeds) y = wave_generator.run(seed, args.new_seq_len, disp_interval=100, label=idx) for i, sample in enumerate(y): # dataset.save_wav(os.path.join(args.generated_sounds, '{}_{}.wav'.format(class_label, i + start_idx)), temp, dataset.sample_rate) sample.resize(sample.size, 1) target = os.path.join(args.generated_sounds, '{}_{}.wav'.format(class_label, i + start_idx)) sf.write(target, sample, dataset.sample_rate, 'PCM_16')
num_time_samples = int(sample_rate * duration) - 1 #Initialize the model before restoring it model = Model(num_time_samples=num_time_samples, num_channels=1, gpu_fraction=1.0, num_classes=Quantification, num_blocks=num_blocks, num_layers=num_layers, num_hidden=num_hidden) #Restoring the model model.restore() #Creating the Generator to make the prediction generator = Generator(model) seed = np.random.uniform(low=-1.0, high=1.0) random_input = [[seed]] random_inputs = [] #for i in range(num_hidden-1): # random_inputs.append(0) #random_inputs.append(seed) #random_inputs = np.asarray(random_inputs).reshape(num_hidden,1).tolist() #print('Making first prediction...') tic = time() combined_pred = generator.run(random_input, sample_rate * duration).tolist() ''' for t in range(duration - 1) :
print('Resuming training.') else: print('Model data not found: {}'.format(args.model_file)) print('Training new model.') args.resume_train = True if args.resume_train: wave_model.criterion = nn.CrossEntropyLoss() wave_model.optimizer = optim.Adam(wave_model.parameters(), lr=args.learn_rate) wave_model.scheduler = optim.lr_scheduler.StepLR( wave_model.optimizer, step_size=args.step_size, gamma=args.gamma) try: wave_model.train(dataloader, num_epochs=args.num_epochs, disp_interval=args.disp_interval, use_visdom=args.visdom) except KeyboardInterrupt: print('Training stopped!') print('Saving model data to file: {}'.format(args.model_file)) torch.save(wave_model.state_dict(), args.model_file) # predict sequence with model wave_generator = Generator(wave_model, dataset) y = wave_generator.run(dataset.tracks[0]['audio'][:args.x_len], args.new_seq_len, disp_interval=100) dataset.save_wav('./tmp.wav', y, dataloader.dataset.sample_rate)
def main(): ############################## # Get args args = get_arguments() ############################## ############################## # Build data chunk config_str = "_".join([ str(args.sample_rate), str(args.sample_size), str(args.sliding_ratio), str(args.silence_threshold) ]) files_dir = args.data_dir npy_dir = files_dir + '/' + config_str lock_file_db = files_dir + '/lock' # Check if exists while (os.path.isfile(lock_file_db)): # Wait for the end of construction by another process time.sleep(1) if not os.path.isdir(npy_dir): try: # Build if not ff = open(lock_file_db, 'w') build_db.main(files_dir, npy_dir, args.sample_rate, args.sample_size, args.sliding_ratio, args.silence_threshold) ff.close() except: shutil.rmtree(npy_dir) finally: os.remove(lock_file_db) # data_statistics.bar_activations(save_dir, save_dir, sample_size_padded) ############################## ############################## # Init dirs utils.init_directory(args.logdir_root) if args.summary: logdir_summary = os.path.join(args.logdir_root, 'summary') utils.init_directory(logdir_summary) # Save logdir_save = os.path.join(args.logdir_root, 'save') # Wave logdir_wav = os.path.join(args.logdir_root, 'wav') utils.init_directory(logdir_wav) ############################## ############################## # Get Data and Split them # Get list of data chunks chunk_list = build_db.find_files(npy_dir, pattern="*.npy") # To always have the same train/validate split, init the random seed random.seed(210691) random.shuffle(chunk_list) # Adapt batch_size if we have very few files num_chunk = len(chunk_list) batch_size = min(args.batch_size, num_chunk) # Split 90 / 10 training_chunks = chunk_list[:int(0.9 * num_chunk)] valid_chunks = chunk_list[int(0.9 * num_chunk):] ############################## ############################## # Create network import time ttt = time.time() model = Model(num_time_samples=args.sample_size, num_channels=1, num_classes=args.q_levels, num_blocks=args.num_blocks, num_layers=args.num_layers, num_hidden=args.num_hidden, filter_width=args.filter_width, gpu_fraction=0.9) print("TTT: Instanciate network : {}".format(time.time() - ttt)) ############################## ############################## # Train tic = time.time() model.train(training_chunks, valid_chunks, args.batch_size, args.valid_freq, args.generate_freq) toc = time.time() print('Training took {} seconds.'.format(toc - tic)) ############################## ############################## # Generate generator = Generator(model) # Get first sample of input input_ = inputs[:, 0:1, 0] tic = time() predictions = generator.run(input_, 32000) toc = time() print('Generating took {} seconds.'.format(toc - tic)) ############################## return