forked from pudae/kaggle-hpa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
144 lines (110 loc) · 4.62 KB
/
inference.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import math
import argparse
import pprint
import tqdm
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from datasets import get_dataloader
from transforms import get_transform
from models import get_model
from losses import get_loss
from optimizers import get_optimizer
from schedulers import get_scheduler
import utils.config
import utils.checkpoint
def inference(config, model, split, output_filename=None):
config.eval.batch_size = 2
if split == 'test':
config.data.name = 'TestDataset'
dataloader = get_dataloader(config, split, get_transform(config, split))
if torch.cuda.device_count() > 1:
model = torch.nn.DataParallel(model)
model = model.cuda()
model.eval()
key_list = []
label_list = []
probability_list = []
with torch.no_grad():
batch_size = config.eval.batch_size
total_size = len(dataloader.dataset)
total_step = math.ceil(total_size / batch_size)
for i, data in tqdm.tqdm(enumerate(dataloader), total=total_step):
images = data['image'].cuda()
if len(images.size()) == 5:
B, T, C, H, W = images.size()
logits = model(images.view(-1, C, H, W))[:,:28]
logits = logits.view(B, T, -1)
probabilities = F.sigmoid(logits)
probabilities = probabilities.mean(dim=1)
else:
logits = model(images)[:,:28]
probabilities = F.sigmoid(logits)
probability_list.append(probabilities.cpu().numpy())
if split != 'test':
label_list.append(data['label'].numpy())
key_list.extend(data['key'])
if split != 'test':
labels = np.concatenate(label_list, axis=0)
assert labels.ndim == 2
assert labels.shape[0] == total_size
assert labels.shape[-1] == 28
probabilities = np.concatenate(probability_list, axis=0)
assert probabilities.ndim == 2
assert probabilities.shape[0] == total_size
assert probabilities.shape[-1] == 28
if split != 'test':
records = []
for label, probability in zip(labels, probabilities):
records.append(tuple([str(l) for l in label] + ['{:.04f}'.format(p) for p in probability]))
columns = ['L{:02d}'.format(l) for l in range(28)] + ['P{:02d}'.format(l) for l in range(28)]
else:
records = []
for key, probability in zip(key_list, probabilities):
records.append(tuple([key] + ['{:.04f}'.format(p) for p in probability]))
columns = ['Id'] + ['P{:02d}'.format(l) for l in range(28)]
df = pd.DataFrame.from_records(records, columns=columns)
print('save {}'.format(output_filename))
df.to_csv(output_filename, index=False)
def run(config, split, checkpoint_name, output_filename):
model = get_model(config).cuda()
checkpoint = utils.checkpoint.get_checkpoint(config, name=checkpoint_name)
utils.checkpoint.load_checkpoint(model, None, checkpoint)
inference(config, model, split, output_filename)
def parse_args():
parser = argparse.ArgumentParser(description='hpa')
parser.add_argument('--output', dest='output_filename',
help='output filename',
default=None, type=str)
parser.add_argument('--config', dest='config_file',
help='configuration filename',
default=None, type=str)
parser.add_argument('--checkpoint', dest='checkpoint_filename',
help='checkpoint filename',
default=None, type=str)
parser.add_argument('--num_tta', dest='num_tta',
help='number of tta images',
default=4, type=int)
parser.add_argument('--split', dest='split',
help='split',
default='test', type=str)
return parser.parse_args()
def main():
import warnings
warnings.filterwarnings("ignore")
torch.multiprocessing.set_sharing_strategy('file_system')
print('inference HPA')
args = parse_args()
config = utils.config.load(args.config_file)
config.transform.name = 'tta_transform'
config.transform.params.num_tta = args.num_tta
os.makedirs(os.path.dirname(args.output_filename), exist_ok=True)
run(config, args.split, args.checkpoint_filename, args.output_filename)
print('success!')
if __name__ == '__main__':
main()