Exemplo n.º 1
0
  def generate_plots(self, input_data, input_labels=None):
    """
    Plot weights, reconstruction, and gradients
    Inputs:
      input_data: data object containing the current image batch
      input_labels: data object containing the current label batch
    """
    super(MlpListaModel, self).generate_plots(input_data, input_labels)
    feed_dict = self.get_feed_dict(input_data, input_labels)

    eval_list = [self.global_step, self.w, self.a]
    eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
    current_step = str(eval_out[0])
    filename_suffix = "_v"+self.params.version+"_"+current_step.zfill(5)+".png"
    weights, lista_activity = eval_out[1:]

    fig = pf.plot_activity_hist(input_data, title="Image Histogram",
      save_filename=self.params.disp_dir+"img_hist"+filename_suffix)
    weights = dp.reshape_data(weights.T, flatten=False)[0] # [num_neurons, height, width]
    #Scale image by max and min of images and/or recon
    r_max = np.max(input_data)
    r_min = np.min(input_data)
    input_data = dp.reshape_data(input_data, flatten=False)[0]
    fig = pf.plot_data_tiled(input_data, normalize=False,
      title="Scaled Images at step "+current_step, vmin=r_min, vmax=r_max,
      save_filename=self.params.disp_dir+"images"+filename_suffix)
    fig = pf.plot_activity_hist(lista_activity, title="LISTA Activity Histogram",
      save_filename=self.params.disp_dir+"lista_act_hist"+filename_suffix)
    fig = pf.plot_data_tiled(weights, normalize=False,
      title="Dictionary at step "+current_step, vmin=None, vmax=None,
      save_filename=self.params.disp_dir+"w_lista"+filename_suffix)
Exemplo n.º 2
0
  def generate_plots(self, input_data, input_labels=None):
    """
    Plot weights, reconstruction, and gradients
    Inputs:
      input_data: data object containing the current image batch
      input_labels: data object containing the current label batch
    """
    super(ListaModel, self).generate_plots(input_data, input_labels)
    feed_dict = self.get_feed_dict(input_data, input_labels)
    eval_list = [self.global_step, self.lca_module.w, self.w,
      self.lca_module.reconstruction, self.lca_module.a, self.get_encodings()]
    eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
    current_step = str(eval_out[0])
    filename_suffix = "_v"+self.params.version+"_"+current_step.zfill(5)+".png"
    lca_weights, lista_weights, recon, lca_activity, lista_activity = eval_out[1:]
    lca_weights_norm = np.linalg.norm(lca_weights, axis=0, keepdims=False)
    lista_weights_norm = np.linalg.norm(lista_weights, axis=0, keepdims=False)
    recon = dp.reshape_data(recon, flatten=False)[0]
    lca_weights = dp.reshape_data(lca_weights.T, flatten=False)[0] # [num_neurons, height, width]
    lista_weights = dp.reshape_data(lista_weights.T, flatten=False)[0] # [num_neurons, height, width]

    fig = pf.plot_activity_hist(input_data, title="Image Histogram",
      save_filename=self.params.disp_dir+"img_hist"+filename_suffix)

    #Scale image by max and min of images and/or recon
    r_max = np.max([np.max(input_data), np.max(recon)])
    r_min = np.min([np.min(input_data), np.min(recon)])

    input_data = dp.reshape_data(input_data, flatten=False)[0]
    fig = pf.plot_data_tiled(input_data, normalize=False,
      title="Scaled Images at step "+current_step, vmin=r_min, vmax=r_max,
      save_filename=self.params.disp_dir+"images"+filename_suffix)
    fig = pf.plot_data_tiled(recon, normalize=False,
      title="Recons at step "+current_step, vmin=r_min, vmax=r_max,
      save_filename=self.params.disp_dir+"recons"+filename_suffix)

    fig = pf.plot_activity_hist(lca_activity, title="LCA Activity Histogram",
      save_filename=self.params.disp_dir+"lca_act_hist"+filename_suffix)

    fig = pf.plot_activity_hist(lista_activity, title="LISTA Activity Histogram",
      save_filename=self.params.disp_dir+"lista_act_hist"+filename_suffix)

    fig = pf.plot_data_tiled(lca_weights, normalize=False,
      title="LCA Dictionary at step "+current_step, vmin=None, vmax=None,
      save_filename=self.params.disp_dir+"lca_w"+filename_suffix)

    fig = pf.plot_data_tiled(lista_weights, normalize=False,
      title="LISTA Dictionary at step "+current_step, vmin=None, vmax=None,
      save_filename=self.params.disp_dir+"lista_w"+filename_suffix)
