How to use test_arguments method in pytest-bdd

Best Python code snippet using pytest-bdd_python

_test.py

Source:_test.py Github

copy

Full Screen

1# -*- coding:utf-8 -*-2#3# Created by Drogo Zhang4#5# On 2018-10-0967import numpy as np8import torch as t9import torch.nn.functional as F10from torch.autograd import Variable11import config as cfg12from model import RCNN13import utils14from utils import reg_to_bbox, non_maximum_suppression, evaluate, get_count_dataframe_by_confusion_matrix, denorm15from utils import first_write, lb_reg_to_bbox16from arguments import TestAruguments1718word_embedding_dim = cfg.WORD_EMBEDDING_DIM19classes_num = cfg.CLASSES_NUM20th_score = cfg.TH_SCORE212223def load_model(test_arguments):24 rcnn = RCNN(test_arguments.pos_loss_method, test_arguments.loss_weight_lambda).cuda()25 rcnn.load_state_dict(t.load(test_arguments.model_path))26 rcnn.eval() # dropout rate = 027 return rcnn282930def load_test_sentence(test_sentence_npz_path):31 npz = np.load(test_sentence_npz_path)32 test_sentences = npz['test_sentences']33 test_sentence_info = npz['test_sentence_info']34 test_roi = npz['test_roi']3536 test_sentences = t.Tensor(test_sentences).cuda()3738 return test_sentences, test_sentence_info, test_roi394041def _test_one_sentence(test_arguments, sentence, rois, rcnn, fold_index, this_sentence_len, info):42 roi_num = rois.shape[0]43 ridx = np.zeros(roi_num).astype(int)44 pred_cls_score, pred_tbbox = rcnn(sentence, rois, ridx)45 pred_cls_score = F.softmax(pred_cls_score, dim=1).data.cpu().numpy() # softmax score for each row46 pred_tbbox = pred_tbbox.data.cpu().numpy()47 if test_arguments.normalize:48 pred_tbbox = denorm(pred_tbbox, fold_index, test_arguments)49 if test_arguments.dx_compute_method == "left_boundary":50 pred_bbox = lb_reg_to_bbox(this_sentence_len, pred_tbbox, rois)51 else:52 pred_bbox = reg_to_bbox(this_sentence_len, pred_tbbox, rois)5354 result_bbox = []55 result_cls = []56 drop_bbox = []57 drop_cls = []58 for c in range(1, classes_num + 1):59 c_cls_score = pred_cls_score[:, c]60 c_bboxs = pred_bbox[:, :, c].T61 if c == 4 and max(c_cls_score) >= test_arguments.score_threshold:62 wait = True63 boxes, _boxes = non_maximum_suppression(c_cls_score, c_bboxs, iou_threshold=test_arguments.th_nms_iou,64 score_threshold=test_arguments.score_threshold, info=info)65 result_bbox.extend(boxes)66 drop_bbox.extend(_boxes)67 result_cls.extend([c] * len(boxes)) # print the predict result of this sentence.68 drop_cls.extend([c] * len(_boxes))69 if len(result_cls) == 0: # if the sentence without detecting anything!, we will lower the score criterion70 drop_bbox = []71 drop_cls = []72 for c in range(1, classes_num + 1):73 c_sc = pred_cls_score[:, c]74 c_bboxs = pred_bbox[:, :, c].T75 boxes, _boxes = non_maximum_suppression(c_sc, c_bboxs, iou_threshold=test_arguments.th_nms_iou,76 score_threshold=test_arguments.score_threshold / 6, info=info)77 result_bbox.extend(boxes)78 result_cls.extend([c] * len(boxes))79 drop_bbox.extend(_boxes)80 drop_cls.extend([c] * len(_boxes))81 if len(result_bbox) > 1:82 wait = True83 result_bbox = result_bbox[:1]84 result_cls = result_cls[:1]85 drop_bbox += result_bbox[1:]86 drop_cls += result_cls[1:]8788 return np.array(result_bbox), np.array(result_cls), np.array(drop_bbox), np.array(drop_cls)899091def _test_epoch(test_arguments, fold_index):92 rcnn = load_model(test_arguments)93 test_sentences, test_sentence_info, test_roi = load_test_sentence(test_arguments.test_sentence_npz_path)9495 sentence_num = test_sentences.size(0)9697 perm = np.random.permutation(sentence_num)9899 for i in range(sentence_num):100 pi = perm[i]101 sentence = Variable(test_sentences[pi:pi + 1]).cuda()102 # sentence_real_len =103104 info = test_sentence_info[pi]105106 idxs = info['roi_ids']107 this_sentence_len = len(info['gt_str'].split(" "))108 rois = test_roi[idxs]109110 result_bbox, result_cls, drop_bbox, drop_cls = _test_one_sentence(test_arguments, sentence, rois, rcnn,111 fold_index,112 this_sentence_len, info=info)113114 evaluate(result_bbox, result_cls, drop_bbox, drop_cls, info, test_arguments.confusion_matrix,115 test_arguments.th_iou_p)116117 return118119120def _test_k_fold(test_arguments, all_csv_result):121 for model_epoch in range(test_arguments.min_test_epoch, test_arguments.max_test_epoch + 1):122 test_arguments.initialize_confusion_matrix()123 for fold_index in range(test_arguments.fold_k):124 test_arguments.model_path = test_arguments.get_model_path(fold_index, model_epoch)125 test_arguments.test_sentence_npz_path = test_arguments.get_test_npz_path(fold_index)126 _test_epoch(test_arguments, fold_index)127128 test_arguments.confusion_matrix = test_arguments.confusion_matrix.astype(np.int32).tolist()129 test_arguments.data_frame = get_count_dataframe_by_confusion_matrix(test_arguments.confusion_matrix)130 print(utils.nms_mis_hit)131 utils.nms_mis_hit = [0] * 7132 test_arguments.write_one_result(all_csv_result)133 print(test_arguments.data_frame)134 return135136137def main():138 pos_loss_method = "mse" # "mse"139 th_train_iou = 0.6 # 0.8140 norm = True # Flase141 min_test_epoch = 40142 max_test_epoch = 40143 loss_weight_lambda = 1.0144 prevent_overfitting_method = "Dropout" # "L2 Regu" # "Dropout"145 partial_l2 = False146 dx_compute_method = "centre" # "left_boundary" # "centre"147148 test_arguments = TestAruguments(norm, pos_loss_method, th_train_iou, min_test_epoch, max_test_epoch,149 dx_compute_method=dx_compute_method,150 loss_weight_lambda=loss_weight_lambda, partial_l2_penalty=partial_l2,151 prevent_overfitting_method=prevent_overfitting_method)152 test_arguments.show_arguments()153 write_result_path = test_arguments.get_write_result_path()154155 all_csv_result = open(write_result_path, "w")156 first_write(all_csv_result)157 th_nms_iou_ls = [0.01]158 th_iou_p_ls = [0.6, 0.8, 1] # [0.8] # [0.6, 0.8, 1] # [1],159 for th_iou_p in th_iou_p_ls:160 for th_nms_iou in th_nms_iou_ls:161 test_arguments.th_nms_iou = th_nms_iou162 test_arguments.th_iou_p = th_iou_p163 _test_k_fold(test_arguments, all_csv_result)164165166if __name__ == '__main__': ...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-bdd automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful