Example #1
0
def build_summary(metrics, labels, logits, learning_rate):
    true_image = map_to_image(utils.tile_images(labels))
    pred_image = map_to_image(
        utils.tile_images(tf.to_float(tf.nn.sigmoid(logits) > 0.5)))

    return tf.summary.merge([
        tf.summary.scalar('learning_rate', learning_rate),
        tf.summary.scalar('loss', metrics['loss']),
        tf.summary.scalar('iou', metrics['iou']),
        tf.summary.scalar('roc_auc', metrics['roc_auc']),
        tf.summary.scalar('pr_auc', metrics['pr_auc']),
        tf.summary.image('true_image', tf.expand_dims(true_image, 0)),
        tf.summary.image('pred_image', tf.expand_dims(pred_image, 0))
    ])
Example #2
0
def draw(trainer=None, model=None, batch=None, prefix=None):
    # save input & predicted images
    with chainer.using_config('enable_backprop', False):
        with chainer.using_config('train', False):
            directory = trainer.out

            if model.dataset_name == 'shapenet':
                images_in, _, viewpoints, _, _ = converter(batch)
                images_prediction = model.predict_and_render(
                    images_in, viewpoints).data.get().squeeze()
                images_prediction_cp = model.predict_and_render(
                    images_in, viewpoints[::-1]).data.get().squeeze()
                images_ref = None
                cmin_in = -1
            elif model.dataset_name == 'pascal':
                images_in, images_ref, rotation_matrices, rotation_matrices_random, labels = converter(
                    batch)
                images_prediction = model.predict_and_render(
                    images_in, rotation_matrices).data.get().squeeze()
                images_prediction_cp = model.predict_and_render(
                    images_in, rotation_matrices_random).data.get().squeeze()
                cmin_in = 0

            # input images
            image = utils.tile_images(images_in.get())
            scipy.misc.toimage(image, cmin=cmin_in, cmax=1).save(
                os.path.join(directory, '%s_images_in.png' % prefix))
            if images_ref is not None:
                image = utils.tile_images(images_ref.get())
                scipy.misc.toimage(image, cmin=cmin_in, cmax=1).save(
                    os.path.join(directory, '%s_images_ref.png' % prefix))

            # predicted images
            images_prediction = utils.tile_images(images_prediction)
            if model.no_texture:
                images_prediction = 1 - images_prediction
            scipy.misc.toimage(images_prediction, cmin=0, cmax=1).save(
                os.path.join(directory, '%s_prediction.png' % prefix))

            # predicted images from viewpoints
            images_prediction_cp = utils.tile_images(images_prediction_cp)
            if model.no_texture:
                images_prediction_cp = 1 - images_prediction_cp
            scipy.misc.toimage(images_prediction_cp, cmin=0, cmax=1).save(
                os.path.join(directory, '%s_prediction_cp.png' % prefix))
Example #3
0
 def render(self, mode='human'):
     imgs = self.get_images()
     bigimg = tile_images(imgs)
     if mode == 'human':
         self.get_viewer().imshow(bigimg)
         return self.get_viewer().isopen
     elif mode == 'rgb_array':
         return bigimg
     else:
         raise NotImplementedError
Example #4
0
 def render(self, mode='human'):
     for pipe in self.remotes:
         pipe.send(('render', None))
     imgs = [pipe.recv() for pipe in self.remotes]
     bigimg = tile_images(imgs)
     if mode == 'human':
         import cv2
         cv2.imshow('vecenv', bigimg[:, :, ::-1])
         cv2.waitKey(1)
     elif mode == 'rgb_array':
         return bigimg
     else:
         raise NotImplementedError
Example #5
0
def generate_between_classes(
    model: torch.nn.Module,
    img_size: list,
    classes: int,
    saveto: str,
    n_classes: int,
    cuda: bool = False,
) -> None:
    y = np.zeros((1, n_classes), dtype="float32")
    y[:, classes] = 1 / len(classes)
    y = np.repeat(y, 10, axis=0)
    gen = utils.tile_images(generate(model, img_size, y, cuda=cuda), n_rows=1)
    imageio.imsave(saveto, gen.astype("uint8"))
Example #6
0
    def render(self, mode="human"):
        for pipe in self.remotes:
            pipe.send(("render", None))
        imgs = [pipe.recv() for pipe in self.remotes]
        bigimg = tile_images(imgs)
        if mode == "human":
            import cv2

            cv2.imshow("vecenv", bigimg[:, :, ::-1])
            cv2.waitKey(1)
        elif mode == "rgb_array":
            return bigimg
        else:
            raise NotImplementedError
    def render(self, mode='human'):
        for pipe in self.remotes:
            pipe.send(('render', None))
        if self.experiment_lvl == 'ego':
            normal = []
            for pipe in self.remotes:
                res = pipe.recv()
                normal.append(res[0]['normal'])

            imgs = np.stack(normal)
        else:
            imgs = [pipe.recv() for pipe in self.remotes]
        bigimg = tile_images(imgs)
        if mode == 'human':
            import cv2
            cv2.imshow('vecenv', bigimg[:, :, ::-1])
            cv2.waitKey(1)
        elif mode == 'rgb_array':
            return bigimg
        else:
            raise NotImplementedError
    def render(self, mode='human'):
        for pipe in self.remotes:
            pipe.send(('render', None))
        imgs = [pipe.recv() for pipe in self.remotes]
        bigimg = tile_images(imgs)
        if mode == 'human':
            #import cv2
            #cv2.imshow('vecenv', bigimg[:,:,::-1])
            #cv2.waitKey(1)
            if self.viewer is None:
                from gym.envs.classic_control import rendering
                self.viewer = rendering.SimpleImageViewer()

            #print(imgs)
            self.viewer.imshow(bigimg[:, :, ::-1])
            #print(bigimg[:, :, ::-1].shape)
            return bigimg[:, :, ::-1]
        elif mode == 'rgb_array':
            return bigimg
        else:
            raise NotImplementedError
