def perturb_adj_continuous(self, adj): self.n_nodes = adj.shape[0] n_edges = len(adj.data) // 2 N = self.n_nodes t = time.time() A = sp.tril(adj, k=-1) print('getting the lower triangle of adj matrix done!') eps_1 = self.args.epsilon * 0.01 eps_2 = self.args.epsilon - eps_1 noise = get_noise(noise_type=self.args.noise_type, size=(N, N), seed=self.args.noise_seed, eps=eps_2, delta=self.args.delta, sensitivity=1) noise *= np.tri(*noise.shape, k=-1, dtype=np.bool) print(f'generating noise done using {time.time() - t} secs!') A += noise print(f'adding noise to the adj matrix done!') t = time.time() n_edges_keep = n_edges + int( get_noise(noise_type=self.args.noise_type, size=1, seed=self.args.noise_seed, eps=eps_1, delta=self.args.delta, sensitivity=1)[0]) print(f'edge number from {n_edges} to {n_edges_keep}') t = time.time() a_r = A.A.ravel() n_splits = 50 len_h = len(a_r) // n_splits ind_list = [] for i in tqdm(range(n_splits - 1)): ind = np.argpartition(a_r[len_h*i:len_h*(i+1)], -n_edges_keep)[-n_edges_keep:] ind_list.append(ind + len_h * i) ind = np.argpartition(a_r[len_h*(n_splits-1):], -n_edges_keep)[-n_edges_keep:] ind_list.append(ind + len_h * (n_splits - 1)) ind_subset = np.hstack(ind_list) a_subset = a_r[ind_subset] ind = np.argpartition(a_subset, -n_edges_keep)[-n_edges_keep:] row_idx = [] col_idx = [] for idx in ind: idx = ind_subset[idx] row_idx.append(idx // N) col_idx.append(idx % N) assert(col_idx < row_idx) data_idx = np.ones(n_edges_keep, dtype=np.int32) print(f'data preparation done using {time.time() - t} secs!') mat = sp.csr_matrix((data_idx, (row_idx, col_idx)), shape=(N, N)) return mat + mat.T
def train(data_root, model, total_epoch, batch_size, lrate): X, Z, Lr = model.inputs() d_loss, g_loss = model.loss(X, Z) d_opt, g_opt = model.optimizer(d_loss, g_loss, Lr) g_sample = model.sample(Z) sample_size = batch_size test_noise = utils.get_noise(sample_size, n_noise) epoch_drop = 3 iterator, image_count = ImageIterator(data_root, batch_size, model.image_size, model.image_channels).get_iterator() next_element = iterator.get_next() total_batch = int(image_count / batch_size) #learning_rate = lrate #G_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='generator') saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(iterator.initializer) for epoch in range(total_epoch): learning_rate = lrate * \ math.pow(0.2, math.floor((epoch + 1) / epoch_drop)) for step in range(total_batch): batch_x = sess.run(next_element) batch_z = utils.get_noise(batch_size, n_noise) _, loss_val_D = sess.run([d_opt, d_loss], feed_dict={ X: batch_x, Z: batch_z, Lr: learning_rate }) _, loss_val_G = sess.run([g_opt, g_loss], feed_dict={ Z: batch_z, Lr: learning_rate }) if step % 300 == 0: #sample_size = 10 #noise = get_noise(sample_size, n_noise) samples = sess.run(g_sample, feed_dict={Z: test_noise}) title = 'samples/%05d_%05d.png' % (epoch, step) utils.save_samples(title, samples) print('Epoch:', '%04d' % epoch, '%05d/%05d' % (step, total_batch), 'D loss: {:.4}'.format(loss_val_D), 'G loss: {:.4}'.format(loss_val_G)) saver.save(sess, './models/dcgan', global_step=epoch)
def build_input(self): # build a noise tensor data_shape = self.img.shape[:-1] self.input_ = u.get_noise(shape=(1, self.args.inputdepth) + data_shape, noise_type=self.args.noise_dist).type( self.dtype) self.input_ *= self.args.noise_std if self.args.filter_noise_with_wavelet: self.input_ = u.np_to_torch( u.filter_noise_traces( self.input_.detach().clone().cpu().numpy(), np.load(os.path.join(self.args.imgdir, 'wavelet.npy')))).type(self.dtype) if self.args.data_forgetting_factor != 0: # build decimated data tensor data_ = self.img_ * self.mask_ # how many times we can repeat the data in order to fill the input depth? num_rep = int(np.ceil(self.args.inputdepth / self.args.imgchannel)) # repeat data along the channel dim and crop to the input depth size data_ = data_.repeat([1, num_rep] + [1] * len(data_shape))[:, :self.args.inputdepth] # normalize data to noise std data_ *= torch.std(self.input_) / torch.std(data_) self.add_data_ = data_ self.add_data_weight = np.logspace( 0, -4, self.args.data_forgetting_factor) self.input_old = self.input_.detach().clone() self.add_noise_ = self.input_.detach().clone() print( colored('The input shape is %s' % str(tuple(self.input_.shape)), 'cyan'))
def build_cluster_adj(self, clean=False): """ build a adjacency matrix which only record what kind of fake labels each node link to """ adj = np.zeros((self.n_nodes, self.n_clusters), dtype=np.float64) for dst, src in self.edges.tolist(): adj[src, self.fake_labels[dst]] += 1 adj[dst, self.fake_labels[src]] += 1 if self.mode in ('clusteradj') and not clean: adj += get_noise(self.args.noise_type, self.n_nodes, self.n_clusters, self.args.noise_seed, eps=self.args.epsilon, delta=self.args.delta) adj = np.clip(adj, a_min=0, a_max=None) adj = normalize(adj) return torch.FloatTensor(adj) adj = sp.coo_matrix(adj) adj = normalize(adj) return sparse_mx_to_torch_sparse_tensor(adj)
def build_cluster_adj(self, clean=False, fnormalize=True): adj = np.zeros((self.n_nodes, self.n_clusters), dtype=np.float64) for dst, src in self.edges.tolist(): adj[src, self.fake_labels[dst]] += 1 adj[dst, self.fake_labels[src]] += 1 if self.mode in ( 'clusteradj' ) and not clean: adj += get_noise( self.args.noise_type, size=self.n_nodes*self.n_clusters, seed=self.args.noise_seed, eps=self.args.epsilon, delta=self.args.delta).reshape(self.n_nodes, self.n_clusters) adj = np.clip(adj, a_min=0, a_max=None) if fnormalize: adj = normalize(adj) else: adj = normalize(np.dot(adj, self.prj.cpu().numpy()) + np.eye(self.n_nodes)) return torch.FloatTensor(adj) # adj = sp.coo_matrix(adj) if fnormalize: adj = normalize(adj) elif self.mode != 'degree_nlp': adj = normalize(np.dot(adj, self.prj.cpu().numpy()) + np.eye(self.n_nodes)) # adj = sparse_mx_to_torch_sparse_tensor(adj) # if not fnormalize and not self.mode == 'degree_mlp': # adj = t_normalize(torch.mm(adj, self.prj) + torch.eye(self.n_nodes)) # return adj return torch.FloatTensor(adj)
def extinction_cycle_main(): epsilon = 3 r = 0.125 curr = 10 f = get_ricker_function(r) values = [] while True: curr = f(curr) + get_noise(epsilon) values.append(curr) if curr <= 0: break indexes = [i for i in range(1, len(values) + 1)] xpath = 'files/pointst.txt' ypath = 'files/pointsv.txt' write_to_files(indexes, values, xpath, ypath) erf_value = 2.33 c1, c2 = get_cycle2_points(r) ys = get_cycle_boundaries(c1, c2, r, erf_value, epsilon) print(ys)
def build_adj_vanilla(self): adj = np.zeros((self.n_nodes, self.n_nodes), dtype=np.float64) for dst, src in self.edges: adj[src][dst] = adj[dst][src] = 1 t0 = time.time() adj += get_noise(self.args.noise_type, self.n_nodes, self.n_nodes, self.args.noise_seed, eps=self.args.epsilon, delta=self.args.delta) adj = np.clip(adj, a_min=0, a_max=None) print('adding noise done using {} secs!'.format(time.time() - t0)) return adj
def average_on_folder(path_to_dataset, net, noise_std, verbose=True, device=torch.device('cuda')): if (verbose): print('Loading data info ...\n') print('Dataset: ', path_to_dataset) files_source = glob.glob(os.path.join(path_to_dataset, '*.png')) files_source.sort() avg_metrics = {} for x in metrics_key: avg_metrics[x] = 0.0 for f in files_source: # image Img = cv2.imread(f) Img = normalize(np.float32(Img[:, :, 0])) Img = np.expand_dims(Img, 0) Img = np.expand_dims(Img, 1) ISource = torch.Tensor(Img) # noisy image INoisy = get_noise(ISource, noise_std=noise_std, mode='S') + ISource ISource, INoisy = ISource.to(device), INoisy.to(device) out = torch.clamp(net(INoisy), 0., 1.) ind_metrics = get_all_comparison_metrics(out, ISource, INoisy, return_title_string=False) for x in metrics_key: avg_metrics[x] += ind_metrics[x] if (verbose): print("%s %s" % (f, convert_dict_to_string(ind_metrics))) for x in metrics_key: avg_metrics[x] /= len(files_source) if (verbose): print("\n Average %s" % (convert_dict_to_string(avg_metrics))) if (not verbose): return avg_metrics
def build_input(self): # build a noise tensor data_shape = self.img.shape[:-1] self.input_ = u.get_noise(shape=(1, self.args.inputdepth) + data_shape, noise_type=self.args.noise_dist).type(self.dtype) self.input_ *= self.args.noise_std if self.args.filter_noise_with_wavelet: W = u.ConvolveKernel_1d( kernel=np.load(os.path.join(self.args.imgdir, 'wavelet.npy')), ndim=self.input_.ndim - 2, dtype=self.dtype, ) self.input_ = W(self.input_) if self.args.lowpass_fs and self.args.lowpass_fc: print(colored("filtering the input tensor with a low pass Butterworth...", "cyan")) # low pass filter input noise tensor with a 4th order butterworth LPF = u.LowPassButterworth(fc=self.args.lowpass_fc, ndim=self.input_.ndim - 2, fs=self.args.lowpass_fs, ntaps=self.args.lowpass_ntaps, order=4, nfft=2 ** u.nextpow2(self.input_.shape[2]), dtype=self.dtype) self.input_ = LPF(self.input_) if self.args.data_forgetting_factor != 0: # build decimated data tensor data_ = self.img_ * self.mask_ # how many times we can repeat the data in order to fill the input depth? num_rep = int(np.ceil(self.args.inputdepth / self.args.imgchannel)) # repeat data along the channel dim and crop to the input depth size data_ = data_.repeat([1, num_rep] + [1] * len(data_shape))[:, :self.args.inputdepth] # normalize data to noise std data_ *= torch.std(self.input_) / torch.std(data_) self.add_data_ = data_ self.add_data_weight = np.logspace(0, -4, self.args.data_forgetting_factor) self.input_old = self.input_.detach().clone() self.add_noise_ = self.input_.detach().clone() print(colored('The input shape is %s' % str(tuple(self.input_.shape)), 'cyan'))
def test(ckpt_root, model, batch_size): X, Z, Lr = model.inputs() g_sample = model.sample(Z, reuse=False) sample_size = batch_size test_noise = utils.get_noise(sample_size, n_noise) # TEST DCGAN interpolation ''' test_noise[0] = np.array([[-0.01403, 0.56896, 1.41881, 0.02516, -1.36731, -0.92614, 1.17105, -0.17130, -0.11242, 0.35453, 0.06243, 0.41190, -0.18923, -1.10846, -0.10500, 0.65989, -0.19307, -0.32606, -1.77017, -0.38637, -0.82117, 0.53288, -0.38393, 1.16999, 0.02266, 0.36757, 0.13555, -1.06630, 0.00951, -0.04134, -0.29982, -0.83991, -0.04059, -0.56064, 0.39640, 0.29686, 0.42023, -1.15875, 0.19443, -0.89730, 0.37836, -2.48704, -0.03874, 0.04086, -0.35425, -0.02359, 0.56843, -0.45289, 1.79295, 0.98343, -0.99543, 0.70134, -1.43882, -0.10630, -0.39800, -1.90689, -0.16606, 0.01075, 0.11386, 0.08757, 0.25799, 1.06645, 0.07529, 1.17719, 1.38717, -0.93715, 0.60258, 0.64817, -0.70972, 1.49177, -0.58564, -1.47612, -0.49625, 2.30098, -0.08210, -0.22495, -0.47805, -0.72601, 0.58665, -0.63158, 0.04414, -0.05951, -0.92667, -0.07905, -2.26017, -0.29677, 0.93230, -0.06546, -0.46701, 1.49024, 0.01060, -0.86621, -0.65857, 0.42297, -1.43760, 0.53813, -0.13808, 0.23095, -0.78151, 0.63207 ]]) test_noise[8] = np.array([[-0.30745, -1.80994, 0.84740, -0.01723, -0.25759, 1.62209, -0.01877, 1.31540, 1.80470, -1.76964, 2.06064, -0.62803, -0.94382, 0.85376, -0.26913, 0.69890, 1.52500, -0.62958, -0.97269, 1.81976, 1.46848, -0.10180, -0.14649, 0.82289, -0.21654, 0.63229, -0.61106, -0.84134, 0.95145, -0.84128, -0.02509, -0.14419, -0.46364, -0.00298, -0.23900, -1.37273, -1.16797, 1.10777, -1.56686, -0.60846, -0.18123, 1.95980, 0.58466, 0.64532, 1.01655, -1.00187, 0.07544, 0.31779, 1.55344, -0.41186, -0.14158, 0.07359, -1.02670, -0.14173, 0.10773, -0.64202, -1.56408, -1.96202, -0.13097, -0.05426, -2.26692, 0.04790, 0.03724, -0.55998, 0.11415, -1.97006, 0.56635, -1.29249, 0.32449, 0.37213, -0.77510, -0.09502, 2.44859, 0.68632, 0.48752, 0.18134, -1.14473, -0.09552, -0.62953, 0.28095, -1.04062, 0.39957, -1.39301, -0.29697, -0.99899, 1.91437, -1.94361, -0.38661, -0.04163, -0.09743, -0.87291, 1.00404, 0.51789, -0.78019, 1.43526, 0.16111, 1.26596, -0.12284, 0.74221, 1.53793 ]]) test_noise[1] = (7*test_noise[0]+1*test_noise[8])/8 test_noise[2] = (6*test_noise[0]+2*test_noise[8])/8 test_noise[3] = (5*test_noise[0]+3*test_noise[8])/8 test_noise[4] = (4*test_noise[0]+4*test_noise[8])/8 test_noise[5] = (3*test_noise[0]+5*test_noise[8])/8 test_noise[6] = (2*test_noise[0]+6*test_noise[8])/8 test_noise[7] = (1*test_noise[0]+7*test_noise[8])/8 ''' saver = tf.train.Saver() with tf.Session() as sess: #sess.run(tf.global_variables_initializer()) saver.restore(sess, ckpt_root) samples = sess.run(g_sample, feed_dict={Z: test_noise}) date = datetime.datetime.now() title = 'samples/%s.png' % date utils.save_samples(title, samples) np.savetxt('samples/test_noise%s.txt' % date, test_noise, fmt='%2.5f', delimiter=', ')
def display_denoising(DnCNN, BF_DnCNN, set12_path, image_num=7, noise_level=90, l=0, h=10, model='DnCNN'): clean_im = single_image_loader(set12_path, image_num) clean_im_tensor = torch.from_numpy(clean_im).unsqueeze(0).unsqueeze(0).to( device).float() noise = utils.get_noise(clean_im_tensor, noise_std=noise_level / 255., mode='S') inp_test = clean_im_tensor + noise noisy_psnr = np.round(utils.psnr(clean_im_tensor, inp_test), 2) noisy_ssim = np.round(utils.ssim(clean_im_tensor, inp_test), 2) denoised_dncnn = DnCNN(inp_test) denoised_dncnn_psnr = np.round(utils.psnr(clean_im_tensor, denoised_dncnn), 2) denoised_dncnn_ssim = np.round(utils.ssim(clean_im_tensor, denoised_dncnn), 2) denoised_dncnn = denoised_dncnn.cpu().data.squeeze(0).squeeze(0).numpy() denoised_bf_dncnn = BF_DnCNN(inp_test) denoised_bf_dncnn_psnr = np.round( utils.psnr(clean_im_tensor, denoised_bf_dncnn), 2) denoised_bf_dncnn_ssim = np.round( utils.ssim(clean_im_tensor, denoised_bf_dncnn), 2) denoised_bf_dncnn = denoised_bf_dncnn.cpu().data.squeeze(0).squeeze( 0).numpy() noisy_im = inp_test.cpu().data.squeeze(0).squeeze(0).numpy() f, axs = plt.subplots(1, 4, figsize=(15, 4), squeeze=True) f.suptitle(r'Training range: $\sigma \in [ $' + str(l) + ' , ' + str(h) + ']', fontname='Times New Roman', fontsize=15) axs[0].imshow(clean_im, 'gray', vmin=0, vmax=1) axs[0].set_title('clean image', fontname='Times New Roman', fontsize=15) axs[1].imshow(noisy_im, 'gray', vmin=0, vmax=1) axs[1].set_title(r'noisy image, $\sigma$ = ' + str(noise_level), fontname='Times New Roman', fontsize=15) axs[1].set_xlabel('psnr ' + str(noisy_psnr) + '\n ssim ' + str(noisy_ssim), fontname='Times New Roman', fontsize=15) axs[2].imshow(denoised_dncnn, 'gray', vmin=0, vmax=1) axs[2].set_title('denoised, ' + model, fontname='Times New Roman', fontsize=15) axs[2].set_xlabel('psnr ' + str(denoised_dncnn_psnr) + '\n ssim ' + str(denoised_dncnn_ssim), fontname='Times New Roman', fontsize=15) axs[3].imshow(denoised_bf_dncnn, 'gray', vmin=0, vmax=1) axs[3].set_title('denoised, BF_' + model, fontname='Times New Roman', fontsize=15) axs[3].set_xlabel('psnr ' + str(denoised_bf_dncnn_psnr) + '\n ssim ' + str(denoised_bf_dncnn_ssim), fontname='Times New Roman', fontsize=15) for i in range(4): axs[i].tick_params(bottom=False, left=False, labelleft=False, labelbottom=False)
def main(hparams): # set up perceptual loss device = 'cuda:0' percept = PerceptualLoss(model="net-lin", net="vgg", use_gpu=device.startswith("cuda")) # Set up some stuff accoring to hparams hparams.n_input = np.prod(hparams.image_shape) #utils.set_num_measurements(hparams) utils.print_hparams(hparams) # get inputs xs_dict = model_input(hparams) estimators = utils.get_estimators(hparams) utils.setup_checkpointing(hparams) measurement_losses, l2_losses, lpips_scores, z_hats, likelihoods = utils.load_checkpoints( hparams) x_hats_dict = {model_type: {} for model_type in hparams.model_types} x_batch_dict = {} A = utils.get_A(hparams) for key, x in xs_dict.items(): if not hparams.not_lazy: # If lazy, first check if the image has already been # saved before by *all* estimators. If yes, then skip this image. save_paths = utils.get_save_paths(hparams, key) is_saved = all([ os.path.isfile(save_path) for save_path in save_paths.values() ]) if is_saved: continue x_batch_dict[key] = x if len(x_batch_dict) < hparams.batch_size: continue # Reshape input x_batch_list = [ x.reshape(1, hparams.n_input) for _, x in x_batch_dict.items() ] x_batch = np.concatenate(x_batch_list) # Construct noise and measurements if hparams.fixed_init: noise = (hparams.noise_std / np.sqrt(hparams.num_measurements)) * np.random.randn( 1, hparams.num_measurements) noise_batch = noise.repeat(hparams.batch_size, 0) else: #noise_batch = (hparams.noise_std/np.sqrt(hparams.num_measurements)) * np.random.randn(hparams.batch_size, hparams.num_measurements) noise_batch = utils.get_noise(hparams) print(noise_batch.shape) y_batch = utils.get_measurements(x_batch, A, noise_batch, hparams) # Construct estimates using each estimator for model_type in hparams.model_types: estimator = estimators[model_type] x_hat_batch, z_hat_batch, likelihood_batch = estimator( A, y_batch, hparams) for i, key in enumerate(x_batch_dict.keys()): x = xs_dict[key] y_train = y_batch[i] x_hat = x_hat_batch[i] # Save the estimate x_hats_dict[model_type][key] = x_hat # Compute and store measurement and l2 loss measurement_losses[model_type][ key] = utils.get_measurement_loss(x_hat, A, y_train, hparams) l2_losses[model_type][key] = utils.get_l2_loss(x_hat, x) lpips_scores[model_type][key] = utils.get_lpips_score( percept, x_hat, x, hparams.image_shape) z_hats[model_type][key] = z_hat_batch[i] likelihoods[model_type][key] = likelihood_batch[i] print('Processed upto image {0} / {1}'.format(key + 1, len(xs_dict))) # Checkpointing if (hparams.save_images) and ((key + 1) % hparams.checkpoint_iter == 0): utils.checkpoint(x_hats_dict, measurement_losses, l2_losses, lpips_scores, z_hats, likelihoods, save_image, hparams) x_hats_dict = { model_type: {} for model_type in hparams.model_types } print('\nProcessed and saved first ', key + 1, 'images\n') x_batch_dict = {} # Final checkpoint if hparams.save_images: utils.checkpoint(x_hats_dict, measurement_losses, l2_losses, lpips_scores, z_hats, likelihoods, save_image, hparams) print('\nProcessed and saved all {0} image(s)\n'.format(len(xs_dict))) if hparams.print_stats: for model_type in hparams.model_types: print(model_type) measurement_loss_list = list( measurement_losses[model_type].values()) l2_loss_list = list(l2_losses[model_type].values()) mean_m_loss = np.mean(measurement_loss_list) mean_l2_loss = np.mean(l2_loss_list) print('mean measurement loss = {0}'.format(mean_m_loss)) print('mean l2 loss = {0}'.format(mean_l2_loss)) if hparams.image_matrix > 0: utils.image_matrix(xs_dict, x_hats_dict, view_image, hparams) # Warn the user that some things were not processsed if len(x_batch_dict) > 0: print( '\nDid NOT process last {} images because they did not fill up the last batch.' .format(len(x_batch_dict))) print('Consider rerunning lazily with a smaller batch size.')
print("Adversarially Trained Results") print("Subject %d" % sbj) for epoch in range(st.total_epoch): # Randomize the dataset rand_idx = np.random.permutation(rest_point * data.shape[-1]) # 68832 # Feed dictionary for batch in range(total_batch): batch_x = np.empty(shape=(st.batch_size, 22, st.window_size, 1)) batch_y = np.empty(shape=(st.batch_size)) for i in range(st.batch_size): position = np.unravel_index(indices=rand_idx[epoch * st.batch_size + i], dims=(rest_point, data.shape[-1])) batch_x[i, :, :, 0] = data[:, position[0]:position[0] + st.window_size, position[1]] batch_y[i] = label[position[1]] batch_z = ut.get_noise(st.batch_size, st.n_noise) _, loss_g_ = sess.run([g_optimizer, loss_g], feed_dict={X:batch_x, Y:batch_y, Z:batch_z}) _, loss_d_ = sess.run([d_optimizer, loss_d], feed_dict={X:batch_x, Y:batch_y, Z:batch_z}) print("%04dth Epoch, Discriminator training Loss: %04f, Generator training Loss: %04f" % (epoch + 1, loss_d_, loss_g_)) # Validation prediction = np.zeros(shape=(288)) grount_truth = np.zeros(shape=(288)) for trials in range(0, 288): batch_x = np.empty(shape=(rest_point, 22, st.window_size, 1)) for batch in range(rest_point): batch_x[batch, :, :, 0] = data_test[:, batch:batch + st.window_size, trials] pred, feature = sess.run([logit_test, feature_tsne], feed_dict={X_valid:batch_x}) pred = np.argmax(np.bincount(np.argmax(ut.sigmoid(np.squeeze(np.asarray(pred))[:, :-1]), -1))) grount_truth[trials] = label_test[trials]
scheduler = MultiStepLR(optimizer, milestones=[5000, 7000, 9000], gamma=0.5) # learning rates l1 = nn.L1Loss() # ltloss = loss.LTLoss() filelist = utils.get_list(lr_path) idx = 0 for img in filelist: idx += 1 img_name, ext = os.path.splitext(os.path.basename(img)) logger.info('{:->2d}--> {:>10s}'.format(idx, img_name+ext)) lr = utils.png2tensor(img) h, w = lr.shape[2:] net_input = utils.get_noise(3, h, w) # hr = utils.png2tensor(hr_path + '/' + img_name.split('x{}'.format(scale))[0] + ext, scale=scale, crop=True) if use_cuda: net_input = Variable(net_input) net_input = net_input.cuda() lr = lr.cuda() # hr = hr.cuda() for iter in range(n_iters): out_hr = model_G(net_input) out_lr = model_D(out_hr) optimizer.zero_grad() bicubic_hr = F.interpolate(lr, scale_factor=scale, mode='bicubic')
if __name__ == '__main__': fp = "./img/Circuit.tif" if not os.path.exists(fp): raise Exception('[Error] image file not exists') img = cv2.cvtColor(cv2.imread(fp, cv2.IMREAD_COLOR), cv2.COLOR_BGR2GRAY) w, h = img.shape opt = parse_args() opt.width = w opt.height = h opt.a = 0 opt.b = 20 opt.mode = "gaussian" noise_gaussian = get_noise(param=opt) img_noise_gaussian = truncate(img + noise_gaussian, dst="./img/gaussian_noise.tif") img_gaussian_arifi = arithmetic_mean_filter(img_noise_gaussian, size=(3, 3)) cv2.imwrite("./img/gaussian_arithmetic.tif", img_gaussian_arifi) img_gaussian_geofi = geometric_mean_filter(img_noise_gaussian, size=(3, 3)) cv2.imwrite("./img/gaussian_geometric.tif", img_gaussian_geofi) opt.a = 0 opt.b = 800**0.5 opt.mode = "uniform" noise_uniform = get_noise(param=opt) img_noise_uniform = truncate(img + noise_uniform, dst="./img/uniform_noise.tif")
def train(data_root, model, total_epoch, batch_size, lrate): X, Z, Lr, Kt = model.inputs() d_loss, g_loss, real_loss, fake_loss = model.loss(X, Z, Kt) d_opt, g_opt = model.optimizer(d_loss, g_loss, Lr) g_sample = model.sample(Z) sample_size = batch_size test_noise = utils.get_noise(sample_size, n_noise) epoch_drop = 3 _lambda = 0.001 _gamma = 0.5 _kt = 0.0 iterator, image_count = ImageIterator(data_root, batch_size, model.image_size, model.image_channels).get_iterator() next_element = iterator.get_next() measure = real_loss + tf.abs(_gamma * real_loss - fake_loss) tf.summary.scalar('measure', measure) merged = tf.summary.merge_all() total_batch = int(image_count / batch_size) #learning_rate = lrate #G_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='generator') saver = tf.train.Saver() with tf.Session() as sess: train_writer = tf.summary.FileWriter('./logs', sess.graph) sess.run(tf.global_variables_initializer()) sess.run(iterator.initializer) for epoch in range(total_epoch): learning_rate = lrate * \ math.pow(0.2, math.floor((epoch + 1) / epoch_drop)) for step in range(total_batch): batch_x = sess.run(next_element) batch_z = utils.get_uniform_noise(batch_size, n_noise) _, val_real_loss = sess.run([d_opt, real_loss], feed_dict={ X: batch_x, Z: batch_z, Lr: learning_rate, Kt: _kt }) _, val_fake_loss = sess.run([g_opt, fake_loss], feed_dict={ Z: batch_z, Lr: learning_rate, Kt: _kt }) _kt = _kt + _lambda * (_gamma * val_real_loss - val_fake_loss) if step % 300 == 0: summary = sess.run(merged, feed_dict={ X: batch_x, Z: batch_z, Lr: learning_rate, Kt: _kt }) train_writer.add_summary(summary, epoch * total_batch + step) val_measure = val_real_loss + np.abs( _gamma * val_real_loss - val_fake_loss) print('Epoch:', '%04d' % epoch, '%05d/%05d' % (step, total_batch), 'measure: {:.4}'.format(val_measure)) #sample_size = 10 #noise = get_noise(sample_size, n_noise) samples = sess.run(g_sample, feed_dict={Z: test_noise}) title = 'samples/%05d_%05d.png' % (epoch, step) utils.save_samples(title, samples) saver.save(sess, './models/began', global_step=epoch)
def main(args): device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') utils.setup_experiment(args) utils.init_logging(args) # Build data loaders, a model and an optimizer model = models.build_model(args).to(device) print(model) optimizer = torch.optim.Adam(model.parameters(), lr=args.lr) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[50, 60, 70, 80, 90, 100], gamma=0.5) logging.info(f"Built a model consisting of {sum(p.numel() for p in model.parameters()):,} parameters") if args.resume_training: state_dict = utils.load_checkpoint(args, model, optimizer, scheduler) global_step = state_dict['last_step'] start_epoch = int(state_dict['last_step']/(403200/state_dict['args'].batch_size))+1 else: global_step = -1 start_epoch = 0 train_loader, valid_loader, _ = data.build_dataset(args.dataset, args.data_path, batch_size=args.batch_size) # Track moving average of loss values train_meters = {name: utils.RunningAverageMeter(0.98) for name in (["train_loss", "train_psnr", "train_ssim"])} valid_meters = {name: utils.AverageMeter() for name in (["valid_psnr", "valid_ssim"])} writer = SummaryWriter(log_dir=args.experiment_dir) if not args.no_visual else None for epoch in range(start_epoch, args.num_epochs): if args.resume_training: if epoch %10 == 0: optimizer.param_groups[0]["lr"] /= 2 print('learning rate reduced by factor of 2') train_bar = utils.ProgressBar(train_loader, epoch) for meter in train_meters.values(): meter.reset() for batch_id, inputs in enumerate(train_bar): model.train() global_step += 1 inputs = inputs.to(device) noise = utils.get_noise(inputs, mode = args.noise_mode, min_noise = args.min_noise/255., max_noise = args.max_noise/255., noise_std = args.noise_std/255.) noisy_inputs = noise + inputs; outputs = model(noisy_inputs) loss = F.mse_loss(outputs, inputs, reduction="sum") / (inputs.size(0) * 2) model.zero_grad() loss.backward() optimizer.step() train_psnr = utils.psnr(outputs, inputs) train_ssim = utils.ssim(outputs, inputs) train_meters["train_loss"].update(loss.item()) train_meters["train_psnr"].update(train_psnr.item()) train_meters["train_ssim"].update(train_ssim.item()) train_bar.log(dict(**train_meters, lr=optimizer.param_groups[0]["lr"]), verbose=True) if writer is not None and global_step % args.log_interval == 0: writer.add_scalar("lr", optimizer.param_groups[0]["lr"], global_step) writer.add_scalar("loss/train", loss.item(), global_step) writer.add_scalar("psnr/train", train_psnr.item(), global_step) writer.add_scalar("ssim/train", train_ssim.item(), global_step) gradients = torch.cat([p.grad.view(-1) for p in model.parameters() if p.grad is not None], dim=0) writer.add_histogram("gradients", gradients, global_step) sys.stdout.flush() if epoch % args.valid_interval == 0: model.eval() for meter in valid_meters.values(): meter.reset() valid_bar = utils.ProgressBar(valid_loader) for sample_id, sample in enumerate(valid_bar): with torch.no_grad(): sample = sample.to(device) noise = utils.get_noise(sample, mode = 'S', noise_std = (args.min_noise + args.max_noise)/(2*255.)) noisy_inputs = noise + sample; output = model(noisy_inputs) valid_psnr = utils.psnr(output, sample) valid_meters["valid_psnr"].update(valid_psnr.item()) valid_ssim = utils.ssim(output, sample) valid_meters["valid_ssim"].update(valid_ssim.item()) if writer is not None and sample_id < 10: image = torch.cat([sample, noisy_inputs, output], dim=0) image = torchvision.utils.make_grid(image.clamp(0, 1), nrow=3, normalize=False) writer.add_image(f"valid_samples/{sample_id}", image, global_step) if writer is not None: writer.add_scalar("psnr/valid", valid_meters['valid_psnr'].avg, global_step) writer.add_scalar("ssim/valid", valid_meters['valid_ssim'].avg, global_step) sys.stdout.flush() logging.info(train_bar.print(dict(**train_meters, **valid_meters, lr=optimizer.param_groups[0]["lr"]))) utils.save_checkpoint(args, global_step, model, optimizer, score=valid_meters["valid_psnr"].avg, mode="max") scheduler.step() logging.info(f"Done training! Best PSNR {utils.save_checkpoint.best_score:.3f} obtained after step {utils.save_checkpoint.best_step}.")
def get_net_input(img_size, input_depth=32, INPUT="noise"): return utils.get_noise(input_depth, INPUT, img_np.shape[1:]).type(dtype)#utils.get_noise(input_depth, INPUT, (img_size[1], img_size[0])).type(dtype).detach()
def Continue_train_WGAN(opt): # ---------------------------------------- # Network training parameters # ---------------------------------------- # cudnn benchmark cudnn.benchmark = opt.cudnn_benchmark # Loss functions criterion_L1 = torch.nn.L1Loss().cuda() # Initialize Generator generator = utils.create_generator(opt) discriminator = utils.create_discriminator(opt) # To device if opt.multi_gpu: generator = nn.DataParallel(generator) generator = generator.cuda() discriminator = nn.DataParallel(discriminator) discriminator = discriminator.cuda() else: generator = generator.cuda() discriminator = discriminator.cuda() # Optimizers optimizer_G = torch.optim.Adam(generator.parameters(), lr=opt.lr_g, betas=(opt.b1, opt.b2), weight_decay=opt.weight_decay) optimizer_D = torch.optim.Adam(discriminator.parameters(), lr=opt.lr_d, betas=(opt.b1, opt.b2)) # Learning rate decrease def adjust_learning_rate(opt, epoch, iteration, optimizer): #Set the learning rate to the initial LR decayed by "lr_decrease_factor" every "lr_decrease_epoch" epochs if opt.lr_decrease_mode == 'epoch': lr = opt.lr_g * (opt.lr_decrease_factor **(epoch // opt.lr_decrease_epoch)) for param_group in optimizer.param_groups: param_group['lr'] = lr if opt.lr_decrease_mode == 'iter': lr = opt.lr_g * (opt.lr_decrease_factor **(iteration // opt.lr_decrease_iter)) for param_group in optimizer.param_groups: param_group['lr'] = lr # Save the model if pre_train == True def save_model(opt, epoch, iteration, len_dataset, generator): """Save the model at "checkpoint_interval" and its multiple""" if opt.multi_gpu == True: if opt.save_mode == 'epoch': if (epoch % opt.save_by_epoch == 0) and (iteration % len_dataset == 0): if opt.save_name_mode: torch.save( generator.module, 'WGAN_%s_epoch%d_bs%d.pth' % (opt.task, epoch, opt.batch_size)) print( 'The trained model is successfully saved at epoch %d' % (epoch)) if opt.save_mode == 'iter': if iteration % opt.save_by_iter == 0: if opt.save_name_mode: torch.save( generator.module, 'WGAN_%s_iter%d_bs%d.pth' % (opt.task, iteration, opt.batch_size)) print( 'The trained model is successfully saved at iteration %d' % (iteration)) else: if opt.save_mode == 'epoch': if (epoch % opt.save_by_epoch == 0) and (iteration % len_dataset == 0): if opt.save_name_mode: torch.save( generator, 'WGAN_%s_epoch%d_bs%d.pth' % (opt.task, epoch, opt.batch_size)) print( 'The trained model is successfully saved at epoch %d' % (epoch)) if opt.save_mode == 'iter': if iteration % opt.save_by_iter == 0: if opt.save_name_mode: torch.save( generator, 'WGAN_%s_iter%d_bs%d.pth' % (opt.task, iteration, opt.batch_size)) print( 'The trained model is successfully saved at iteration %d' % (iteration)) # ---------------------------------------- # Network dataset # ---------------------------------------- # Define the dataset trainset = dataset.NormalRGBDataset(opt) print('The overall number of images:', len(trainset)) # Define the dataloader dataloader = DataLoader(trainset, batch_size=opt.batch_size, shuffle=True, num_workers=opt.num_workers, pin_memory=True) # ---------------------------------------- # Training # ---------------------------------------- # Count start time prev_time = time.time() # For loop training for epoch in range(opt.epochs): for i, (true_input, true_target) in enumerate(dataloader): # To device true_input = true_input.cuda() true_target = true_target.cuda() # Sample noise and get data noise1 = utils.get_noise(true_input.shape[0], opt.z_dim, opt.random_type) noise1 = noise1.cuda() # out: batch * z_dim noise2 = utils.get_noise(true_input.shape[0], opt.z_dim, opt.random_type) noise2 = noise2.cuda() # out: batch * z_dim concat_noise = torch.cat((noise1, noise2), 0) # out: 2batch * z_dim concat_input = torch.cat((true_input, true_input), 0) # out: 2batch * 1 * 256 * 256 concat_target = torch.cat((true_target, true_target), 0) # out: 2batch * 3 * 256 * 256 # Train Generator optimizer_G.zero_grad() fake_target = generator( concat_input, concat_noise) # out: 2batch * 3 * 256 * 256 # L1 Loss Pixellevel_L1_Loss = criterion_L1(fake_target, concat_target) # MSGAN Loss fake_target1, fake_target2 = fake_target.split( true_input.shape[0], 0) ms_value = torch.mean( torch.abs(fake_target2 - fake_target1)) / torch.mean( torch.abs(noise2 - noise1)) eps = 1e-5 ModeSeeking_Loss = 1 / (ms_value + eps) # GAN Loss fake_scalar = discriminator(concat_input, fake_target) GAN_Loss = -torch.mean(fake_scalar) # Overall Loss and optimize loss = opt.lambda_l1 * Pixellevel_L1_Loss + opt.lambda_gan * GAN_Loss + opt.lambda_ms * ModeSeeking_Loss loss.backward() optimizer_G.step() # Train Discriminator for j in range(opt.additional_training_d): optimizer_D.zero_grad() # Generator output fake_target = generator(concat_input, concat_noise) fake_target1, fake_target2 = fake_target.split( concat_noise.shape[0], 0) # Fake samples fake_scalar_d1 = discriminator(true_input, fake_target1.detach()) fake_scalar_d2 = discriminator(true_input, fake_target2.detach()) # True samples true_scalar_d = discriminator(true_input, true_target) # Overall Loss and optimize loss_D1 = -torch.mean(true_scalar_d) + torch.mean( fake_scalar_d1) loss_D2 = -torch.mean(true_scalar_d) + torch.mean( fake_scalar_d2) loss_D = loss_D1 + loss_D2 loss_D.backward() # Determine approximate time left iters_done = epoch * len(dataloader) + i iters_left = opt.epochs * len(dataloader) - iters_done time_left = datetime.timedelta(seconds=iters_left * (time.time() - prev_time)) prev_time = time.time() # Print log print( "\r[Epoch %d/%d] [Batch %d/%d] [Pixellevel L1 Loss: %.4f] [GAN Loss: %.4f] [D Loss: %.4f] Time_left: %s" % ((epoch + 1), opt.epochs, i, len(dataloader), Pixellevel_L1_Loss.item(), GAN_Loss.item(), loss_D.item(), time_left)) # Save model at certain epochs or iterations save_model(opt, (epoch + 1), (iters_done + 1), len(dataloader), generator) # Learning rate decrease at certain epochs adjust_learning_rate(opt, (epoch + 1), (iters_done + 1), optimizer_G)