def test_string_tab(net, test_data): net.load_state_dict(torch.load('2fac_checkpoint.pt')) net.eval() criterion = nn.MSELoss() testset = MyDataset(parameters=test_data['parameters'], cqt_spectrograms=test_data['cqt_spec']) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2) inputs, targets = iter(testloader).next() inputs = inputs.to(device) targets = targets.to(device) gt_samples = [] gt_stringNumbers = [] gt_tabs = [] for i in range(len(targets)): gt_stringNumber, gt_tab = targets.cpu().numpy()[i] guitar = Guitar(options=Options()) gt_stringNumbers.append(gt_stringNumber) gt_tabs.append(gt_tab) #print("gt_stringNumber: %.3f, gt_tab: %.3f" % (gt_stringNumber, gt_tab)) audio_buffer = sequencer.play_note(guitar, int(round(gt_stringNumber)), int(round(gt_tab))) #cqt_spec = cqt_transform.compute_cqt_spec(audio_buffer).T #padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) gt_samples.append(audio_buffer) with open("2fac_gt_data.pkl", 'wb') as fh: data_dict = {'gt_samples' : np.array(gt_samples), 'gt_stringNumbers' : np.array(gt_stringNumbers), 'gt_tabs' : np.array(gt_tabs), 'gt_cqts' : inputs.cpu().numpy()} pkl.dump(data_dict, fh) fh.close() preds = net(inputs.unsqueeze_(1)) preds = preds.detach().cpu().numpy() pred_samples = [] pred_cqts = [] pred_stringNumbers = [] pred_tabs = [] for i in range(preds.shape[0]): pred_stringNumber, pred_tab = preds[i] guitar = Guitar(options=Options()) #print("pred_stringNumber: %d, pred_tab: %d" % (int(round(pred_stringNumber)), int(round(pred_tab)))) pred_stringNumbers.append(pred_stringNumber) pred_tabs.append(pred_tab) audio_buffer = sequencer.play_note(guitar, int(round(pred_stringNumber)), int(round(pred_tab))) cqt_spec = cqt_transform.compute_cqt_spec(audio_buffer, n_bins=336, bins_per_octave=48, hop_length=256).T padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) pred_cqts.append(padded_cqt.T) pred_samples.append(audio_buffer) with open("2fac_pred_data.pkl", 'wb') as fh: data_dict = {'pred_samples' : np.array(pred_samples), 'pred_stringNumbers' : np.array(pred_stringNumbers), 'pred_tabs' : np.array(pred_tabs), 'pred_cqts' : pred_cqts} pkl.dump(data_dict, fh) fh.close() print('test_loss: %.3f' % evaluate(net, testloader))
def sample_params_pitch_sf(size): freqs = np.array(utils.compute_freqs(num_frets)) character_variation = np.array([np.random.uniform(0, 1) for _ in range(size)], dtype=np.float32) string_damping = np.array([np.random.uniform(0, 0.7) for _ in range(size)], dtype=np.float32) string_damping_variation = np.array([np.random.uniform(0, 0.5) for _ in range(size)], dtype=np.float32) pluck_damping = np.array([np.random.uniform(0, 0.9) for _ in range(size)], dtype=np.float32) pluck_damping_variation = np.array([np.random.uniform(0, 0.5) for _ in range(size)], dtype=np.float32) string_tension = np.array([np.random.uniform(0, 1) for _ in range(size)], dtype=np.float32) stereo_spread = np.array([np.random.uniform(0, 1) for _ in range(size)], dtype=np.float32) smoothing_factor = np.array([np.random.uniform(0.5, 1) for _ in range(size)], dtype=np.float32) pitch = np.array([np.random.choice(freqs) for _ in range(size)], dtype=np.float32) # ipdb.set_trace() options = [] guitars = [] audio_buffers = [] cqt_specs = [] for i in range(size): options.append(Options(character_variation[i], string_damping[i], string_damping_variation[i], pluck_damping[i], pluck_damping_variation[i], string_tension[i], stereo_spread[i])) guitars.append(Guitar(options=options[i])) audio_buffers.append(sequencer.play_note(guitars[i], 0, 0, pitch[i], smoothing_factor[i])) # print(audio_buffers[i]) # try: # cqt_spec = compute_cqt_spec(audio_buffers[i]).T # except ParameterError: # print(audio_buffers[i]) cqt_spec = compute_cqt_spec(audio_buffers[i]).T padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) cqt_specs.append(padded_cqt) cqt_specs = np.array(cqt_specs, dtype=np.float32) print(cqt_specs.shape) return character_variation, string_damping, string_damping_variation, pluck_damping, pluck_damping_variation, string_tension, stereo_spread, pitch, smoothing_factor, cqt_specs
def save_data_hdf5(fname, size): print('Generating {}...'.format(fname)) freqs = np.array(utils.compute_freqs(num_frets)) character_variation = np.array([np.random.uniform(0, 1) for _ in range(size)], dtype=np.float32) string_damping = np.array([np.random.uniform(0, 0.7) for _ in range(size)], dtype=np.float32) string_damping_variation = np.array([np.random.uniform(0, 0.5) for _ in range(size)], dtype=np.float32) pluck_damping = np.array([np.random.uniform(0, 0.9) for _ in range(size)], dtype=np.float32) pluck_damping_variation = np.array([np.random.uniform(0, 0.5) for _ in range(size)], dtype=np.float32) string_tension = np.array([np.random.uniform(0, 1) for _ in range(size)], dtype=np.float32) stereo_spread = np.array([np.random.uniform(0, 1) for _ in range(size)], dtype=np.float32) smoothing_factor = np.array([np.random.uniform(0.5, 1) for _ in range(size)], dtype=np.float32) pitch = np.array([np.random.choice(freqs) for _ in range(size)], dtype=np.float32) with h5py.File(fname, 'w') as f: dset_parameters = f.create_dataset('parameters', (size, 9), maxshape=(None, None), dtype='float32', chunks=(size, 9)) dset_cqt_specs = f.create_dataset('cqt_spec', (size, 336, 336), maxshape=(None, None, None), dtype='float32', chunks=(1000, 336, 336)) dset_parameters[:] = np.array([character_variation, string_damping, string_damping_variation, pluck_damping, pluck_damping_variation, string_tension, stereo_spread, pitch, smoothing_factor]).T for i in range(size): options = Options(character_variation[i], string_damping[i], string_damping_variation[i], pluck_damping[i], pluck_damping_variation[i], string_tension[i], stereo_spread[i]) guitar = Guitar(options=options) audio_buffer = sequencer.play_note(guitar, 0, 0, pitch[i], smoothing_factor[i]) cqt_spec = compute_cqt_spec(audio_buffer).T padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) dset_cqt_specs[i, :, :] = padded_cqt print('Finished generating {}!'.format(fname))
def sample_params_string_tab(size): stringNumber = np.array([math.floor(np.random.choice(np.arange(0, 6))) for _ in range(size)], dtype=np.int32) tab = np.array([math.floor(np.random.choice(np.arange(0, 12))) for _ in range(size)], dtype=np.int32) # print(stringNumber) # print(tab) # ipdb.set_trace() guitars = [] audio_buffers = [] cqt_specs = [] for i in range(size): guitars.append(Guitar(options=Options())) audio_buffers.append(sequencer.play_note(guitars[i], stringNumber[i], tab[i])) cqt_spec = cqt_transform.compute_cqt_spec(audio_buffers[i], n_bins = 336, bins_per_octave=48, hop_length=256).T padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) cqt_specs.append(padded_cqt) cqt_specs = np.array(cqt_specs, dtype=np.float32) print(cqt_specs.shape) return stringNumber, tab, cqt_specs
def test_pitch_sf(net, test_data, batch_size, suffix ,testsize, factor): net.load_state_dict(torch.load("checkpoint" + suffix + ".pt")) net.eval() criterion = nn.MSELoss() testset = MyDataset(parameters=test_data['parameters'], cqt_spectrograms=test_data['cqt_spec']) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2) inputs, targets = iter(testloader).next() targets = np.delete(targets, 6, axis=1) inputs = inputs.to(device) targets = targets.to(device) gt_samples = [] gt_character_variations = [] gt_string_dampings = [] gt_string_damping_variations = [] gt_pluck_dampings = [] gt_pluck_damping_variations = [] gt_string_tensions = [] gt_pitches = [] gt_smoothing_factors = [] gt_dumping_variations = [] for i in range(len(targets)): gt_character_variation, gt_string_damping, gt_string_damping_variation, gt_pluck_damping, gt_pluck_damping_variation, gt_string_tension, gt_pitch, gt_smoothing_factor= targets.cpu().numpy()[i] gt_dumping_variations.append(gt_pluck_damping_variation) options = Options(gt_character_variation.astype(np.float64), gt_string_damping.astype(np.float64), gt_string_damping_variation.astype(np.float64), gt_pluck_damping.astype(np.float64), gt_pluck_damping_variation.astype(np.float64), gt_string_tension.astype(np.float64)) guitar = Guitar(options=options) gt_character_variations.append(gt_character_variation) gt_string_dampings.append(gt_string_damping) gt_string_damping_variations.append(gt_string_damping_variation) gt_pluck_dampings.append(gt_pluck_damping) gt_pluck_damping_variations.append(gt_pluck_damping_variation) gt_string_tensions.append(gt_string_tension) gt_pitches.append(gt_pitch) gt_smoothing_factors.append(gt_smoothing_factor) #print("gt_stringNumber: %.3f, gt_tab: %.3f" % (gt_stringNumber, gt_tab)) audio_buffer = sequencer.play_note(guitar, 0, 0, gt_pitch.astype(np.float64), gt_smoothing_factor.astype(np.float64)) cqt_spec = compute_cqt_spec(audio_buffer).T padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) gt_samples.append(audio_buffer) print("gt:\n") print(gt_character_variations) print(gt_string_dampings) print(gt_string_damping_variations) print(gt_pluck_dampings) print(gt_pluck_damping_variations) print(gt_string_tensions) print(gt_pitches) print(gt_smoothing_factors) with open("gt_data" + suffix + ".pkl", 'wb') as fh: data_dict = {'gt_samples' : np.array(gt_samples), 'gt_character_variations': np.array(gt_character_variations) , 'gt_string_dampings' : np.array(gt_string_dampings), 'gt_string_damping_variations' : np.array(gt_string_damping_variations), 'gt_pluck_dampings' : np.array(gt_pluck_dampings), 'gt_pluck_damping_variations' : np.array(gt_pluck_damping_variations), 'gt_string_tensions' : np.array(gt_string_tensions), 'gt_pitches' : np.array(gt_pitches), 'gt_smoothing_factors' : np.array(gt_smoothing_factors), 'gt_cqts' : inputs.cpu().numpy()} pkl.dump(data_dict, fh) fh.close() preds = net(inputs.unsqueeze_(1)) #m = nn.Sigmoid() #preds = m(5 * (preds - 0.5)) preds = preds.detach().cpu().numpy() pred_samples = [] pred_cqts = [] pred_character_variations = [] pred_string_dampings = [] pred_string_damping_variations = [] pred_pluck_dampings = [] pred_pluck_damping_variations = [] pred_string_tensions = [] pred_pitches = [] pred_smoothing_factors = [] pred_dumping_variations = [] pred_log_pitches = [] for i in range(preds.shape[0]): pred_character_variation, pred_string_damping, pred_string_damping_variation, pred_pluck_damping, pred_pluck_damping_variation, pred_string_tension, pred_pitch, pred_smoothing_factor = preds[i] #options = Options(pred_character_variation.astype(np.float64), pred_string_damping.astype(np.float64), pred_string_damping_variation.astype(np.float64), pred_pluck_damping.astype(np.float64), pred_pluck_damping_variation.astype(np.float64), pred_string_tension.astype(np.float64), pred_stereo_spread.astype(np.float64)) pred_dumping_variations.append(pred_pluck_damping_variation) options = Options(pred_character_variation.astype(np.float64), pred_string_damping.astype(np.float64), pred_string_damping_variation.astype(np.float64), pred_pluck_damping.astype(np.float64), pred_pluck_damping_variation.astype(np.float64), pred_string_tension.astype(np.float64)) guitar = Guitar(options=options) pred_character_variations.append(pred_character_variation) pred_string_dampings.append(pred_string_damping) pred_string_damping_variations.append(pred_string_damping_variation) pred_pluck_dampings.append(pred_pluck_damping) pred_pluck_damping_variations.append(pred_pluck_damping_variation) pred_string_tensions.append(pred_string_tension) pred_pitches.append(np.exp(pred_pitch)) pred_log_pitches.append(pred_pitch) pred_smoothing_factors.append(pred_smoothing_factor) #print("gt_stringNumber: %.3f, gt_tab: %.3f" % (gt_stringNumber, gt_tab)) audio_buffer = sequencer.play_note(guitar, 0, 0, np.exp(pred_pitch.astype(np.float64)), pred_smoothing_factor.astype(np.float64)) #audio_buffer = sequencer.play_note(guitar, 0, 0, pred_pitch, pred_smoothing_factor) cqt_spec = compute_cqt_spec(audio_buffer).T padded_cqt = pad_zeros(cqt_spec, (cqt_spec.shape[1], cqt_spec.shape[1])) pred_cqts.append(padded_cqt.T) pred_samples.append(audio_buffer) print("predicted:\n") print(pred_character_variations) print(pred_string_dampings) print(pred_string_damping_variations) print(pred_pluck_dampings) print(pred_pluck_damping_variations) print(pred_string_tensions) print(pred_pitches) print(pred_log_pitches) print(pred_smoothing_factors) #print(pred_dumping_variations) with open("pred_data" + suffix + ".pkl", 'wb') as fh: data_dict = {'pred_samples' : np.array(pred_samples), 'pred_character_variations': np.array(pred_character_variations) , 'pred_string_dampings' : np.array(pred_string_dampings), 'pred_string_damping_variations' : np.array(pred_string_damping_variations), 'pred_pluck_dampings' : np.array(pred_pluck_dampings), 'pred_pluck_damping_variations' : np.array(pred_pluck_damping_variations), 'pred_string_tensions' : np.array(pred_string_tensions), 'pred_pitches' : np.array(pred_pitches), 'pred_smoothing_factors' : np.array(pred_smoothing_factors), 'pred_cqts' : pred_cqts} pkl.dump(data_dict, fh) fh.close() print('test_loss: %.3f' % evaluate(net, testloader, testsize, factor))