text
stringlengths
1
93.6k
def test(model_global, model_local, model_fusion, test_loader):
# initialize the ground truth and output tensor
gt = torch.FloatTensor().cuda()
pred_global = torch.FloatTensor().cuda()
pred_local = torch.FloatTensor().cuda()
pred_fusion = torch.FloatTensor().cuda()
# switch to evaluate mode
model_global.eval()
model_local.eval()
model_fusion.eval()
cudnn.benchmark = True
for i, (inp, target) in enumerate(test_loader):
with torch.no_grad():
if i % 2000 == 0:
print('testing process:',i)
target = target.cuda()
gt = torch.cat((gt, target), 0)
input_var = torch.autograd.Variable(inp.cuda())
#output = model_global(input_var)
output_global, fm_global, pool_global = model_global(input_var)
patchs_var = Attention_gen_patchs(inp,fm_global)
output_local, _, pool_local = model_local(patchs_var)
output_fusion = model_fusion(pool_global,pool_local)
pred_global = torch.cat((pred_global, output_global.data), 0)
pred_local = torch.cat((pred_local, output_local.data), 0)
pred_fusion = torch.cat((pred_fusion, output_fusion.data), 0)
AUROCs_g = compute_AUCs(gt, pred_global)
AUROC_avg = np.array(AUROCs_g).mean()
print('Global branch: The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
for i in range(N_CLASSES):
print('The AUROC of {} is {}'.format(CLASS_NAMES[i], AUROCs_g[i]))
AUROCs_l = compute_AUCs(gt, pred_local)
AUROC_avg = np.array(AUROCs_l).mean()
print('\n')
print('Local branch: The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
for i in range(N_CLASSES):
print('The AUROC of {} is {}'.format(CLASS_NAMES[i], AUROCs_l[i]))
AUROCs_f = compute_AUCs(gt, pred_fusion)
AUROC_avg = np.array(AUROCs_f).mean()
print('\n')
print('Fusion branch: The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
for i in range(N_CLASSES):
print('The AUROC of {} is {}'.format(CLASS_NAMES[i], AUROCs_f[i]))
def compute_AUCs(gt, pred):
"""Computes Area Under the Curve (AUC) from prediction scores.
Args:
gt: Pytorch tensor on GPU, shape = [n_samples, n_classes]
true binary labels.
pred: Pytorch tensor on GPU, shape = [n_samples, n_classes]
can either be probability estimates of the positive class,
confidence values, or binary decisions.
Returns:
List of AUROCs of all classes.
"""
AUROCs = []
gt_np = gt.cpu().numpy()
pred_np = pred.cpu().numpy()
for i in range(N_CLASSES):
AUROCs.append(roc_auc_score(gt_np[:, i], pred_np[:, i]))
return AUROCs
if __name__ == '__main__':
main()
# <FILESEP>
"""
Crop submit images
1. zoom original image, fix height to 512, let width adjust itself to keep ratio
2. predict box y_min, y_max percentage
"""
import numpy as np
import glob
import torch
import os.path as path
import folders as f
import cv2
import os
class Box():
def __init__(self):
import torchvision
import torch.nn as nn
import torch
net = torchvision.models.densenet121()
num_conv_features = net.features[-1].num_features