def update_row2sheet(service, sheet_id, row_starting_position, data, debug=True): ''' update a list of data to a google sheet continuously parameters: service: a service request to google sheet sheet_di: a string to identify the sheet uniquely starting_position: a string existing in the sheet to represent the let-top corner of patch to fill in data: a of list data to fill ''' if debug: isstring(sheet_id), 'the sheet id is not a string' isstring(row_starting_position), 'the starting position is not correct' islist(data), 'the input data is not a list' # How the input data should be interpreted. value_input_option = 'RAW' # TODO: Update placeholder value. value_range_body = {'values': [data]} request = service.spreadsheets().values().update( spreadsheetId=sheet_id, range=row_starting_position, valueInputOption=value_input_option, body=value_range_body) response = request.execute()
def calculate_truncated_mse(error_list, truncated_list, debug=True): ''' calculate the mse truncated by a set of thresholds, and return the truncated MSE and the percentage of how many points' error is lower than the threshold parameters: error_list: a list of error truncated_list: a list of threshold return tmse_dict: a dictionary where each entry is a dict and has key 'T-MSE' & 'percentage' ''' if debug: assert islist(error_list) and all( isscalar(error_tmp) for error_tmp in error_list), 'the input error list is not correct' assert islist(truncated_list) and all( isscalar(thres_tmp) for thres_tmp in truncated_list), 'the input truncated list is not correct' assert len(truncated_list) > 0, 'there is not entry in truncated list' tmse_dict = dict() num_entry = len(error_list) error_array = np.asarray(error_list) for threshold in truncated_list: error_index = np.where(error_array[:] < threshold)[0].tolist( ) # plot visible points in red color error_interested = error_array[error_index] entry = dict() entry['T-MSE'] = np.mean(error_interested) entry['percentage'] = len(error_index) / float(num_entry) tmse_dict[threshold] = entry return tmse_dict
def generate_list_from_data(save_path, src_data, debug=True): ''' generate a file which contains a 1-d numpy array data parameter: src_data: a list of 1 element data, or a 1-d numpy array data ''' save_path = safepath(save_path) if debug: if isnparray(src_data): assert src_data.ndim == 1, 'source data is incorrect' elif islist(src_data): assert all(np.array(data_tmp).size == 1 for data_tmp in src_data), 'source data is in correct' assert isfolder(save_path) or isfile( save_path), 'save path is not correct' if isfolder(save_path): save_path = os.path.join(save_path, 'datalist.txt') if debug: assert is_path_exists_or_creatable( save_path), 'the file cannot be created' with open(save_path, 'w') as file: for item in src_data: file.write('%f\n' % item) file.close()
def safe_2dptsarray(input_pts, homogeneous=False, dimen_add=0, warning=True, debug=True): ''' make sure to copy the pts array without modifying it and make the dimension to 2(3 if homogenous) x N parameters: input_pts: a list of 2(3 if homogenous) elements, a listoflist of 2 elements: e.g., [[1,2], [5,6]], a numpy array with shape or (2, N) or (2, ) homogeneous: the input points are in the homogenous coordinate dimen_add: additional dimension, used to accommdate for higher dimensional array outputs: np_pts: 2 (3 if homogenous) X N numpy array ''' if homogeneous: dimension = 3 + dimen_add else: dimension = 2 + dimen_add if islist(input_pts): if islistoflist(input_pts): if debug: assert all( len(list_tmp) == dimension for list_tmp in input_pts ), 'all sub-lists should have length of %d' % dimension np_pts = np.array(input_pts).transpose() else: if debug: assert len( input_pts ) == dimension, 'the input pts list does not have a good shape' np_pts = np.array(input_pts).reshape((dimension, 1)) elif isnparray(input_pts): input_pts = input_pts.copy() if input_pts.shape == (dimension, ): np_pts = input_pts.reshape((dimension, 1)) else: np_pts = input_pts else: assert False, 'only list and numpy array for pts are supported' if debug: if homogeneous: assert is2dptsarray_homogeneous( np_pts), 'the input pts array does not have a good shape' else: assert is2dptsarray( np_pts), 'the input pts array does not have a good shape' return np_pts
def get_data_from_sheet(service, sheet_id, search_range, debug=True): ''' get a list of data from a google sheet continuously parameters: service: a service request to google sheet sheet_di: a string to identify the sheet uniquely search_range: a list of position queried ''' if debug: isstring(sheet_id), 'the sheet id is not a string' islist(search_range), 'the search range is not a list' # print(search_range) # How the input data should be interpreted. # value_input_option = 'RAW' # TODO: Update placeholder value. # value_range_body = {'values': [data]} request = service.spreadsheets().values().batchGet(spreadsheetId=sheet_id, ranges=search_range) while True: try: response = request.execute() break except: continue data = list() # print(response['valueRanges']) for raw_data in response['valueRanges']: if 'values' in raw_data: data.append(raw_data['values'][0][0]) else: data.append('') return data
def visualize_bar(data, bin_size=2.0, title='Bar Graph of Key-Value Pair', xlabel='index', ylabel='count', vis=True, save_path=None, debug=True, closefig=True): ''' visualize the bar graph of a data, which can be a dictionary or list of dictionary different from function of visualize_bar_graph, this function does not depend on panda and dataframe, it's simpler but with less functionality also the key of this function takes continuous scalar variable ''' if debug: assert isstring(title) and isstring(xlabel) and isstring( ylabel), 'title/xlabel/ylabel is not correct' assert isdict(data) or islist(data), 'input data is not correct' assert isscalar(bin_size), 'the bin size is not a floating number' if isdict(data): index_list = data.keys() if debug: assert islistofscalar( index_list ), 'the input dictionary does not contain a scalar key' frequencies = data.values() else: index_list = range(len(data)) frequencies = data index_str_list = scalarlist2strlist(index_list, debug=debug) index_list = np.array(index_list) fig, ax = get_fig_ax_helper(fig=None, ax=None) # ax.set_xticks(index_list) # ax.set_xticklabels(index_str_list) plt.bar(index_list, frequencies, bin_size, color='r', alpha=0.5) plt.title(title, fontsize=20) plt.xlabel(xlabel) plt.ylabel(ylabel) return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, debug=debug, transparent=False, closefig=closefig)
def safe_npdata(input_data, warning=True, debug=True): ''' copy a list of data or a numpy data to the buffer for use parameters: input_data: a list, a scalar or numpy data outputs: np_data: a copy of numpy data ''' if islist(input_data): np_data = np.array(input_data) elif isscalar(input_data): np_data = np.array(input_data).reshape((1, )) elif isnparray(input_data): np_data = input_data.copy() else: assert False, 'only list of data, scalar or numpy data are supported' return np_data
def pol2cart_2d_degree(pts, debug=True): ''' input point: (rho, phi) phi is in degree ''' if debug: assert istuple(pts) or islist(pts) or isnparray( pts), 'input point is not correct' assert np.array(pts).size == 2, 'input point is not 2d points' rho = pts[0] phi = math.radians(pts[1]) x = rho * np.cos(phi) y = rho * np.sin(phi) return (x, y)
def load_hdf5_file(hdf5_file, dataname, debug=True): ''' load a single hdf5 file ''' if debug: assert is_path_exists(hdf5_file) and isfile( hdf5_file), 'input hdf5 path does not exist: %s' % hdf5_file assert islist(dataname), 'dataset queried is not correct' assert all( isstring(dataset_tmp) for dataset_tmp in dataname), 'dataset queried is not correct' hdf5 = h5py.File(hdf5_file, 'r') datadict = dict() for dataset in dataname: datadict[dataset] = np.array(hdf5[dataset]) return datadict
def get_image_ids(self, class_ids): ''' return a list of image IDs containing the given class IDs parameters: class_ids: a list of class IDs requested outputs: image_ids: a list of image IDs containing the requested classes ''' assert islist(class_ids), 'the input class ids is not a list' image_id_list = [] for image_id_tmp, image_data in self.images_dict.items(): obj_ids_tmp = image_data['ids'] intersect_list = find_unique_common_from_lists( class_ids, obj_ids_tmp) if len(intersect_list) > 0: image_id_list.append(image_id_tmp) return image_id_list
def combine_txt_file(file_path_list, save_path=None, debug=True): ''' combine txt file line by line ''' if debug: assert islist(file_path_list), 'input is not a list' data = [] num_lines = 0 for file_path in file_path_list: data_tmp, num_lines_tmp = load_txt_file(file_path, debug=debug) num_lines += num_lines_tmp data += data_tmp if save_path is not None: save_txt_file(data, save_path, debug=debug) return data, num_lines
def safe_center_bbox(input_bbox, warning=True, debug=True): ''' make sure to copy the center bbox without modifying it and make the dimension to N x 4 or N x 2 parameters: input_bbox: a list of 4 (2) elements, a listoflist of 4 (2) elements: e.g., [[1,2,3,4], [5,6,7,8]], a numpy array with shape or (N, 4) or (4, ) or (N, 2) or (2, ) outputs: np_bboxes: N X 4 (2) numpy array ''' if islist(input_bbox): if islistoflist(input_bbox): if debug: assert all( len(list_tmp) == 4 or len(list_tmp) == 2 for list_tmp in input_bbox), 'all sub-lists should have length of 4' np_bboxes = np.array(input_bbox) else: if debug: assert len(input_bbox) == 4 or len( input_bbox ) == 2, 'the center bboxes list does not have a good shape' if len(input_bbox) == 4: np_bboxes = np.array(input_bbox).reshape((1, 4)) else: np_bboxes = np.array(input_bbox).reshape((1, 2)) elif isnparray(input_bbox): input_bbox = input_bbox.copy() if input_bbox.shape == (4, ): np_bboxes = input_bbox.reshape((1, 4)) elif input_bbox.shape == (2, ): np_bboxes = input_bbox.reshape((1, 2)) else: if debug: assert iscenterbbox( input_bbox ), 'the input center bbox numpy array does not have a good shape' np_bboxes = input_bbox else: assert False, 'only list and numpy array for bbox are supported' return np_bboxes
def rand_load_hdf5_from_folder(hdf5_src, dataname, debug=True): ''' randomly load a single hdf5 file from a hdf5 folder ''' if debug: assert is_path_exists(hdf5_src) and isfolder( hdf5_src), 'input hdf5 path does not exist: %s' % hdf5_src assert islist(dataname), 'dataset queried is not correct' assert all( isstring(dataset_tmp) for dataset_tmp in dataname), 'dataset queried is not correct' hdf5list, num_hdf5_files = load_list_from_folder(folder_path=hdf5_src, ext_filter='.hdf5') check_index = random.randrange(0, num_hdf5_files) hdf5_path_sample = hdf5list[check_index] hdf5_file = h5py.File(hdf5_path_sample, 'r') datadict = dict() for dataset in dataname: datadict[dataset] = np.array(hdf5_file[dataset]) return datadict
def cart2pol_2d_degree(pts, debug=True): ''' input a 2d point and convert to polar coordinate return for degree: [0, 360) ''' if debug: assert istuple(pts) or islist(pts) or isnparray( pts), 'input point is not correct' assert np.array(pts).size == 2, 'input point is not 2d points' x = pts[0] y = pts[1] rho = np.sqrt(x**2 + y**2) phi = math.degrees(np.arctan2(y, x)) while phi < 0: phi += 360 while phi >= 360.: phi -= 360 return (rho, phi)
def load_list_from_folders(folder_path_list, ext_filter=None, depth=1, recursive=False, save_path=None, debug=True): ''' load a list of files or folders from a list of system path ''' if debug: assert islist(folder_path_list) or isstring( folder_path_list), 'input path list is not correct' if isstring(folder_path_list): folder_path_list = [folder_path_list] fulllist = list() num_elem = 0 for folder_path_tmp in folder_path_list: fulllist_tmp, num_elem_tmp = load_list_from_folder( folder_path_tmp, ext_filter=ext_filter, depth=depth, recursive=recursive) fulllist += fulllist_tmp num_elem += num_elem_tmp # save list to a path if save_path is not None: save_path = safepath(save_path) if debug: assert is_path_exists_or_creatable( save_path), 'the file cannot be created' with open(save_path, 'w') as file: for item in fulllist: file.write('%s\n' % item) file.close() return fulllist, num_elem
def safe_ptsarray_occlusion(input_pts, warning=True, debug=True): ''' make sure to copy the pts array without modifying it and make the dimension to 3 x N the occlusion (3rd) row should contain 0, 1 or -1 parameters: input_pts: a list of 3 elements, a listoflist of 3 elements: e.g., [[1,2], [5,6], [0, 1]], a numpy array with shape or (3, N) or (3, ) outputs: np_pts: 3 X N numpy array, with the third row as the occlusion ''' if islist(input_pts): if islistoflist(input_pts): if debug: assert all(len(list_tmp) == 3 for list_tmp in input_pts), 'all sub-lists should have length of 3' np_pts = np.array(input_pts).transpose() else: if debug: assert len( input_pts ) == 3, 'the input pts list does not have a good shape' np_pts = np.array(input_pts).reshape((3, 1)) elif isnparray(input_pts): input_pts = input_pts.copy() if input_pts.shape == (3, ): np_pts = input_pts.reshape((3, 1)) else: np_pts = input_pts else: assert False, 'only list and numpy array for pts are supported' if debug: assert is2dptsarray_occlusion( np_pts), 'the input pts array does not have a good shape' return np_pts
def facial_landmark_evaluation(pred_dict_all, anno_dict, num_pts, error_threshold, normalization_ced=True, normalization_vec=False, covariance=True, display_list=None, debug=True, vis=False, save=True, save_path=None): ''' evaluate the performance of facial landmark detection parameter: pred_dict_all: a dictionary for all basline methods. Each key is the method name and the value is corresponding prediction dictionary, which keys are the image path and values are 2 x N prediction results anno_dict: a dictionary which keys are the image path and values are 2 x N annotation results num_pts: number of points vis: determine if visualizing the pck curve save: determine if saving the visualization results save_path: a directory to save all the results visualization: 1. 2d pck curve (total and point specific) for all points for all methods 2. point error vector (total and point specific) for all points and for all methods 3. mean square error return: metrics_all: a list of list to have detailed metrics over all methods ptswise_mse: a list of list to have average MSE over all key-points for all methods ''' num_methods = len(pred_dict_all) if debug: assert isdict(pred_dict_all) and num_methods > 0 and all(isdict(pred_dict) for pred_dict in pred_dict_all.values()), 'predictions result format is not correct' assert isdict(anno_dict), 'annotation result format is not correct' assert ispositiveinteger(num_pts), 'number of points is not correct' assert isscalar(error_threshold), 'error threshold is not correct' assert islogical(normalization_ced) and islogical(normalization_vec), 'normalization flag is not correct' if display_list is not None: assert len(display_list) == num_methods, 'display list is not correct %d vs %d' % (len(display_list), num_methods) num_images = len(pred_dict_all.values()[0]) if debug: assert num_images > 0, 'the predictions are empty' assert num_images == len(anno_dict), 'number of images is not equal to number of annotations: %d vs %d' % (num_images, len(anno_dict)) assert all(num_images == len(pred_dict) for pred_dict in pred_dict_all.values()), 'number of images in results from different methods are not equal' # calculate normalized mean error for each single image based on point-to-point Euclidean distance normalized by the bounding box size # calculate point error vector for each single image based on error vector normalized by the bounding box size normed_mean_error_dict = dict() normed_mean_error_pts_specific_dict = dict() normed_mean_error_pts_specific_valid_dict = dict() pts_error_vec_dict = dict() pts_error_vec_pts_specific_dict = dict() mse_error_dict_dict = dict() for method_name, pred_dict in pred_dict_all.items(): normed_mean_error_total = np.zeros((num_images, ), dtype='float32') normed_mean_error_pts_specific = np.zeros((num_images, num_pts), dtype='float32') normed_mean_error_pts_specific_valid = np.zeros((num_images, num_pts), dtype='bool') pts_error_vec = np.zeros((num_images, 2), dtype='float32') pts_error_vec_pts_specific = np.zeros((num_images, 2, num_pts), dtype='float32') mse_error_dict = dict() count = 0 count_skip_num_images = 0 # it's possible that no annotation exists on some images, than no error should be counted for those images, we count the number of those images for image_path, pts_prediction in pred_dict.items(): _, filename, _ = fileparts(image_path) pts_anno = anno_dict[filename] # 2 x N annotation pts_keep_index = range(num_pts) # to avoid list object type, do conversion here if islist(pts_anno): pts_anno = np.asarray(pts_anno) if islist(pts_prediction): pts_prediction = np.asarray(pts_prediction) if debug: assert (is2dptsarray(pts_anno) or is2dptsarray_occlusion(pts_anno)) and pts_anno.shape[1] == num_pts, 'shape of annotations is not correct (%d x %d) vs (%d x %d)' % (2, num_pts, pts_anno.shape[0], pts_anno.shape[1]) # if the annotation has 3 channels (include extra occlusion channel, we keep only the points with annotations) # occlusion: -1 -> visible but not annotated, 0 -> invisible and not annotated, 1 -> visible, we keep only visible and annotated points if pts_anno.shape[0] == 3: pts_keep_index = np.where(pts_anno[2, :] == 1)[0].tolist() if len(pts_keep_index) <= 0: # if no point is annotated in current image count_skip_num_images += 1 continue pts_anno = pts_anno[0:2, pts_keep_index] pts_prediction = pts_prediction[:, pts_keep_index] # to avoid the point location includes the score or occlusion channel, only take the first two channels here if pts_prediction.shape[0] == 3 or pts_prediction.shape[0] == 4: pts_prediction = pts_prediction[0:2, :] num_pts_tmp = len(pts_keep_index) if debug: assert pts_anno.shape[1] <= num_pts, 'number of points is not correct: %d vs %d' % (pts_anno.shape[1], num_pts) assert pts_anno.shape == pts_prediction.shape, 'shape of annotations and predictions are not the same {} vs {}'.format(print_np_shape(pts_anno, debug=debug), print_np_shape(pts_prediction, debug=debug)) # print 'number of points to keep is %d' % num_pts_tmp # calculate bbox for normalization if normalization_ced or normalization_vec: assert len(pts_keep_index) == num_pts, 'some points are not annotated. Normalization on PCK curve is not allowed.' bbox_anno = pts2bbox(pts_anno, debug=debug) # 1 x 4 bbox_TLWH = bbox_TLBR2TLWH(bbox_anno, debug=debug) # 1 x 4 bbox_size = math.sqrt(bbox_TLWH[0, 2] * bbox_TLWH[0, 3]) # scalar # calculate normalized error for all points normed_mean_error, _ = pts_euclidean(pts_prediction, pts_anno, debug=debug) # scalar if normalization_ced: normed_mean_error /= bbox_size normed_mean_error_total[count] = normed_mean_error mse_error_dict[image_path] = normed_mean_error if normed_mean_error == 0: print pts_prediction print pts_anno # calculate normalized error point specifically for pts_index in xrange(num_pts): if pts_index in pts_keep_index: # if current point not annotated in current image, just keep 0 normed_mean_error_pts_specific_valid[count, pts_index] = True else: continue pts_index_from_keep_list = pts_keep_index.index(pts_index) pts_prediction_tmp = np.reshape(pts_prediction[:, pts_index_from_keep_list], (2, 1)) pts_anno_tmp = np.reshape(pts_anno[:, pts_index_from_keep_list], (2, 1)) normed_mean_error_pts_specifc_tmp, _ = pts_euclidean(pts_prediction_tmp, pts_anno_tmp, debug=debug) if normalization_ced: normed_mean_error_pts_specifc_tmp /= bbox_size normed_mean_error_pts_specific[count, pts_index] = normed_mean_error_pts_specifc_tmp # calculate the point error vector error_vector = pts_prediction - pts_anno # 2 x num_pts_tmp if normalization_vec: error_vector /= bbox_size pts_error_vec_pts_specific[count, :, pts_keep_index] = np.transpose(error_vector) pts_error_vec[count, :] = np.sum(error_vector, axis=1) / num_pts_tmp count += 1 print 'number of skipped images is %d' % count_skip_num_images assert count + count_skip_num_images == num_images, 'all cells in the array must be filled %d vs %d' % (count + count_skip_num_images, num_images) # print normed_mean_error_total # time.sleep(1000) # save results to dictionary normed_mean_error_dict[method_name] = normed_mean_error_total[:count] normed_mean_error_pts_specific_dict[method_name] = normed_mean_error_pts_specific[:count, :] normed_mean_error_pts_specific_valid_dict[method_name] = normed_mean_error_pts_specific_valid[:count, :] pts_error_vec_dict[method_name] = np.transpose(pts_error_vec[:count, :]) # 2 x num_images pts_error_vec_pts_specific_dict[method_name] = pts_error_vec_pts_specific[:count, :, :] mse_error_dict_dict[method_name] = mse_error_dict # calculate mean value if mse: mse_value = dict() # dictionary to record all average MSE for different methods mse_dict = dict() # dictionary to record all point-wise MSE for different keypoints for method_name, error_array in normed_mean_error_dict.items(): mse_value[method_name] = np.mean(error_array) else: mse_value = None # save mse error list to file for each method error_list_savedir = os.path.join(save_path, 'error_list') mkdir_if_missing(error_list_savedir) for method_name, mse_error_dict in mse_error_dict_dict.items(): mse_error_list_path = os.path.join(error_list_savedir, 'error_%s.txt' % method_name) mse_error_list = open(mse_error_list_path, 'w') sorted_tuple_list = sorted(mse_error_dict.items(), key=operator.itemgetter(1), reverse=True) for tuple_index in range(len(sorted_tuple_list)): image_path_tmp = sorted_tuple_list[tuple_index][0] mse_error_tmp = sorted_tuple_list[tuple_index][1] mse_error_list.write('{:<200} {}\n'.format(image_path_tmp, '%.2f' % mse_error_tmp)) mse_error_list.close() print '\nsave mse error list for %s to %s' % (method_name, mse_error_list_path) # visualize the ced (cumulative error distribution curve) print('visualizing pck curve....\n') pck_savedir = os.path.join(save_path, 'pck') mkdir_if_missing(pck_savedir) pck_savepath = os.path.join(pck_savedir, 'pck_curve_overall.png') table_savedir = os.path.join(save_path, 'metrics') mkdir_if_missing(table_savedir) table_savepath = os.path.join(table_savedir, 'detailed_metrics_overall.txt') _, metrics_all = visualize_ced(normed_mean_error_dict, error_threshold=error_threshold, normalized=normalization_ced, truncated_list=truncated_list, title='2D PCK curve (all %d points)' % num_pts, display_list=display_list, debug=debug, vis=vis, pck_savepath=pck_savepath, table_savepath=table_savepath) metrics_title = ['Method Name / Point Index'] ptswise_mse_table = [[normed_mean_error_pts_specific_dict.keys()[index_tmp]] for index_tmp in xrange(num_methods)] for pts_index in xrange(num_pts): metrics_title.append(str(pts_index + 1)) normed_mean_error_dict_tmp = dict() for method_name, error_array in normed_mean_error_pts_specific_dict.items(): normed_mean_error_pts_specific_valid_temp = normed_mean_error_pts_specific_valid_dict[method_name] # Some points at certain images might not be annotated. When calculating MSE for these specific point, we remove those images to avoid "false" mean average error valid_array_per_pts_per_method = np.where(normed_mean_error_pts_specific_valid_temp[:, pts_index] == True)[0].tolist() error_array_per_pts = error_array[:, pts_index] error_array_per_pts = error_array_per_pts[valid_array_per_pts_per_method] num_image_tmp = len(valid_array_per_pts_per_method) # print(num_image_tmp) if num_image_tmp == 0: continue # aaa normed_mean_error_dict_tmp[method_name] = np.reshape(error_array_per_pts, (num_image_tmp, )) pck_savepath = os.path.join(pck_savedir, 'pck_curve_pts_%d.png' % (pts_index+1)) table_savepath = os.path.join(table_savedir, 'detailed_metrics_pts_%d.txt' % (pts_index+1)) if len(normed_mean_error_dict_tmp) == 0: continue metrics_dict, _ = visualize_ced(normed_mean_error_dict_tmp, error_threshold=error_threshold, normalized=normalization_ced, truncated_list=truncated_list, display2terminal=False, title='2D PCK curve for point %d' % (pts_index+1), display_list=display_list, debug=debug, vis=vis, pck_savepath=pck_savepath, table_savepath=table_savepath) for method_index in range(num_methods): method_name = normed_mean_error_pts_specific_dict.keys()[method_index] ptswise_mse_table[method_index].append('%.1f' % metrics_dict[method_name]['MSE']) # reorder the table order_index_list = [display_list.index(method_name_tmp) for method_name_tmp in normed_mean_error_pts_specific_dict.keys()] order_index_list = [0] + [order_index_tmp + 1 for order_index_tmp in order_index_list] # print table to terminal ptswise_mse_table = list_reorder([metrics_title] + ptswise_mse_table, order_index_list, debug=debug) table = AsciiTable(ptswise_mse_table) print '\nprint point-wise average MSE' print table.table # save table to file ptswise_savepath = os.path.join(table_savedir, 'pointwise_average_MSE.txt') table_file = open(ptswise_savepath, 'w') table_file.write(table.table) table_file.close() print '\nsave point-wise average MSE to %s' % ptswise_savepath # visualize the error vector map # print('visualizing error vector distribution map....\n') # error_vec_save_dir = os.path.join(save_path, 'error_vec') # mkdir_if_missing(error_vec_save_dir) # savepath_tmp = os.path.join(error_vec_save_dir, 'error_vector_distribution_all.png') # visualize_pts(pts_error_vec_dict, title='Point Error Vector Distribution (all %d points)' % num_pts, mse=mse, mse_value=mse_value, display_range=display_range, display_list=display_list, xlim=xlim, ylim=ylim, covariance=covariance, debug=debug, vis=vis, save_path=savepath_tmp) # for pts_index in xrange(num_pts): # pts_error_vec_pts_specific_dict_tmp = dict() # for method_name, error_vec_dict in pts_error_vec_pts_specific_dict.items(): # pts_error_vec_pts_specific_valid = normed_mean_error_pts_specific_valid_dict[method_name] # get valid flag # valid_image_index_per_pts = np.where(pts_error_vec_pts_specific_valid[:, pts_index] == True)[0].tolist() # get images where the points with current index are annotated # print(len(valid_image_index_per_pts)) # pts_error_vec_pts_specific_dict_tmp[method_name] = np.transpose(error_vec_dict[valid_image_index_per_pts, :, pts_index]) # 2 x num_images # savepath_tmp = os.path.join(error_vec_save_dir, 'error_vector_distribution_pts_%d.png' % (pts_index+1)) # if mse: # mse_dict_tmp = visualize_pts(pts_error_vec_pts_specific_dict_tmp, title='Point Error Vector Distribution for Point %d' % (pts_index+1), mse=mse, display_range=display_range, display_list=display_list, xlim=xlim, ylim=ylim, covariance=covariance, debug=debug, vis=vis, save_path=savepath_tmp) # mse_best = min(mse_dict_tmp.values()) # mse_single = dict() # mse_single['mse'] = mse_best # mse_single['num_images'] = len(valid_image_index_per_pts) # assume number of valid images is equal for all methods # mse_dict[pts_index] = mse_single # else: # visualize_pts(pts_error_vec_pts_specific_dict_tmp, title='Point Error Vector Distribution for Point %d' % (pts_index+1), mse=mse, display_range=display_range, display_list=display_list, xlim=xlim, ylim=ylim, covariance=covariance, debug=debug, vis=vis, save_path=savepath_tmp) # save mse to json file for further use # if mse: # json_path = os.path.join(save_path, 'mse_pts.json') # # if existing, compare and select the best # if is_path_exists(json_path): # with open(json_path, 'r') as file: # mse_dict_old = json.load(file) # file.close() # for pts_index, mse_single in mse_dict_old.items(): # mse_dict_new = mse_dict[int(pts_index)] # mse_new = mse_dict_new['mse'] # if mse_new < mse_single['mse']: # mse_single['mse'] = mse_new # mse_dict_old[pts_index] = mse_single # with open(json_path, 'w') as file: # print('overwrite old mse to {}'.format(json_path)) # json.dump(mse_dict_old, file) # file.close() # else: # with open(json_path, 'w') as file: # print('save mse for all keypoings to {}'.format(json_path)) # json.dump(mse_dict, file) # file.close() print('\ndone!!!!!\n') return metrics_all, ptswise_mse_table
def visualize_distribution(data, bin_size=None, vis=False, save_path=None, debug=True, closefig=True): ''' visualize the histogram of a data, which can be a dictionary or list or numpy array or tuple or a list of list ''' if debug: assert istuple(data) or isdict(data) or islist(data) or isnparray( data), 'input data is not correct' # convert data type if istuple(data): data = list(data) elif isdict(data): data = data.values() elif isnparray(data): data = data.tolist() num_bins = 1000.0 fig, ax = get_fig_ax_helper(fig=None, ax=None) # calculate bin size if bin_size is None: if islistoflist(data): max_value = np.max(np.max(data)) min_value = np.min(np.min(data)) else: max_value = np.max(data) min_value = np.min(data) bin_size = (max_value - min_value) / num_bins else: try: bin_size = float(bin_size) except TypeError: print('size of bin should be an float value') # plot if islistoflist(data): max_value = np.max(np.max(data)) min_value = np.min(np.min(data)) bins = np.arange(min_value - bin_size, max_value + bin_size, bin_size) # fixed bin size plt.xlim([min_value - bin_size, max_value + bin_size]) for data_list_tmp in data: if debug: assert islist(data_list_tmp), 'the nested list is not correct!' # plt.hist(data_list_tmp, bins=bins, alpha=0.3) sns.distplot(data_list_tmp, bins=bins, kde=False) # sns.distplot(data_list_tmp, bins=bins, kde=False) else: bins = np.arange( min(data) - 10 * bin_size, max(data) + 10 * bin_size, bin_size) # fixed bin size plt.xlim([min(data) - bin_size, max(data) + bin_size]) plt.hist(data, bins=bins, alpha=0.5) plt.title('distribution of data') plt.xlabel('data (bin size = %f)' % bin_size) plt.ylabel('count') return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, debug=debug, closefig=closefig)
def visualize_ced(normed_mean_error_dict, error_threshold, normalized=True, truncated_list=None, display2terminal=True, display_list=None, title='2D PCK curve', debug=True, vis=False, pck_savepath=None, table_savepath=None, closefig=True): ''' visualize the cumulative error distribution curve (alse called NME curve or pck curve) all parameters are represented by percentage parameter: normed_mean_error_dict: a dictionary whose keys are the method name and values are (N, ) numpy array to represent error in evaluation error_threshold: threshold to display in x axis return: AUC: area under the curve MSE: mean square error ''' if debug: assert isdict( normed_mean_error_dict ), 'the input normalized mean error dictionary is not correct' assert islogical( normalized), 'the normalization flag should be logical' if normalized: assert error_threshold > 0 and error_threshold < 100, 'threshold percentage is not well set' if save: assert is_path_exists_or_creatable( pck_savepath ), 'please provide a valid path to save the pck results' assert is_path_exists_or_creatable( table_savepath ), 'please provide a valid path to save the table results' assert isstring(title), 'title is not correct' if truncated_list is not None: assert islistofscalar( truncated_list), 'the input truncated list is not correct' if display_list is not None: assert islist(display_list) and len( display_list) == len(normed_mean_error_dict ), 'the input display list is not correct' assert CHECK_EQ_LIST_UNORDERED( display_list, normed_mean_error_dict.keys(), debug=debug ), 'the input display list does not match the error dictionary key list' else: display_list = normed_mean_error_dict.keys() # set display parameters width, height = 1000, 800 legend_fontsize = 10 scale_distance = 48.8 line_index, color_index = 0, 0 figsize = width / float(dpi), height / float(dpi) fig = plt.figure(figsize=figsize) # set figure handle num_bins = 1000 if normalized: maximum_x = 1 scale = num_bins / 100 else: maximum_x = error_threshold + 1 scale = num_bins / maximum_x x_axis = np.linspace( 0, maximum_x, num_bins) # error axis, percentage of normalization factor y_axis = np.zeros(num_bins) interval_y = 10 interval_x = 1 plt.xlim(0, error_threshold) plt.ylim(0, 100) plt.yticks(np.arange(0, 100 + interval_y, interval_y)) plt.xticks(np.arange(0, error_threshold + interval_x, interval_x)) plt.grid() plt.title(title, fontsize=20) if normalized: plt.xlabel('Normalized error euclidean distance (%)', fontsize=16) else: plt.xlabel('Absolute error euclidean distance', fontsize=16) # calculate metrics for each method num_methods = len(normed_mean_error_dict) num_images = len(normed_mean_error_dict.values()[0]) metrics_dict = dict() metrics_table = list() table_title = ['Method Name / Metrics', 'AUC', 'MSE'] append2title = False assert num_images > 0, 'number of error array should be larger than 0' for ordered_index in range(num_methods): method_name = display_list[ordered_index] normed_mean_error = normed_mean_error_dict[method_name] if debug: assert isnparray( normed_mean_error ) and normed_mean_error.ndim == 1, 'shape of error distance is not good' assert len( normed_mean_error ) == num_images, 'number of testing images should be equal for all methods' assert len(linestyle_set) * len(color_set) >= len( normed_mean_error_dict) color_tmp = color_set[color_index] line_tmp = linestyle_set[line_index] for i in range(num_bins): y_axis[i] = float( (normed_mean_error < x_axis[i]).sum()) / num_images # percentage of error # calculate area under the curve and mean square error entry = dict() entry['AUC'] = np.sum(y_axis[:error_threshold * scale]) / ( error_threshold * scale) # bigger, better entry['MSE'] = np.mean(normed_mean_error) # smaller, better metrics_table_tmp = [ str(method_name), '%.2f' % (entry['AUC']), '%.1f' % (entry['MSE']) ] if truncated_list is not None: tmse_dict = calculate_truncated_mse(normed_mean_error.tolist(), truncated_list, debug=debug) for threshold in truncated_list: entry['AUC/%s' % threshold] = np.sum(y_axis[:error_threshold * scale]) / ( error_threshold * scale) # bigger, better entry['MSE/%s' % threshold] = tmse_dict[threshold]['T-MSE'] entry['percentage/%s' % threshold] = tmse_dict[threshold]['percentage'] if not append2title: table_title.append('AUC/%s' % threshold) table_title.append('MSE/%s' % threshold) table_title.append('pct/%s' % threshold) metrics_table_tmp.append('%.2f' % (entry['AUC/%s' % threshold])) metrics_table_tmp.append('%.1f' % (entry['MSE/%s' % threshold])) metrics_table_tmp.append( '%.1f' % (100 * entry['percentage/%s' % threshold]) + '%') # print metrics_table_tmp metrics_table.append(metrics_table_tmp) append2title = True metrics_dict[method_name] = entry # draw label = '%s, AUC: %.2f, MSE: %.1f (%.0f um)' % ( method_name, entry['AUC'], entry['MSE'], entry['MSE'] * scale_distance) if normalized: plt.plot(x_axis * 100, y_axis * 100, color=color_tmp, linestyle=line_tmp, label=label, lw=3) else: plt.plot(x_axis, y_axis * 100, color=color_tmp, linestyle=line_tmp, label=label, lw=3) plt.legend(loc=4, fontsize=legend_fontsize) color_index += 1 if color_index / len(color_set) == 1: line_index += 1 color_index = color_index % len(color_set) # plt.grid() plt.ylabel('{} Test Images (%)'.format(num_images), fontsize=16) save_vis_close_helper(fig=fig, ax=None, vis=vis, transparent=False, save_path=pck_savepath, debug=debug, closefig=closefig) # reorder the table order_index_list = [ display_list.index(method_name_tmp) for method_name_tmp in normed_mean_error_dict.keys() ] order_index_list = [0] + [ order_index_tmp + 1 for order_index_tmp in order_index_list ] # print table to terminal metrics_table = [table_title] + metrics_table # metrics_table = list_reorder([table_title] + metrics_table, order_index_list, debug=debug) table = AsciiTable(metrics_table) if display2terminal: print('\nprint detailed metrics') print(table.table) # save table to file if table_savepath is not None: table_file = open(table_savepath, 'w') table_file.write(table.table) table_file.close() if display2terminal: print('\nsave detailed metrics to %s' % table_savepath) return metrics_dict, metrics_table
def load_list_from_folder(folder_path, ext_filter=None, depth=1, recursive=False, sort=True, save_path=None, debug=True): ''' load a list of files or folders from a system path parameters: folder_path: root to search ext_filter: a string to represent the extension of files interested depth: maximum depth of folder to search, when it's None, all levels of folders will be searched recursive: False: only return current level True: return all levels till to the input depth outputs: fulllist: a list of elements num_elem: number of the elements ''' folder_path = safepath(folder_path) if debug: assert isfolder( folder_path), 'input folder path is not correct: %s' % folder_path if not is_path_exists(folder_path): return [], 0 if debug: assert islogical( recursive), 'recursive should be a logical variable: {}'.format( recursive) assert depth is None or ( isinteger(depth) and depth >= 1), 'input depth is not correct {}'.format(depth) assert ext_filter is None or (islist(ext_filter) and all( isstring(ext_tmp) for ext_tmp in ext_filter)) or isstring( ext_filter), 'extension filter is not correct' if isstring(ext_filter): # convert to a list ext_filter = [ext_filter] fulllist = list() if depth is None: # find all files recursively recursive = True wildcard_prefix = '**' if ext_filter is not None: for ext_tmp in ext_filter: wildcard = os.path.join(wildcard_prefix, '*' + string2ext_filter(ext_tmp)) curlist = glob2.glob(os.path.join(folder_path, wildcard)) if sort: curlist = sorted(curlist) fulllist += curlist else: wildcard = wildcard_prefix curlist = glob2.glob(os.path.join(folder_path, wildcard)) if sort: curlist = sorted(curlist) fulllist += curlist else: # find files based on depth and recursive flag wildcard_prefix = '*' for index in range(depth - 1): wildcard_prefix = os.path.join(wildcard_prefix, '*') if ext_filter is not None: for ext_tmp in ext_filter: wildcard = wildcard_prefix + string2ext_filter(ext_tmp) curlist = glob.glob(os.path.join(folder_path, wildcard)) if sort: curlist = sorted(curlist) fulllist += curlist else: wildcard = wildcard_prefix curlist = glob.glob(os.path.join(folder_path, wildcard)) if sort: curlist = sorted(curlist) fulllist += curlist if recursive and depth > 1: newlist, _ = load_list_from_folder(folder_path=folder_path, ext_filter=ext_filter, depth=depth - 1, recursive=True) fulllist += newlist fulllist = [os.path.normpath(path_tmp) for path_tmp in fulllist] num_elem = len(fulllist) # save list to a path if save_path is not None: save_path = safepath(save_path) if debug: assert is_path_exists_or_creatable( save_path), 'the file cannot be created' with open(save_path, 'w') as file: for item in fulllist: file.write('%s\n' % item) file.close() return fulllist, num_elem
def generate_hdf5(data_src, save_dir, data_name='data', batch_size=1, ext_filter='png', label_src1=None, label_name1='label', label_preprocess_function1=identity, label_range1=None, label_src2=None, label_name2='label2', label_preprocess_function2=identity, label_range2=None, debug=True, vis=False): ''' # this function creates data in hdf5 format from a image path # input parameter # data_src: source of image data, which can be a list of image path, a txt file contains a list of image path, a folder contains a set of images, a list of numpy array image data # label_src: source of label data, which can be none, a file contains a set of labels, a dictionary of labels, a 1-d numpy array data, a list of label data # save_dir: where to store the hdf5 data # batch_size: how many image to store in a single hdf file # ext_filder: what format of data to use for generating hdf5 data ''' # parse input assert is_path_exists_or_creatable( save_dir), 'save path should be a folder to save all hdf5 files' mkdir_if_missing(save_dir) assert isstring( data_name), 'dataset name is not correct' # name for hdf5 data # convert data source to a list of numpy array image data if isfolder(data_src): print 'data is loading from %s with extension .%s' % (data_src, ext_filter) filelist, num_data = load_list_from_folder(data_src, ext_filter=ext_filter) datalist = None elif isfile(data_src): print 'data is loading from %s with extension .%s' % (data_src, ext_filter) filelist, num_data = load_list_from_file(data_src) datalist = None elif islist(data_src): if debug: assert all( isimage(data_tmp) for data_tmp in data_src ), 'input data source is not a list of numpy array image data' datalist = data_src num_data = len(datalist) filelist = None else: assert False, 'data source format is not correct.' if debug: assert (datalist is None and filelist is not None) or ( filelist is None and datalist is not None), 'data is not correct' if datalist is not None: assert len(datalist) == num_data, 'number of data is not equal' if filelist is not None: assert len(filelist) == num_data, 'number of data is not equal' # convert label source to a list of numpy array label if label_src1 is None: labeldict1 = None labellist1 = None elif isfile(label_src1): assert is_path_exists(label_src1), 'file not found' _, _, ext = fileparts(label_src1) assert ext == '.json', 'only json extension is supported' labeldict1 = json.load(label_src1) num_label1 = len(labeldict1) assert num_data == num_label1, 'number of data and label is not equal.' labellist1 = None elif isdict(label_src1): labeldict1 = label_src1 labellist1 = None elif isnparray(label_src1): if debug: assert label_src1.ndim == 1, 'only 1-d label is supported' labeldict1 = None labellist1 = label_src1 elif islist(label_src1): if debug: assert all( np.array(label_tmp).size == 1 for label_tmp in label_src1), 'only 1-d label is supported' labellist1 = label_src1 labeldict1 = None else: assert False, 'label source format is not correct.' assert isfunction(label_preprocess_function1 ), 'label preprocess function is not correct.' # convert label source to a list of numpy array label if label_src2 is None: labeldict2 = None labellist2 = None elif isfile(label_src2): assert is_path_exists(label_src2), 'file not found' _, _, ext = fileparts(label_src2) assert ext == '.json', 'only json extension is supported' labeldict2 = json.load(label_src2) num_label2 = len(labeldict2) assert num_data == num_label2, 'number of data and label is not equal.' labellist2 = None elif isdict(label_src2): labeldict2 = label_src2 labellist2 = None elif isnparray(label_src2): if debug: assert label_src2.ndim == 1, 'only 1-d label is supported' labeldict2 = None labellist2 = label_src2 elif islist(label_src2): if debug: assert all( np.array(label_tmp).size == 1 for label_tmp in label_src2), 'only 1-d label is supported' labellist2 = label_src2 labeldict2 = None else: assert False, 'label source format is not correct.' assert isfunction(label_preprocess_function2 ), 'label preprocess function is not correct.' # warm up if datalist is not None: size_data = datalist[0].shape else: size_data = imread(filelist[0]).shape if labeldict1 is not None: if debug: assert isstring(label_name1), 'label name is not correct' labels1 = np.zeros((batch_size, 1), dtype='float32') # label_value1 = [float(label_tmp_char) for label_tmp_char in labeldict1.values()] # label_range1 = np.array([min(label_value1), max(label_value1)]) if labellist1 is not None: labels1 = np.zeros((batch_size, 1), dtype='float32') # label_range1 = [np.min(labellist1), np.max(labellist1)] if label_src1 is not None and debug: assert label_range1 is not None, 'label range is not correct' assert (labeldict1 is not None and labellist1 is None) or ( labellist1 is not None and labeldict1 is None), 'label is not correct' if labeldict2 is not None: if debug: assert isstring(label_name2), 'label name is not correct' labels2 = np.zeros((batch_size, 1), dtype='float32') # label_value2 = [float(label_tmp_char) for label_tmp_char in labeldict2.values()] # label_range2 = np.array([min(label_value2), max(label_value2)]) if labellist2 is not None: labels2 = np.zeros((batch_size, 1), dtype='float32') # label_range2 = [np.min(labellist2), np.max(labellist2)] if label_src2 is not None and debug: assert label_range2 is not None, 'label range is not correct' assert (labeldict2 is not None and labellist2 is None) or ( labellist2 is not None and labeldict2 is None), 'label is not correct' # start generating count_hdf = 1 # count number of hdf5 file clock = Timer() datalist_batch = list() for i in xrange(num_data): clock.tic() if filelist is not None: imagefile = filelist[i] _, name, _ = fileparts(imagefile) img = imread(imagefile).astype('float32') max_value = np.max(img) if max_value > 1 and max_value <= 255: img = img / 255.0 # [rows,col,channel,numbers], scale the image data to (0, 1) if debug: min_value = np.min(img) assert min_value >= 0 and min_value <= 1, 'data is not in [0, 1]' if datalist is not None: img = datalist[i] if debug: assert size_data == img.shape datalist_batch.append(img) # process label if labeldict1 is not None: if debug: assert len(filelist) == len( labeldict1), 'file list is not equal to label dictionary' labels1[i % batch_size, 0] = float(labeldict1[name]) if labellist1 is not None: labels1[i % batch_size, 0] = float(labellist1[i]) if labeldict2 is not None: if debug: assert len(filelist) == len( labeldict2), 'file list is not equal to label dictionary' labels2[i % batch_size, 0] = float(labeldict2[name]) if labellist2 is not None: labels2[i % batch_size, 0] = float(labellist2[i]) # save to hdf5 if i % batch_size == 0: data = preprocess_image_caffe( datalist_batch, debug=debug, vis=vis ) # swap channel, transfer from list of HxWxC to NxCxHxW # write to hdf5 format if filelist is not None: save_path = os.path.join(save_dir, '%s.hdf5' % name) else: save_path = os.path.join(save_dir, 'image_%010d.hdf5' % count_hdf) h5f = h5py.File(save_path, 'w') h5f.create_dataset(data_name, data=data, dtype='float32') if (labeldict1 is not None) or (labellist1 is not None): # print(labels1) labels1 = label_preprocess_function1(data=labels1, data_range=label_range1, debug=debug) # print(labels1) h5f.create_dataset(label_name1, data=labels1, dtype='float32') labels1 = np.zeros((batch_size, 1), dtype='float32') if (labeldict2 is not None) or (labellist2 is not None): labels2 = label_preprocess_function2(data=labels2, data_range=label_range2, debug=debug) h5f.create_dataset(label_name2, data=labels2, dtype='float32') labels2 = np.zeros((batch_size, 1), dtype='float32') h5f.close() count_hdf = count_hdf + 1 del datalist_batch[:] if debug: assert len(datalist_batch) == 0, 'list has not been cleared' average_time = clock.toc() print( 'saving to %s: %d/%d, average time:%.3f, elapsed time:%s, estimated time remaining:%s' % (save_path, i + 1, num_data, average_time, convert_secs2time(average_time * i), convert_secs2time(average_time * (num_data - i)))) return count_hdf - 1, num_data
def visualize_pts(pts, title=None, fig=None, ax=None, display_range=False, xlim=[-100, 100], ylim=[-100, 100], display_list=None, covariance=False, mse=False, mse_value=None, vis=True, save_path=None, debug=True, closefig=True): ''' visualize point scatter plot parameter: pts: 2 x num_pts numpy array or a dictionary containing 2 x num_pts numpy array ''' if debug: if isdict(pts): for pts_tmp in pts.values(): assert is2dptsarray( pts_tmp ), 'input points within dictionary are not correct: (2, num_pts) vs %s' % print_np_shape( pts_tmp) if display_list is not None: assert islist(display_list) and len(display_list) == len( pts), 'the input display list is not correct' assert CHECK_EQ_LIST_UNORDERED( display_list, pts.keys(), debug=debug ), 'the input display list does not match the points key list' else: display_list = pts.keys() else: assert is2dptsarray( pts ), 'input points are not correct: (2, num_pts) vs %s' % print_np_shape( pts) if title is not None: assert isstring(title), 'title is not correct' else: title = 'Point Error Vector Distribution Map' assert islogical( display_range ), 'the flag determine if to display in a specific range should be logical value' if display_range: assert islist(xlim) and islist(ylim) and len(xlim) == 2 and len( ylim) == 2, 'the input range for x and y is not correct' assert xlim[1] > xlim[0] and ylim[1] > ylim[ 0], 'the input range for x and y is not correct' # figure setting width, height = 1024, 1024 fig, _ = get_fig_ax_helper(fig=fig, ax=ax, width=width, height=height) if ax is None: plt.title(title, fontsize=20) if isdict(pts): num_pts_all = pts.values()[0].shape[1] if all(pts_tmp.shape[1] == num_pts_all for pts_tmp in pts.values()): plt.xlabel('x coordinate (%d points)' % pts.values()[0].shape[1], fontsize=16) plt.ylabel('y coordinate (%d points)' % pts.values()[0].shape[1], fontsize=16) else: print('number of points is different across different methods') plt.xlabel('x coordinate', fontsize=16) plt.ylabel('y coordinate', fontsize=16) else: plt.xlabel('x coordinate (%d points)' % pts.shape[1], fontsize=16) plt.ylabel('y coordinate (%d points)' % pts.shape[1], fontsize=16) plt.axis('equal') ax = plt.gca() ax.grid() # internal parameters pts_size = 5 std = None conf = 0.98 color_index = 0 marker_index = 0 hatch_index = 0 alpha = 0.2 legend_fontsize = 10 scale_distance = 48.8 linewidth = 2 # plot points handle_dict = dict() # for legend if isdict(pts): num_methods = len(pts) assert len(color_set) * len(marker_set) >= num_methods and len( color_set ) * len( hatch_set ) >= num_methods, 'color in color set is not enough to use, please use different markers' mse_return = dict() for method_name, pts_tmp in pts.items(): color_tmp = color_set[color_index] marker_tmp = marker_set[marker_index] hatch_tmp = hatch_set[hatch_index] # plot covariance ellipse if covariance: _, covariance_number = visualize_pts_covariance( pts_tmp[0:2, :], std=std, conf=conf, ax=ax, debug=debug, color=color_tmp, hatch=hatch_tmp, linewidth=linewidth) handle_tmp = ax.scatter(pts_tmp[0, :], pts_tmp[1, :], color=color_tmp, marker=marker_tmp, s=pts_size, alpha=alpha) if mse: if mse_value is None: num_pts = pts_tmp.shape[1] mse_tmp, _ = pts_euclidean(pts_tmp[0:2, :], np.zeros((2, num_pts), dtype='float32'), debug=debug) else: mse_tmp = mse_value[method_name] display_string = '%s, MSE: %.1f (%.1f um), Covariance: %.1f' % ( method_name, mse_tmp, mse_tmp * scale_distance, covariance_number) mse_return[method_name] = mse_tmp else: display_string = method_name handle_dict[display_string] = handle_tmp color_index += 1 if color_index / len(color_set) == 1: marker_index += 1 hatch_index += 1 color_index = color_index % len(color_set) # reorder the handle before plot handle_key_list = handle_dict.keys() handle_value_list = handle_dict.values() order_index_list = [ display_list.index(method_name_tmp.split(', ')[0]) for method_name_tmp in handle_dict.keys() ] ordered_handle_key_list = list_reorder(handle_key_list, order_index_list, debug=debug) ordered_handle_value_list = list_reorder(handle_value_list, order_index_list, debug=debug) plt.legend(list2tuple(ordered_handle_value_list), list2tuple(ordered_handle_key_list), scatterpoints=1, markerscale=4, loc='lower left', fontsize=legend_fontsize) else: color_tmp = color_set[color_index] marker_tmp = marker_set[marker_index] hatch_tmp = hatch_set[hatch_index] handle_tmp = ax.scatter(pts[0, :], pts[1, :], color=color_tmp, marker=marker_tmp, s=pts_size, alpha=alpha) # plot covariance ellipse if covariance: _, covariance_number = visualize_pts_covariance( pts[0:2, :], std=std, conf=conf, ax=ax, debug=debug, color=color_tmp, hatch=hatch_tmp, linewidth=linewidth) if mse: if mse_value is None: num_pts = pts.shape[1] mse_tmp, _ = pts_euclidean(pts[0:2, :], np.zeros((2, num_pts), dtype='float32'), debug=debug) display_string = 'MSE: %.1f (%.1f um), Covariance: %.1f' % ( mse_tmp, mse_tmp * scale_distance, covariance_number) mse_return = mse_tmp else: display_string = 'MSE: %.1f (%.1f um), Covariance: %.1f' % ( mse_value, mse_value * scale_distance, covariance_number) mse_return = mse_value handle_dict[display_string] = handle_tmp plt.legend(list2tuple(handle_dict.values()), list2tuple(handle_dict.keys()), scatterpoints=1, markerscale=4, loc='lower left', fontsize=legend_fontsize) # display only specific range if display_range: axis_bin = 10 * 2 interval_x = (xlim[1] - xlim[0]) / axis_bin interval_y = (ylim[1] - ylim[0]) / axis_bin plt.xlim(xlim[0], xlim[1]) plt.ylim(ylim[0], ylim[1]) plt.xticks(np.arange(xlim[0], xlim[1] + interval_x, interval_x)) plt.yticks(np.arange(ylim[0], ylim[1] + interval_y, interval_y)) plt.grid() save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, warning=warning, debug=debug, closefig=closefig, transparent=False) return mse_return
def visualize_bbox(input_bbox, linewidth=0.5, edge_color_index=15, scores=None, threshold=0.0, textsize=8, fig=None, ax=None, save_path=None, vis=False, warning=True, debug=True, closefig=True): ''' visualize a set of bounding box parameters: input_bbox: a list of 4 elements, a listoflist of 4 elements: e.g., [[1,2,3,4], [5,6,7,8]], a numpy array with shape or (N, 4) or (4, ) TLBR format scores: a list of floating numbers representing the confidences ''' if islist(input_bbox) and len(input_bbox) == 0: return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, warning=warning, debug=debug, closefig=closefig) elif isnparray(input_bbox) and input_bbox.size == 0: return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, warning=warning, debug=debug, closefig=closefig) np_bboxes = safe_bbox(input_bbox, warning=warning, debug=debug) if debug: assert bboxcheck_TLBR( np_bboxes, warning=warning, debug=debug), 'input bounding boxes are not correct' edge_color = color_set_big[edge_color_index % len(color_set_big)] np_bboxes = bbox_TLBR2TLWH( np_bboxes, warning=warning, debug=debug) # convert TLBR format to TLWH format for bbox_index in range(np_bboxes.shape[0]): bbox_tmp = np_bboxes[bbox_index, :] if scores is not None: score = float(scores[bbox_index]) if score < threshold: continue caption = '{:.2f}'.format(score) # score = str(scores[bbox_index]) # caption = '%s' % (score) ax.text(bbox_tmp[0], bbox_tmp[1] + textsize, caption, color='r', size=textsize, backgroundcolor='none') ax.add_patch( plt.Rectangle((bbox_tmp[0], bbox_tmp[1]), bbox_tmp[2], bbox_tmp[3], fill=False, edgecolor=edge_color, linewidth=linewidth)) return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, warning=warning, debug=debug, closefig=closefig)
def visualize_pts_line(pts_array, line_index_list, method=2, seed=0, alpha=0.5, vis_threshold=0.3, pts_size=20, line_size=10, line_color_index=0, fig=None, ax=None, save_path=None, vis=False, warning=True, debug=True, closefig=True): ''' given a list of index, and a point array, to plot a set of points with line on it parameters: pts_array: 2(3) x num_pts line_index_list: a list of index method: 1: all points are connected, if some points are missing in the middle, just ignore that point and connect the two nearby points 2: if some points are missing in the middle of a line, the line is decomposed to sub-lines vis_threshold: confidence to draw the points ''' if debug: assert is2dptsarray(pts_array) or is2dptsarray_occlusion( pts_array) or is2dptsarray_confidence( pts_array), 'input points are not correct' assert islist(line_index_list), 'the list of index is not correct' assert method in [1, 2], 'the plot method is not correct' num_pts = pts_array.shape[1] # expand the pts_array to 3 rows if the confidence row is not provided if pts_array.shape[0] == 2: pts_array = np.vstack((pts_array, np.ones((1, num_pts)))) fig, ax = get_fig_ax_helper(fig=fig, ax=ax) np.random.seed(seed) color_option = 'hsv' if color_option == 'rgb': color_set_random = np.random.rand(3, num_pts) elif color_option == 'hsv': h_random = np.random.rand(num_pts, ) color_set_random = np.zeros((3, num_pts), dtype='float32') for pts_index in range(num_pts): color_set_random[:, pts_index] = colorsys.hsv_to_rgb( h_random[pts_index], 1, 1) line_color = color_set[line_color_index] pts_line = pts_array[:, line_index_list] if method == 1: valid_pts_list = np.where(pts_line[2, :] > vis_threshold)[0].tolist() pts_line_tmp = pts_line[:, valid_pts_list] ax.plot(pts_line_tmp[0, :], pts_line_tmp[1, :], lw=line_size, color=line_color, alpha=alpha) # plot all lines # plot all points for pts_index in valid_pts_list: pts_index_original = line_index_list[pts_index] # ax.plot(pts_array[0, pts_index_original], pts_array[1, pts_index_original], 'o', color=color_set_big[pts_index_original % len(color_set_big)], alpha=alpha) ax.plot(pts_array[0, pts_index_original], pts_array[1, pts_index_original], marker='o', ms=pts_size, lw=line_size, color=color_set_random[:, pts_index], alpha=alpha) else: not_valid_pts_list = np.where( pts_line[2, :] < vis_threshold)[0].tolist() if len(not_valid_pts_list) == 0: # all valid ax.plot(pts_line[0, :], pts_line[1, :], lw=line_size, color=line_color, alpha=alpha) # plot points for pts_index in line_index_list: # ax.plot(pts_array[0, pts_index], pts_array[1, pts_index], 'o', color=color_set_big[pts_index % len(color_set_big)], alpha=alpha) ax.plot(pts_array[0, pts_index], pts_array[1, pts_index], marker='o', ms=pts_size, lw=line_size, color=color_set_random[:, pts_index], alpha=alpha) else: prev_index = 0 for not_valid_index in not_valid_pts_list: plot_list = range(prev_index, not_valid_index) pts_line_tmp = pts_line[:, plot_list] ax.plot(pts_line_tmp[0, :], pts_line_tmp[1, :], lw=line_size, color=line_color, alpha=alpha) # plot points for pts_index in plot_list: pts_index_original = line_index_list[pts_index] ax.plot(pts_array[0, pts_index_original], pts_array[1, pts_index_original], marker='o', ms=pts_size, lw=line_size, color=color_set_random[:, pts_index_original], alpha=alpha) # ax.plot(pts_array[0, pts_index_original], pts_array[1, pts_index_original], 'o', color=color_set_big[pts_index_original % len(color_set_big)], alpha=alpha) prev_index = not_valid_index + 1 pts_line_tmp = pts_line[:, prev_index:] ax.plot(pts_line_tmp[0, :], pts_line_tmp[1, :], lw=line_size, color=line_color, alpha=alpha) # plot last line # plot last points for pts_index in range(prev_index, pts_line.shape[1]): pts_index_original = line_index_list[pts_index] # ax.plot(pts_array[0, pts_index_original], pts_array[1, pts_index_original], 'o', color=color_set_big[pts_index_original % len(color_set_big)], alpha=alpha) ax.plot(pts_array[0, pts_index_original], pts_array[1, pts_index_original], marker='o', ms=pts_size, lw=line_size, color=color_set_random[:, pts_index_original], alpha=alpha) return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, warning=warning, debug=debug, closefig=closefig)
def visualize_pts_array(input_pts, color_index=0, pts_size=20, label=False, label_list=None, label_size=20, vis_threshold=0.3, covariance=False, plot_occl=False, xlim=None, ylim=None, fig=None, ax=None, save_path=None, vis=False, warning=True, debug=True, closefig=True): ''' plot keypoints with covariance ellipse parameters: pts_array: 2(3) x num_pts numpy array, the third channel could be confidence or occlusion ''' # obtain the points try: pts_array = safe_2dptsarray(input_pts, homogeneous=True, warning=warning, debug=debug) except AssertionError: pts_array = safe_2dptsarray(input_pts, homogeneous=False, warning=warning, debug=debug) if debug: assert is2dptsarray(pts_array) or is2dptsarray_occlusion( pts_array) or is2dptsarray_confidence( pts_array), 'input points are not correct' num_pts = pts_array.shape[1] # obtain a label list if required but not provided if debug: assert islogical(label), 'label flag is not correct' if label and (label_list is None): label_list = [str(i) for i in xrange(num_pts)] if label_list is not None and debug: assert islistofstring(label_list), 'labels are not correct' # obtain the color index if islist(color_index): if debug: assert not ( plot_occl or covariance ), 'the occlusion or covariance are not compatible with plotting different colors during scattering' color_tmp = [color_set_big[index_tmp] for index_tmp in color_index] else: color_tmp = color_set_big[color_index % len(color_set_big)] fig, ax = get_fig_ax_helper(fig=fig, ax=ax) std, conf = None, 0.95 if is2dptsarray(pts_array): # only 2d points without third rows if debug and islist(color_tmp): assert len( color_tmp ) == num_pts, 'number of points to plot is not equal to number of colors provided' ax.scatter(pts_array[0, :], pts_array[1, :], color=color_tmp, s=pts_size) pts_visible_index = range(pts_array.shape[1]) pts_ignore_index = [] pts_invisible_index = [] else: # automatically justify if the third row is confidence or occlusion flag num_float_elements = np.where( np.logical_and( pts_array[2, :] != -1, np.logical_and(pts_array[2, :] != 0, pts_array[2, :] != 1)))[0].tolist() if len(num_float_elements) > 0: type_3row = 'conf' else: type_3row = 'occu' if type_3row == 'occu': pts_visible_index = np.where(pts_array[ 2, :] == 1)[0].tolist() # plot visible points in red color pts_ignore_index = np.where(pts_array[2, :] == -1)[0].tolist( ) # do not plot points with annotation, usually visible, but not annotated pts_invisible_index = np.where(pts_array[ 2, :] == 0)[0].tolist() # plot invisible points in blue color else: pts_visible_index = np.where( pts_array[2, :] > vis_threshold)[0].tolist() pts_invisible_index = np.where( pts_array[2, :] <= vis_threshold)[0].tolist() pts_ignore_index = [] if debug and islist(color_tmp): assert len(color_tmp) == len( pts_visible_index ), 'number of points to plot is not equal to number of colors provided' ax.scatter(pts_array[0, pts_visible_index], pts_array[1, pts_visible_index], color=color_tmp, s=pts_size) if plot_occl: ax.scatter(pts_array[0, pts_invisible_index], pts_array[1, pts_invisible_index], color=color_set_big[(color_index + 1) % len(color_set_big)], s=pts_size) if covariance: visualize_pts_covariance(pts_array[0:2, :], std=std, conf=conf, fig=fig, ax=ax, debug=debug, color=color_tmp) if plot_occl: not_plot_index = pts_ignore_index else: not_plot_index = pts_ignore_index + pts_invisible_index if label_list is not None: for pts_index in xrange(num_pts): label_tmp = label_list[pts_index] if pts_index in not_plot_index: continue else: # note that the annotation is based on the coordinate instead of the order of plotting the points, so the orider in pts_index does not matter if islist(color_index): plt.annotate( label_tmp, xy=(pts_array[0, pts_index], pts_array[1, pts_index]), xytext=(-1, 1), color=color_set_big[(color_index[pts_index] + 5) % len(color_set_big)], textcoords='offset points', ha='right', va='bottom', fontsize=label_size) else: plt.annotate(label_tmp, xy=(pts_array[0, pts_index], pts_array[1, pts_index]), xytext=(-1, 1), color=color_set_big[(color_index + 5) % len(color_set_big)], textcoords='offset points', ha='right', va='bottom', fontsize=label_size) # set axis if xlim is not None: if debug: assert islist(xlim) and len(xlim) == 2, 'the x lim is not correct' plt.xlim(xlim[0], xlim[1]) if ylim is not None: if debug: assert islist(ylim) and len(ylim) == 2, 'the y lim is not correct' plt.ylim(ylim[0], ylim[1]) return save_vis_close_helper(fig=fig, ax=ax, vis=vis, save_path=save_path, warning=warning, debug=debug, closefig=closefig, transparent=False)