-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.py
51 lines (46 loc) · 1.76 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from torch.utils.data import DataLoader
import option
import matplotlib.pyplot as plt
import torch
from sklearn.metrics import auc, roc_curve, precision_recall_curve
from tqdm import tqdm
args=option.parse_args()
from config import *
from models.mgfn import mgfn as Model
from datasets.dataset import Dataset
def test(dataloader, model, args, device):
plt.clf()
with torch.no_grad():
model.eval()
pred = torch.zeros(0)
featurelen =[]
for i, inputs in tqdm(enumerate(dataloader)):
input = inputs[0].to(device)
input = input.permute(0, 2, 1, 3)
_, _, _, _, logits = model(input)
logits = torch.squeeze(logits, 1)
logits = torch.mean(logits, 0)
sig = logits
featurelen.append(len(sig))
pred = torch.cat((pred, sig))
gt = np.load(args.gt)
pred = list(pred.cpu().detach().numpy())
pred = np.repeat(np.array(pred), 16)
fpr, tpr, threshold = roc_curve(list(gt), pred)
rec_auc = auc(fpr, tpr)
precision, recall, th = precision_recall_curve(list(gt), pred)
pr_auc = auc(recall, precision)
print('pr_auc : ' + str(pr_auc))
print('rec_auc : ' + str(rec_auc))
return rec_auc, pr_auc
if __name__ == '__main__':
args = option.parse_args()
config = Config(args)
device = torch.device("cuda")
model = Model()
test_loader = DataLoader(Dataset(args, test_mode=True),
batch_size=1, shuffle=False,
num_workers=0, pin_memory=False)
model = model.to(device)
model_dict = model.load_state_dict({k.replace('module.', ''): v for k, v in torch.load('mgfn_ucf.pkl').items()})
auc = test(test_loader, model, args, device)