def main(args): query_files = sorted(glob.glob(os.path.join(args.queries_path, '*.jpg'))) image_files = sorted(glob.glob(os.path.join(args.images_path, '*.jpg'))) if args.mode == 'eval': with open(args.corresp_file, 'rb') as f: query_gt = dict(pickle.load(f)) keypoint_methods = ['sift'] descriptor_methods = ['sift'] match_methods = ['brute_force'] distance_metrics = ['l1'] for keypoint_method, descriptor_method, match_method, distance_metric in product( keypoint_methods, descriptor_methods, match_methods, distance_metrics): results = query_batch(query_files, image_files, keypoint_method, descriptor_method, match_method, distance_metric) if args.mode == 'eval': actual = [] predicted = [] for query_file, result in zip(query_files, results): actual.append(query_gt[_filename_to_id(query_file)]) image_files, scores = zip(*result) if scores[0] == 0: predicted.append([-1]) else: predicted.append([ _filename_to_id(image_file) for image_file in image_files ]) print('{}: {}'.format(_filename_to_id(query_file), [(_filename_to_id(image_file), score) for image_file, score in result][:5])) print('MAP@{}: {}'.format(10, mapk(actual, predicted, 10))) print('MAP@{}: {}'.format(5, mapk(actual, predicted, 5))) print('MAP@{}: {}'.format(3, mapk(actual, predicted, 3))) print('MAP@{}: {}'.format(1, mapk(actual, predicted, 1))) elif args.mode == 'test': predicted = [] for query_file, result in zip(query_files, results): image_files, scores = zip(*result) if scores[0] == 0: predicted.append([-1]) else: predicted.append([ _filename_to_id(image_file) for image_file in image_files ]) _save_results(predicted, args.results_path, method='{}_{}_{}'.format(keypoint_method, descriptor_method, distance_metric)) else: raise ValueError('Invalid mode.')
def test_mapk(): # Single entry np.testing.assert_almost_equal([mapk([1], [[2, 3, 1, 1]], 'full')[0]], [0.41666666]) # Multiple entries np.testing.assert_almost_equal( [mapk([1, 1], [[2, 3, 1, 1], [1, 1]], 'full')[0]], [0.70833333])
def main(args): query_files = sorted(glob.glob(args.queries_path)) image_files = sorted(glob.glob(args.images_path)) if args.mode == 'eval': with open(args.corresp_file, 'rb') as f: query_gt = pickle.load(f) color_methods = [ 'rgb_histogram', 'hsv_histogram', 'lab_histogram', 'ycrcb_histogram', 'cld', 'rgb_histogram_pyramid', 'hsv_histogram_pyramid', 'lab_histogram_pyramid', 'ycrcb_histogram_pyramid' ] texture_methods = [None, 'gabor', 'glcm'] dist_metrics = ['euclidean_distance', 'l1_distance', 'cosine_distance'] hist_metrics = [ 'intersection', 'correlation', 'chi_square', 'hellinguer_distance', 'bhattacharya_distance' ] for texture_method, color_method, metric in product( texture_methods, color_methods, dist_metrics + hist_metrics): print('({}, {}, {})'.format(color_method, texture_method, metric)) with Timer('query_batch'): results = query_batch(query_files, image_files, color_method, metric, texture_method) if args.mode == 'eval': actual = [] predicted = [] for query_file, result in zip(query_files, results): actual.append([query_gt[_filename_to_id(query_file)]]) predicted.append([ _filename_to_id(image_file) for image_file, dist in result ]) print('MAP@K: {}'.format(mapk(actual, predicted))) elif args.mode == 'test': predicted = [] for query_file, result in zip(query_files, results): predicted.append([ _filename_to_id(image_file) for image_file, dist in result ]) _save_results(predicted, args.results_path, method='{}_{}_{}'.format(color_method, texture_method, metric)) else: raise ValueError('Invalid mode.')
text_list = None if args.text: with open(os.path.join(query_path, "text_boxes.pkl"), 'rb') as file: text_list = pickle.load(file) result_list_of_lists = search_batch(query_list=query_list, mask_list=mask_list, descriptor=args.descriptor, metric=args.metric, bins=args.bins, k=k, multiple=args.multiple, text_list=text_list) map_k = mapk(ground_truth, result_list_of_lists, k=k) print(f'map@{k} for the current run is {map_k}') if k > 1: map_1 = mapk(ground_truth, result_list_of_lists, k=1) print(f'map@{1} for the current run is {map_1}') # extra functions for pickling and visualizing results # use -p in cmdline args if args.pickle: print(f'writing this list to results.pkl \n {result_list_of_lists}') with open('result.pkl', 'wb') as f: pickle.dump(result_list_of_lists, f) # use -v in cmdline args if args.plot: query_index = np.random.randint(0, 30)