Best Python code snippet using lemoncheesecake
evaluator.py
Source:evaluator.py
1import numpy as np2from sklearn.metrics import recall_score, precision_score, f1_score3from dataloader import Dataloader4from distances import calculate_distances5from feature_extractor import compute_histogram6from metrics import mapk, averge_masks_metrics7from opt import parse_args8from utils import save_mask, load_pickle, save_predictions, detect_bboxes, detect_paintings, mkdir, transform_color, \9 remove_bg10import os11from denoise import detect_denoise12def calc_FV(image, opt, mask=None):13 return compute_histogram(opt.histogram, image, splits=opt.mr_splits, rec_level=opt.pyramid_rec_lvl, bins=opt.bins,14 mask=mask,15 sqrt=opt.sqrt,16 concat=opt.concat)17def eval_set(loader, gt_correspondences, bbdd_fvs, opt):18 masks_metrics = {"precision": [], "recall": [], "f1": []}19 ious = []20 predictions = []21 set_bboxes = []22 for name, query_image, gt_mask in loader:23 if opt.apply_denoise:24 query_image, Noise_level_before, Noise_level_after, blur_type_last = detect_denoise(query_image, opt.blur_type)25 # transform to another color space26 multiple_painting, split_point, bg_mask = detect_paintings(query_image)27 bboxes, bbox_mask = detect_bboxes(query_image)28 res_mask = bg_mask.astype(bool) ^ bbox_mask.astype(bool) if loader.detect_bboxes else bg_mask29 if loader.compute_masks:30 if loader.evaluate:31 calc_mask_metrics(masks_metrics, gt_mask / 255, bg_mask)32 if opt.save:33 mask_name = name.split("/")[-1].replace(".jpg", ".png")34 save_mask(os.path.join(opt.output, loader.root.split("/")[-1], mask_name), res_mask * 255)35 # cropped sets, no need to mask image for retrieval36 if gt_mask is None:37 res_mask = None38 if loader.detect_bboxes:39 set_bboxes.append(bboxes)40 # change colorspace before computing feature vector41 query_image = transform_color(query_image, opt.color) if opt.color is not None else query_image42 if multiple_painting and gt_mask is not None:43 im_preds = []44 left_paint = np.zeros_like(res_mask)45 right_paint = np.zeros_like(res_mask)46 left_paint[:, split_point:] = res_mask[:, split_point:]47 right_paint[:, :split_point] = res_mask[:, :split_point]48 res_masks = [left_paint, right_paint]49 for submasks in res_masks:50 query_fv = calc_FV(query_image, opt, submasks).ravel()51 distances = calculate_distances(bbdd_fvs, query_fv, mode=opt.dist)52 im_preds.append((distances.argsort()[:10]).tolist())53 predictions.append(im_preds)54 else:55 query_fv = calc_FV(query_image, opt, res_mask).ravel()56 distances = calculate_distances(bbdd_fvs, query_fv, mode=opt.dist)57 predictions.append((distances.argsort()[:10]).tolist())58 if opt.save:59 save_predictions("{}/{}/result.pkl".format(opt.output, loader.root.split("/")[-1]), predictions)60 save_predictions("{}/{}/text_boxes.pkl".format(opt.output, loader.root.split("/")[-1]), set_bboxes)61 map_k = {i: mapk(gt_correspondences, predictions, k=i) for i in [10, 3, 1]} if loader.evaluate else None62 avg_mask_metrics = averge_masks_metrics(masks_metrics) if loader.evaluate else None63 return map_k, avg_mask_metrics64def calc_mask_metrics(out_dict, gt_mask, pred_mask):65 gt_mask = gt_mask.astype("uint8").ravel()66 pred_mask = pred_mask.astype("uint8").ravel()67 # TODO check what happen with some masks68 if pred_mask.max() != 1:69 pred_mask = (pred_mask / 255).astype("uint8")70 out_dict["recall"].append(recall_score(gt_mask, pred_mask))71 out_dict["precision"].append(precision_score(gt_mask, pred_mask))72 out_dict["f1"].append(f1_score(gt_mask, pred_mask))73if __name__ == '__main__':74 opt = parse_args()75 opt.histogram = "multiresolution"76 opt.dist = "intersection"77 opt.mr_splits = [5, 5]78 opt.color = "RGB"79 opt.bins = 25680 opt.concat = True81 opt.save = True82 os.chdir("..")83 mkdir(opt.output)84 log = os.path.join(opt.output, "log.txt")85 log_file = open(log, "a")86 print(opt, file=log_file)87 train = Dataloader("data/bbdd")88 '''89 test_1_1 = Dataloader("data/qsd1_w1", evaluate=True)90 gt_1_1 = load_pickle("data/qsd1_w1/gt_corresps.pkl")91 mkdir(os.path.join(opt.output, test_1_1.root.split("/")[-1]))92 test_2_1 = Dataloader("data/qsd2_w1", compute_masks=True, evaluate=True)93 gt_2_1 = load_pickle("data/qsd2_w1/gt_corresps.pkl")94 mkdir(os.path.join(opt.output, test_2_1.root.split("/")[-1]))95 test_1_2 = Dataloader("data/qsd1_w2", detect_bboxes=True, evaluate=True)96 gt_1_2 = load_pickle("data/qsd1_w2/gt_corresps.pkl")97 mkdir(os.path.join(opt.output, test_1_2.root.split("/")[-1]))98 99 test_2_2 = Dataloader("data/qsd2_w2", compute_masks=True, detect_bboxes=True, evaluate=True)100 gt_2_2 = load_pickle("data/qsd2_w2/gt_corresps.pkl")101 mkdir(os.path.join(opt.output, test_2_2.root.split("/")[-1]))102 103 testset_1_2 = Dataloader("data/qst1_w2", detect_bboxes=True)104 mkdir(os.path.join(opt.output, testset_1_2.root.split("/")[-1]))105 testset_2_2 = Dataloader("data/qst2_w2", compute_masks=True, detect_bboxes=True)106 mkdir(os.path.join(opt.output, testset_2_2.root.split("/")[-1]))107 '''108 bbdd_matrix = np.array(109 [calc_FV(110 transform_color(image, opt.color) if opt.color is not None else image, opt).ravel()111 for _, image, _ in train112 ])113 print("Train sample loaded", bbdd_matrix.shape)114 '''115 print(test_1_1.root, file=log_file)116 result = eval_set(test_1_1, gt_1_1, bbdd_matrix, opt)117 print(result, file=log_file)118 print(test_2_1.root, file=log_file)119 result = eval_set(test_2_1, gt_2_1, bbdd_matrix, opt)120 print(result, file=log_file)121 print(test_1_2.root, file=log_file)122 print(eval_set(test_1_2, gt_1_2, bbdd_matrix, opt), file=log_file)123 124 print(test_2_2.root, file=log_file)125 result = eval_set(test_2_2, gt_2_2, bbdd_matrix, opt)126 print(result)127 print(result, file=log_file)128 129 print(testset_1_2.root, file=log_file)130 print(eval_set(testset_1_2, None, bbdd_matrix, opt), file=log_file)131 print(testset_2_2.root, file=log_file)132 print(eval_set(testset_2_2, None, bbdd_matrix, opt), file=log_file)133 '''134 '''135 test_2_2 = Dataloader("data/qsd2_w2", compute_masks=True, detect_bboxes=True, evaluate=True)136 gt_2_2 = load_pickle("data/qsd2_w2/gt_corresps.pkl")137 mkdir(os.path.join(opt.output, test_2_2.root.split("/")[-1]))138 print(test_2_2.root, file=log_file)139 result = eval_set(test_2_2, gt_2_2, bbdd_matrix, opt)140 print(result)141 print(result, file=log_file)142 '''143 # test_1_3 = Dataloader("data/qsd1_w3", detect_bboxes=True, evaluate=True)144 # gt_1_3 = load_pickle("data/qsd1_w3/gt_corresps.pkl")145 # mkdir(os.path.join(opt.output, test_1_3.root.split("/")[-1]))146 test_2_3 = Dataloader("data/qsd1_w4", compute_masks=True, detect_bboxes=True, evaluate=True)147 gt_2_3 = load_pickle("data/qsd1_w4/gt_corresps.pkl")148 mkdir(os.path.join(opt.output, test_2_3.root.split("/")[-1]))149 opt.apply_denoise = False150 #for blur_type in ["GaussianBlur", "medianBlur", "bilateralFilter", "blur", "best"]:151 for blur_type in ["best"]:152 opt.blur_type = blur_type153 print(opt, file=log_file)154 # print(test_1_3.root, file=log_file)155 # result = eval_set(test_1_3, gt_1_3, bbdd_matrix, opt)156 # print(result)157 # print(result, file=log_file)158 print(test_2_3.root, file=log_file)159 result = eval_set(test_2_3, gt_2_3, bbdd_matrix, opt)160 print(result)...
test_rename.py
Source:test_rename.py
1#!/usr/bin/env python2# -*- coding: utf8 -*-3import warnings4from agate import Table5from agate.testcase import AgateTestCase6from agate.data_types import *7class TestRename(AgateTestCase):8 def setUp(self):9 self.rows = (10 (1, 4, 'a'),11 (2, 3, 'b'),12 (None, 2, 'c')13 )14 self.number_type = Number()15 self.text_type = Text()16 self.column_names = ['one', 'two', 'three']17 self.column_types = [self.number_type, self.number_type, self.text_type]18 def test_rename_row_names(self):19 table = Table(self.rows, self.column_names, self.column_types)20 table2 = table.rename(row_names=['a', 'b', 'c'])21 self.assertSequenceEqual(table2.row_names, ['a', 'b', 'c'])22 self.assertSequenceEqual(table2.column_names, self.column_names)23 self.assertIs(table.row_names, None)24 self.assertSequenceEqual(table.column_names, self.column_names)25 def test_rename_row_names_dict(self):26 table = Table(self.rows, self.column_names, self.column_types, row_names=['a', 'b', 'c'])27 table2 = table.rename(row_names={'b': 'd'})28 self.assertSequenceEqual(table2.row_names, ['a', 'd', 'c'])29 self.assertSequenceEqual(table2.column_names, self.column_names)30 self.assertSequenceEqual(table.row_names, ['a', 'b', 'c'])31 self.assertSequenceEqual(table.column_names, self.column_names)32 def test_rename_column_names(self):33 table = Table(self.rows, self.column_names, self.column_types)34 table2 = table.rename(column_names=['d', 'e', 'f'])35 self.assertIs(table2.row_names, None)36 self.assertSequenceEqual(table2.column_names, ['d', 'e', 'f'])37 self.assertIs(table.row_names, None)38 self.assertSequenceEqual(table.column_names, self.column_names)39 def test_rename_column_names_dict(self):40 table = Table(self.rows, self.column_names, self.column_types)41 table2 = table.rename(column_names={'two': 'second'})42 self.assertIs(table2.row_names, None)43 self.assertSequenceEqual(table2.column_names, ['one', 'second', 'three'])44 self.assertIs(table.row_names, None)45 self.assertSequenceEqual(table.column_names, self.column_names)46 def test_rename_column_names_renames_row_values(self):47 table = Table(self.rows, self.column_names, self.column_types)48 new_column_names = ['d', 'e', 'f']49 table2 = table.rename(column_names=new_column_names)50 self.assertColumnNames(table2, new_column_names)51 def test_rename_slugify_columns(self):52 strings = ['Test kož', 'test 2', 'test 2']53 table = Table(self.rows, self.column_names, self.column_types)54 table2 = table.rename(strings, slug_columns=True)55 table3 = table.rename(strings, slug_columns=True, separator='.')56 self.assertColumnNames(table, ['one', 'two', 'three'])57 self.assertColumnNames(table2, ['test_koz', 'test_2', 'test_2_2'])58 self.assertColumnNames(table3, ['test.koz', 'test.2', 'test.2.2'])59 def test_rename_slugify_rows(self):60 strings = ['Test kož', 'test 2', 'test 2']61 table = Table(self.rows, self.column_names, self.column_types)62 table2 = table.rename(row_names=strings, slug_rows=True)63 table3 = table.rename(row_names=strings, slug_rows=True, separator='.')64 self.assertIs(table.row_names, None)65 self.assertRowNames(table2, ['test_koz', 'test_2', 'test_2_2'])66 self.assertRowNames(table3, ['test.koz', 'test.2', 'test.2.2'])67 def test_rename_slugify_columns_in_place(self):68 column_names = [u'Test kož', 'test 2', 'test 2']69 warnings.simplefilter('ignore')70 try:71 table = Table(self.rows, column_names, self.column_types)72 finally:73 warnings.resetwarnings()74 table2 = table.rename(slug_columns=True)75 table3 = table.rename(slug_columns=True, separator='.')76 self.assertColumnNames(table, [u'Test kož', 'test 2', 'test 2_2'])77 self.assertColumnNames(table2, ['test_koz', 'test_2', 'test_2_2'])78 self.assertColumnNames(table3, ['test.koz', 'test.2', 'test.2.2'])79 def test_rename_slugify_rows_in_place(self):80 strings = ['Test kož', 'test 2', 'test 2']81 table = Table(self.rows, self.column_names, self.column_types, row_names=strings)82 table2 = table.rename(slug_rows=True)83 table3 = table.rename(slug_rows=True, separator='.')84 self.assertRowNames(table, ['Test kož', 'test 2', 'test 2'])85 self.assertRowNames(table2, ['test_koz', 'test_2', 'test_2_2'])...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!