def test_input(id, signals, sizes, Us, As, n_p, last_act_fn, batch_norm, V): error = np.zeros(N_EXPS) for i, x in enumerate(signals): x_n = ds.GraphSignal.add_noise(x, n_p) for j, exp in enumerate(EXPERIMENTS): cont = i*len(EXPERIMENTS)+j if not isinstance(exp, dict): x_est = utils.bandlimited_model(x_n, V, n_coefs=63) else: dec = GraphDeepDecoder(exp['arch'], sizes[j], Us[j], As=As[j], ups=exp['ups'], batch_norm=batch_norm, last_act_fn=last_act_fn) x_est, _ = dec.fit(x_n, n_iter=3000) error[cont] = np.sum(np.square(x-x_est))/np.square(np.linalg.norm(x)) print('Signal: {} Scenario: {} Error: {:.4f}' .format(id, cont+1, error[cont])) return error
def test_resolution(id, x, sizes, descendances, hier_As): mse_est = np.zeros(N_SCENARIOS) x_n = gs.GraphSignal.add_noise(x, n_p) for i in range(N_SCENARIOS): dec = GraphDeepDecoder(descendances[i], hier_As[i], sizes[i], n_channels=n_chans, upsampling=up_method, batch_norm=batch_norm, last_act_fun=last_act_fun, gamma=gamma) dec.build_network() x_est, _ = dec.fit(x_n) mse_est[i] = np.sum(np.square(x - x_est)) / np.square( np.linalg.norm(x)) print('Signal: {} Exp: {}: Err: {}'.format(id, i, mse_est[i])) return mse_est
def denoise_real(id, x, sizes, descendances, hier_As, n_p, V): error = np.zeros(N_EXPS) x_n = gs.GraphSignal.add_noise(x, n_p) for i, exp in enumerate(EXPERIMENTS): if not isinstance(exp, dict): x_est = utils.bandlimited_model(x_n, V, n_coefs=N_PARAMS, max_coefs=False) n_params = N_PARAMS else: dec = GraphDeepDecoder(descendances[i], hier_As[i], sizes[i], n_channels=exp['arch'], upsampling=exp['ups'], gamma=exp['gamma'], last_act_fun=LAST_ACT_FUN, act_fun=ACT_FUN, batch_norm=BATCH_NORM) dec.build_network() n_params = dec.count_params() x_est, _ = dec.fit(x_n, n_iter=4000) error[i] = np.sum(np.square(x - x_est)) / np.square(np.linalg.norm(x)) print( 'Signal: {} Scenario {}: Error: {:.4f}, Params: {}, N: {}'.format( id, i, error[i], n_params, x_n.size)) return error
def test_upsampling(id, x, sizes, Us, As, n_p): error = np.zeros(N_SCENARIOS) mse_fit = np.zeros(N_SCENARIOS) x_n = ds.GraphSignal.add_noise(x, n_p) for i in range(N_SCENARIOS): dec = GraphDeepDecoder(n_chans, sizes[i], Us[i], As=As[i], ups=EXPS[i][0], gamma=EXPS[i][1], batch_norm=batch_norm, act_fn=act_fun, last_act_fn=last_act_fun) # Implement model?? x_est, mse_fit[i] = dec.fit(x_n) error[i] = np.sum(np.square(x - x_est)) / np.square(np.linalg.norm(x)) print('Signal: {} Scenario {}: Error: {:.4f}'.format( id, i + 1, error[i])) mse_fit = mse_fit / np.linalg.norm(x_n) * x_n.size return error
def test_architecture(id, x, size, descendance, hier_A, n_p): error = np.zeros(N_EXPS) mse_fit = np.zeros(N_EXPS) x_n = gs.GraphSignal.add_noise(x, n_p) for i in range(N_EXPS): dec = GraphDeepDecoder(descendance, hier_A, size, n_channels=n_chans, upsampling=up_method, batch_norm=batch_norm, last_act_fun=EXPERIMENTS[i]['laf'], act_fun=EXPERIMENTS[i]['af'], gamma=gamma) dec.build_network() x_est, mse_fit[i] = dec.fit(x_n, n_iter=3500) error[i] = np.sum(np.square(x - x_est)) / np.square(np.linalg.norm(x)) print('Signal: {} Scenario {}: Error: {:.4f}'.format( id, i + 1, error[i])) return error
def test_architecture(id, x, sizes, Us, As, n_p): error = np.zeros(N_EXPS) mse_fit = np.zeros(N_EXPS) params = np.zeros(N_EXPS) x_n = ds.GraphSignal.add_noise(x, n_p) for i in range(N_EXPS): dec = GraphDeepDecoder(EXPS[i]['feats'], sizes[i], Us[i], As=As[i], ups=up_method, gamma=gamma, batch_norm=batch_norm, act_fn=act_fun, last_act_fn=last_act_fun) # Implement model?? x_est, mse_fit[i] = dec.fit(x_n, n_iter=3000) error[i] = np.sum(np.square(x - x_est)) / np.square(np.linalg.norm(x)) params[i] = dec.count_params() print('Signal: {} Scenario {}: ({} params): Error: {:.4f}'.format( id, i + 1, params[i], error[i])) return error, params
def run(id, Gs, Signals, Net, p_miss): G = ds.create_graph(Gs, SEED) clts = compute_clusters(G, Gs['k'], Net['type_A']) signal = ds.GraphSignal.create(Signals['type'], G, Signals['non_lin'], Signals['L'], Signals['deltas'], to_0_1=Signals['to_0_1']) x_n = ds.GraphSignal.add_noise(signal.x, Signals['noise']) inp_mask = ds.GraphSignal.generate_inpaint_mask(signal.x, p_miss) mask_err = np.sum(np.square(signal.x - x_n * inp_mask)) err = np.zeros(N_EXPS) node_err = np.zeros(N_EXPS) params = np.zeros(N_EXPS) for i in range(N_EXPS): dec = GraphDeepDecoder(EXPS[i]['fts'], clts[i].sizes, clts[i].Us, As=clts[i].As, act_fn=Net['af'], ups=EXPS[i]['ups'], gamma=EXPS[i]['gamma'], batch_norm=Net['bn'], last_act_fn=Net['laf']) model = Inpaint(dec, inp_mask, learning_rate=Net['lr'], epochs=EXPS[i]['epochs']) model.fit(x_n) node_err[i], err[i] = model.test(signal.x) params[i] = model.count_params() print('Graph {}-{} ({}):\tEpochs: {}\tNode Err: {:.8f}\tErr: {:.6f}'. format(id, i + 1, params[i], EXPS[i]['epochs'], node_err[i], err[i])) return node_err, err, mask_err, params
def denoise(x, sizes, descendances, hier_As, n_p, last_act_fn, batch_norm): error = np.zeros(len(EXPERIMENTS)) params = np.zeros(len(EXPERIMENTS)) x_n = gs.GraphSignal.add_noise(x, n_p) for i, exp in enumerate(EXPERIMENTS): dec = GraphDeepDecoder(descendances[i], hier_As[i], sizes[i], n_channels=exp['arch'], upsampling=exp['ups'], gamma=exp['gamma'], last_act_fun=last_act_fn, batch_norm=batch_norm) dec.build_network() x_est, _ = dec.fit(x_n) error[i] = np.sum(np.square(x - x_est)) / np.linalg.norm(x) params[i] = dec.count_params() print('\tScenario {}: Error: {:.4f}'.format(i, error[i])) return error, params
np.save(path, data) print('SAVED as:', path) if __name__ == '__main__': # Graph parameters G_params = {} G_params['type'] = 'SBM' # SBM or ER G_params['N'] = N = 256 G_params['k'] = 4 G_params['p'] = 0.15 G_params['q'] = 0.01 / 4 # Set seeds gs.GraphSignal.set_seed(SEED) GraphDeepDecoder.set_seed(SEED) G = utils.create_graph(G_params) sizes, descendances, hier_As = compute_clusters(G_params['k']) start_time = time.time() mse_est = np.zeros((n_signals, N_SCENARIOS)) results = [] with Pool(processes=cpu_count()) as pool: for i in range(n_signals): signal = gs.DifussedSparseGS(G, L, G_params['k']) signal.signal_to_0_1_interval() signal.to_unit_norm() results.append( pool.apply_async( test_resolution,