Exemplo n.º 3
0
 def generate_plots(self, input_data, input_labels=None):
   """
   Plot weights, reconstruction, and gradients
   Inputs:
     input_data: data object containing the current image batch
     input_labels: data object containing the current label batch
   """
   super(RicaModel, self).generate_plots(input_data, input_labels)
   feed_dict = self.get_feed_dict(input_data, input_labels)
   eval_list = [self.global_step, self.w, self.reconstruction,  self.a]
   eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
   current_step = str(eval_out[0])
   filename_suffix = "_v"+self.params.version+"_"+current_step.zfill(5)+".png"
   weights, recon, activity = eval_out[1:]
   #w_lengths = np.sqrt(np.sum(np.square(weights), axis=0))
   recon = dp.reshape_data(recon, flatten=False)[0]
   weights = dp.reshape_data(weights.T, flatten=False)[0] # [units, pixels]
   fig = pf.plot_activity_hist(input_data, title="Image Histogram",
     save_filename=self.params.disp_dir+"img_hist"+filename_suffix)
   input_data = dp.reshape_data(input_data, flatten=False)[0]
   fig = pf.plot_data_tiled(input_data, normalize=False,
     title="Images at step "+current_step, vmin=None, vmax=None,
     save_filename=self.params.disp_dir+"images"+filename_suffix)
   fig = pf.plot_activity_hist(activity, title="Activity Histogram",
     save_filename=self.params.disp_dir+"act_hist"+filename_suffix)
   fig = pf.plot_data_tiled(weights, normalize=False,
     title="Dictionary at step "+current_step, vmin=None, vmax=None,
     save_filename=self.params.disp_dir+"w"+filename_suffix)
   #fig = pf.plot_bar(w_lengths, title="Weight L2 Norms", xlabel="Weight Index", ylabel="L2 Norm",
   #  save_filename=self.params.disp_dir+"w_norms"+filename_suffix)
   fig = pf.plot_data_tiled(recon, normalize=False,
     title="Recons at step "+current_step, vmin=None, vmax=None,
     save_filename=self.params.disp_dir+"recons"+filename_suffix)
   if self.params.optimizer != "lbfgsb":
     for weight_grad_var in self.grads_and_vars[self.sched_idx]:
       grad = weight_grad_var[0][0].eval(feed_dict)
       shape = grad.shape
       name = weight_grad_var[0][1].name.split('/')[1].split(':')[0]#np.split
       grad = dp.reshape_data(grad.T, flatten=False)[0]
       fig = pf.plot_data_tiled(grad, normalize=True,
         title="Gradient for w at step "+current_step, vmin=None, vmax=None,
         save_filename=self.params.disp_dir+"dw"+filename_suffix)
Exemplo n.º 4
0
 def run_analysis(self, images, labels=None, save_info=""):
   super(LcaPcaAnalyzer, self).run_analysis(images, labels, save_info=save_info)
   # Need to create a dataset object for cov analysis
   image_dataset = {"test":Dataset(dp.reshape_data(images, flatten=False)[0], lbls=None)}
   image_dataset = self.model.reshape_dataset(image_dataset, self.model_params)
   cov = self.cov_analysis(image_dataset["test"][:self.analysis_params.num_LCA_PCA_cov_images],
     save_info)
   self.act_cov, self.a_eigvals, self.a_eigvecs, self.pooling_filters, self.a2, self.pooled_act = cov
   self.evec_atas = self.compute_atas(self.a2, image_dataset["test"].images)
   self.pool_atas = self.compute_atas(self.pooled_act, image_dataset["test"].images)
   np.savez(self.analysis_out_dir+"savefiles/second_layer_atas_"+save_info+".npz",
     data={"evec_atas":self.evec_atas, "pool_atas":self.pool_atas})
   self.analysis_logger.log_info("2nd layer activity  analysis is complete.")
