def __init__(self, params, input_vectors, dimensions, plot_for_itr=0, activity_classes=None, output_loc=None): self.parameters = params self.inputs = np.asarray(input_vectors) self.growth_handler = Growth_Handler.GrowthHandler() self.dimensions = dimensions self.learn_smooth_sample_size = self.parameters.get_learn_smooth_sample_size( len(self.inputs)) self.gsom_nodemap = {} self.plot_for_itr = plot_for_itr self.display = Display_Utils.Display(None, None) self.activity_classes = activity_classes self.output_save_location = output_loc # Parameters for recurrent gsom self.globalContexts = np.zeros( (self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions)) self.globalContexts_evaluation = np.zeros( (self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions)) self.alphas = Utils.Utilities.get_decremental_alphas( self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS) self.previousBMU = np.zeros( (1, self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions)) self.previousBMU_evaluation = np.zeros( (1, self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions))
def GSOM_model(SF,forget_threshold,temporal_contexts,learning_itr,smoothing_irt,plot_for_itr,data_filename,output_save_location,name): # Init GSOM Parameters gsom_params = Params.GSOMParameters(SF, learning_itr, smoothing_irt, distance=Params.DistanceFunction.EUCLIDEAN, temporal_context_count=temporal_contexts, forget_itr_count=forget_threshold) generalise_params = Params.GeneraliseParameters(gsom_params) # Process the input files input_vector_database, labels, classes = Parser.InputParser.parse_input_train_data(data_filename, None) # Setup the age threshold based on the input vector length generalise_params.setup_age_threshold(input_vector_database[0].shape[0]) # Process the clustering algorithm controller = Core.Controller(generalise_params) controller_start = time.time() result_dict = controller.run(input_vector_database, plot_for_itr, classes) print('Algorithms completed in', round(time.time() - controller_start, 2), '(s)') gsom_nodemap = result_dict[0]['gsom'] # Saving gsom node map saved_gsom_nodemap_for_0_7 = joblib.dump(gsom_nodemap, output_save_location+'gsom_nodemap_{}.joblib'.format(name)) # Display display = Display_Utils.Display(result_dict[0]['gsom'], None) display.setup_labels_for_gsom_nodemap(classes, 2, 'Latent Space of cnn_5100_input_file_to_gsom : SF=0.7',output_save_location+'latent_space_{}_hitvalues'.format(name)) print('Completed.')
def __init__(self, params, dimensions, plot_for_itr=0, activity_classes=None, output_loc=None): threading.Thread.__init__(self) self.parameters = params self.growth_handler = Growth_Handler.GrowthHandler() self.dimensions = dimensions # self.learn_smooth_sample_size = self.parameters.get_learn_smooth_sample_size(len(inputs)) self.gsom_nodemap = {} self.plot_for_itr = plot_for_itr self.display = Display_Utils.Display(None, None) self.activity_classes = activity_classes self.output_save_location = output_loc self.att = 0.5 self.att_learning_rate = 0.01 self.recurrent_weights_batch = [] self.emotion_features_batch = [] self.behaviour_features_batch = [] self.BATCH_SIZE = 10 # Parameters for recurrent gsom self.globalContexts = np.zeros( (self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions)) self.globalContexts_evaluation = np.zeros( (self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions)) self.alphas = Utils.Utilities.get_decremental_alphas( self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS) self.previousBMU = np.zeros( (1, self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions)) self.previousBMU_evaluation = np.zeros( (1, self.parameters.NUMBER_OF_TEMPORAL_CONTEXTS, self.dimensions))
def dispaly(self, result_dict, classes): gsom_nodemap = result_dict[0]['gsom'] # Display display = Display_Utils.Display(result_dict[0]['gsom'], None) display.setup_labels_for_gsom_nodemap(classes, 2, 'Latent Space of {} : SF={}'.format("Data", self.SF), join(self.output_loc, 'latent_space_' + str(self.SF) + '_hitvalues')) display.setup_labels_for_gsom_nodemap(classes, 2, 'Latent Space of {} : SF={}'.format("Data", self.SF), join(self.output_loc, 'latent_space_' + str(self.SF) + '_labels')) print('Completed.')
def dispaly(gsom_nodemap, classes, name): # Display display = Display_Utils.Display(gsom_nodemap, None) display.setup_labels_for_gsom_nodemap( classes, 2, name + ' Latent Space of {} : SF={}'.format("Data", SF), join(output_loc, name + ' latent_space_' + str(SF) + '_hitvalues')) display.setup_labels_for_gsom_nodemap( classes, 2, name + ' Latent Space of {} : SF={}'.format("Data", SF), join(output_loc, name + ' latent_space_' + str(SF) + '_labels')) print(name + ' Plotting Completed. \n')
def plot_interactive(map_name, output_folder, sf, forget_level_text): # Construct configs title = '{} Analysis - SF:{} Forget:{}'.format(map_name, sf, forget_level_text) # Load pickle files filename = [join(output_folder, fname) for fname in listdir(output_folder) if '.pickle' in fname][0] gsom_nodemap = Utils.Utilities.load_object(filename.replace('.pickle', ''))[0]['gsom'] # Setup image root folder image_root_folder = join(output_folder, 'images') image_filename_prefix = 'node_profile_' display = Display_Utils.Display(None, None) # Make the plot fig = plt.figure() ax = fig.add_subplot(111) plt.title(title) max_count = max([node.get_hit_count() for _, node in gsom_nodemap.items()]) listed_color_map = display.get_color_map(max_count, alpha=0.9) for key, value in gsom_nodemap.items(): key_split = key.split(':') x = int(key_split[0]) y = int(key_split[1]) if value.get_hit_count() > 0: ax.plot(x, y, 'o', color=listed_color_map.colors[value.get_hit_count()], markersize=3) ax.text(x, y + 0.1, str(value.get_hit_count()), fontsize=4) else: ax.plot(x, y, 'o', color=listed_color_map.colors[value.get_hit_count()], markersize=3) def onclick(event, args_list): ix, iy = event.xdata, event.ydata x, y = int(round(ix)), int(round(iy)) print("Clicked at x={0:5.2f}, y={1:5.2f}".format(ix, iy), "Select to show", x, y) image_folder = args_list[0] prefix = args_list[1] key = '{}_{}'.format(str(x).replace('-', 'm'), str(y).replace('-', 'm')) fname = prefix + key + '.png' try: plt.figure() print('fname', join(image_folder, fname)) plt.title('Node: {}-{}'.format(x, y)) img = mpimg.imread(join(image_folder, fname)) imgplot = plt.imshow(img, interpolation='none') imgplot.axes.set_axis_off() plt.show() except: print('No cluster w.r.t. selected node') cid = fig.canvas.mpl_connect('button_press_event', lambda event: onclick(event, [image_root_folder, image_filename_prefix])) plt.show()
def generate_latent_map(visualize=False): # File config dataset_location = config.Configuration.dataset_location experiment_id = 'Exp-' + datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d-%H-%M-%S') output_save_location = join('output/', experiment_id) ID_column_name = config.Configuration.ID_column_name is_normalized = config.Configuration.normalize_dataset # Grid search config sheet_names = config.Configuration.excel_sheet_names # GSOM Config SF_values = config.Configuration.SF_values learning_itr = config.Configuration.learning_itr smoothing_irt = config.Configuration.smoothing_irt forget_threshold_values = [ int(learning_itr * (1 - tp)) for tp in config.Configuration.transience_percentages ] temporal_contexts = 1 plot_for_itr = 4 for dataset in sheet_names: # Process the input files input_vector_database, labels, selected_vars = Parser.InputParser.parse_data( dataset_location, dataset, ID_column_name, is_normalized) print('Latent Spaces for {}, using: \n{}'.format( dataset, list(selected_vars))) for SF, forget_threshold in itertools.product(SF_values, forget_threshold_values): print('\nProcessing with SF={} with Forgetting={}'.format( SF, forget_threshold)) forget_threshold_label = Utils.Utilities.forget_thresh_label( learning_itr, forget_threshold) output_loc, output_loc_images = generate_output_config( dataset, SF, forget_threshold, temporal_contexts, output_save_location) # Init GSOM Parameters gsom_params = Params.GSOMParameters( SF, learning_itr, smoothing_irt, distance=Params.DistanceFunction.EUCLIDEAN, temporal_context_count=temporal_contexts, forget_itr_count=forget_threshold) generalise_params = Params.GeneraliseParameters(gsom_params) # Setup the age threshold based on the input vector length generalise_params.setup_age_threshold( input_vector_database[0].shape[0]) # Process the clustering algorithm algorithm controller = Core.Controller(generalise_params) result_dict = controller.run(input_vector_database, plot_for_itr, labels, output_loc_images) Utils.Utilities.save_object( result_dict, join( output_loc, 'gsom_nodemap_SF-{}_F-{}'.format(SF, forget_threshold_label))) gsom_nodemap = result_dict[0]['gsom'] print('Latent Space generated with {} neurons.'.format( len(gsom_nodemap))) # Visualizations display = Display_Utils.Display(gsom_nodemap, None) display.setup_labels_for_gsom_nodemap( gsom_nodemap, labels, 'Latent Space of {} : SF={} with Forget={}'.format( dataset, SF, forget_threshold_label), join( output_loc, '{}_latent_space_SF-{}_F-{}_hit_count'.format( dataset, SF, forget_threshold_label)), selected_vars=selected_vars) # Plot profile images profiler.Profiler.plot_profile_images(dataset_location, dataset, ID_column_name, output_loc, SF, forget_threshold_label, is_normalized) # Plot interactive profile visualization if visualize: int_display.InteractiveDisplay.plot_interactive( dataset, output_loc, SF, forget_threshold_label)
# Init GSOM Parameters gsom_params = Params.GSOMParameters(SF, learning_itr, smoothing_irt, distance=Params.DistanceFunction.EUCLIDEAN, temporal_context_count=temporal_contexts, forget_itr_count=forget_threshold) generalise_params = Params.GeneraliseParameters(gsom_params) # Process the input files input_vector_database, labels, classes = Parser.InputParser.parse_input_zoo_data(data_filename, None) output_loc, output_loc_images = generate_output_config(dataset, SF, forget_threshold) # Setup the age threshold based on the input vector length generalise_params.setup_age_threshold(input_vector_database[0].shape[0]) # Process the clustering algorithm algorithm controller = Core.Controller(generalise_params) controller_start = time.time() result_dict = controller.run(input_vector_database, plot_for_itr, classes, output_loc_images) print('Algorithms completed in', round(time.time() - controller_start, 2), '(s)') saved_name = Utils.Utilities.save_object(result_dict, join(output_loc, 'gsom_nodemap_SF-{}'.format(SF))) gsom_nodemap = result_dict[0]['gsom'] # Display display = Display_Utils.Display(result_dict[0]['gsom'], None) display.setup_labels_for_gsom_nodemap(classes, 2, 'Latent Space of {} : SF={}'.format(dataset, SF), join(output_loc, 'latent_space_' + str(SF) + '_hitvalues')) display.setup_labels_for_gsom_nodemap(labels, 2, 'Latent Space of {} : SF={}'.format(dataset, SF), join(output_loc, 'latent_space_' + str(SF) + '_labels')) print('Completed.')
# Process the input files input_vector_database, labels = Parser.InputParser.parse_input( input_folder_path) # Process the IKASL algorithm print('Starting IKASL ...') ikasl = Core_IKASL.IKASL(ikasl_params) ikasl_start = time.time() ikasl_sequence = ikasl.run(input_vector_database) print('IKASL algorithms completed in', round(time.time() - ikasl_start, 2), '(s)') saved_name = Utils.Utilities.save_object( ikasl_sequence, output_save_location + output_save_filename) display = Display_Utils.Display(ikasl_sequence) display.display_tree(output_image_title, saved_name, weight_labels=labels, labels_to_show=labels_to_show, enable_style=enable_style, save_format=display_output_format) # Note: commenting the code due to large image data set. This is just for visualisation purposes. # display.view_clusters(image_files_root_folder, single_image_width, single_image_height, view_cluster_sequence) print('Visualisation saved in output folder.') elif mode == 2: # Process the input files labels = Parser.InputParser.get_labels(labels_file)