-
Notifications
You must be signed in to change notification settings - Fork 1
/
infer.py
119 lines (92 loc) · 3.75 KB
/
infer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import pickle
import pprint
from collections import OrderedDict, defaultdict
import numpy as np
import torch
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader
from tqdm import tqdm
from batch_engine import valid_trainer, batch_trainer
from config import argument_parser
from dataset.AttrDataset import AttrDataset, get_transform
from loss.CE_loss import CEL_Sigmoid
from models.base_block import FeatClassifier, BaseClassifier
from models.resnet import resnet50, resnet101
from models.resnest.resnest import resnest50
from tools.function import get_model_log_path, get_pedestrian_metrics, get_reload_weight
from tools.utils import time_str, save_ckpt, ReDirectSTD, set_seed, AverageMeter
set_seed(605)
def main(args):
visenv_name = args.dataset
exp_dir = os.path.join('exp_result', args.dataset)
model_dir, log_dir = get_model_log_path(exp_dir, visenv_name)
stdout_file = os.path.join(log_dir, f'stdout_{time_str()}.txt')
if args.redirector:
print('redirector stdout')
ReDirectSTD(stdout_file, 'stdout', False)
pprint.pprint(OrderedDict(args.__dict__))
print('-' * 60)
print(f'use GPU{args.device} for training')
print(f'train set: {args.dataset} {args.train_split}, test set: {args.valid_split}')
train_tsfm, valid_tsfm = get_transform(args)
print(train_tsfm)
train_set = AttrDataset(args=args, split=args.train_split, transform=train_tsfm)
train_loader = DataLoader(
dataset=train_set,
batch_size=args.batchsize,
shuffle=True,
num_workers=4,
pin_memory=True,
)
valid_set = AttrDataset(args=args, split=args.valid_split, transform=valid_tsfm)
valid_loader = DataLoader(
dataset=valid_set,
batch_size=args.batchsize,
shuffle=False,
num_workers=4,
pin_memory=True,
)
print(f'{args.train_split} set: {len(train_loader.dataset)}, '
f'{args.valid_split} set: {len(valid_loader.dataset)}, '
f'attr_num : {train_set.attr_num}')
backbone = resnet50()
classifier = BaseClassifier(nattr=26)
model = FeatClassifier(backbone, classifier)
if torch.cuda.is_available():
model = torch.nn.DataParallel(model).cuda()
print("reloading pretrained models")
exp_dir = os.path.join('exp_result', args.dataset)
model_path = os.path.join(exp_dir, args.dataset, 'img_model')
model = get_reload_weight(model_path, model)
model.eval()
preds_probs = []
gt_list = []
with torch.no_grad():
for step, (imgs, gt_label, imgname) in enumerate(tqdm(valid_loader)):
imgs = imgs.cuda()
gt_label = gt_label.cuda()
gt_list.append(gt_label.cpu().numpy())
gt_label[gt_label == -1] = 0
valid_logits = model(imgs)
valid_probs = torch.sigmoid(valid_logits)
preds_probs.append(valid_probs.cpu().numpy())
gt_label = np.concatenate(gt_list, axis=0)
preds_probs = np.concatenate(preds_probs, axis=0)
valid_result = get_pedestrian_metrics(gt_label, preds_probs)
print(f'Evaluation on test set, \n',
'ma: {:.4f}, pos_recall: {:.4f} , neg_recall: {:.4f} \n'.format(
valid_result.ma, np.mean(valid_result.label_pos_recall), np.mean(valid_result.label_neg_recall)),
'Acc: {:.4f}, Prec: {:.4f}, Rec: {:.4f}, F1: {:.4f}'.format(
valid_result.instance_acc, valid_result.instance_prec, valid_result.instance_recall,
valid_result.instance_f1))
if __name__ == '__main__':
parser = argument_parser()
args = parser.parse_args()
main(args)
"""
载入的时候要:
from tools.function import LogVisual
sys.modules['LogVisual'] = LogVisual
log = torch.load('./save/2018-10-29_21:17:34trlog')
"""