Exemplo n.º 5
0
 def generate_plots(self, input_data, input_labels=None):
     """
 Plot weights, reconstruction, and gradients
 Inputs:
   input_data: data object containing the current image batch
   input_labels: data object containing the current label batch
 """
     super(IcaModel, self).generate_plots(input_data, input_labels)
     feed_dict = self.get_feed_dict(input_data, input_labels)
     eval_list = [self.global_step, self.w_analysis, self.a, self.z]
     eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
     current_step = str(eval_out[0])
     weights, a_vals, z_vals = eval_out[1:]
     #input_data = dp.reshape_data(input_data, flatten=False)[0]
     #pf.plot_data_tiled(input_data, normalize=False,
     #  title="Images at step "+current_step, vmin=np.min(input_data), vmax=np.max(input_data),
     #  save_filename=(self.params.disp_dir+"images-"+current_step.zfill(5)+".png"))
     weights_norm = np.linalg.norm(weights, axis=0,
                                   keepdims=False)  # norm across pixels
     pf.plot_bar(weights_norm,
                 num_xticks=5,
                 title="$W_{analysis}$ l$_{2}$ norm",
                 xlabel="Basis Index",
                 ylabel="L2 Norm",
                 save_filename=(self.params.disp_dir + "w_analysis_norm_v" +
                                self.params.version + "-" +
                                current_step.zfill(5) + ".png"))
     weights = dp.reshape_data(
         weights.T, flatten=False)[0]  #[neurons, pixels_y, pixels_x]
     pf.plot_weights(
         weights.squeeze(),
         title="Unnormalized weights at step " + current_step,
         save_filename=(self.params.disp_dir + "w_analysis_unnormalized_v" +
                        self.params.version + "-" + current_step.zfill(5) +
                        ".png"))
     #pf.plot_data_tiled(weights, normalize=True,
     #  title="Weights at step "+current_step, vmin=-1.0, vmax=1.0,
     #  save_filename=(self.params.disp_dir+"w_analysis_v"+self.params.version+"-"
     #  +current_step.zfill(5)+".png"))
     pf.plot_activity_hist(
         a_vals,
         num_bins=1000,
         title="a Activity Histogram at step " + current_step,
         save_filename=(self.params.disp_dir + "act_hist_v" +
                        self.params.version + "-" + current_step.zfill(5) +
                        ".png"))
def get_dsc_activations_cell(analyzer,
                             images,
                             neuron,
                             batch_size=10,
                             activation_operation=None):
    """
    Returns the activations from a model for given input images
    Parameters:
        analyzer [DSC analyzer object] an object from the DeepSparseCoding library
        images [np.ndarray] of size NumImages x W x H
        neuron [int or vector of ints] that points to the neuron index
        batch_size [int] specifying the batch size to use for the getting the neuron activations
        activation_operation [function] to be used if the DSC model has a unique function handle for getting neuron activations (e.g. in the case of lca_subspace)
    Output:
        activations [np.ndarray] vector of length len(neuron)
    """
    images = dp.reshape_data(images[..., None],
                             flatten=analyzer.model.params.vectorize_data)[0]
    activations = analyzer.compute_activations(images, batch_size,
                                               activation_operation)[:, neuron]
    return activations