Example #9
0
    def test_RBM_timep(self, learning_rate=0.1, training_epochs=10,
                 batch_size=50,
                 n_chains=20, n_samples=5, output_folder='rbm_plots',
                 n_hidden=500):
        """
        Demonstrate how to train and afterwards sample from it using Theano.
        
        :param learning_rate: learning rate used for training the RBM
    
        :param training_epochs: number of epochs used for training
    
        :param dataset: path the the pickled dataset
    
        :param batch_size: size of a batch used to train the RBM
    
        :param n_chains: number of parallel Gibbs chains to be used for sampling
    
        :param n_samples: number of samples to plot for each chain
    
        from DLRBM_2D_temporal import *
        funcs = DLRBM_2D_temporal()
        funcs.test_RBM_timep()
    
        """
    
        traindata_path= 'allLpatches_10x10x5.pklz' #'allLpatches_subs_smaller.pklz' #'allLpatches.pklz'
        trainUdata_path= 'allUpatches_10x10x5.pklz'#'allUpatches_subs_smaller.pklz' #'allUpatches.pklz'
        labeldata_path= 'allLabels_10x10x5.pklz' #'allLabels_subs_smaller.pklz' #'allLabels.pklz'
        
        #############
        ## LOAD datasets
        #############
        datasets = self.load_wUdata(traindata_path, labeldata_path, trainUdata_path)
        
        train_set_x, train_set_y = datasets[0]
        np_train_x, np_train_y = datasets[3]
        valid_set_x, valid_set_y = datasets[1]
        np_valid_x, np_valid_y = datasets[4]        
        test_set_x, test_set_y = datasets[2]
        np_test_x, np_test_y = datasets[5]
    
        # inpect one image class 1
        Vol = np_train_x[0].reshape(5,10,10)
        imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
        subslicestime = []  
        
        # Display image
        fig, ax = plt.subplots(nrows=4, ncols=4, figsize=(16, 8))
        for k in range(1,5):
            ax[k-1,0].imshow(imgslicestime[k], cmap=plt.cm.gray)
            ax[k-1,0].set_axis_off()
            ax[k-1,0].set_adjustable('box-forced')
        
            # Display Original histogram
            ax[k-1,1].hist(imgslicestime[k].ravel(), bins=50, color='black')
            ax[k-1,1].ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
            ax[k-1,1].set_xlabel('original')
            
            # substract volume
            subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
            subslicestime.append(subVol)
        
            # Display normalized histogram
            ax[k-1,2].hist(subVol.ravel(), bins=50, color='black')
            ax[k-1,2].ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
            ax[k-1,2].set_xlabel('substracted histogram')
       
            # display normalized 0-1
            ax[k-1,3].imshow(subVol, cmap=plt.cm.gray)
            ax[k-1,3].set_axis_off()
            ax[k-1,3].set_adjustable('box-forced')
        
        plt.show(block=False)
        
        #################
        # Substract pathces from pre-contrast
        #################
        subsnp_train_x = []
        subsnp_valid_x = []
        subsnp_test_x = []

        for img in np_train_x:
            Vol = img.reshape(5,10,10)
            imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
            subslicestime = []  
            for k in range(1,5):
                # substract volume
                subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
                subslicestime.append(subVol)
            #append
            subsnp_train_x.append( np.asarray(subslicestime).reshape(4*10*10) )          
        for img in np_valid_x:
            Vol = img.reshape(5,10,10)
            imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
            subslicestime = []  
            for k in range(1,5):
                # substract volume
                subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
                subslicestime.append(subVol)
            #append
            subsnp_valid_x.append( np.asarray(subslicestime).reshape(4*10*10) )
        for img in np_test_x:
            Vol = img.reshape(5,10,10)
            imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
            subslicestime = []  
            for k in range(1,5):
                # substract volume
                subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
                subslicestime.append(subVol)
            #append
            subsnp_test_x.append( np.asarray(subslicestime).reshape(4*10*10) )
        
