def test(opt, test_dataloader, model): ''' Test the model trained with the prototypical learning algorithm ''' device = 'cuda:{}'.format( opt.gpu_id) if torch.cuda.is_available() and opt.cuda else 'cpu' scores = defaultdict(list) res = {} for epoch in tqdm(range(opt.test_epochs)): test_iter = iter(test_dataloader) for batch in test_iter: x, y = batch x, y = x.to(device), y.to(device) model_output = model(x) _, metrics = loss_fn(input=model_output, target=y, n_support=opt.num_support_test, all_metrics=True) acc, macro_f1, micro_f1 = metrics scores['accuracy'].append(acc.item()) scores['macro_f1'].append(macro_f1) scores['micro_f1'].append(micro_f1) for metric in scores: res[metric] = np.mean(scores[metric]) pprint(res) with open(os.path.join(opt.experiment_root, opt.test_result_file), 'w') as f: json.dump(res, f) return res
def new(): imgs = [] ground = [] labels = [] j = 0 for clas in classlist.keys(): j += 1 clpath = path + clas + '/' for i in range(3): x = random.choice([ x for x in os.listdir(clpath) if os.path.isfile(os.path.join(path + clas + '/', x)) ]) file = os.path.join(clpath, x) imgs.append(file) ground.append(clas) for l in range(len(imgs)): jpg = load_img(imgs[l]) emd = get_embd(jpg, model) lbl = loss_fn(emd, means_path) labels.append(lbl[1].item()) acc = 0 for i in range(len(labels)): if (sorted_x[labels[i]][0] == ground[i]): acc += 1 return acc / len(imgs)