Exemplo n.º 7
0
    def generate_plots(self, input_data, input_labels=None):
        """
    Plot weights, reconstruction, and gradients
    Inputs:
      input_data: data object containing the current image batch
      input_labels: data object containing the current label batch
    """
        super(AeModel, self).generate_plots(input_data, input_labels)
        feed_dict = self.get_feed_dict(input_data, input_labels)
        eval_list = [
            self.global_step, self.module.w_list[0], self.module.w_list[-1],
            self.module.b_list, self.module.u_list[1:]
        ]
        eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
        current_step = str(eval_out[0])
        w_enc, w_dec, b_list, activations = eval_out[1:]
        recon = activations[-1]
        # compute weight norms
        num_features = w_enc.shape[-1]
        w_enc_norm = np.linalg.norm(np.reshape(w_enc, (-1, num_features)),
                                    axis=0,
                                    keepdims=False)
        # reshapes flat data into image & normalize
        if (len(w_enc.shape) == 2):
            w_enc_img = dp.reshape_data(w_enc.T, flatten=False)[0]
        else:
            w_enc_img = np.transpose(w_enc, (3, 0, 1, 2))
        w_enc_img = dp.norm_weights(w_enc_img)

        if (not self.params.tie_dec_weights):
            if (len(w_dec.shape) == 2):
                w_dec_norm = np.linalg.norm(w_dec, axis=1, keepdims=False)
                w_dec_img = dp.reshape_data(w_dec, flatten=False)[0]
            else:
                #Decoder in same shape as encoder if multi dimensional
                #conv2d_transpose requires it to be the same shape as decoder
                w_dec_norm = np.linalg.norm(np.reshape(w_dec,
                                                       (-1, num_features)),
                                            axis=0,
                                            keepdims=False)
                w_dec_img = np.transpose(w_dec, (3, 0, 1, 2))
            w_dec_img = dp.norm_weights(w_dec_img)

        # generate figures
        filename_suffix = "_v" + self.params.version + "_" + current_step.zfill(
            5) + ".png"

        fig = pf.plot_data_tiled(
            w_enc_img,
            normalize=False,
            title="Encoding weights at step " + current_step,
            vmin=None,
            vmax=None,
            save_filename=self.params.disp_dir + "w_enc" + filename_suffix)

        fig = pf.plot_bar(w_enc_norm,
                          num_xticks=5,
                          title="w_enc l2 norm",
                          xlabel="Basis Index",
                          ylabel="L2 Norm",
                          save_filename=self.params.disp_dir + "w_enc_norm" +
                          filename_suffix)

        if (not self.params.tie_dec_weights):
            fig = pf.plot_data_tiled(
                w_dec_img,
                normalize=False,
                title="Decoding weights at step " + current_step,
                vmin=None,
                vmax=None,
                save_filename=self.params.disp_dir + "w_dec" + filename_suffix)
            fig = pf.plot_bar(w_dec_norm,
                              num_xticks=5,
                              title="w_dec l2 norm",
                              xlabel="Basis Index",
                              ylabel="L2 Norm",
                              save_filename=self.params.disp_dir +
                              "w_dec_norm" + filename_suffix)

        for layer_id, activity in enumerate(activations[:-1]):
            num_features = activity.shape[-1]
            fig = pf.plot_activity_hist(
                np.reshape(activity, (-1, num_features)),
                title="Activity Encoder " + str(layer_id) + " Histogram",
                save_filename=self.params.disp_dir + "act_enc_" +
                str(layer_id) + "_hist" + filename_suffix)

        for layer_id, bias in enumerate(b_list):
            fig = pf.plot_activity_hist(
                np.squeeze(bias),
                title="Bias " + str(layer_id) + " Histogram",
                save_filename=self.params.disp_dir + "bias_" + str(layer_id) +
                "_hist" + filename_suffix)
        if eval_out[0] * 10 % self.params.cp_int == 0:
            #Scale image by max and min of images and/or recon
            r_max = np.max([np.max(input_data), np.max(recon)])
            r_min = np.min([np.min(input_data), np.min(recon)])
            batch_size = input_data.shape[0]
            fig = pf.plot_activity_hist(np.reshape(input_data,
                                                   (batch_size, -1)),
                                        title="Image Histogram",
                                        save_filename=self.params.disp_dir +
                                        "img_hist" + filename_suffix)
            input_data = dp.reshape_data(input_data, flatten=False)[0]
            fig = pf.plot_data_tiled(
                input_data,
                normalize=False,
                title="Scaled Images at step " + current_step,
                vmin=r_min,
                vmax=r_max,
                save_filename=self.params.disp_dir + "images" +
                filename_suffix)
            #TODO: This plot hangs sometimes?
            #fig = pf.plot_activity_hist(recon, title="Recon Histogram",
            #save_filename=self.params.disp_dir+"recon_hist"+filename_suffix)
            recon = dp.reshape_data(recon, flatten=False)[0]
            fig = pf.plot_data_tiled(recon,
                                     normalize=False,
                                     title="Recons at step " + current_step,
                                     vmin=r_min,
                                     vmax=r_max,
                                     save_filename=self.params.disp_dir +
                                     "recons" + filename_suffix)
Exemplo n.º 8
0
data = ds.get_data(analyzer.model_params)

data = analyzer.model.preprocess_dataset(data, analyzer.model_params)
data = analyzer.model.reshape_dataset(data, analyzer.model_params)
analyzer.model_params.data_shape = list(data["train"].shape[1:])

#analyzer.model_schedule[0]["sparse_mult"]  = 0.4
analyzer.setup_model(analyzer.model_params)
#analyzer.model_params.input_shape = [
#  data["train"].num_rows*data["train"].num_cols*data["train"].num_channels]

