forked from Kevinz-code/CSRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
val.py
86 lines (68 loc) · 2.72 KB
/
val.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import argparse
import time
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from pipeline.resnet_csra import ResNet_CSRA
from pipeline.resnet_dataset import ResNet_Dataset
from utils.eval import evaluation
from utils.warmUpLR import WarmUpLR
from tqdm import tqdm
def Args():
parser = argparse.ArgumentParser(description="settings")
# model default resnet101
parser.add_argument("--num_heads", default=1, type=int)
parser.add_argument("--lam",default=0.1, type=float)
parser.add_argument("--load_from", default="models_local/resnet101_voc07_head1_lam0.1_94.7.pth", type=str)
# dataset
parser.add_argument("--dataset", default="voc07", type=str)
parser.add_argument("--num_cls", default=20, type=int)
parser.add_argument("--test_aug", default=[], type=list)
parser.add_argument("--img_size", default=448, type=int)
parser.add_argument("--batch_size", default=16, type=int)
args = parser.parse_args()
return args
def val(args, model, test_loader, test_file):
model.eval()
print("Test on Pretrained Models")
result_list = []
# calculate logit
for index, data in enumerate(tqdm(test_loader)):
img = data['img'].cuda()
target = data['target'].cuda()
img_path = data['img_path']
with torch.no_grad():
logit = model(img)
result = nn.Sigmoid()(logit).cpu().detach().numpy().tolist()
for k in range(len(img_path)):
result_list.append(
{
"file_name": img_path[k].split("/")[-1].split(".")[0],
"scores": result[k]
}
)
# cal_mAP OP OR
evaluation(result=result_list, types=args.dataset, ann_path=test_file[0])
def main():
args = Args()
# model
model = ResNet_CSRA(num_heads=args.num_heads, lam=args.lam, num_classes=args.num_cls)
model.cuda()
print("Loading weights from {}".format(args.load_from))
if torch.cuda.device_count() > 1:
print("lets use {} GPUs.".format(torch.cuda.device_count()))
model = nn.DataParallel(model, device_ids=range(torch.cuda.device_count))
model.module.load_state_dict(torch.load(args.load_from))
else:
model.load_state_dict(torch.load(args.load_from))
# data
if args.dataset == "voc07":
test_file = ['data/voc07/test_voc07.json']
if args.dataset == "coco":
test_file = ['data/coco/val_coco2014.json']
test_dataset = ResNet_Dataset(test_file, args.test_aug, args.img_size)
test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=8)
val(args, model, test_loader, test_file)
if __name__ == "__main__":
main()