def validate(epoch, valid_loader, model, loss_func, mlb): ## Volatile variables do not save intermediate results and build graphs for backprop, achieving massive memory savings. model.eval() total_loss = 0 predictions = [] true_labels = [] print("Starting Validation") for batch_idx, (data, target) in enumerate(tqdm(valid_loader)): true_labels.append(target.cpu().numpy()) data, target = data.cuda(async=True), target.cuda(async=True) data, target = Variable(data, volatile=True), Variable(target, volatile=True) pred = model(data) predictions.append(F.sigmoid(pred).data.cpu().numpy()) total_loss += loss_func(pred, target).data[0] avg_loss = total_loss / len(valid_loader) predictions = np.vstack(predictions) true_labels = np.vstack(true_labels) score, threshold = best_f2_score(true_labels, predictions) print("Corresponding tags\n{}".format(mlb.classes_)) print("===> Validation - Avg. loss: {:.4f}\tF2 Score: {:.4f}".format( avg_loss, score)) return score, avg_loss, threshold
def run_multiple(): lambdas = [ 0.0, 0.001, 0.01, 0.1, 0.5, 1.0, 2.0, 4.0, 5.0, 10.0, 20.0, 50.0 ] num_trials = 3 acc_afr = () acc_other = () acc_overall = () avg_afr = (0, ) * len(lambdas) avg_other = (0, ) * len(lambdas) avg_overall = (0, ) * len(lambdas) for trial in range(num_trials): acc_afr = () acc_other = () acc_overall = () for lam in lambdas: if (lam == 0.001): weights, bias, test_labels, test_features = mn.model(lam, True) else: weights, bias, test_labels, test_features = mn.model(lam, True) weights = np.copy(weights) bias = np.copy(bias) test_labels = np.copy(test_labels) test_features = np.copy(test_features) test_labels = np.reshape(test_labels, (-1, 1)) afr_feat, afr_lab, rest_feat, rest_lab = rd.get_groups( { "x": test_features, "y": test_labels }, 1, 4) acc_afr += (get_accuracy(afr_feat, weights, bias, afr_lab), ) acc_other += (get_accuracy(rest_feat, weights, bias, rest_lab), ) acc_overall += (get_accuracy(test_features, weights, bias, test_labels), ) avg_afr = np.add(avg_afr, np.divide(acc_afr, num_trials)) avg_other = np.add(avg_other, np.divide(acc_other, num_trials)) avg_overall = np.add(avg_overall, np.divide(acc_overall, num_trials)) plot_values(acc_afr, acc_other, acc_overall, trial) plot_values(avg_afr, avg_other, avg_overall, "avg") plt.show()
def apply_snc(mixedpath, pospath, negpath, save_to): mixedlist = [] mixedlist.append(mixedpath) posnoiselist = [] negnoiselist = [] posnoiselist.append(pospath) negnoiselist.append(negpath) # data processing g = tf.Graph() with g.as_default(): with tf.device('/cpu:0'): mixedlist = tf.constant(np.array(mixedlist)) posnoiselist = tf.constant(np.array(posnoiselist)) negnoiselist = tf.constant(np.array(negnoiselist)) mixedpath_ph = mixedlist[0] pospath_ph = posnoiselist[0] negpath_ph = negnoiselist[0] noise_pos_wav, noise_neg_wav, mix_wav = tf.py_func(handle_signals, [mixedpath_ph, pospath_ph, negpath_ph], [tf.float32, tf.float32, tf.float32]) noise_pos_wav, noise_neg_wav, mix_wav = [tf.reshape(x, [-1]) for x in (noise_pos_wav, noise_neg_wav, mix_wav)] sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) mix_stft, pos_stft, neg_stft = [tf.contrib.signal.stft(wav, 400, 160, fft_length=400) for wav in [mix_wav, noise_pos_wav, noise_neg_wav]] mix_spectrum, pos_spectrum, neg_spectrum = [tf.log(tf.abs(wav_stft) + 1e-5) for wav_stft in [mix_stft, pos_stft, neg_stft]] mix_phase = tf.angle(mix_stft) # crop data mix_spectra = strided_crop(mix_spectrum, Mix_Win, 1) # postive noise & negative noise pos_spectrum = pos_spectrum[:Noise_Win] pos_spectrum = tf.reshape(pos_spectrum, [Noise_Win, pos_spectrum.shape[1].value]) pos_spectra = tf.tile(tf.expand_dims(pos_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) neg_spectrum = neg_spectrum[:Noise_Win] neg_spectrum = tf.reshape(neg_spectrum, [Noise_Win, neg_spectrum.shape[1].value]) neg_spectra = tf.tile(tf.expand_dims(neg_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) mixedphs = strided_crop(mix_phase, 1, 1) # get data # print('------------------------------------------------------------------------------------') mix_spectra_, pos_spectra_, neg_spectra_ = sess.run([mix_spectra, pos_spectra, neg_spectra]) mixedphs_ = sess.run(mixedphs, feed_dict={mixedpath_ph: mixedpath, pospath_ph: pospath, negpath_ph: negpath}) mb = 100 batches = int(math.ceil(len(mix_spectra_) / float(mb))) denoised = [] mix_centers = [] ## denoiseing # graph eg = tf.Graph() with eg.as_default(): esess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) with tf.device('/gpu:0'): in_ph = [tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='targetph'), tf.placeholder(dtype=tf.float32, shape=[None, 35, 201], name='mixedph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='mixedphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='targetphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, 35, 201], name='posph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='posphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, 35, 201], name='negph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='negphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, 200, 201], name='noiseposcontextph'), tf.placeholder(dtype=tf.float32, shape=[None, 200, 201], name='noisenegcontextph'), tf.placeholder(dtype=tf.int32, shape=[None], name='locationph'), tf.placeholder(dtype=tf.string, shape=[None], name='targetpathph'), tf.placeholder(dtype=tf.string, shape=[None], name='noisepospathph'), tf.placeholder(dtype=tf.string, shape=[None], name='noisenegpathph'), tf.placeholder(dtype=tf.int32, shape=[None], name='snrposph'), tf.placeholder(dtype=tf.int32, shape=[None], name='snrnegph'), ] _, _, outputs = model(in_ph, False) esaver = tf.train.Saver(tf.global_variables()) # load in trained model checkpoints_dir = FLAGS.trained_model_dir model_name = FLAGS.trained_model_name esaver = tf.train.import_meta_graph(os.path.join(checkpoints_dir, model_name + '.meta')) esaver.restore(esess, os.path.join(checkpoints_dir, model_name)) mixed_tensor = eg.get_tensor_by_name('mixedph:0') pos_noise_tensor = eg.get_tensor_by_name('noiseposcontextph:0') neg_noise_tensor = eg.get_tensor_by_name('noisenegcontextph:0') denoised_tensor = eg.get_tensor_by_name('add_72:0') # nn processing for i in range(batches): batch_mix_spectrum, batch_pos_spectrum, batch_neg_spectrum = [spectra[i * mb:(i + 1) * mb] for spectra in [mix_spectra_, pos_spectra_, neg_spectra_]] batch_denoised_ = esess.run(denoised_tensor, feed_dict={mixed_tensor: batch_mix_spectrum, pos_noise_tensor: batch_pos_spectrum, neg_noise_tensor: batch_neg_spectrum}) denoised.append(batch_denoised_) mix_center = batch_mix_spectrum[:, 17, :] mix_centers.append(mix_center) # reconstruction denoised = np.concatenate(denoised, axis=0) mix_centers = np.concatenate(mix_centers, axis=0) denoised_samples = recover_samples_from_spectrum(denoised, mixedphs_[:, 0, :], save_to) mixed_samples = recover_samples_from_spectrum(mix_centers, mixedphs_[:, 0, :]) removed_samples = mixed_samples - denoised_samples snr_est = np.mean(np.square(denoised_samples)) / np.mean(np.square(removed_samples)) if not FLAGS.ac: compensation_factor = FLAGS.compensate else: compensation_factor = snr_est/20 denoised_samples += removed_samples * compensation_factor verbose = 0 if verbose: print(snr_est) print('---------------------------------------------------------------------------------------') save_to = save_to if len(save_to.split('/')[-1]) == 0 else save_to[:-len(save_to.split('/')[-1])] mix_save_to = save_to + 'mixed_processed.wav' mixed_samples = recover_samples_from_spectrum(mix_centers, mixedphs_[:, 0, :], mix_save_to) removed_save_to = save_to + 'removed.wav' wavwrite(removed_save_to, 16000, removed_samples) compensated_save_to = save_to + 'compensated.wav' compensated_samples = denoised_samples + removed_samples * compensation_factor wavwrite(compensated_save_to, 16000, compensated_samples)
def apply_demo(speechpath, pospath, negpath, save_to): speechlist = [] speechlist.append(speechpath) noiselist = [] noiselist.append(pospath) noiselist.append(negpath) # data processing g = tf.Graph() with g.as_default(): with tf.device('/cpu:0'): speechlist = tf.constant(np.array(speechlist)) noiselist = tf.constant(np.array(noiselist)) seeds = [] seeds.append(speechlist) seeds.append(noiselist) speechpath_ph = speechlist[0] pospath_ph = noiselist[0] negpath_ph = noiselist[1] noise_pos_wav, noise_neg_wav, mix_wav, snr_pos, snr_neg = tf.py_func( combine_signals, [speechpath_ph, pospath_ph, negpath_ph], [tf.float32, tf.float32, tf.float32, tf.int32, tf.int32]) noise_pos_wav, noise_neg_wav, mix_wav = [ tf.reshape(x, [-1]) for x in (noise_pos_wav, noise_neg_wav, mix_wav) ] sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) win_samples = int(FLAGS.Fs * 0.025) hop_samples = int(FLAGS.Fs * 0.010) mix_stft, pos_stft, neg_stft = [ tf.signal.stft(wav, win_samples, hop_samples, fft_length=win_samples) for wav in [mix_wav, noise_pos_wav, noise_neg_wav] ] mix_spectrum, pos_spectrum, neg_spectrum = [ tf.log(tf.abs(wav_stft) + 1e-5) for wav_stft in [mix_stft, pos_stft, neg_stft] ] mix_phase = tf.angle(mix_stft) # crop data mix_spectra = strided_crop(mix_spectrum[Noise_Win:], Mix_Win, 1) # postive noise & negative noise pos_spectrum = pos_spectrum[:Noise_Win] pos_spectrum = tf.reshape(pos_spectrum, [Noise_Win, pos_spectrum.shape[1].value]) pos_spectra = tf.tile(tf.expand_dims(pos_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) neg_spectrum = neg_spectrum[:Noise_Win] neg_spectrum = tf.reshape(neg_spectrum, [Noise_Win, neg_spectrum.shape[1].value]) neg_spectra = tf.tile(tf.expand_dims(neg_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) mixedphs = strided_crop(mix_phase[Noise_Win:], 1, 1) # get data print('--------------------------------') mix_spectra_, pos_spectra_, neg_spectra_ = sess.run( [mix_spectra, pos_spectra, neg_spectra]) mixedphs_ = sess.run(mixedphs, feed_dict={ speechpath_ph: speechpath, pospath_ph: pospath, negpath_ph: negpath }) mb = 100 batches = int(math.ceil(len(mix_spectra_) / float(mb))) denoised = [] mix_centers = [] # denoiseing # graph eg = tf.Graph() with eg.as_default(): esess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) with tf.device('/gpu:0'): num_fea = int(FLAGS.Fs * 0.025) / 2 + 1 in_ph = [ tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='targetph'), tf.placeholder(dtype=tf.float32, shape=[None, Mix_Win, num_fea], name='mixedph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='mixedphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='targetphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, Mix_Win, num_fea], name='posph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='posphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, Mix_Win, num_fea], name='negph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='negphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, Noise_Win, num_fea], name='noiseposcontextph'), tf.placeholder(dtype=tf.float32, shape=[None, Noise_Win, num_fea], name='noisenegcontextph'), tf.placeholder(dtype=tf.int32, shape=[None], name='locationph'), tf.placeholder(dtype=tf.string, shape=[None], name='targetpathph'), tf.placeholder(dtype=tf.string, shape=[None], name='noisepospathph'), tf.placeholder(dtype=tf.string, shape=[None], name='noisenegpathph'), tf.placeholder(dtype=tf.int32, shape=[None], name='snrposph'), tf.placeholder(dtype=tf.int32, shape=[None], name='snrnegph'), ] _, _, outputs = model(in_ph, False) esaver = tf.train.Saver(tf.global_variables()) checkpoints_dir = './trained_model' esaver = tf.train.import_meta_graph(checkpoints_dir + '/81448_0-1000000.meta') esaver.restore(esess, checkpoints_dir + '/81448_0-1000000') mixed_tensor = eg.get_tensor_by_name('mixedph:0') pos_noise_tensor = eg.get_tensor_by_name('noiseposcontextph:0') neg_noise_tensor = eg.get_tensor_by_name('noisenegcontextph:0') denoised_tensor = eg.get_tensor_by_name('add_72:0') # nn processing for i in range(batches): batch_mix_spectrum, batch_pos_spectrum, batch_neg_spectrum = [ spectra[i * mb:(i + 1) * mb] for spectra in [mix_spectra_, pos_spectra_, neg_spectra_] ] batch_denoised_ = esess.run(denoised_tensor, feed_dict={ mixed_tensor: batch_mix_spectrum, pos_noise_tensor: batch_pos_spectrum, neg_noise_tensor: batch_neg_spectrum }) denoised.append(batch_denoised_) mix_center = batch_mix_spectrum[:, 17, :] mix_centers.append(mix_center) # reconstruction denoised = np.concatenate(denoised, axis=0) mix_centers = np.concatenate(mix_centers, axis=0) recover_samples_from_spectrum(denoised, mixedphs_[:, 0, :], save_to) mix_save_to = save_to[:-15] + 'mixed_demo.wav' recover_samples_from_spectrum(mix_centers, mixedphs_[:, 0, :], mix_save_to)
def apply_separator(mixedpath, cleanpath, noisepath, save_to): mixedlist = [] mixedlist.append(mixedpath) cleanlist = [] cleanlist.append(cleanpath) noiselist = [] noiselist.append(noisepath) # data processing g = tf.Graph() with g.as_default(): with tf.device('/cpu:0'): mixedlist = tf.constant(np.array(mixedlist)) cleanlist = tf.constant(np.array(cleanlist)) noiselist = tf.constant(np.array(noiselist)) mixedpath_ph = mixedlist[0] cleanpath_ph = cleanlist[0] noisepath_ph = noiselist[0] clean_wav, noise_wav, mix_wav = tf.py_func( handle_signals, [mixedpath_ph, cleanpath_ph, noisepath_ph], [tf.float32, tf.float32, tf.float32]) clean_wav, noise_wav, mix_wav = [ tf.reshape(x, [-1]) for x in (clean_wav, noise_wav, mix_wav) ] sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) win_samples = int(FLAGS.Fs * 0.025) hop_samples = int(FLAGS.Fs * 0.010) mix_stft, clean_stft, noise_stft = [ tf.signal.stft(wav, win_samples, hop_samples, fft_length=win_samples) for wav in [mix_wav, clean_wav, noise_wav] ] mix_spectrum, clean_spectrum, noise_spectrum = [ tf.log(tf.abs(wav_stft) + 1e-5) for wav_stft in [mix_stft, clean_stft, noise_stft] ] mix_phase = tf.angle(mix_stft) # crop data mix_spectra = strided_crop(mix_spectrum, Mix_Win, 1) # postive noise & negative noise clean_spectrum = clean_spectrum[:Noise_Win] clean_spectrum = tf.reshape( clean_spectrum, [Noise_Win, clean_spectrum.shape[1].value]) clean_spectra = tf.tile(tf.expand_dims(clean_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) noise_spectrum = noise_spectrum[:Noise_Win] noise_spectrum = tf.reshape( noise_spectrum, [Noise_Win, noise_spectrum.shape[1].value]) noise_spectra = tf.tile(tf.expand_dims(noise_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) mixedphs = strided_crop(mix_phase, 1, 1) # get data print( '---------------------------------------------------------------------------------------------' ) mix_spectra_, clean_spectra_, noise_spectra_ = sess.run( [mix_spectra, clean_spectra, noise_spectra]) mixedphs_ = sess.run(mixedphs, feed_dict={ mixedpath_ph: mixedpath, cleanpath_ph: cleanpath, noisepath_ph: noisepath }) mb = 100 batches = int(math.ceil(len(mix_spectra_) / float(mb))) denoised = [] mix_centers = [] # denoiseing # graph eg = tf.Graph() with eg.as_default(): esess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) with tf.device('/gpu:0'): num_fea = int(FLAGS.Fs * 0.025) / 2 + 1 in_ph = [ tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='cleanph'), tf.placeholder(dtype=tf.float32, shape=[None, Mix_Win, num_fea], name='mixedph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, num_fea], name='mixedphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, Noise_Win, num_fea], name='noisecontextph'), tf.placeholder(dtype=tf.float32, shape=[None, Noise_Win, num_fea], name='cleancontextph'), tf.placeholder(dtype=tf.int32, shape=[None], name='locationph'), tf.placeholder(dtype=tf.string, shape=[None], name='cleanpathph'), tf.placeholder(dtype=tf.string, shape=[None], name='noisepathph'), tf.placeholder(dtype=tf.int32, shape=[None], name='snrph') ] _, _, outputs = model(in_ph, False) esaver = tf.train.Saver(tf.global_variables()) checkpoints_dir = './trained_model' # checkpoints_dir = '/home/user/on_gpu/checkpoints/N_HANS___Source_Separation' esaver = tf.train.import_meta_graph(checkpoints_dir + '/81457_2-545000.meta') esaver.restore(esess, checkpoints_dir + '/81457_2-545000') mixed_tensor = eg.get_tensor_by_name('mixedph:0') noise_tensor = eg.get_tensor_by_name('noisecontextph:0') clean_tensor = eg.get_tensor_by_name('cleancontextph:0') denoised_tensor = eg.get_tensor_by_name('add_72:0') # nn processing for i in range(batches): batch_mix_spectrum, batch_clean_spectrum, batch_noise_spectrum = [ spectra[i * mb:(i + 1) * mb] for spectra in [mix_spectra_, clean_spectra_, noise_spectra_] ] batch_denoised_ = esess.run(denoised_tensor, feed_dict={ mixed_tensor: batch_mix_spectrum, noise_tensor: batch_noise_spectrum, clean_tensor: batch_clean_spectrum }) denoised.append(batch_denoised_) mix_center = batch_mix_spectrum[:, 17, :] mix_centers.append(mix_center) # reconstruction denoised = np.concatenate(denoised, axis=0) mix_centers = np.concatenate(mix_centers, axis=0) recover_samples_from_spectrum(denoised, mixedphs_[:, 0, :], save_to) mix_save_to = save_to[:-12] + 'mixed_processed.wav' recover_samples_from_spectrum(mix_centers, mixedphs_[:, 0, :], mix_save_to)
def apply_demo(cleanpath, noisepath, save_to): cleanlist = [] cleanlist.append(cleanpath) noiselist = [] noiselist.append(noisepath) # data processing g = tf.Graph() with g.as_default(): with tf.device('/cpu:0'): cleanlist = tf.constant(np.array(cleanlist)) noiselist = tf.constant(np.array(noiselist)) cleanpath_ph = cleanlist[0] noisepath_ph = noiselist[0] clean_wav, noise_wav, mix_wav, snr = tf.py_func( combine_signals, [cleanpath_ph, noisepath_ph], [tf.float32, tf.float32, tf.float32, tf.int32]) clean_wav, noise_wav, mix_wav = [ tf.reshape(x, [-1]) for x in (clean_wav, noise_wav, mix_wav) ] sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) mix_stft, clean_stft, noise_stft = [ tf.contrib.signal.stft(wav, 400, 160, fft_length=400) for wav in [mix_wav, clean_wav, noise_wav] ] mix_spectrum, clean_spectrum, noise_spectrum = [ tf.log(tf.abs(wav_stft) + 1e-5) for wav_stft in [mix_stft, clean_stft, noise_stft] ] mix_phase = tf.angle(mix_stft) # crop data mix_spectra = strided_crop(mix_spectrum[Noise_Win:], Mix_Win, 1) # postive noise & negative noise clean_spectrum = clean_spectrum[:Noise_Win] clean_spectrum = tf.reshape( clean_spectrum, [Noise_Win, clean_spectrum.shape[1].value]) clean_spectra = tf.tile(tf.expand_dims(clean_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) noise_spectrum = noise_spectrum[:Noise_Win] noise_spectrum = tf.reshape( noise_spectrum, [Noise_Win, noise_spectrum.shape[1].value]) noise_spectra = tf.tile(tf.expand_dims(noise_spectrum, 0), [tf.shape(mix_spectra)[0], 1, 1]) mixedphs = strided_crop(mix_phase[Noise_Win:], 1, 1) # get data # print('--------------------------------') mix_spectra_, clean_spectra_, noise_spectra_ = sess.run( [mix_spectra, clean_spectra, noise_spectra]) mixedphs_ = sess.run(mixedphs, feed_dict={ cleanpath_ph: cleanpath, noisepath_ph: noisepath }) mb = 100 batches = int(math.ceil(len(mix_spectra_) / float(mb))) # batches = 1 denoised = [] mix_centers = [] ## denoiseing # graph eg = tf.Graph() with eg.as_default(): esess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) with tf.device('/gpu:0'): in_ph = [ tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='cleanph'), tf.placeholder(dtype=tf.float32, shape=[None, Mix_Win, 201], name='mixedph'), tf.placeholder(dtype=tf.float32, shape=[None, 1, 201], name='mixedphaseph'), tf.placeholder(dtype=tf.float32, shape=[None, Noise_Win, 201], name='noisecontextph'), tf.placeholder(dtype=tf.float32, shape=[None, Noise_Win, 201], name='cleancontextph'), tf.placeholder(dtype=tf.int32, shape=[None], name='locationph'), tf.placeholder(dtype=tf.string, shape=[None], name='cleanpathph'), tf.placeholder(dtype=tf.string, shape=[None], name='noisepathph'), tf.placeholder(dtype=tf.int32, shape=[None], name='snrph') ] _, _, outputs = model(in_ph, False) esaver = tf.train.Saver(tf.global_variables()) # load in trained model checkpoints_dir = FLAGS.trained_model_dir model_name = FLAGS.trained_model_name esaver = tf.train.import_meta_graph( os.path.join(checkpoints_dir, model_name + '.meta')) esaver.restore(esess, os.path.join(checkpoints_dir, model_name)) mixed_tensor = eg.get_tensor_by_name('mixedph:0') noise_tensor = eg.get_tensor_by_name('noisecontextph:0') clean_tensor = eg.get_tensor_by_name('cleancontextph:0') denoised_tensor = eg.get_tensor_by_name('add_72:0') # nn processing for i in range(batches): batch_mix_spectrum, batch_clean_spectrum, batch_noise_spectrum = [ spectra[i * mb:(i + 1) * mb] for spectra in [mix_spectra_, clean_spectra_, noise_spectra_] ] batch_denoised_ = esess.run(denoised_tensor, feed_dict={ mixed_tensor: batch_mix_spectrum, noise_tensor: batch_noise_spectrum, clean_tensor: batch_clean_spectrum }) denoised.append(batch_denoised_) mix_center = batch_mix_spectrum[:, 17, :] mix_centers.append(mix_center) # reconstruction denoised = np.concatenate(denoised, axis=0) mix_centers = np.concatenate(mix_centers, axis=0) recover_samples_from_spectrum(denoised, mixedphs_[:, 0, :], save_to) verbose = 0 if verbose: save_to = save_to if len(save_to.split( '/')[-1]) == 0 else save_to[:-len(save_to.split('/')[-1])] mix_save_to = save_to + 'mixed.wav' recover_samples_from_spectrum(mix_centers, mixedphs_[:, 0, :], mix_save_to)
def genU(myargs, genX='False'): ''' method to generate U for given set of natural images data set images are in directory naturalimages/ and are named image_0001.jpg to image_0526.jpg :param myargs: Command line arguments :param genX: a bool value to specify to generate X from given pool of data set :return: returns a dictionary of SNR vs K/N ''' op = dict() if (myargs['-genX'] == 'True'): X = np.zeros(64) if (myargs['-input'] == 'natimages'): for i in range(1, 527): print "image:", i image_name = '' if (len(str(i)) == 1): image_name = 'image_000' + str(i) + '.jpg' elif (len(str(i)) == 2): image_name = 'image_00' + str(i) + '.jpg' elif (len(str(i)) == 3): image_name = 'image_0' + str(i) + '.jpg' image = scipy.misc.imread('naturalimages/' + image_name, mode='L', flatten=True) for patches in range(100): rand_i = np.random.randint(0, image.shape[0] / 8) rand_j = np.random.randint(0, image.shape[1] / 8) patch = ops.extarctpatch_image(rand_i, rand_j, image) patch = np.array(patch).flatten() X = np.vstack((X, patch)) X = X[1:, ] elif (myargs['-input'] == 'imgcomp'): for i in range(1, 11): img = imageio.imread('../compdata/' + str(i) + '.pgm') number_of_patches = img.shape[0] * img.shape[1] / 64 for patches in range(1000): rand_i = np.random.randint(0, img.shape[0] / 8) rand_j = np.random.randint(0, img.shape[1] / 8) patch = ops.extarctpatch_image(rand_i, rand_j, img) patch = np.array(patch).flatten() X = np.vstack((X, patch)) X = X[1:, ] elif (myargs['-input'] == 'MNIST'): input_file = 'MNIST/train.csv' X = np.genfromtxt(input_file, delimiter=',', dtype=np.uint8) X = skimage.img_as_float(X) print X[1] X = X[1:, 1:] else: print "Input format:", "python main.py -input [imgcomp | train.csv | natimages ] -t [integer] -k [[0,1] float ratio k/n] -genX [bool - True to regnerate X or flse for " \ "using the .csv file" return np.savetxt("../output/output_" + myargs['-input'] + "_X_" + ".csv", X, delimiter=',') X = np.genfromtxt('../output/output_' + myargs['-input'] + '_X_', delimiter=',', dtype=np.uint8) X = skimage.img_as_float(X) n = X.shape[1] l = X.shape[0] t_max = int(myargs['-t']) e_init = 2.8 e_final = 2.8 * (10)**-3 U = np.identity(n) k = int(myargs['-k']) * n m = model(X=X, U=U, t_max=t_max, e_init=e_init, e_final=e_final, k=k, n=n, l=l) trained_U = m.init_training() np.savetxt("../output/output_output_" + myargs['-input'] + "_U" + str(t_max) + '_' + str(k / float(n)) + ".csv", trained_U, delimiter=',') img = scipy.misc.toimage(trained_U, high=255, low=0) img.save("../output/output_output_" + myargs['-input'] + "_U" + str(t_max) + '_' + str(k / float(n)) + ".png") op[k / float(n)] = scipy.stats.signaltonoise(trained_U, axis=None) return op
def Upload(): if request.method == 'POST': #if 'url' not in request.orgs: # return jsonify({"error": "No image url in the request"}), 400 url = request.form['url'] fov = float(request.form['fov']) gamma = float(request.form['gamma']) if (gamma < 0): gamma = np.abs(gamma) gamma = gamma * 57.2958 gamma = gamma + 90 else: gamma = gamma * 57.2958 print(url) print(fov) print(gamma) #if url and allowed_file(url): # URL of the image to be downloaded is defined as image_url r = requests.get(url) # create HTTP response object # send a HTTP request to the server and save # the HTTP response in a response object called r extension = '' if '.jpg' in url: extension = '.jpg' if '.png' in url: extension = '.png' if '.jpeg' in url: extension = '.jpeg' res = ''.join( random.choices(string.ascii_uppercase + string.digits, k=N)) randomStr = str(res) with open("/home/rath772k/temp/static/" + randomStr + extension, 'wb') as f: # Saving received content as a png file in # binary format # write the contents of the response (r.content) # to a new file in binary mode. f.write(r.content) newName = "/home/rath772k/temp/static/" + randomStr + extension filename = newName # call the ML model im = cv2.imread(newName) size = 512 mean = (0.485, 0.456, 0.406) std = (0.229, 0.224, 0.225) transform = Compose([ Normalize(mean=mean, std=std, p=1), Resize(size, size), ToTensor(), ]) image_in = transform(image=im)["image"] image_in = image_in.reshape(1, 3, 512, 512).to(torch.device("cpu")) model.eval() with torch.no_grad(): op = model(image_in) op = op.cpu().numpy() op = (op > 0) + 0 op = (op == 1) + 0 op = op.reshape((512, 512)) op, _ = post_process(op, 50) op = op.astype('uint8') op = op * 255 alpha = 0.6 im = cv2.resize(im, (512, 512)) redImg = np.zeros(im.shape, im.dtype) bluImg = redImg redImg[:, :] = (0, 0, 255) redMask = cv2.bitwise_and(redImg, redImg, mask=op) final_image = cv2.addWeighted(redMask, alpha, im, 1 - alpha, 0, im) cv2.imwrite(filename, final_image) angleOfElevation = generate_angle(op, gamma, fov) fig, ax = plt.subplots(nrows=1, ncols=1) ax.plot(angleOfElevation) angle_plot_image_filename = os.path.join(UPLOAD_FOLDER, "angle_plot_img.jpg") fig.savefig(angle_plot_image_filename, bbox_inches='tight') responseImgPath = "http://34.69.240.165:3000/" + randomStr + extension return jsonify({ "segmentedImagePath": responseImgPath, "angle_plot_img_path": "http://34.69.240.165:3000/angle_plot_img.jpg" }), 200