analyzer.run_analysis(data[analysis_params.analysis_dataset].images,
                      data[analysis_params.analysis_dataset].labels,
                      save_info=analysis_params.save_info)

if analysis_params.do_full_recon:

    class img_params():
        data_type = analysis_params.data_type
        num_images = 2
        extract_patches = False
        image_edge_size = analysis_params.image_edge_size
        data_dir = os.path.expanduser("~") + "/Work/Datasets/"
        rand_seed = analysis_params.rand_seed
        rand_state = np.random.RandomState(analysis_params.rand_seed)

    full_img = dp.reshape_data(ds.get_data(img_params)["train"].images[0],
                               flatten=False)[0]
    analyzer.run_patch_recon_analysis(full_img,
                                      save_info=analysis_params.save_info)
Exemplo n.º 9
0
 def testBasic(self):
     # mutable parameters
     num_examples_list = [
         1, 5
     ]  # there are conditions where this is assumed 1 and therefore ignored
     num_rows_list = [4]
     num_channels_list = [1, 3]
     # immutable parameters
     num_cols_list = num_rows_list  # assumed square objects
     flatten_list = [None, True, False]
     gpu_args = [True, False] if tf.test.is_gpu_available(
         cuda_only=True) else [False]
     for use_gpu in gpu_args:
         with self.session(use_gpu=use_gpu):
             for num_examples in num_examples_list:
                 orig_num_examples = num_examples
                 for num_rows, num_cols in zip(
                         num_rows_list,
                         num_cols_list):  #assumed to be square
                     for num_channels in num_channels_list:
                         num_elements = num_rows * num_cols * num_channels
                         input_array_list = [
                             np.zeros((num_elements
                                       )),  # assumed num_examples == 1
                             np.zeros((num_examples, num_elements)),
                             np.zeros((num_rows, num_cols, num_channels
                                       )),  # assumed num_examples == 1
                             np.zeros((num_examples, num_rows, num_cols,
                                       num_channels))
                         ]
                         for input_array in input_array_list:
                             input_shape = input_array.shape
                             input_ndim = input_array.ndim
                             if input_ndim == 1 or input_ndim == 3:  # assign num_examples to 1
                                 num_examples = 1
                                 out_shape_list = [
                                     None, (num_elements, ),
                                     (num_rows, num_cols, num_channels)
                                 ]
                                 if num_channels == 1:
                                     out_shape_list.append(
                                         (num_rows, num_cols))
                             else:
                                 num_examples = orig_num_examples
                                 out_shape_list = [
                                     None, (num_examples, num_elements),
                                     (num_examples, num_rows, num_cols,
                                      num_channels)
                                 ]
                                 if num_channels == 1:
                                     out_shape_list.append(
                                         (num_examples, num_rows, num_cols))
                             for out_shape in out_shape_list:
                                 for flatten in flatten_list:
                                     if out_shape is None and flatten == False and num_channels != 1:
                                         # This condition is ill-posed, so the function assumes the image is square
                                         # with num_channels == 1. Other conditions will not be tested.
                                         continue
                                     err_msg = (
                                         "\nnum_examples=" +
                                         str(num_examples) + "\nnum_rows=" +
                                         str(num_rows) + "\nnum_cols=" +
                                         str(num_cols) + "\nnum_channels=" +
                                         str(num_channels) +
                                         "\ninput_shape=" +
                                         str(input_shape) +
                                         "\ninput_ndim=" + str(input_ndim) +
                                         "\nout_shape=" + str(out_shape) +
                                         "\nflatten=" + str(flatten))
                                     reshape_outputs = dp.reshape_data(
                                         input_array, flatten, out_shape)
                                     self.assertEqual(
                                         len(reshape_outputs), 6)
                                     reshaped_array = reshape_outputs[0]
                                     err_msg += "\nreshaped_array.shape=" + str(
                                         reshaped_array.shape)
                                     self.assertEqual(
                                         reshape_outputs[1], input_shape,
                                         err_msg)  # orig_shape
                                     #TODO: Add conditional assertions for these outputs - it's more tricky than this
                                     #self.assertEqual(reshape_outputs[2], expected_out_shape[0], err_msg) # num_examples
                                     #self.assertEqual(reshape_outputs[3], expected_out_shape[1], err_msg) # num_rows
                                     #self.assertEqual(reshape_outputs[4], expected_out_shape[2], err_msg) # num_cols
                                     #self.assertEqual(reshape_outputs[5], expected_out_shape[3], err_msg) # num_channels
                                     if out_shape is None:
                                         if flatten is None:
                                             if input_ndim == 1 or input_ndim == 3:  #ignore num_examples
                                                 expected_out_shape = tuple(
                                                     [num_examples] +
                                                     list(input_shape))
                                                 err_msg += (
                                                     "\nexpected_out_shape="
                                                     +
                                                     str(expected_out_shape)
                                                 )
                                                 self.assertEqual(
                                                     reshaped_array.shape,
                                                     expected_out_shape,
                                                     err_msg)
                                             else:
                                                 expected_out_shape = input_shape
                                                 err_msg += (
                                                     "\nexpected_out_shape="
                                                     +
                                                     str(expected_out_shape)
                                                 )
                                                 self.assertEqual(
                                                     reshaped_array.shape,
                                                     expected_out_shape,
                                                     err_msg)
                                         elif flatten == True:
                                             expected_out_shape = (
                                                 num_examples, num_elements)
                                             err_msg += (
                                                 "\nexpected_out_shape=" +
                                                 str(expected_out_shape))
                                             self.assertEqual(
                                                 reshaped_array.shape,
                                                 expected_out_shape,
                                                 err_msg)
                                         elif flatten == False:
                                             expected_out_shape = (
                                                 num_examples, num_rows,
                                                 num_cols, num_channels)
                                             err_msg += (
                                                 "\nexpected_out_shape=" +
                                                 str(expected_out_shape))
                                             self.assertEqual(
                                                 reshaped_array.shape,
                                                 expected_out_shape,
                                                 err_msg)
                                         else:
                                             self.assertTrue(False)
                                     else:
                                         expected_out_shape = out_shape
                                         err_msg += (
                                             "\nexpected_out_shape=" +
                                             str(expected_out_shape))
                                         self.assertEqual(
                                             reshaped_array.shape,
                                             expected_out_shape, err_msg)
