'add_4': 'rectangle', 'add_6': 'rectangle', 'add_8': 'rectangle' } n_nodes_in_graph = 3 edge_label_font_size = '22' header_font_size = '40' add_heat_map_on_connections = True calc_heat_map_on_connections = False # Get the saved weights list_of_weights = glob.glob(pjoin(EXP_PATH, 'weights.*.hdf5')) weights_dir = list_of_weights[-1] # Load config config = load_from_file(EXP_PATH, ['config'])[0] # Update model config config['set_gmm_activation_layer_as_output'] = True # Load model model = GMM_CNN() model.load_model(weights_dir=weights_dir, config=config) layers_to_inference = [] for key in vis_option: layers_to_inference.append(key) layers_to_inference.append(model.keras_model.output_names[0]) # make clusters saving directory vis_dir = pjoin(EXP_PATH, 'clusters_vis') # get data (_, _), (x_val, y_val) = cifar10.load_data()
import matplotlib.pyplot as plt import numpy as np from keras.datasets import cifar10 from keras.utils import to_categorical from gmm_cnn import GMM_CNN from receptivefield import ReceptiveField from utils.file import load_from_file from utils.file import makedir from utils.vis_utils import crop_image, get_cifar10_labels EXP_PATH = pjoin( *['C:', os.environ["HOMEPATH"], 'Desktop', 'tmp', 'resnet20_cifar10']) # Load config config = load_from_file(EXP_PATH, ['config'])[0] # Load model model = GMM_CNN() model.load_model(config=config) # Visualization parameters # How to vis the representatives images: 1. 'rectangle' around the origin image. 2. 'patches' draw only the rf vis_option = 'rectangle' label_fontsize = 26 dot_radius = 0.5 n_representatives = 6 # make clusters saving directory vis_dir = pjoin(EXP_PATH, 'clusters_vis') makedir(vis_dir)
def create_connections_heat_maps(gmm_model, exp_path, RF, data, connections_dict, saving_dir): """ This script creates the heat map between two clusters connected in the inference tree""" # TODO the data needs to be with mean subtraction makedir(saving_dir) clusters_rep_path = pjoin(exp_path, 'clusters_representatives.json') if os.path.isfile(clusters_rep_path) and os.access(clusters_rep_path, os.R_OK): # checks if file exists print("File exists and is readable") print("Load results...") clusters_representatives = load_from_file(exp_path, ['clusters_representatives'])[0] else: raise ValueError("Either file is missing or is not readable") for explained_key in connections_dict: if 'class' in explained_key or 'fc' in explained_key: continue explained_layer = explained_key.split('_')[0] for i in range(len(explained_key.split('_')) - 2): explained_layer = explained_layer + '_' + explained_key.split('_')[i+1] explained_cluster = explained_key.split('_')[-1] explained_gmm = gmm_model.get_correspond_gmm_layer(explained_layer) c_reps = clusters_representatives[explained_gmm][explained_cluster] c_imgs_indice = np.asarray(c_reps['image']) c_imgs = data[c_imgs_indice] if len(connections_dict[explained_key]) != 0: explaining_key = next(iter(connections_dict[explained_key])) explaining_layer = explaining_key.split('_')[0] + '_' + explaining_key.split('_')[1] explaining_gmm = gmm_model.get_correspond_gmm_layer(explaining_layer) imgs_pred = gmm_model.predict(c_imgs) explaining_pred = imgs_pred['GMM'][explaining_gmm] for explaining_key in connections_dict[explained_key]: explaining_layer = explaining_key.split('_')[0] + '_' + explaining_key.split('_')[1] explaining_cluster = int(explaining_key.split('_')[2]) - 1 saving_plot_path = pjoin(saving_dir, explained_key + '_' + explaining_key + '.png') if os.path.isfile(saving_plot_path) and os.access(saving_plot_path, os.R_OK): # checks if heat map file exists continue _, _, origin_size = RF.target_neuron_rf(explained_layer, [0, 0], rf_layer_name=explaining_layer, return_origin_size=True) heat_map = np.zeros((origin_size[0], origin_size[1])) for i in range(len(c_imgs_indice)): h = c_reps['spatial_location']['row'][i] w = c_reps['spatial_location']['col'][i] size, center, UL_pos, origin_center = RF.target_neuron_rf(explained_layer, [h, w], rf_layer_name=explaining_layer, return_upper_left_pos=True, return_origin_center=True) row_offset = abs(min(0, origin_center[0] - np.floor_divide(origin_size[0], 2))) col_offset = abs(min(0, origin_center[1] - np.floor_divide(origin_size[1], 2))) upper_left_row = UL_pos[0] upper_left_col = UL_pos[1] rf_preds = explaining_pred[i, upper_left_row:upper_left_row + size[0], upper_left_col:upper_left_col + size[1], explaining_cluster] pad_rf = np.zeros((origin_size[0], origin_size[1])) pad_rf[row_offset:rf_preds.shape[0] + row_offset, col_offset:rf_preds.shape[1] + col_offset] = rf_preds heat_map += pad_rf heat_map = heat_map / len(c_imgs_indice) fig = plt.figure() label = str(connections_dict[explained_key][explaining_key]['CN']) + ', ' +\ str(connections_dict[explained_key][explaining_key]['LR']) ax = fig.add_subplot(111) ax.matshow(heat_map, interpolation='hermite') ax.axis('off') ax.set_title(label=label, fontsize=70) plt.tight_layout() plt.savefig(saving_plot_path) plt.close(fig)