-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathensemble_test.py
179 lines (138 loc) · 5.61 KB
/
ensemble_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import time
import os
import math
import argparse
from glob import glob
from collections import OrderedDict
import random
import warnings
from datetime import datetime
import json
import re
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import pandas as pd
import joblib
import cv2
import yaml
from sklearn.model_selection import KFold, StratifiedKFold, train_test_split
from skimage.io import imread
from apex import amp
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
from torch.utils.data.sampler import WeightedRandomSampler
import torch.backends.cudnn as cudnn
import torchvision
from lib.datasets import Dataset
from lib.utils.utils import *
from lib.models.model_factory import get_model
from lib.optimizers import RAdam
from lib import losses
from lib.decodes import decode
from lib.utils.vis import visualize
from lib.utils.nms import nms
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--name', default=None)
parser.add_argument('--models', default=None)
parser.add_argument('--score_th', default=0.3, type=float)
parser.add_argument('--nms', default=True, type=str2bool)
parser.add_argument('--nms_th', default=0.1, type=float)
parser.add_argument('--min_samples', default=1, type=int)
parser.add_argument('--show', action='store_true')
args = parser.parse_args()
return args
def main():
config = vars(parse_args())
if config['name'] is None:
config['name'] = 'ensemble_%s' % datetime.now().strftime('%m%d%H')
if os.path.exists('models/detection/%s/config.yml' % config['name']):
with open('models/detection/%s/config.yml' % config['name'], 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
else:
config['models'] = config['models'].split(',')
if not os.path.exists('models/detection/%s' % config['name']):
os.makedirs('models/detection/%s' % config['name'])
with open('models/detection/%s/config.yml' % config['name'], 'w') as f:
yaml.dump(config, f)
print('-'*20)
for key in config.keys():
print('%s: %s' % (key, str(config[key])))
print('-'*20)
with open('models/detection/%s/config.yml' % re.sub('_uncropped', '', config['models'][0]), 'r') as f:
model_config = yaml.load(f, Loader=yaml.FullLoader)
df = pd.read_csv('inputs/sample_submission.csv')
img_paths = np.array('inputs/test_images/' + df['ImageId'].values + '.jpg')
mask_paths = np.array('inputs/test_masks/' + df['ImageId'].values + '.jpg')
labels = np.array([convert_str_to_labels(s, names=['yaw', 'pitch', 'roll',
'x', 'y', 'z', 'score']) for s in df['PredictionString']])
if os.path.exists('outputs/raw/test/%s.pth' %config['name']):
merged_outputs = torch.load('outputs/raw/test/%s.pth' %config['name'])
else:
merged_outputs = {}
for i in tqdm(range(len(df))):
img_id = df.loc[i, 'ImageId']
output = {
'hm': 0,
'reg': 0,
'depth': 0,
'eular': None,
'trig': 0,
'quat': None,
'wh': 0,
'mask': 0,
}
merged_outputs[img_id] = output
for model_name in config['models']:
outputs = torch.load('outputs/raw/test/%s.pth' %model_name)
for i in tqdm(range(len(df))):
img_id = df.loc[i, 'ImageId']
output = outputs[img_id]
merged_outputs[img_id]['hm'] += output['hm'] / len(config['models'])
merged_outputs[img_id]['reg'] += output['reg'] / len(config['models'])
merged_outputs[img_id]['depth'] += output['depth'] / len(config['models'])
merged_outputs[img_id]['trig'] += output['trig'] / len(config['models'])
merged_outputs[img_id]['wh'] += output['wh'] / len(config['models'])
merged_outputs[img_id]['mask'] += output['mask'] / len(config['models'])
torch.save(merged_outputs, 'outputs/raw/test/%s.pth' %config['name'])
# decode
dets = {}
for i in tqdm(range(len(df))):
img_id = df.loc[i, 'ImageId']
output = merged_outputs[img_id]
det = decode(
model_config,
output['hm'],
output['reg'],
output['depth'],
eular=output['eular'] if model_config['rot'] == 'eular' else None,
trig=output['trig'] if model_config['rot'] == 'trig' else None,
quat=output['quat'] if model_config['rot'] == 'quat' else None,
wh=output['wh'] if model_config['wh'] else None,
mask=output['mask'],
)
det = det.numpy()[0]
dets[img_id] = det.tolist()
if config['nms']:
det = nms(det, dist_th=config['nms_th'])
if np.sum(det[:, 6] > config['score_th']) >= config['min_samples']:
det = det[det[:, 6] > config['score_th']]
else:
det = det[:config['min_samples']]
if config['show']:
img = cv2.imread('inputs/test_images/%s.jpg' %img_id)
img_pred = visualize(img, det)
plt.imshow(img_pred[..., ::-1])
plt.show()
df.loc[i, 'PredictionString'] = convert_labels_to_str(det[:, :7])
with open('outputs/decoded/test/%s.json' %config['name'], 'w') as f:
json.dump(dets, f)
df.to_csv('outputs/submissions/test/%s.csv' %config['name'], index=False)
if __name__ == '__main__':
main()