Exemplo n.º 10
0
 def generate_plots(self, input_data, input_labels=None):
     """
 Plot weights, reconstruction, and gradients
 Inputs:
   input_data: data object containing the current image batch
   input_labels: data object containing the current label batch
 """
     super(LcaModel, self).generate_plots(input_data, input_labels)
     feed_dict = self.get_feed_dict(input_data, input_labels)
     eval_list = [
         self.global_step, self.module.w, self.module.reconstruction,
         self.get_encodings()
     ]
     eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
     current_step = str(eval_out[0])
     filename_suffix = "_v" + self.params.version + "_" + current_step.zfill(
         5) + ".png"
     weights, recon, activity = eval_out[1:]
     weights_norm = np.linalg.norm(weights, axis=0, keepdims=False)
     recon = dp.reshape_data(recon, flatten=False)[0]
     weights = dp.reshape_data(
         weights.T, flatten=False)[0]  # [num_neurons, height, width]
     fig = pf.plot_activity_hist(input_data,
                                 title="Image Histogram",
                                 save_filename=self.params.disp_dir +
                                 "img_hist" + filename_suffix)
     #Scale image by max and min of images and/or recon
     r_max = np.max([np.max(input_data), np.max(recon)])
     r_min = np.min([np.min(input_data), np.min(recon)])
     input_data = dp.reshape_data(input_data, flatten=False)[0]
     fig = pf.plot_data_tiled(input_data,
                              normalize=False,
                              title="Scaled Images at step " + current_step,
                              vmin=r_min,
                              vmax=r_max,
                              save_filename=self.params.disp_dir +
                              "images" + filename_suffix)
     fig = pf.plot_data_tiled(recon,
                              normalize=False,
                              title="Recons at step " + current_step,
                              vmin=r_min,
                              vmax=r_max,
                              save_filename=self.params.disp_dir +
                              "recons" + filename_suffix)
     fig = pf.plot_activity_hist(activity,
                                 title="Activity Histogram",
                                 save_filename=self.params.disp_dir +
                                 "act_hist" + filename_suffix)
     fig = pf.plot_data_tiled(weights,
                              normalize=False,
                              title="Dictionary at step " + current_step,
                              vmin=None,
                              vmax=None,
                              save_filename=self.params.disp_dir + "phi" +
                              filename_suffix)
     for weight_grad_var in self.grads_and_vars[self.sched_idx]:
         grad = weight_grad_var[0][0].eval(feed_dict)
         shape = grad.shape
         name = weight_grad_var[0][1].name.split('/')[1].split(':')[
             0]  #np.split
         grad = dp.reshape_data(grad.T, flatten=False)[0]
         fig = pf.plot_data_tiled(
             grad,
             normalize=True,
             title="Gradient for w at step " + current_step,
             vmin=None,
             vmax=None,
             save_filename=self.params.disp_dir + "dphi" + filename_suffix)