#        train_set_x, train_set_y = self.shared_dataset(subsnp_train_x, np_train_y)
#        valid_set_x, valid_set_y = self.shared_dataset(subsnp_valid_x, np_valid_y)
#        test_set_x, test_set_y = self.shared_dataset(subsnp_test_x, np_test_y)
#        
#        subsdatasets = [(train_set_x, train_set_y), 
#                        (valid_set_x, valid_set_y), 
#                        (test_set_x, test_set_y) ]
                        
            
        print "n_chains %d " % n_chains
        print "n_hidden %d " %  n_hidden
        print "training_epochs %d " %  training_epochs
        print "batch_size %d " %  batch_size
        print "learning_rate %f " %  learning_rate      
        print "n_samples %d " %  n_samples
        print "output_folder %s " %  output_folder
               
        ##############################
        # compute number of minibatches for training, validation and testing
        n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size
    
        # allocate symbolic variables for the data
        index = T.lscalar()    # index to a [mini]batch
        x = T.matrix('x')  # the data is presented as rasterized images
    
        rng = numpy.random.RandomState(123)
        theano_rng = RandomStreams(rng.randint(2 ** 30))
    
        # initialize storage for the persistent chain (state = hidden
        # layer of chain)
        persistent_chain = theano.shared(numpy.zeros((batch_size, n_hidden),
                                                     dtype=theano.config.floatX),
                                         borrow=True)
    
        # construct the RBM class
        rbm = RBM(input=x, n_visible=5*10*10,
                  n_hidden=n_hidden, numpy_rng=rng, theano_rng=theano_rng)
    
        # get the cost and the gradient corresponding to one step of CD-15
        cost, updates = rbm.get_cost_updates(lr=learning_rate,
                                             persistent=persistent_chain, k=15)
    
        #################################
        #     Training the RBM          #
        #################################
        if not os.path.isdir(output_folder):
            os.makedirs(output_folder)
        os.chdir(output_folder)
    
        # it is ok for a theano function to have no output
        # the purpose of train_rbm is solely to update the RBM parameters
        train_rbm = theano.function(
            [index],
            cost,
            updates=updates,
            givens={
                x: train_set_x[index * batch_size: (index + 1) * batch_size]
            },
            name='train_rbm'
        )
    
        plotting_time = 0.
        start_time = timeit.default_timer()
    
        # go through training epochs
        for epoch in range(training_epochs):
    
            # go through the training set
            mean_cost = []
            for batch_index in range(n_train_batches):
                mean_cost += [train_rbm(batch_index)]
    
            print('Training epoch %d, cost is ' % epoch, numpy.mean(mean_cost))
    
            # Plot filters after each training epoch
            plotting_start = timeit.default_timer()
            # Construct image from the weight matrix
            Wrbm = rbm.W.get_value(borrow=True).T
            
            fig, axes = plt.subplots(ncols=4, nrows=1, figsize=(16, 6))
            for k in range(1,5):
                image = utils.tile_images(
                        X=Wrbm.reshape( Wrbm.shape[0], 5, 10, 10)[:,k,:,:],
                        img_shape=(10,10),
                        tile_shape=(25,20),
                        tile_spacing=(0, 0) )
                
                im = axes[k-1].imshow(image, vmin=np.min(image.ravel()), vmax=np.max(image.ravel()), interpolation='nearest', cmap="Greys_r")  
                axes[k-1].get_xaxis().set_visible(False)
                axes[k-1].get_yaxis().set_visible(False)
                
            cax,kw = mpl.colorbar.make_axes([ax for ax in axes.flat])
            plt.colorbar(im, cax=cax, **kw)  
            plt.show(block=False)
            fig.savefig('filters_at_epoch_%i.png' % epoch)
            
            plotting_stop = timeit.default_timer()
            plotting_time += (plotting_stop - plotting_start)
    
        end_time = timeit.default_timer()
        pretraining_time = (end_time - start_time) - plotting_time
        print ('Plotting took %f minutes' % (plotting_time / 60.))
        print ('Training took %f minutes' % (pretraining_time / 60.))

        #####################################
        # save the best model
        #####################################
        with open('bestRBM_10x10x5.obj', 'wb') as fp:
            pickle.dump(rbm, fp)
        
        #####################################
        # Open best model
        #####################################
        with open('bestRBM_10x10x5.obj', 'rb') as fp:
            rbm = pickle.load(fp)
            
        #################################
        #     Sampling from the RBM     #
        #################################
        # find out the number of test samples
        number_of_test_samples = test_set_x.get_value(borrow=True).shape[0]
    
        # pick random test examples, with which to initialize the persistent chain
        test_idx = rng.randint(number_of_test_samples - n_chains)
        persistent_vis_chain = theano.shared(
            numpy.asarray(
                test_set_x.get_value(borrow=True)[test_idx:test_idx + n_chains],
                dtype=theano.config.floatX
            )
        )
        plot_every = 1000
        # define one step of Gibbs sampling (mf = mean-field) define a
        # function that does `plot_every` steps before returning the
        # sample for plotting
        (
            [
                presig_hids,
                hid_mfs,
                hid_samples,
                presig_vis,
                vis_mfs,
                vis_samples
            ],
            updates
        ) = theano.scan(
            rbm.gibbs_vhv,
            outputs_info=[None, None, None, None, None, persistent_vis_chain],
            n_steps=plot_every
        )
    
        # add to updates the shared variable that takes care of our persistent
        # chain :.
        updates.update({persistent_vis_chain: vis_samples[-1]})
        # construct the function that implements our persistent chain.
        # we generate the "mean field" activations for plotting and the actual
        # samples for reinitializing the state of our persistent chain
        sample_fn = theano.function(
            [],
            [
                vis_mfs[-1],
                vis_samples[-1]
            ],
            updates=updates,
            name='sample_fn'
        )
    
        # create a space to store the image for plotting ( we need to leave
        # room for the tile_spacing as well)
        sub_pre = numpy.zeros(
            (11 * n_samples + 1, 11 * n_chains - 1),
            dtype='uint8')
        sub_post1 = numpy.zeros(
            (11 * n_samples + 1, 11 * n_chains - 1),
            dtype='uint8')
        sub_post2 = numpy.zeros(
            (11 * n_samples + 1, 11 * n_chains - 1),
            dtype='uint8')
        sub_post3 = numpy.zeros(
            (11 * n_samples + 1, 11 * n_chains - 1),
            dtype='uint8')
        sub_post4 = numpy.zeros(
            (11 * n_samples + 1, 11 * n_chains - 1),
            dtype='uint8')
        
        fig, axes = plt.subplots(ncols=1, nrows=5)
        for idx in range(n_samples):
            # generate `plot_every` intermediate samples that we discard,
            # because successive samples in the chain are too correlated
            vis_mf, vis_sample = sample_fn()
            print(' ... plotting sample %d' % idx)
            Xvis = vis_mf.reshape( vis_mf.shape[0],5,100)
            
            sub_pre[11 * idx:11 * idx + 10, :] = utils.tile_raster_images(
                X=Xvis[:,0,:],
                img_shape=(10, 10),
                tile_shape=(1, n_chains),
                tile_spacing=(1, 1)
            )
            sub_post1[11 * idx:11 * idx + 10, :] = utils.tile_raster_images(
                X=Xvis[:,1,:],
                img_shape=(10, 10),
                tile_shape=(1, n_chains),
                tile_spacing=(1, 1)
            )
            sub_post2[11 * idx:11 * idx + 10, :] = utils.tile_raster_images(
                X=Xvis[:,2,:],
                img_shape=(10, 10),
                tile_shape=(1, n_chains),
                tile_spacing=(1, 1)
            )
            sub_post3[11 * idx:11 * idx + 10, :] = utils.tile_raster_images(
                X=Xvis[:,3,:],
                img_shape=(10, 10),
                tile_shape=(1, n_chains),
                tile_spacing=(1, 1)
            )
            sub_post4[11 * idx:11 * idx + 10, :] = utils.tile_raster_images(
                X=Xvis[:,4,:],
                img_shape=(10, 10),
                tile_shape=(1, n_chains),
                tile_spacing=(1, 1)
            )
    
        # construct image
        axes[0].imshow( Image.fromarray( sub_pre), cmap="Greys_r")
        axes[1].imshow( Image.fromarray( sub_post1), cmap="Greys_r")
        axes[2].imshow( Image.fromarray( sub_post2), cmap="Greys_r")
        axes[3].imshow( Image.fromarray( sub_post3), cmap="Greys_r") 
        axes[4].imshow( Image.fromarray( sub_post4), cmap="Greys_r")  
        axes[0].get_xaxis().set_visible(False)
        axes[0].get_yaxis().set_visible(False)
        axes[1].get_xaxis().set_visible(False)
        axes[1].get_yaxis().set_visible(False)
        axes[2].get_xaxis().set_visible(False)
        axes[2].get_yaxis().set_visible(False)
        axes[3].get_xaxis().set_visible(False)
        axes[3].get_yaxis().set_visible(False)
        axes[4].get_xaxis().set_visible(False)
        axes[4].get_yaxis().set_visible(False)
        plt.show() 
        item='test'
        #show and save                     
        fig.savefig('samples_fromchain'+str(item)+'.pdf')

        return 
