def kroghInterpolation(): sample_rate, sample = wavfile.read('songs/hakuna_matata.wav') sample = sample[5000000:5000100] sample_rateBAD, sampleBAD = wavfile.read( 'songs/bad_songs/not_good_song.wav') sampleBAD = sampleBAD[5000000:5000100] BadSample = sample.copy() dz.theEvilMethod(BadSample, 0.5) matches = recognize.cheat(sample, BadSample) x, y = utils.tovalidxy(BadSample, matches) f = KroghInterpolator(x, y) utils.repair(BadSample, matches, f) IwannaSee(sample, BadSample, sampleBAD)
#! /usr/bin/env python from scipy.io import wavfile from interpolate import NewtonInterpolator import damage, recognize, utils sample_rate, samples = wavfile.read('songs/hakuna_matata.wav') samples = samples[5000000:5000100] newsamples = samples.copy() damage.zerofill(newsamples, 0.3) matches = recognize.cheat(samples, newsamples) x, y = utils.tovalidxy(newsamples, matches) f = NewtonInterpolator(x, y) utils.repair(newsamples, matches, f) import matplotlib.pyplot as plt plt.title('Newton interpolation') plt.xlabel('Frame') plt.ylabel('Amplitude') plt.plot(samples, label='real') plt.plot(newsamples, label='interpolated') plt.legend(loc='best') plt.show()
gt_test_set = ColoredDataset(test_set, classes=10, colors=[1, 1], std=0) gt_test_loader = DataLoader(gt_test_set, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True) # measure bias & generalization before resampling color_fn = lambda x: x.view(x.size(0), x.size(1), -1).max(2)[0] # color of digit color_dim = 3 # rgb bias_0 = measure_bias(train_loader, test_loader, color_fn, color_dim)[0] model = create_mnist_model(args.model) gt_acc_0 = measure_generalization(train_loader, [test_loader, gt_test_loader], model)[1] print('Color bias before resampling = {:.3f}'.format(bias_0 + 0)) print('Generalization accuracy before resampling = {:.2%}'.format(gt_acc_0)) # learn resampling weights repair_dataset = IndexedDataset(ConcatDataset([colored_train_set, colored_test_set])) train_loader = DataLoader(repair_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True) w, q, cls_idx, cls_w, bias = repair(train_loader, color_fn, color_dim, args.epochs, args.lr, args.lr_w) # plot histogram of reampling weights sns.distplot(w.cpu(), bins=100, kde=False) plt.xlabel('Resampling weights') plt.ylabel('# Examples') plt.show() # perform resampling print('Resampling strategy:', args.sampling) keep_idx = get_keep_idx(w, cls_idx, mode=args.sampling) keep_idx_train = keep_idx[keep_idx < len(colored_train_set)] keep_idx_test = keep_idx[keep_idx >= len(colored_train_set)] - len(colored_train_set) print('Keep examples: {}/{} ({:.2%})'.format(len(keep_idx), len(w), len(keep_idx) / len(w))) train_loader = DataLoader(Subset(colored_train_set, keep_idx_train), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True) test_loader = DataLoader(Subset(colored_test_set, keep_idx_test), batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True)
samples = samples[5000000:5000100] newsamples = samples.copy() damage.noiseadd(newsamples, 0.7, 0.3) matches = recognize.cheat(samples, newsamples, false_positives=0.04, false_negatives=0.1) x, y = utils.tovalidxy(newsamples, matches) flinear = interp1d(x, y, fill_value='extrapolate') fcubic = interp1d(x, y, kind='cubic', fill_value='extrapolate') plt.subplot(211) utils.repair(newsamples, matches, flinear) plt.title('Linear') plt.ylabel('Amplitude') plt.plot(samples, label='real') plt.plot(newsamples, label='interpolated') plt.legend(loc='best') plt.subplot(212) utils.repair(newsamples, matches, fcubic) plt.title('Cubic') plt.xlabel('Frame') plt.ylabel('Amplitude') plt.plot(samples, label='real')