Exemplo n.º 11
0
#analyzer.model_params.data_shape = [256]
#analyzer.model.setup(analyzer.model_params, analyzer.model_schedule)
#analyzer.model_params.input_shape = [256]
#gabors = pickle.load(open("./random_gabor_stim.p", "rb"))
#raw_data_batch = gabors[:tsne_params.batch_size].reshape(tsne_params.batch_size, 256)

# Preprocess data
if hasattr(analyer.model_params,
           "whiten_data") and analyzer.model_params.whiten_data:
    data_batch, data_pre_wht_mean, data_wht_filter = \
      dp.whiten_data(raw_data_batch, method=analyzer.model_params.whiten_method)
if hasattr(analyzer.model_params,
           "lpf_data") and analyzer.model_params.lpf_data:
    data_batch, data_pre_lpf_mean, data_lp_filter = \
      dp.lpf_data(raw_data_batch, cutoff=analyzer.model_params.lpf_cutoff)
raw_data_batch, orig_shape, num_examples, num_rows, num_cols = dp.reshape_data(
    raw_data_batch, flatten=False)[:5]
#data_batch = tsne_params.input_scale * (data_batch / np.max(np.abs(data_batch)))
data_batch = raw_data_batch
assert num_rows == num_cols, ("The data samples must be square")

data_shape = data_batch.shape
imgs_per_edge = int(np.sqrt(tsne_params.batch_size))
sprite_edge_size = imgs_per_edge * num_rows

#reformat data into a tiled image for visualization
sprite = data_batch.reshape(((imgs_per_edge, imgs_per_edge) + data_shape[1:]))
sprite = sprite.transpose(
    ((0, 2, 1, 3) + tuple(range(4, data_batch.ndim + 1))))
sprite = sprite.reshape((sprite_edge_size, sprite_edge_size) +
                        sprite.shape[4:])
sprite = dp.rescale_data_to_one(sprite)[0]
Exemplo n.º 12
0
 def generate_plots(self, input_data, input_labels=None):
     """
 Plot weights, gradients, etc
 Inputs: input_data and input_labels used for the session
 """
     super(MlpModel, self).generate_plots(input_data, input_labels)
     feed_dict = self.get_feed_dict(input_data, input_labels)
     eval_list = [
         self.global_step,
         self.get_encodings(), self.mlp_module.weight_list
     ]
     train_on_adversarial = feed_dict[self.train_on_adversarial]
     if (train_on_adversarial):
         eval_list += [self.adv_module.get_adv_input()]
     eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
     current_step = str(eval_out[0])
     filename_suffix = "_v" + self.params.version + "_" + current_step.zfill(
         5) + ".png"
     activity = eval_out[1]
     fig = pf.plot_activity_hist(activity,
                                 title="Logit Histogram",
                                 save_filename=self.params.disp_dir +
                                 "act_hist" + filename_suffix)
     #First layer weights
     mlp_weights = eval_out[2]
     w_enc = mlp_weights[0]
     if self.params.mlp_layer_types[0] == "fc":
         w_enc_norm = np.linalg.norm(w_enc, axis=0, keepdims=False)
         # Don't plot weights as images if input is not square
         w_input_sqrt = np.sqrt(w_enc.shape[0])
         if (np.floor(w_input_sqrt) == np.ceil(w_input_sqrt)):
             w_enc = dp.reshape_data(
                 w_enc.T, flatten=False)[0]  # [num_neurons, height, width]
             fig = pf.plot_data_tiled(
                 w_enc,
                 normalize=False,
                 title="Weights at step " + current_step,
                 vmin=None,
                 vmax=None,
                 save_filename=self.params.disp_dir + "w_enc" +
                 filename_suffix)
     else:  # conv
         w_enc = np.transpose(dp.rescale_data_to_one(w_enc.T)[0].T,
                              axes=(3, 0, 1, 2))
         if (w_enc.shape[-1] == 1 or w_enc.shape[-1] == 3):
             pf.plot_data_tiled(w_enc,
                                normalize=False,
                                title="Weights at step " + current_step,
                                save_filename=self.params.disp_dir +
                                "w_enc" + filename_suffix)
     for (w, tf_w) in zip(mlp_weights, self.mlp_module.weight_list):
         #simplify tensorflow node name to only be the last one
         w_name = tf_w.name.split("/")[-1].split(":")[0]
         num_f = w.shape[-1]
         w_reshape = np.reshape(w, [-1, num_f])
         w_norm = np.linalg.norm(w_reshape, axis=0, keepdims=False)
         fig = pf.plot_bar(w_norm,
                           num_xticks=5,
                           title=w_name + " l2 norm",
                           xlabel="w index",
                           ylabel="L2 Norm",
                           save_filename=self.params.disp_dir + "w_norm_" +
                           w_name + filename_suffix)
     if (train_on_adversarial):
         adv_input = eval_out[-1]
         adv_input = dp.reshape_data(adv_input, flatten=False)[0]
         fig = pf.plot_data_tiled(adv_input,
                                  normalize=False,
                                  title="Adv inputs at " + current_step,
                                  save_filename=self.params.disp_dir +
                                  "adv_input" + filename_suffix)
