def feat_map_ica(layer_name, subset_file='subset_cutoff_200_images.txt', map_index=0, n_plots=25): files = get_feature_files(layer_name, subset_file) maps = [] for idx, file in enumerate(files): mat = np.load(file) map_shape = mat[:, :, map_index].shape maps.append(mat[:, :, map_index].flatten()) maps = np.stack(maps, axis=0) ica = FastICA() ica.fit(maps) cols = int(np.ceil(np.sqrt(n_plots))) rows = int(np.ceil(n_plots // cols)) fig, ax_list = plt.subplots(ncols=cols, nrows=rows) ax_list = ax_list.flatten() for idx, ax in enumerate(ax_list): if idx >= n_plots: ax.axis('off') else: ax.matshow(np.reshape(ica.components_[idx, :], map_shape), interpolation='none') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.savefig('./plots/ica_' + layer_name + '_map_' + str(map_index) + '.png', format='png', dpi=1500) plt.close()
def inverse_avg_covariance_matrix(layer_name, subset_file='subset_cutoff_200_images.txt', log=False): files = get_feature_files(layer_name, subset_file) avg_cov = None for idx, file in enumerate(files): if idx % 10 == 0: print('processing file ' + str(idx) + ' / ' + str(len(files))) mat = np.load(file) if avg_cov is None: avg_cov = covariance_matrix(mat) else: avg_cov += covariance_matrix(mat) avg_cov /= len(files) inv_cov = np.linalg.pinv(avg_cov) inv_cov[inv_cov == np.inf] = inv_cov[inv_cov != np.inf].max() if log: inv_cov[inv_cov == -np.inf] = inv_cov[inv_cov != -np.inf].min() inv_cov += inv_cov.min() + 0.0000000001 norm = LogNorm(vmin=inv_cov.min(), vmax=inv_cov.max()) plt.matshow(inv_cov, norm=norm, interpolation='none') plt.savefig('./plots/inv_avg_cov_' + layer_name + '_log.png', format='png', dpi=1500) else: inv_cov[inv_cov == -np.inf] = inv_cov[inv_cov != -np.inf].min() plt.matshow(inv_cov, interpolation='none') plt.savefig('./plots/inv_avg_cov_' + layer_name + '_lin.png', format='png', dpi=1500)
def visualize_feature_map(layer_name, subset_file='subset_cutoff_200_images.txt', image_index=0, max_n=25, highest_act=True): file = get_feature_files(layer_name, subset_file)[image_index] feat_map = np.load(file) feat_map_vis(feat_map, max_n, highest_act)
def gini_index_hist(layer_name, subset_file='subset_cutoff_200_images.txt'): files = get_feature_files(layer_name, subset_file) ginis = [] for idx, file in enumerate(files): if idx % 10 == 0: print('processing file ' + str(idx) + ' / ' + str(len(files))) mat = np.load(file) ginis.append(gini_index(mat)) plt.hist(ginis, 50, normed=1, facecolor='green', alpha=0.75) plt.savefig('./plots/gini_hist_' + layer_name + '.png', format='png')
def avg_gram_matrix(layer_name, subset_file='subset_cutoff_200_images.txt'): files = get_feature_files(layer_name, subset_file) avg_gram = None for idx, file in enumerate(files): if idx % 10 == 0: print('processing file ' + str(idx) + ' / ' + str(len(files))) mat = np.load(file) if avg_gram is None: avg_gram = gram_matrix(mat) else: avg_gram += gram_matrix(mat) avg_gram /= len(files) plt.matshow(avg_gram, interpolation='none') plt.savefig('./plots/avg_gram_' + layer_name + '.png', format='png')