def plot_filters_top_alphas(self, n_filters, save_path, save_as_mat=False, save_as_plot=True): if not os.path.exists(save_path): os.makedirs(save_path) unwhitening_mat, a_mat, w_mat = self.load_filters_for_plotting() top_alphas = np.argsort(a_mat)[::-1] filter_ids = top_alphas[:n_filters] rotated_w_mat = np.dot(w_mat[:, filter_ids].T, unwhitening_mat) for idx, filter_id in enumerate(filter_ids): flat_filter = rotated_w_mat[idx, :] alpha = a_mat[filter_id] plottable_filters = np.reshape(flat_filter, [self.n_channels, self.ph, self.pw]) if save_as_mat: file_name = 'filter_{}_alpha_{:.3e}.npy'.format( filter_id, float(alpha)) np.save(save_path + file_name, plottable_filters) if save_as_plot: file_name = 'filter_{}_alpha_{:.3e}.png'.format( filter_id, float(alpha)) plottable_filters -= np.min(plottable_filters) plottable_filters /= np.max(plottable_filters) plot_img_mats(plottable_filters, rescale=False, show=False, save_path=save_path + file_name) print('filter {} done'.format(filter_id))
def plot_filters_after_training(self, w_res, unwhiten_mat, n_vis): comps = np.dot(w_res.T, unwhiten_mat) comps -= np.min(comps) comps /= np.max(comps) co = np.reshape(comps[:n_vis, :], [n_vis, 3, self.ph, self.pw]) co = np.transpose(co, axes=[0, 2, 3, 1]) plot_img_mats(co, color=True, rescale=True)
def plot_filters_after_training(self, w_res, unwhiten_mat, n_vis): comps = np.dot(w_res[0, :, :].T, unwhiten_mat[0, :, :]) print(comps.shape) comps -= np.min(comps) comps /= np.max(comps) co = np.reshape(comps[:n_vis, :], [-1, self.ph, self.pw]) plot_img_mats(co, color=False, rescale=True)
def plot_channels_top_filters(self, channel_ids, save_path, save_as_mat=False, save_as_plot=True, n_vis=144): """ visualizes filters of selected channels sorted descending by norm of the filter saves these as one plot per channel. :param channel_ids: collection of filter indices :param save_path: location to save plots :param save_as_mat: if true, saves each filter as channel x height x width matrix :param save_as_plot: if true, saves each filter as image :param n_vis: ... :return: None """ if not os.path.exists(save_path): os.makedirs(save_path) unwhitening_mat, a_mat, w_mat = self.load_filters_for_plotting() rotated_w_mat = np.dot(w_mat.T, unwhitening_mat) for channel_id in channel_ids: chan_start = self.ph * self.pw * channel_id chan_end = self.ph * self.pw * (channel_id + 1) flat_channel = rotated_w_mat[:, chan_start:chan_end] w_min = np.min(flat_channel) w_max = np.max(flat_channel) w_norms = np.linalg.norm(flat_channel, axis=1) norm_ids = np.argsort(w_norms)[-n_vis:][::-1] flat_channel = flat_channel[norm_ids, :] print(np.linalg.norm(flat_channel, axis=1)) plottable_channels = np.reshape( flat_channel, [flat_channel.shape[0], self.ph, self.pw]) if save_as_mat: file_name = 'channel_{}.npy'.format(channel_id) np.save(save_path + file_name, plottable_channels) if save_as_plot: file_name = 'channel_{}.png'.format(channel_id) plottable_channels -= w_min plottable_channels /= (w_max - w_min) plot_img_mats(plottable_channels, rescale=False, show=False, save_path=save_path + file_name) print('filter {} done'.format(channel_id))
def plot_filters_after_training(self, w_res, unwhiten_mat, n_vis): w_res_t = w_res.T.reshape( (self.n_components, self.n_channels, 1, self.ph * self.pw)) if self.channelwise_data: comps = np.matmul(w_res_t, unwhiten_mat).squeeze() comps -= np.min(comps) comps /= np.max(comps) co = np.reshape(comps[:n_vis, :, :], [n_vis, self.n_channels, self.ph, self.pw]) else: comps = np.dot(w_res.T, unwhiten_mat) comps -= np.min(comps) comps /= np.max(comps) co = np.reshape(comps[:n_vis, :], [n_vis, 3, self.ph, self.pw]) co = np.transpose(co, axes=[0, 2, 3, 1]) plot_img_mats(co, color=True, rescale=True)
def plot_filters_all_channels(self, filter_ids, save_path, save_as_mat=False, save_as_plot=True): """ visualizes the patch for each channel of a trained filter and saves this as one plot. does so for the filter of each given index :param filter_ids: collection of filter indices :param save_path: location to save plots :param save_as_mat: if true, saves each filter as channel x height x width matrix :param save_as_plot: if true, saves each filter as image :return: None """ if not os.path.exists(save_path): os.makedirs(save_path) unwhitening_mat, a_mat, w_mat = self.load_filters_for_plotting() rotated_w_mat = np.dot(w_mat[:, filter_ids].T, unwhitening_mat) for idx, filter_id in enumerate(filter_ids): flat_filter = rotated_w_mat[idx, :] alpha = a_mat[filter_id] plottable_filters = np.reshape(flat_filter, [self.n_channels, self.ph, self.pw]) if save_as_mat: file_name = 'filter_{}_alpha_{:.3e}.npy'.format( filter_id, float(alpha)) np.save(save_path + file_name, plottable_filters) if save_as_plot: file_name = 'filter_{}_alpha_{:.3e}.png'.format( filter_id, float(alpha)) plottable_filters -= np.min(plottable_filters) plottable_filters /= np.max(plottable_filters) plot_img_mats(plottable_filters, rescale=False, show=False, save_path=save_path + file_name) print('filter {} done'.format(filter_id))
def plot_channels_all_filters(self, channel_ids, save_path, save_as_mat=False, save_as_plot=True, n_vis=144): """ visualizes all filters of selected channels and saves these as one plot per channel. :param channel_ids: ... :param save_path: location to save plots :param save_as_mat: if true, saves each filter as channel x height x width matrix :param save_as_plot: if true, saves each filter as image :param n_vis: ... :return: None """ if not os.path.exists(save_path): os.makedirs(save_path) unwhitening_mat, a_mat, w_mat = self.load_filters_for_plotting() rotated_w_mat = np.dot(w_mat[:, :n_vis].T, unwhitening_mat) for channel_id in channel_ids: chan_start = self.ph * self.pw * channel_id chan_end = self.ph * self.pw * (channel_id + 1) flat_channel = rotated_w_mat[:, chan_start:chan_end] plottable_channels = np.reshape( flat_channel, [flat_channel.shape[0], self.ph, self.pw]) if save_as_mat: file_name = 'channel_{}.npy'.format(channel_id) np.save(save_path + file_name, plottable_channels) if save_as_plot: file_name = 'channel_{}.png'.format(channel_id) plottable_channels -= np.min(plottable_channels) plottable_channels /= np.max(plottable_channels) plot_img_mats(plottable_channels, rescale=False, show=False, save_path=save_path + file_name) print('filter {} done'.format(channel_id))
def plot_filters_all_channels(self, channel_ids, save_path, save_as_mat=False, save_as_plot=True): """ visualizes the patch for each channel of a trained filter and saves this as one plot. does so for the filter of each given index :param channel_ids: collection of channel indices :param save_path: location to save plots :param save_as_mat: if true, saves each filter as channel x height x width matrix :param save_as_plot: if true, saves each filter as image :return None """ if not os.path.exists(save_path): os.makedirs(save_path) unwhitening_mat, a_mat, w_mat = self.load_filters_for_plotting() w_mat_select = w_mat[channel_ids, :, :].transpose((0, 2, 1)) rotated_w_mat = w_mat_select @ unwhitening_mat[channel_ids, :, :] for idx, channel_id in enumerate(channel_ids): chan_filters = rotated_w_mat[idx, :, :] plottable_filters = np.reshape( chan_filters, [self.n_components, self.ph, self.pw]) if save_as_mat: file_name = 'channel_{}_filters.npy'.format(channel_id) np.save(save_path + file_name, plottable_filters) if save_as_plot: file_name = 'channel_{}_filters.png'.format(channel_id) plottable_filters -= np.min(plottable_filters) plottable_filters /= np.max(plottable_filters) plot_img_mats(plottable_filters, rescale=False, show=False, save_path=save_path + file_name) print('channel {} done'.format(channel_id))