Exemplo n.º 13
0
 def generate_plots(self, input_data, input_labels=None):
     """
 Plot weights, reconstruction, and gradients
 Inputs:
   input_data: data object containing the current image batch
   input_labels: data object containing the current label batch
 """
     super(MlpLcaModel, self).generate_plots(input_data, input_labels)
     feed_dict = self.get_feed_dict(input_data, input_labels)
     eval_list = [
         self.global_step, self.lca_module.w,
         self.lca_module.reconstruction, self.lca_module.a
     ]
     eval_out = tf.compat.v1.get_default_session().run(eval_list, feed_dict)
     current_step = str(eval_out[0])
     filename_suffix = "_v" + self.params.version + "_" + current_step.zfill(
         5) + ".png"
     weights, recon, lca_activity = eval_out[1:]
     batch_size = input_data.shape[0]
     fig = pf.plot_activity_hist(np.reshape(input_data, [batch_size, -1]),
                                 title="Image Histogram",
                                 save_filename=self.params.disp_dir +
                                 "img_hist" + filename_suffix)
     fig = pf.plot_activity_hist(np.reshape(recon, [batch_size, -1]),
                                 title="Recon Histogram",
                                 save_filename=self.params.disp_dir +
                                 "recon_hist" + filename_suffix)
     recon = dp.reshape_data(recon, flatten=False)[0]
     #Scale image by max and min of images and/or recon
     r_max = np.max([np.max(input_data), np.max(recon)])
     r_min = np.min([np.min(input_data), np.min(recon)])
     input_data = dp.reshape_data(input_data, flatten=False)[0]
     fig = pf.plot_data_tiled(input_data,
                              normalize=False,
                              title="Scaled Images at step " + current_step,
                              vmin=r_min,
                              vmax=r_max,
                              save_filename=self.params.disp_dir +
                              "images" + filename_suffix)
     fig = pf.plot_data_tiled(recon,
                              normalize=False,
                              title="Recons at step " + current_step,
                              vmin=r_min,
                              vmax=r_max,
                              save_filename=self.params.disp_dir +
                              "recons" + filename_suffix)
     num_features = lca_activity.shape[-1]
     lca_activity = np.reshape(lca_activity, [-1, num_features])
     fig = pf.plot_activity_hist(lca_activity,
                                 title="LCA Activity Histogram",
                                 save_filename=self.params.disp_dir +
                                 "lca_act_hist" + filename_suffix)
     if (len(weights.shape) == 4):  # conv
         weights = np.transpose(weights, (0, 2, 3, 1))
     else:  # fc
         weights = dp.reshape_data(
             weights.T, flatten=False)[0]  # [num_neurons, height, width]
     fig = pf.plot_data_tiled(weights,
                              normalize=False,
                              title="Dictionary at step " + current_step,
                              vmin=None,
                              vmax=None,
                              save_filename=self.params.disp_dir + "phi" +
                              filename_suffix)