def test_eval(model_obj, data_dict, num_anomaly_sets): test_X = data_dict['test'] test_scores = model_obj.score_samples(test_X) auc_list = [] for idx in range(num_anomaly_sets): key = 'anom_' + str(idx + 1) anom_X = data_dict[key] anom_scores = model_obj.score_samples(anom_X) auPR = eval.eval(anom_scores, test_scores, order='descending') auc_list.append(auPR) print("AUC : {:0.4f} ".format(auPR)) _mean = np.mean(auc_list) _std = np.std(auc_list) print(' Mean AUC ', np.mean(auc_list)) print(' AUC std', np.std(auc_list)) return _mean, _std
def main(opt): logger.info('* batch_size: %d' % opt.batch_size) logger.info('* model_path: {}'.format(opt.models[0])) testset = loads_json(opt.data, 'load test file') translator = build_translator(opt, report_score=False) translator.model.cpu() translated = translator.translate(data_path=opt.data, batch_size=opt.batch_size) # find first 3 not similar distractors hypothesis = {} for translation in translated: question_id = str(translation.ex_raw.id['file_id']) + '_' + str( translation.ex_raw.id['question_id']) pred1 = translation.pred_sents[0] pred2, pred3 = None, None for pred in translation.pred_sents[1:]: if jaccard_similarity(pred1, pred) < 0.5: if pred2 is None: pred2 = pred else: if pred3 is None: if jaccard_similarity(pred2, pred) < 0.5: pred3 = pred if pred2 is not None and pred3 is not None: break if pred2 is None: pred2 = translation.pred_sents[1] if pred3 is None: pred3 = translation.pred_sents[2] else: if pred3 is None: pred3 = translation.pred_sents[1] hypothesis[question_id] = [pred1, pred2, pred3] reference = {} for sample in testset: question_id = str(sample['id']['file_id']) + '_' + str( sample['id']['question_id']) if question_id not in reference.keys(): reference[question_id] = [sample['distractor']] else: reference[question_id].append(sample['distractor']) _ = eval(hypothesis, reference)
def main(args): ea = EA(1) ea.load_config(args.config) reseedPeriod = int(args.reseed) taskNum = int(args.task_num) np.random.seed(0) seed = np.random.randint(0, 2**32 - 1, size=(taskNum), dtype=np.uint32) seed = seed.tolist() print(seed) for i in range(int(args.generation)): if ((reseedPeriod > 0) and (i % reseedPeriod == 0)): for j in range(taskNum): seed[j] = random.randint(0, 2**32 - 1) ea_time = time.time() pop = ea.ask() ea_time = time.time() - ea_time fitnesses = [] workloads = [] num_workers = int(args.num_workers) - 1 gc.collect() prep_time = time.time() for j in range(len(pop)): workloads.append((pop[j], args.task, seed)) prep_time = time.time() - prep_time eval_time = time.time() if (num_workers > 1): with mp.Pool(num_workers) as pool: results = pool.map(eval, workloads) else: results = [] for w in workloads: results.append(eval(w)) eval_time = time.time() - eval_time ea.tell(results, args.task, seed) ea.write_history(args.output) print( 'iter: {0} fit: {1}, Q: {2}, ea_time: {3}, prep_time: {4}, eval_time: {5}, max_depth:{6}' .format(i, ea.fitnesses[0], np.mean(ea.Q), ea_time, prep_time, eval_time, ea.pop[0].maxDepth))
boxes, probs = test_img(net, data_encoder, img, scale, thresh=0.5) # print(boxes) face_num = len(boxes) / 4 s = str(int(face_num)) + " " for i in range(int(face_num)): x1 = int(boxes[i * 4] * 1) y1 = int(boxes[i * 4 + 1] * 1) x2 = int(boxes[i * 4 + 2] * 1) y2 = int(boxes[i * 4 + 3] * 1) s = s + str(x1) + " " + str(y1) + " " + str(x2) + " " + str(y2) + " " print(file, s[:-1]) # cv2.imwrite("./all_test_detected/" + file + "_result.jpg", img) #test img # if 0: # files = os.listdir('./eval/badcase_edge/') # for file in files: # imgPath = os.path.join('./eval/badcase_edge/',file) # print(imgPath) # t = time.time() # for k in range(30): # boxes,probs = test_img(net,data_encoder,imgPath,scale,thresh=0.5) # print( 'each time: ',(time.time()-t)/30.0) if __name__ == '__main__': test_faceInCar() eval(label_path, outfile, output_recall_far_file) #test()
def main(opt): testset = loads_json(opt.data, 'load test file') f = open("data/corpus.json", "r") data = f.readlines() samples = [] for i in data: samples.append(json.loads(i)["sents"]) corpus = [] for i in samples: corpus.append(" ".join([" ".join(sent) for sent in i])) vectorizer = TfidfVectorizer() tfidf = vectorizer.fit(corpus) translator = build_translator(opt, report_score=False) translated = translator.translate(data_path=opt.data, batch_size=opt.batch_size, tfidf=tfidf) # find first 3 not similar distractors hypothesis = {} for translation in translated: sample_id = str(translation.ex_raw.id).split("_") question_id = sample_id[0] + "_" + sample_id[1] pred1 = translation.pred_sents[0] pred2, pred3 = None, None for pred in translation.pred_sents[1:]: if jaccard_similarity(pred1, pred) < 0.5: if pred2 is None: pred2 = pred else: if pred3 is None: if jaccard_similarity(pred2, pred) < 0.5: pred3 = pred if pred2 is not None and pred3 is not None: break if pred2 is None: pred2 = translation.pred_sents[1] if pred3 is None: pred3 = translation.pred_sents[2] else: if pred3 is None: pred3 = translation.pred_sents[1] hypothesis[question_id] = [pred1, pred2, pred3] torch.save(hypothesis, "translated.hypothesis.pt") reference = {} f = open(opt.output, "w") for sample in testset: line = copy.deepcopy(sample) sample_id = sample["id"].split("_") question_id = sample_id[0] + "_" + sample_id[1] if question_id not in reference.keys(): reference[question_id] = [sample['distractor']] else: reference[question_id].append(sample['distractor']) line["pred"] = hypothesis[question_id] f.write(json.dumps(line) + "\n") f.close() _ = eval(hypothesis, reference)