Esempio n. 1
0
def view(xs_dict, patterns, images_nums, hparams, **kws):
    """View the images"""
    x_hats_dict = {}
    for model_type, pattern in zip(hparams.model_types, patterns):
        outfiles = [pattern.format(i) for i in images_nums]
        x_hats_dict[model_type] = {
            i: 2 * plt.imread(outfile) - 1
            for i, outfile in enumerate(outfiles)
        }
    xs_dict_temp = {i: xs_dict[i] for i in images_nums}
    utils.image_matrix(xs_dict_temp, x_hats_dict, view_image, hparams, **kws)
Esempio n. 2
0
def view(xs_dict, patterns_images, patterns_lpips, patterns_l2, images_nums,
         hparams, **kws):
    """View the images"""
    x_hats_dict = {}
    lpips_dict = {}
    l2_dict = {}
    for model_type, pattern_image, pattern_lpips, pattern_l2 in zip(
            hparams.model_types, patterns_images, patterns_lpips, patterns_l2):
        outfiles = [pattern_image.format(i) for i in images_nums]
        x_hats_dict[model_type] = {
            i: plt.imread(outfile)
            for i, outfile in enumerate(outfiles)
        }
        with open(pattern_lpips, 'rb') as f:
            lpips_dict[model_type] = pkl.load(f)
        with open(pattern_l2, 'rb') as f:
            l2_dict[model_type] = pkl.load(f)
    xs_dict_temp = {i: xs_dict[i] for i in images_nums}
    utils.image_matrix(xs_dict_temp, x_hats_dict, lpips_dict, l2_dict,
                       view_image, hparams, **kws)
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.')
Esempio n. 4
0
def main(hparams):
    hparams.n_input = np.prod(hparams.image_shape)
    hparams.model_type = 'vae'
    maxiter = hparams.max_outer_iter
    utils.print_hparams(hparams)
    xs_dict = model_input(hparams)  # returns the images
    estimators = utils.get_estimators(hparams)
    utils.setup_checkpointing(hparams)
    measurement_losses, l2_losses = utils.load_checkpoints(hparams)

    x_hats_dict = {'vae': {}}
    x_batch_dict = {}

    for key, x in xs_dict.iteritems():
        print key
        x_batch_dict[key] = x  #placing images in dictionary
        if len(x_batch_dict) < hparams.batch_size:
            continue
        x_coll = [
            x.reshape(1, hparams.n_input) for _, x in x_batch_dict.iteritems()
        ]  #Generates the columns of input x
        x_batch = np.concatenate(x_coll)  # Generates entire X

        A_outer = utils.get_outer_A(hparams)  # Created the random matric A

        noise_batch = hparams.noise_std * np.random.randn(
            hparams.batch_size, 100)

        y_batch_outer = np.sign(
            np.matmul(x_batch, A_outer)
        )  # Multiplication of A and X followed by quantization on 4 levels

        #y_batch_outer = np.matmul(x_batch, A_outer)

        x_main_batch = 0.0 * x_batch
        z_opt_batch = np.random.randn(hparams.batch_size,
                                      20)  #Input to the generator of the GAN

        for k in range(maxiter):

            x_est_batch = x_main_batch + hparams.outer_learning_rate * (
                np.matmul(
                    (y_batch_outer -
                     np.sign(np.matmul(x_main_batch, A_outer))), A_outer.T))
            #x_est_batch = x_main_batch + hparams.outer_learning_rate * (np.matmul((y_batch_outer - np.matmul(x_main_batch, A_outer)), A_outer.T))
            # Gradient decent in x is done
            estimator = estimators['vae']
            x_hat_batch, z_opt_batch = estimator(
                x_est_batch, z_opt_batch, hparams)  # Projectin on the GAN
            x_main_batch = x_hat_batch

        dist = np.linalg.norm(x_batch - x_main_batch) / 784
        print 'cool'
        print dist

        for i, key in enumerate(x_batch_dict.keys()):
            x = xs_dict[key]
            y = y_batch_outer[i]
            x_hat = x_hat_batch[i]

            # Save the estimate
            x_hats_dict['vae'][key] = x_hat

            # Compute and store measurement and l2 loss
            measurement_losses['vae'][key] = utils.get_measurement_loss(
                x_hat, A_outer, y)
            l2_losses['vae'][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 = {'dcgan' : {}}
            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.'
Esempio n. 5
0
def main(hparams):
    # Set up some stuff according to hparams
    hparams.n_input = np.prod(hparams.image_shape)
    maxiter = hparams.max_outer_iter
    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 = {'dcgan' : {}}
    x_batch_dict = {}
    for key, x in xs_dict.iteritems():
        if hparams.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 measurements
        A_outer = utils.get_outer_A(hparams)

        y_batch_outer=np.matmul(x_batch, A_outer)


        x_main_batch = 0.0 * x_batch
        z_opt_batch = np.random.randn(hparams.batch_size, 100)
        for k in range(maxiter):

            x_est_batch=x_main_batch + hparams.outer_learning_rate*(np.matmul((y_batch_outer-np.matmul(x_main_batch,A_outer)),A_outer.T))



            estimator = estimators['dcgan']
            x_hat_batch,z_opt_batch = estimator(x_est_batch,z_opt_batch, hparams)
            x_main_batch=x_hat_batch


        for i, key in enumerate(x_batch_dict.keys()):
            x = xs_dict[key]
            y = y_batch_outer[i]
            x_hat = x_hat_batch[i]

            # Save the estimate
            x_hats_dict['dcgan'][key] = x_hat

            # Compute and store measurement and l2 loss
            measurement_losses['dcgan'][key] = utils.get_measurement_loss(x_hat, A_outer, y)
            l2_losses['dcgan'][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 = {'dcgan' : {}}
            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 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.')
Esempio n. 7
0
def main(hparams):
    hparams.n_input = np.prod(hparams.image_shape)
    maxiter = hparams.max_outer_iter
    utils.print_hparams(hparams)
    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 = {'dcgan': {}}
    x_batch_dict = {}
    for key, x in xs_dict.iteritems():
        x_batch_dict[key] = x
        if len(x_batch_dict) < hparams.batch_size:
            continue
        x_coll = [
            x.reshape(1, hparams.n_input) for _, x in x_batch_dict.iteritems()
        ]
        x_batch = np.concatenate(x_coll)
        A_outer = utils.get_outer_A(hparams)
        # 1bitify
        y_batch_outer = np.sign(np.matmul(x_batch, A_outer))

        x_main_batch = 0.0 * x_batch
        z_opt_batch = np.random.randn(hparams.batch_size, 100)
        for k in range(maxiter):
            x_est_batch = x_main_batch + hparams.outer_learning_rate * (
                np.matmul(
                    (y_batch_outer -
                     np.sign(np.matmul(x_main_batch, A_outer))), A_outer.T))
            estimator = estimators['dcgan']
            x_hat_batch, z_opt_batch = estimator(x_est_batch, z_opt_batch,
                                                 hparams)
            x_main_batch = x_hat_batch

        for i, key in enumerate(x_batch_dict.keys()):
            x = xs_dict[key]
            y = y_batch_outer[i]
            x_hat = x_hat_batch[i]
            x_hats_dict['dcgan'][key] = x_hat
            measurement_losses['dcgan'][key] = utils.get_measurement_loss(
                x_hat, A_outer, y)
            l2_losses['dcgan'][key] = utils.get_l2_loss(x_hat, x)
        print 'Processed upto image {0} / {1}'.format(key + 1, len(xs_dict))
        if (hparams.save_images) and ((key + 1) % hparams.checkpoint_iter
                                      == 0):
            utils.checkpoint(x_hats_dict, measurement_losses, l2_losses,
                             save_image, hparams)
            print '\nProcessed and saved first ', key + 1, 'images\n'

        x_batch_dict = {}

    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):
#    if not hparams.use_gpu:
#        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
    # Set up some stuff accoring to hparams
    hparams.n_input = np.prod(hparams.image_shape)
    #hparams.stdv = 10 #adjust to HPARAM in model_def.py
    #hparams.mean = 0 #adjust to HPARAM in model_def.py
    utils.set_num_measurements(hparams)
    utils.print_hparams(hparams)

    hparams.bol = False
 #   hparams.dict_flag = False
    # get inputs
    if hparams.input_type == 'dict-input':# or hparams.dict_flag:
        hparams_load_key = copy.copy(hparams)
        hparams_load_key.input_type = 'full-input'
        hparams_load_key.measurement_type = 'project'
        hparams_load_key.zprior_weight = 0.0
        hparams.key_field = np.load(utils.get_checkpoint_dir(hparams_load_key, hparams.model_types[0])+'candidates.npy').item()
        print(hparams.measurement_type)
    xs_dict, label_dict = model_input(hparams)    

    estimators = utils.get_estimators(hparams)
    utils.setup_checkpointing(hparams)
    sh = utils.SaveHandler()
    sh.load_or_init_all(hparams.save_images,hparams.model_types,sh.get_pkl_filepaths(hparams,use_all=True))
    if label_dict is None:
        print('No labels exist.')
        del sh.class_loss
#    measurement_losses, l2_losses, emd_losses, x_orig, x_rec, noise_batch = utils.load_checkpoints(hparams)
    
    if hparams.input_type == 'gen-span':
        np.save(utils.get_checkpoint_dir(hparams, hparams.model_types[0])+'z.npy',hparams.z_from_gen)
        np.save(utils.get_checkpoint_dir(hparams, hparams.model_types[0])+'images.npy',hparams.images_mat)
    
    

    x_hats_dict = {model_type : {} for model_type in hparams.model_types}
    x_batch_dict = {}
    x_batch=[]
    x_hat_batch=[]
#    l2_losses2=np.zeros((len(xs_dict),1))
#    distances_arr=[]
    image_distance =np.zeros((len(xs_dict),1))
    hparams.x = [] # TO REMOVE
    for key, x in xs_dict.iteritems(): #//each batch once (x_batch_dict emptied at end)
        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       
        hparams.x.append(x)#To REMOVE
        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)
#        x_batch, known_distortion, distances = get_random_distortion(x_batch)
#        distances_arr[(key-1)*hparams.batch_size:key*hparams.batch_size] = distances
#        xs_dict[(key-1)*hparams.batch_size:key*hparams.batch_size] =x_batch
        
        # Construct noise and measurements
        recovered, optim = utils.load_if_optimized(hparams)
        if recovered and np.linalg.norm(optim.x_orig-x_batch) < 1e-10:
            hparams.optim = optim
            hparams.recovered = True
        else:
            hparams.recovered=False
            optim.x_orig = x_batch
            
            hparams.optim = optim
            
        A, noise_batch, y_batch, c_val = utils.load_meas(hparams,sh,x_batch,xs_dict)
        hparams.optim.noise_batch = noise_batch
        if c_val:
            continue
        
        if hparams.measurement_type == 'sample_distribution':
            plot_distribution(hparams,x_batch)
            
#            for i in range(z.shape[1]):#range(1):
#                plt.hist(z[i,:], facecolor='blue', alpha=0.5)
#                directory_distr = 
#                pl.savefig("abc.png")            
        elif hparams.measurement_type == 'autoencoder':
            plot_reconstruction(hparams,x_batch) 
        else:
            # Construct estimates using each estimator
            for model_type in hparams.model_types:
                estimator = estimators[model_type]
                start = time.time()

                tmp = estimator(A, y_batch, hparams)
                if isinstance(tmp,tuple):
                    x_hat_batch = tmp[0]
                    sh.z_rec = tmp[1]                    
                else:
                    x_hat_batch = tmp
                    del sh.z_rec
                end = time.time()
                duration = end-start
                print('The calculation needed {} time'.format(datetime.timedelta(seconds=duration)))
                np.save(utils.get_checkpoint_dir(hparams, model_type)+'elapsed_time',duration)
