def main(hparams): # 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 = utils.load_checkpoints(hparams) x_hats_dict = {model_type: {} for model_type in hparams.model_types} x_batch_dict = {} for key, x in xs_dict.iteritems(): 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.iteritems() ] x_batch = np.concatenate(x_batch_list) # Construct noise and measurements A = utils.get_A(hparams) noise_batch = hparams.noise_std * np.random.randn( hparams.batch_size, hparams.num_measurements) if hparams.measurement_type == 'project': y_batch = x_batch + noise_batch else: y_batch = np.matmul(x_batch, A) + noise_batch # Construct estimates using each estimator for model_type in hparams.model_types: estimator = estimators[model_type] x_hat_batch = estimator(A, y_batch, hparams) for i, key in enumerate(x_batch_dict.keys()): x = xs_dict[key] y = 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) l2_losses[model_type][key] = utils.get_l2_loss(x_hat, x) 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, 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, 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) mean_m_loss = np.mean(measurement_losses[model_type].values()) mean_l2_loss = np.mean(l2_losses[model_type].values()) 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.')
def main(hparams): # set up perceptual loss device = 'cuda:0' percept = PerceptualLoss( model="net-lin", net="vgg", use_gpu=device.startswith("cuda") ) 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 = utils.load_checkpoints(hparams) x_hats_dict = {model_type : {} for model_type in hparams.model_types} x_batch_dict = {} A = utils.get_A(hparams) noise_batch = hparams.noise_std * np.random.standard_t(2, size=(hparams.batch_size, hparams.num_measurements)) 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 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, m_loss_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] = m_loss_batch[key] 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] 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, 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, 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.')
def main(hparams): # Set up some stuff accoring to hparams hparams.n_input = np.prod(hparams.image_shape) utils.set_num_measurements(hparams) utils.print_hparams(hparams) if hparams.dataset == 'mnist': hparams.n_z = latent_dim elif hparams.dataset == 'celebA': hparams.z_dim = latent_dim # get inputs xs_dict = model_input(hparams) estimators = utils.get_estimators(hparams) utils.setup_checkpointing(hparams) measurement_losses, l2_losses = utils.load_checkpoints(hparams) image_loss_mnist = [] meas_loss_mnist = [] x_hat_mnist = [] x_hats_dict = {model_type : {} for model_type in hparams.model_types} x_batch_dict = {} for key, x in xs_dict.iteritems(): 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.iteritems()] x_batch = np.concatenate(x_batch_list) # Construct noise and measurements A = utils.get_A(hparams) noise_batch = hparams.noise_std * np.random.randn(hparams.batch_size, hparams.num_measurements) if hparams.measurement_type == 'project': y_batch = x_batch + noise_batch else: measure = np.matmul(x_batch, A) y_batch = np.absolute(measure) + noise_batch # Construct estimates using each estimator for model_type in hparams.model_types: x_main_batch = 10000*np.ones_like(x_batch) for k in range(num_restarts): print "Restart #", str(k+1) # Solve deep pr problem with random initial iterate init_iter = np.random.randn(hparams.batch_size, latent_dim) # First gradient descent z_opt_batch = init_iter estimator = estimators[model_type] items = estimator(A, y_batch, z_opt_batch, hparams) x_hat_batch1 = items[0] z_opt_batch1 = items[1] losses_val1 = items[2] x_hat_batch = x_hat_batch1 x_hat_batch = utils.resolve_ambiguity(x_hat_batch, x_batch, hparams.batch_size) # Use reflection of initial iterate z_opt_batch2 = -1*init_iter items = estimator(A, y_batch, z_opt_batch2, hparams) x_hat_batch2 = items[0] z_opt_batch2 = items[1] losses_val2 = items[2] x_hat_batch2 = utils.resolve_ambiguity(x_hat_batch2, x_batch, hparams.batch_size) x_hat_batchnew = utils.get_optimal_x_batch(x_hat_batch, x_hat_batch2, x_batch, hparams.batch_size) x_main_batch = utils.get_optimal_x_batch(x_hat_batchnew, x_main_batch, x_batch, hparams.batch_size) x_hat_batch = x_main_batch if hparams.dataset == 'mnist': utils.print_stats(x_hat_batch, x_batch, hparams.batch_size) for i, key in enumerate(x_batch_dict.keys()): x = xs_dict[key] y = 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) meas_loss_mnist.append(utils.get_measurement_loss(x_hat, A, y)) l2_losses[model_type][key] = utils.get_l2_loss(x_hat, x) image_loss_mnist.append(utils.get_l2_loss(x_hat,x)) 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, 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, 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: mean_m_loss = np.mean(measurement_losses[model_type].values()) mean_l2_loss = np.mean(l2_losses[model_type].values()) 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.'