Example #10
0
def main(args):
    dset = load_data(args.dataset, args.test)
    if not args.test:
        val_images = test_images = dset.validation.images
    else:
        val_images = dset.test.images

    def get_val_batch():
        idcs = np.arange(val_images.shape[0])
        np.random.shuffle(idcs)
        return val_images[idcs[:args.batch_size * 2]]

    compute_fid, _fid_lc = load_fid(dset.test.images, args)

    use_restored_s1 = len(args.restore_s1_from) > 0
    if use_restored_s1:
        args_json = json.load(
            open(os.path.join(args.restore_s1_from, 'hps.txt')))
        assert args_json['dataset'] == args.dataset and \
          args_json['sd_param'] == args.sd_param
        keys_to_restore = ['sd_param', 'z_dims_s1', 'skip_last_fc']
        vars(args).update(dict((k, args_json[k]) for k in keys_to_restore))

    tf.set_random_seed(args.seed)
    np.random.seed(args.seed)
    G = Graph(args, val_images.shape[1], val_images.shape[3])
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())

    if use_restored_s1:
        s1_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                    scope=FIRST_STAGE)
        saver = tfv1.train.Saver(var_list=s1_vars)
        ckpt_dir = tf.train.get_checkpoint_state(
            args.restore_s1_from).model_checkpoint_path
        print('RESTORING S1 FROM', ckpt_dir)
        saver.restore(sess, ckpt_dir)

    saver = tfv1.train.Saver(keep_checkpoint_every_n_hours=2, max_to_keep=1)
    print(args)
    B = 100

    if args.decoder_warmup and not use_restored_s1:
        print('Warmup stage:')
        es = EarlyStopper(args.n_iter_s0 // (B * 4))
        with LogContext(args.n_iter_s0 // B, logdir=args.dir,
                        tfsummary=True) as ctx:
            for i in ctx:
                for j in range(B):  # training
                    x_mb, _ = dset.train.next_batch(args.batch_size)
                    G.train_step_0(sess, x_mb, args.lr * 2, i)

                x_mb = G.process_batch(get_val_batch())
                to_log = sess.run({**G.optimizer_s0.print}, {
                    G.x_ph: x_mb,
                    G.is_training_ph: False
                })
                ctx.log_scalars(to_log, list(to_log))
                if es.add_check(to_log['__loss']):
                    print('Early stopping')
                    break

    if not use_restored_s1:
        print('First stage:')
        es = EarlyStopper(args.n_iter_s1 // (B * 8))
        with LogContext(args.n_iter_s1 // B, logdir=args.dir,
                        tfsummary=True) as ctx:
            for i in ctx:
                for j in range(B):  # training
                    lr = compute_lr(args.lr, args.lr_halve_every_s1, i * B + j)
                    x_mb, _ = dset.train.next_batch(args.batch_size)
                    G.train_step_1(sess, x_mb, lr, i)
                # plot validation
                x_mb = G.process_batch(get_val_batch())
                to_log = sess.run({**G.optimizer_s1.print}, {
                    G.x_ph: x_mb,
                    G.is_training_ph: False
                })
                # if i % 80 == 0:
                #   fids, _ = G.get_fids('s1', sess, val_images, compute_fid)
                #   to_log.update(fids)
                ctx.log_scalars(to_log, list(to_log))
                if args.s1_early_stop and es.add_check(to_log['__loss']):
                    print('Early stop')
                    break

    e_start = args.n_iter_s1 // B
    N = dset.train.images.shape[0]
    B = (N + args.batch_size - 1) // args.batch_size
    G.gen_qzx(sess, dset.train.images)

    if args.production:
        if use_restored_s1:
            fids, _ = G.get_fids('s1', sess, val_images, compute_fid)
            print('S1 FID:', fids)
        else:
            saver.save(sess,
                       os.path.join(args.dir, 'model'),
                       global_step=e_start)
            print('Model saved')

    if args.decoder_warmup_s2:
        print('S2 warmup stage:')
        es = EarlyStopper(args.n_iter_s0 // (B * 4))
        with LogContext(args.n_iter_s0 // B, logdir=args.dir,
                        tfsummary=True) as ctx:
            for i in ctx:
                idcs = np.arange(N)
                np.random.shuffle(idcs)
                z_samples = np.random.normal(G.s1_mu_z, G.s1_sig_z)[idcs]
                for j in range(B):  # training
                    G.train_step_02(sess, z_samples[j * B:(j + 1) * B],
                                    args.lr, i)
                to_log = sess.run(G.optimizer_s02.print, {
                    G.model_s1.z: z_samples,
                    G.is_training_ph: False
                })
                ctx.log_scalars(to_log, list(to_log))
                if es.add_check(to_log['__loss']):
                    print('Early stopping')
                    break

    with LogContext(args.n_iter_s2 // B + 1,
                    logdir=args.dir,
                    tfsummary=True,
                    n_ep_start=e_start) as ctx:
        for i in ctx:
            idcs = np.arange(N)
            np.random.shuffle(idcs)
            z_samples = np.random.normal(G.s1_mu_z, G.s1_sig_z)[idcs]
            for j in range(B):  # training
                lr = compute_lr(args.lr, args.lr_halve_every_s2, i * B + j)
                z_mb = z_samples[j * B:(j + 1) * B]
                G.train_step_2(sess, z_mb, lr, i)
            # plot validation
            x_mb = G.process_batch(get_val_batch())
            to_log = sess.run(G.toprint_s2, {
                G.x_ph: x_mb,
                G.is_training_ph: False
            })
            if i % 80 == 0:
                fids, _ = G.get_fids('s2', sess, val_images, compute_fid)
                to_log.update(fids)
            ctx.log_scalars(to_log, list(to_log))
            if (i * 100) % args.save_every == 0 and args.save_every > 0:
                saver.save(sess,
                           os.path.join(args.dir, 'model'),
                           global_step=i + e_start)
                print('Model saved')

    print('Test/validation:')
    test_images = val_images
    _, sample_images = G.get_fids('s2', sess, val_images, compute_fid)
    im_tiled = tile_images(sample_images[:100].reshape((100, G.HW, G.HW, G.C)))
    save_image(os.path.join(args.dir, 'sampled.png'), im_tiled)
    score = compute_fid_inception(dset, sess, G)
    print('FID SCORE (Bin Dai\'s) =', score)
Example #11
0
    def test_SdA_timep(self, pretraining_epochs, pretrain_lr, batch_size,
                        training_epochs, finetune_lr,  
                        corruption_levels, 
                        hidden_layers_sizes, hidden_layers_sidelen, output_folder):
        """
        Demonstrates how to train and test a stochastic denoising autoencoder.
        
        :type learning_rate: float
        :param learning_rate: learning rate used in the finetune stage
        (factor for the stochastic gradient)
    
        :type pretraining_epochs: int
        :param pretraining_epochs: number of epoch to do pretraining
    
        :type pretrain_lr: float
        :param pretrain_lr: learning rate to be used during pre-training
    
        :type n_iter: int
        :param n_iter: maximal number of iterations ot run the optimizer
    
        :type dataset: string
        :param dataset: path the the pickled dataset
    
        """
        traindata_path= 'allLpatches_10x10x5.pklz' #'allLpatches_subs_smaller.pklz' #'allLpatches.pklz'
        trainUdata_path= 'allUpatches_10x10x5.pklz'#'allUpatches_subs_smaller.pklz' #'allUpatches.pklz'
        labeldata_path= 'allLabels_10x10x5.pklz' #'allLabels_subs_smaller.pklz' #'allLabels.pklz'
        
        #############
        ## LOAD datasets
        #############
        datasets = self.load_wUdata(traindata_path, labeldata_path, trainUdata_path)
        
        train_set_x, train_set_y = datasets[0]
        np_train_x, np_train_y = datasets[3]
        valid_set_x, valid_set_y = datasets[1]
        np_valid_x, np_valid_y = datasets[4]        
        test_set_x, test_set_y = datasets[2]
        np_test_x, np_test_y = datasets[5]
    
        # inpect one image class 1
        Vol = np_train_x[0].reshape(5,10,10)
        imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
        subslicestime = []  
        
        # Display image
        fig, ax = plt.subplots(nrows=4, ncols=4, figsize=(12, 4))
        for k in range(1,5):
            ax[k-1,0].imshow(imgslicestime[k], cmap=plt.cm.gray)
            ax[k-1,0].set_axis_off()
            ax[k-1,0].set_adjustable('box-forced')
        
            # Display Original histogram
            ax[k-1,1].hist(imgslicestime[k].ravel(), bins=50, color='black')
            ax[k-1,1].ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
            ax[k-1,1].set_xlabel('original')
            
            # substract volume
            subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
            subslicestime.append(subVol)
        
            # Display normalized histogram
            ax[k-1,2].hist(subVol.ravel(), bins=50, color='black')
            ax[k-1,2].ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
            ax[k-1,2].set_xlabel('substracted histogram')
       
            # display normalized 0-1
            ax[k-1,3].imshow(subVol, cmap=plt.cm.gray)
            ax[k-1,3].set_axis_off()
            ax[k-1,3].set_adjustable('box-forced')
        
        plt.close()
        
        #################
        # Substract pathces from pre-contrast
        #################
        subsnp_train_x = []
        subsnp_valid_x = []
        subsnp_test_x = []

        for img in np_train_x:
            Vol = img.reshape(5,10,10)
            imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
            subslicestime = []  
            for k in range(1,5):
                # substract volume
                subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
                subslicestime.append(subVol)
            #append
            subsnp_train_x.append( np.asarray(subslicestime).reshape(4*10*10) )          
        for img in np_valid_x:
            Vol = img.reshape(5,10,10)
            imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
            subslicestime = []  
            for k in range(1,5):
                # substract volume
                subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
                subslicestime.append(subVol)
            #append
            subsnp_valid_x.append( np.asarray(subslicestime).reshape(4*10*10) )
        for img in np_test_x:
            Vol = img.reshape(5,10,10)
            imgslicestime = [Vol[0,:,:], Vol[1,:,:], Vol[2,:,:], Vol[3,:,:], Vol[4,:,:]]
            subslicestime = []  
            for k in range(1,5):
                # substract volume
                subVol =  np.asarray(imgslicestime[k]) - np.asarray(imgslicestime[0])
                subslicestime.append(subVol)
            #append
            subsnp_test_x.append( np.asarray(subslicestime).reshape(4*10*10) )
        
        train_set_x, train_set_y = self.shared_dataset(subsnp_train_x, np_train_y)
        valid_set_x, valid_set_y = self.shared_dataset(subsnp_valid_x, np_valid_y)
        test_set_x, test_set_y = self.shared_dataset(subsnp_test_x, np_test_y)
        
        subsdatasets = [(train_set_x, train_set_y), 
                        (valid_set_x, valid_set_y), 
                        (test_set_x, test_set_y) ]
                                        
        #################
        # To normalize find mean and std, then convert to float in [0-1] range
        # ref on mininatch normalization: chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/https://arxiv.org/pdf/1502.03167v3.pdf
        # stackedOver: http://stats.stackexchange.com/questions/211436/why-do-we-normalize-images-by-subtracting-the-datasets-image-mean-and-not-the-c?rq=1
        #################
#        Xtrain = np.asarray(np_train_x)
#        muXtrain = np.mean(Xtrain)
#        varXtrain = np.var(Xtrain)
#        print(muXtrain,varXtrain)
#        
#        Xvalid = np.asarray(np_valid_x)
#        muXvalid = np.mean(Xvalid)
#        varXvalid = np.var(Xvalid)
#        print(muXvalid,varXvalid)
#        
#        Xtest = np.asarray(np_test_x)
#        muXtest = np.mean(Xtest)
#        varXtest = np.var(Xtest)
#        print(muXtest,varXtest)
#        
#        fnp_train_x = []
#        fnp_valid_x = []
#        fnp_test_x = []
#        for img in np_train_x:
#            #img=np_train_x[1]
#            fimg = (img - muXtrain)/np.sqrt(varXtrain+0.00001)
#            rVol = exposure.rescale_intensity(fimg.reshape(4,10,10), out_range=(0, 1))
#            img = sitk.GetImageFromArray(rVol)
#            aimg = sitk.Cast(img,sitk.sitkFloat32)
#            sitkVol = sitk.GetArrayFromImage(aimg)
#            img = sitkVol.reshape(400)
#            fnp_train_x.append(img)
#        for img in np_valid_x:
#            fimg = (img - muXvalid)/np.sqrt(varXvalid+0.00001)
#            rVol = exposure.rescale_intensity(fimg.reshape(4,10,10), out_range=(0, 1))
#            img = sitk.GetImageFromArray(rVol)
#            aimg = sitk.Cast(img,sitk.sitkFloat32)
#            sitkVol = sitk.GetArrayFromImage(aimg)
#            img = sitkVol.reshape(400)
#            fnp_valid_x.append(img)
#        for img in np_test_x:
#            fimg = (img - muXtest)/np.sqrt(varXtest+0.00001)
#            rVol = exposure.rescale_intensity(fimg.reshape(4,10,10), out_range=(0, 1))
#            img = sitk.GetImageFromArray(rVol)
#            aimg = sitk.Cast(img,sitk.sitkFloat32)
#            sitkVol = sitk.GetArrayFromImage(aimg)
#            img = sitkVol.reshape(400)
#            fnp_test_x.append(img)
#           
        #################
        ## plot ooptional
        #################
#        fig, axes = plt.subplots(ncols=4, nrows=2)
#        axes[0,0].imshow(np_train_x[1].reshape(5,10,10)[0,:,:],  cmap="Greys_r")
#        axes[0,1].imshow(np_train_x[1].reshape(5,10,10)[1,:,:],  cmap="Greys_r")
#        axes[0,2].imshow(np_train_x[1].reshape(5,10,10)[2,:,:],  cmap="Greys_r")
#        axes[0,3].imshow(np_train_x[1].reshape(5,10,10)[3,:,:],  cmap="Greys_r")
        
#        axes[1,0].imshow( fnp_train_x[1].reshape(5,10,10)[0,:,:],  cmap="Greys_r")
#        axes[1,1].imshow( fnp_train_x[1].reshape(5,10,10)[1,:,:],  cmap="Greys_r")
#        axes[1,2].imshow( fnp_train_x[1].reshape(5,10,10)[2,:,:],  cmap="Greys_r")
#        axes[1,3].imshow( fnp_train_x[1].reshape(5,10,10)[3,:,:],  cmap="Greys_r")
            
#        train_set_x, train_set_y = self.shared_dataset(fnp_train_x, np_train_y)
#        valid_set_x, valid_set_y = self.shared_dataset(fnp_valid_x, np_valid_y)
#        test_set_x, test_set_y = self.shared_dataset(fnp_test_x, np_test_y)
    
        
        # compute number of minibatches for training, validation and testing
        n_train_batches = train_set_x.get_value(borrow=True).shape[0]
        n_train_batches //= batch_size
    
        # numpy random generator
        numpy_rng = np.random.RandomState(89677)
        
        print('... building the Stacked Autoenconder model')
        
        if not os.path.isdir(output_folder):
            os.makedirs(output_folder)
        
        # construct the stacked denoising autoencoder class
        sda = SdA(
            numpy_rng=numpy_rng,
            n_ins = 4*10*10, # before 30*30*4
            hidden_layers_sizes=hidden_layers_sizes,
            corruption_levels=corruption_levels,
            n_outs=2
        )

        #########################
        # PRETRAINING THE MODEL #
        #########################
        dA_avg_costs = []
        dA_iter = []
        layer_i = []
        
        print('... getting the pretraining functions')
        pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x,
                                                    np_train_y=np_train_y,
                                                    batch_size=batch_size)
    
        print('... pre-training the model')
        start_time = timeit.default_timer()
        
        ## Pre-train layer-wise
        for i in range(sda.n_layers):
            # go through pretraining epochs
            meanc = []
            for epoch in range(pretraining_epochs):
                # go through the training set
                c = []; 
                if( epoch % 100 == 0):
                    pretrain_lr = pretrain_lr/10
                    
                for batch_index in range(n_train_batches):
                    c.append(pretraining_fns[i](index=batch_index,
                             corruption=corruption_levels[i],
                             lr=pretrain_lr))
                
                # append      
                dA_avg_costs.append(  np.mean(c) )
                dA_iter.append(epoch)
                layer_i.append(i)
                print('Pre-training layer %i, epoch %d, cost %f' % (i, epoch, np.mean(c)))
                
                # check that Cost does not improve more than 1% on the training set after an epoch 
                meanc.append( np.mean(c) )
                if (epoch > 195):
                    decrCost = abs(meanc[epoch-1] - meanc[epoch])/meanc[epoch]*100
                    if(decrCost <= 0.01):
                        break
        
        end_time = timeit.default_timer()
        print(('The pretraining code for file ' +
            os.path.split(__file__)[1] +
                ' ran for %.2fm' % ((end_time - start_time) / 60.)))

        
        #####################################
        # Plot images in 2D
        #####################################   
        Xtmp = sda.dA_layers[0].W.get_value(borrow=True).T
        imagefilters = []
        for k in range(4):
            imgX = Xtmp.reshape( Xtmp.shape[0], 4, 10, 10)[:,k,:,:]
            image = tile_images(X=imgX , img_shape=(10, 10), 
                               tile_shape=(10, 10),
                               tile_spacing=(1, 1)) 
            # append to all 
            imagefilters.append( image )
                               
        ##############
        # Format      
        #################           
        LLdata = [float(L) for L in dA_avg_costs]
        LLiter = [float(it) for it in dA_iter]
        LLilayer = [ilayer for ilayer in layer_i]
        dfpredata = pd.DataFrame( LLdata )
        dfpredata.columns = ['LL_iter']
        dfpredata['iter'] = LLiter
        dfpredata['layer'] = LLilayer
        
        
        ########################
        # FINETUNING THE MODEL #
        ########################
        # for fine tunning make minibatch size = 1
        # compute number of minibatches for training, validation and testing
        #batch_size = 4
        #n_train_batches = train_set_x.get_value(borrow=True).shape[0]//4
        
        # get the training, validation and testing function for the model
        print('... getting the finetuning functions')
        train_fn, validate_model, test_model = sda.build_finetune_functions(
            datasets=subsdatasets,
            batch_size=batch_size,
            learning_rate=finetune_lr
        )
    
        print('... finetunning the Stacked model')
        ############
        ### for plotting likelihood or cost, accumulate returns of train_model
        ############
        minibatch_avg_costs = []
        minibatch_iter = []
        minibatch_loss = []
        
        # early-stopping parameters
        patience = 1000 * n_train_batches  # look as this many examples regardless
        patience_increase = 2.  # wait this much longer when a new best is
                                # found
        improvement_threshold = 0.995  # a relative improvement of this much is
                                       # considered significant
        validation_frequency = min(n_train_batches, patience // 2)
                                      # go through this many
                                      # minibatche before checking the network
                                      # on the validation set; in this case we
                                      # check every epoch
    
        best_validation_loss = np.inf
        test_score = 0.
        start_time = timeit.default_timer()
    
        done_looping = False
        epoch = 0
    
        while (epoch < training_epochs) and (not done_looping):
            epoch = epoch + 1
            for minibatch_index in range(n_train_batches):
                
                minibatch_avg_cost = train_fn(minibatch_index)
                iter = (epoch - 1) * n_train_batches + minibatch_index
    
                if (iter + 1) % validation_frequency == 0:
                    validation_losses = validate_model()
                    this_validation_loss = np.mean(validation_losses)
                    print('epoch %i, minibatch %i/%i, validation error %f %%' %
                          (epoch, minibatch_index + 1, 
                           n_train_batches,
                           this_validation_loss * 100.))
                           
                    ##############
                    # append      
                    #################
                    minibatch_avg_costs.append(minibatch_avg_cost)
                    minibatch_iter.append(iter)
                    minibatch_loss.append(this_validation_loss*100)
    
                    # if we got the best validation score until now
                    if this_validation_loss < best_validation_loss:
    
                        #improve patience if loss improvement is good enough
                        if (
                            this_validation_loss < best_validation_loss *
                            improvement_threshold
                        ):
                            patience = max(patience, iter * patience_increase)
    
                        # save best validation score and iteration number
                        best_validation_loss = this_validation_loss
                        best_iter = iter
    
                        # test it on the test set
                        test_losses = test_model()
                        test_score = np.mean(test_losses)
                        print(('     epoch %i, minibatch %i/%i, test error of '
                               'best model %f %%') %
                              (epoch, minibatch_index + 1, n_train_batches,
                               test_score * 100.))
    
                if patience <= iter:
                    done_looping = True
                    break
    
        end_time = timeit.default_timer()
        print 'Optimization complete with best validation score of %f, on iteration %i, with test performance %f' % ((best_validation_loss * 100.), (best_iter + 1), (test_score * 100.))
    
        print(('The training code for file ' +
               os.path.split(__file__)[1] +
               ' ran for %.2fm' % ((end_time - start_time) / 60.)))
               
        ##############
        # Format      
        #################           
        LLdata = [float(L) for L in minibatch_avg_costs]
        LLiter = [float(it) for it in minibatch_iter]
        LLoss = [float(l) for l in minibatch_loss]        
        dfinedata = pd.DataFrame( LLdata )
        dfinedata.columns = ['LL_iter']
        dfinedata['iter'] = LLiter
        dfinedata['loss'] = LLoss
        
        
        ###############
        ## Predictions
        ###############
        # get training data in numpy format   
        X,y = subsnp_train_x, np_train_y #datasets[3] 
        Xtrain = np.asarray(X)
        ytrain = utils.makeMultiClass(y)
        # get valid data in numpy format   
        X,y = subsnp_valid_x, np_valid_y # #datasets[4] 
        Xvalid = np.asarray(X)
        yvalid = utils.makeMultiClass(y)
        # get test data in numpy format           
        X,y = subsnp_test_x, np_test_y  #datasets[5] 
        Xtest = np.asarray(X)
        ytest = utils.makeMultiClass(y)
        
        ###############
        # predicting using the SDA
        ###############
        # in train
        predtrain = sda.predict_functions(Xtrain).argmax(1)
        # let's see how the network did
        y = ytrain.argmax(1)
        e0 = 0.0; y0 = len([0 for yi in range(len(y)) if y[yi]==0])
        e1 = 0.0; y1 = len([1 for yi in range(len(y)) if y[yi]==1])
        for i in range(len(y)):
            if(y[i] == 1):
                e1 += y[i]==predtrain[i]
            if(y[i] == 0):
                e0 += y[i]==predtrain[i]

        # printing the result, this structure should result in 80% accuracy
        Acutrain0=100*e0/y0
        Acutrain1=100*e1/y1
        print "Train Accuracy for class 0: %2.2f%%"%(Acutrain0)
        print "Train Accuracy for class 1: %2.2f%%"%(Acutrain1)  
        
        # in Valid
        predvalid = sda.predict_functions(Xvalid).argmax(1)
        # let's see how the network did
        y = yvalid.argmax(1)
        e0 = 0.0; y0 = len([0 for yi in range(len(y)) if y[yi]==0])
        e1 = 0.0; y1 = len([1 for yi in range(len(y)) if y[yi]==1])
        for i in range(len(y)):
            if(y[i] == 1):
                e1 += y[i]==predvalid[i]
            if(y[i] == 0):
                e0 += y[i]==predvalid[i]

        # printing the result, this structure should result in 80% accuracy
        Acuvalid0=100*e0/y0
        Acuvalid1=100*e1/y1
        print "Valid Accuracy for class 0: %2.2f%%"%(Acuvalid0)
        print "Valid Accuracy for class 1: %2.2f%%"%(Acuvalid1) 
        
        # in Xtest
        predtest = sda.predict_functions(Xtest).argmax(1)
        # let's see how the network did
        y = ytest.argmax(1)
        e0 = 0.0; y0 = len([0 for yi in range(len(y)) if y[yi]==0])
        e1 = 0.0; y1 = len([1 for yi in range(len(y)) if y[yi]==1])
        for i in range(len(y)):
            if(y[i] == 1):
                e1 += y[i]==predtest[i]
            if(y[i] == 0):
                e0 += y[i]==predtest[i]

        # printing the result, this structure should result in 80% accuracy
        Acutest0=100*e0/y0
        Acutest1=100*e1/y1
        print "Test Accuracy for class 0: %2.2f%%"%(Acutest0)
        print "Test Accuracy for class 1: %2.2f%%"%(Acutest1) 
            
    
        return [dfpredata, dfinedata, imagefilters, sda, 
                Acutrain0, Acutrain1,
                Acuvalid0, Acuvalid1,
                Acutest0, Acutest1]
Example #12
0
def fit(
    train_loader: torch.utils.data.DataLoader,
    val_loader: torch.utils.data.DataLoader,
    model: torch.nn.Module,
    exp_path: str,
    label_preprocess: Callable,
    loss_fn: torch.autograd.Function,
    label2onehot: Callable,
    n_classes: int = 10,
    optimizer: str = "adam",
    learning_rate: float = 1e-4,
    cuda: bool = True,
    patience: int = 10,
    max_epochs: int = 200,
    resume: bool = False,
):
    def _save_img(x: np.array, filename: str):
        "save numpy array representaion of image to image file"
        Image.fromarray((255 * x).astype("uint8")).save(filename)

    def _run_epoch(dataloader: torch.utils.data.DataLoader, training: bool):
        p_bar = ProgressBar()
        losses = []
        mean_outs = []
        # import pdb; pdb.set_trace()
        # batch_images = [bs, ch, w, h], batch_targets = [bs, n_classes] OHE
        for batch_images, batch_targets in p_bar(dataloader):
            batch_labels = label_preprocess(batch_images)
            if cuda:
                batch_images, batch_targets = batch_images.cuda(), batch_targets.cuda()
                batch_labels = batch_labels.cuda()

            if training:
                optimizer.zero_grad()
                model.train()
            else:
                model.eval()
            predicted_labels = model(batch_images, batch_targets)
            # cross entropy between image's pixel value vs predicted
            loss = loss_fn(predicted_labels, batch_labels)
            # track mean output
            predicted_labels = predicted_labels.data.cpu().numpy()
            # TODO: why do we need to keep track of mean?
            mean_outs.append(
                np.mean(np.argmax(predicted_labels, axis=1)) / predicted_labels.shape[1]
            )
            if training:
                loss.backward()
                optimizer.step()
            losses.append(loss.data.cpu().numpy())
        utils.clearline()
        return float(np.mean(losses)), np.mean(mean_outs)

    if cuda:
        model = model.cuda()

    exp_path = Path(exp_path)

    if not os.path.isdir(exp_path):
        os.makedirs(exp_path)
    statsfile = exp_path / "stats.json"

    optimizer = {
        "adam": torch.optim.Adam(model.parameters(), lr=learning_rate),
        "sgd": torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9),
        "adamax": torch.optim.Adamax(model.parameters(), lr=learning_rate),
    }[optimizer.lower()]

    # load a single example from the iterator to get the image size
    x = train_loader.sampler.data_source.__getitem__(0)[0]
    img_size = list(x.numpy().shape[1:])

    if not resume:
        stats = {
            "loss": {"train": [], "val": []},
            "mean_output": {"train": [], "val": []},
        }
        best_val = np.inf
        stall = 0
        start_epoch = 0
        generated = []
        plots = []

    else:
        with open(statsfile, "r") as js:
            stats = json.load(js)
        best_val = np.min(stats["loss"]["val"])
        stall = len(stats["loss"]["val"]) - np.argmin(stats["loss"]["val"]) - 1
        start_epoch = len(stats["loss"]["val"]) - 1
        generated = list(np.load(exp_path / "generated.npy"))
        plots = list(np.load(exp_path / "generated_plots.npy"))
        print(f"Resuming from epoch {start_epoch}")

    for epoch in range(start_epoch, max_epochs):
        # Training
        t0 = time.time()
        loss, mean_out = _run_epoch(train_loader, training=True)
        time_per_example = (time.time() - t0) / len(train_loader.dataset)
        stats["loss"]["train"].append(loss)
        stats["mean_output"]["train"].append(mean_out)
        print(
            (
                f"Epoch {epoch}:    Training loss = {loss:.4f}    mean output = {mean_out:.2f}    "
                f"{time_per_example*1000:.2f} msec/example"
            )
        )

        # Validation
        t0 = time.time()
        loss, mean_out = _run_epoch(val_loader, training=False)
        time_per_example = (time.time() - t0) / len(val_loader.dataset)
        stats["loss"]["val"].append(loss)
        stats["mean_output"]["val"].append(mean_out)
        print(
            (
                f"            Validation loss = {loss:.4f}    mean output = {mean_out:.2f}    "
                f"{time_per_example*1000:.2f} msec/example"
            )
        )

        # Generate images and update gif
        new_frame = utils.tile_images(
            generate_images(model, img_size, n_classes, label2onehot, cuda)
        )
        generated.append(new_frame)

        # Update gif with loss plot
        plot_frame = plot_loss(stats["loss"]["train"], stats["loss"]["val"])
        if new_frame.ndim == 2:
            new_frame = np.repeat(new_frame[:, :, np.newaxis], 3, axis=2)
        nw = int(new_frame.shape[1] * plot_frame.shape[0] / new_frame.shape[0])
        new_frame = resize(
            new_frame,
            [plot_frame.shape[0], nw],
            order=0,
            preserve_range=True,
            mode="constant",
        )
        plots.append(
            np.concatenate(
                (plot_frame.astype("uint8"), new_frame.astype("uint8")), axis=1
            )
        )

        # Save gif arrays so it can resume training if interrupted
        np.save(exp_path / "generated.npy", generated)
        np.save(exp_path / "generated_plots.npy", plots)

        # Save stats and update training curves
        with open(statsfile, "w") as sf:
            json.dump(stats, sf)
        utils.plot_stats(stats, exp_path)

        # Early stopping
        torch.save(model, exp_path / "last_checkpoint.pth")

        if loss < best_val:
            best_val = loss
            stall = 0
            torch.save(model, exp_path / "best_checkpoint.pth")
            imageio.imsave(
                exp_path / "best_generated.jpeg", generated[-1].astype("uint8")
            )
            imageio.imsave(
                exp_path / "best_generated_plots.jpeg", plots[-1].astype("uint8")
            )
            imageio.mimsave(
                exp_path / "generated.gif",
                np.array(generated),
                format="gif",
                loop=0,
                fps=2,
            )
            imageio.mimsave(
                exp_path / "generated_plot.gif",
                np.array(plots),
                format="gif",
                loop=0,
                fps=2,
            )
        else:
            stall += 1
        if stall >= patience:
            break
Example #13
0
def main(args):

    from tensorflow.examples.tutorials.mnist import input_data
    if not args.test:
        mnist = input_data.read_data_sets(
            'data/',
            one_hot=False,
            source_url='http://yann.lecun.com/exdb/mnist/')
        val_images = test_images = mnist.validation.images
    else:
        mnist = input_data.read_data_sets(
            'data/',
            one_hot=False,
            source_url='http://yann.lecun.com/exdb/mnist/',
            validation_size=0)
        val_images = mnist.test.images

    np.random.seed(args.seed)
    tf.set_random_seed(args.seed)

    if args.do_fid:
        compute_fid, _fid_lc = load_fid(
            mnist.test.images, args, binarize=(args.observation != 'normal'))

    # digit placeholder
    x_ph = tf.placeholder(tf.float32, shape=(None, 784))

    is_training_ph = tf.placeholder(tf.bool, shape=(), name='is_training')

    z_dims = args.z_dims if args.latent == 'euc' else args.z_dims + 1
    activation = {'elu': tf.nn.elu, 'tanh': tf.nn.tanh}[args.ae_activation]

    if args.implicit:
        x1 = tf.tile(x_ph, [args.n_particles, 1])
        model_cls = models.ImplicitAE
        model = model_cls(x=x1,
                          h_dim=args.h_dims,
                          z_dim=z_dims,
                          eps_dim=args.eps_dims,
                          latent_space=args.latent,
                          ae_activation=args.ae_activation,
                          energy_activation=args.energy_activation,
                          rescale_sph_latent=args.rescale_sph_latent,
                          observation=args.observation,
                          gan=(args.model == 'wae-gan'),
                          is_training_ph=is_training_ph)

        optimizer = {
            'vae': optimizers.ImplicitVAE,
            'wae': optimizers.ImplicitWAE,
            'wae-gan': optimizers.GanWAE,
            'wae-mmd': optimizers.MMDWAE,
        }[args.model](model, args)
        ais_method = args.ais_method if args.latent == 'euc' else 'riem_ld'
        log_lh_sym = log_lh_ais(model, optimizer, ais_method)
    else:
        assert not args.model.startswith('wae')
        dist = {'euc': 'normal', 'sph': 'vmf'}[args.latent]
        model = models.ExplicitAE(x=x_ph,
                                  h_dim=args.h_dims,
                                  z_dim=z_dims,
                                  distribution=dist,
                                  rescale_sph_latent=args.rescale_sph_latent)
        optimizer = optimizers.ExplicitVAE(model, args)
        log_lh_lb_sym = log_likelihood(model, optimizer, n=1000)
        ais_method = args.ais_method if args.latent == 'euc' else 'riem_ld'
        log_lh_sym = log_lh_ais(model, optimizer, ais_method)

    # === FOR VIS ===
    if args.latent == 'euc':
        dist_pz = tf.distributions.Normal(tf.zeros_like(model.z),
                                          tf.ones_like(model.z))
        pz_sample = dist_pz.sample()
    elif args.latent == 'sph':
        dist_pz = HypersphericalUniform(model.z_dim - 1, dtype=model.x.dtype)
        pz_sample = dist_pz.sample([tf.shape(model.z)[0]])
    x_sample_sym = tf.nn.sigmoid(model._decoder(pz_sample))

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())
    saver = tfv1.train.Saver(keep_checkpoint_every_n_hours=2, max_to_keep=2)

    print(args)

    with LogContext(args.n_iter // 100, logdir=args.dir,
                    tfsummary=True) as ctx:
        for i in ctx:
            for j in range(100):
                # training
                x_mb, _ = mnist.train.next_batch(args.batch_size)
                if args.observation != 'normal':
                    x_mb = (x_mb > np.random.random(size=x_mb.shape)).astype(
                        np.float32)
                optimizer.step(sess, {x_ph: x_mb, is_training_ph: True})

            # plot validation
            x_mb = val_images
            if args.observation != 'normal':
                x_mb = (x_mb > np.random.random(size=x_mb.shape)).astype(
                    np.float32)
            to_log = sess.run({**optimizer.print}, {
                x_ph: x_mb,
                is_training_ph: False
            })
            ctx.log_scalars(to_log, list(to_log))

            if i % 80 == 0 and i > 0:
                if not args.implicit:
                    idcs = np.arange(x_mb.shape[0])
                    np.random.shuffle(idcs)
                    idcs = idcs[:48]
                    loglh_val = sess.run(log_lh_sym, {
                        model.x: x_mb[idcs],
                        is_training_ph: False
                    })
                    to_log.update({
                        'log_lh_mean':
                        np.mean(loglh_val),
                        'log_lh_sd':
                        np.std(loglh_val) / (idcs.shape[0]**0.5)
                    })
                if args.do_fid:
                    sample_images = sess.run(
                        x_sample_sym, {
                            model.x: np.zeros_like(
                                val_images[:10000]).astype('f'),
                            is_training_ph: False
                        })
                    fid_score = compute_fid(sample_images)
                    to_log['fid'] = fid_score
                print(i, to_log)

            if (i * 100) % args.save_every == 0 and args.save_every > 0:
                saver.save(sess,
                           os.path.join(args.dir, 'model'),
                           global_step=i)
                print('Model saved')

    print('Test/validation:')

    test_images = val_images

    sample_images = sess.run(
        x_sample_sym, {
            model.x: np.zeros_like(val_images[:10000]).astype('f'),
            is_training_ph: False
        })
    im_tiled = tile_images(sample_images[:100].reshape((100, 28, 28)))
    save_image(os.path.join(args.dir, 'sampled.png'), im_tiled)

    if args.do_fid:
        fid_score = compute_fid(sample_images)
        print('FID SCORE =', fid_score)
        return

    if not args.implicit:
        x_mb = val_images
        if args.observation != 'normal':
            # dynamic binarization
            x_mb = (x_mb > np.random.random(size=x_mb.shape)).astype(
                np.float32)
        print_ = {**optimizer.print}
        print_['LL'] = log_lh_lb_sym
        print(sess.run(print_, {model.x: x_mb, is_training_ph: False}))

    lsum = 0
    lden = 0
    idc = np.arange(test_images.shape[0])
    np.random.shuffle(idc)
    test_images = test_images[idc]
    with LogContext(test_images.shape[0] // args.batch_size) as ctx:
        for j in ctx:
            ti = test_images[j * args.batch_size:(j + 1) * args.batch_size]
            if args.observation != 'normal':
                ti = (ti > np.random.random(size=ti.shape)).astype(np.float32)
            lhs = sess.run(log_lh_sym, {model.x: ti, is_training_ph: False})
            lsum += lhs.mean() * ti.shape[0]
            lden += ti.shape[0]
            if j % 10 == 0:
                ctx.log_scalars({'avg': lsum / lden}, ['avg'])
    print('AIS lb = {}'.format(lsum / lden))
Example #14
0
from utils import tile_images, save_image
import os, sys
import numpy as np
from matplotlib import pyplot as plt

dir_ = sys.argv[1]
ims = []
for j in range(100):
    im = plt.imread(os.path.join(dir_, '{}.png'.format(j)))
    ims.append(im)
ims = np.array(ims)
save_image(os.path.join(dir_, 'all.png'), tile_images(ims))