#                DEBUGGING = []
                for i, key in enumerate(x_batch_dict.keys()):
    #                x = xs_dict[key]+known_distortion[i]
                    x = xs_dict[key]
                    y = y_batch[i]
                    x_hat = x_hat_batch[i]
#                    plt.figure()
#                    plt.imshow(np.reshape(x_hat, [64, 64, 3])*255)#, interpolation="nearest", cmap=plt.cm.gray)
#                    plt.show()
    
                    # Save the estimate
                    x_hats_dict[model_type][key] = x_hat
    
                    # Compute and store measurement and l2 loss
                    sh.measurement_losses[model_type][key] = utils.get_measurement_loss(x_hat, A, y)
#                    DEBUGGING.append(np.sum((x_hat.dot(A)-y)**2)/A.shape[1])
                    sh.l2_losses[model_type][key] = utils.get_l2_loss(x_hat, x)
                    if hparams.class_bol and label_dict is not None:
                        try:
                            sh.class_losses[model_type][key] = utils.get_classifier_loss(hparams,x_hat,label_dict[key])
                        except:
                            sh.class_losses[model_type][key] = NaN
                            warnings.warn('Class loss unsuccessfull, most likely due to corrupted memory. Simply retry.')
                    if hparams.emd_bol:
                        try:
                            _,sh.emd_losses[model_type][key] = utils.get_emd_loss(x_hat, x)
                            if 'nonneg' not in hparams.tv_or_lasso_mode and 'pca'  in model_type:
                                warnings.warn('EMD requires nonnegative images, for safety insert nonneg into tv_or_lasso_mode')
                        except ValueError:
                            warnings.warn('EMD calculation unsuccesfull (most likely due to negative images)')
                            pass
    #                    if l2_losses[model_type][key]-measurement_losses[model_type][key]!=0:
    #                        print('NO')
    #                        print(y)
    #                        print(x)
    #                        print(np.mean((x-y)**2))
                    image_distance[i] = np.linalg.norm(x_hat-x)
    #                l2_losses2[key] = np.mean((x_hat-x)**2)
    #                print('holla')
    #                print(l2_losses2[key])
    #                print(np.linalg.norm(x_hat-x)**2/len(xs_dict[0]))
    #                print(np.linalg.norm(x_hat-x)/len(xs_dict[0]))
    #                print(np.linalg.norm(x_hat-x))
            print('Processed upto image {0} / {1}'.format(key+1, len(xs_dict)))
            sh.x_orig = x_batch
            sh.x_rec = x_hat_batch
            sh.noise = noise_batch
    
            #ACTIVATE ON DEMAND
            #plot_bad_reconstruction(measurement_losses,x_batch)
            # Checkpointing
            if (hparams.save_images) and ((key+1) % hparams.checkpoint_iter == 0):           
                utils.checkpoint(x_hats_dict, save_image, sh, 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 = {}
                   

    if 'wavelet' in hparams.model_types[0]:
        print np.abs(sh.x_rec)
        print('The average sparsity is {}'.format(np.sum(np.abs(sh.x_rec)>=0.0001)/float(hparams.batch_size)))

    # Final checkpoint
    if hparams.save_images:
        utils.checkpoint(x_hats_dict, save_image, sh, hparams)
        print('\nProcessed and saved all {0} image(s)\n'.format(len(xs_dict)))
        if hparams.dataset in ['mnist', 'fashion-mnist']:
            if np.array(x_batch).size:
                utilsM.save_images(np.reshape(x_batch, [-1, 28, 28]),
                                          [8, 8],utils.get_checkpoint_dir(hparams, hparams.model_types[0])+'original.png')
            if np.array(x_hat_batch).size:
                utilsM.save_images(np.reshape(x_hat_batch, [-1, 28, 28]),
                                          [8, 8],utils.get_checkpoint_dir(hparams, hparams.model_types[0])+'reconstruction.png')

        for model_type in hparams.model_types:
#            print(model_type)
            mean_m_loss = np.mean(sh.measurement_losses[model_type].values())
            mean_l2_loss = np.mean(sh.l2_losses[model_type].values()) #\|XHUT-X\|**2/784/64
            if hparams.emd_bol:
                mean_emd_loss = np.mean(sh.emd_losses[model_type].values())
            if label_dict is not None:
                mean_class_loss = np.mean(sh.class_losses[model_type].values())
                print('mean class loss = {0}'.format(mean_class_loss))
#            print(image_distance)
            mean_norm_loss = np.mean(image_distance)#sum_i(\|xhut_i-x_i\|)/64
#            mean_rep_error = np.mean(distances_arr)
#            mean_opt_meas_error_pixel = np.mean(np.array(l2_losses[model_type].values())-np.array(distances_arr)/xs_dict[0].shape)
#            mean_opt_meas_error = np.mean(image_distance-distances_arr)
            print('mean measurement loss = {0}'.format(mean_m_loss))
#            print np.sum(np.asarray(DEBUGGING))/64
            print('mean l2 loss = {0}'.format(mean_l2_loss))
            if hparams.emd_bol:
                print('mean emd loss = {0}'.format(mean_emd_loss))            
            print('mean distance = {0}'.format(mean_norm_loss))
            print('mean distance pixelwise = {0}'.format(mean_norm_loss/len(xs_dict[xs_dict.keys()[0]])))
#            print('mean representation error = {0}'.format(mean_rep_error))
#            print('mean optimization plus measurement error = {0}'.format(mean_opt_meas_error))
#            print('mean optimization plus measurement error per pixel = {0}'.format(mean_opt_meas_error_pixel))

    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.')
Esempio n. 9
0
def main(hparams):
    hparams.n_input = np.prod(hparams.image_shape)
    images = model_input(hparams)

    estimators = utils.get_estimators(hparams)
    utils.setup_checkpointing(hparams)
    measurement_losses, l2_losses = utils.load_checkpoints(hparams)

    est_images = {model_type: {} for model_type in hparams.model_types}
    for i, image in images.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, i)
            if all([
                    os.path.isfile(save_path)
                    for save_path in save_paths.values()
            ]):
                continue

        # Reshape input
        x_val = image.reshape(1, hparams.n_input)

        # Construct noise and measurements
        noise_val = hparams.noise_std * np.random.randn(
            1, hparams.num_measurements)
        A_val = np.random.randn(hparams.n_input, hparams.num_measurements)
        y_val = np.matmul(x_val, A_val) + noise_val

        # Construct estimates using each estimator
        print 'Processing image {0}'.format(i)
        for model_type in hparams.model_types:
            estimator = estimators[model_type]
            est_image = estimator(A_val, y_val, hparams)
            est_images[model_type][i] = est_image
            # Compute and store measurement and l2 loss
            measurement_losses[model_type][i] = utils.get_measurement_loss(
                est_image, A_val, y_val)
            l2_losses[model_type][i] = utils.get_l2_loss(est_image, image)

        # Checkpointing
        if (hparams.save_images) and ((i + 1) % 100 == 0):
            utils.checkpoint(est_images, measurement_losses, l2_losses,
                             save_image, hparams)
            est_images = {model_type: {} for model_type in hparams.model_types}
            print '\nProcessed and saved first ', i + 1, 'images\n'

    # Final checkpoint
    if hparams.save_images:
        utils.checkpoint(est_images, measurement_losses, l2_losses, save_image,
                         hparams)
        print '\nProcessed and saved all {0} images\n'.format(len(images))

    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(images, est_images, view_image, hparams)
Esempio n. 10
0
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.'