-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
226 lines (176 loc) · 7.85 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import argparse
import multiprocessing
import os
import warnings
from importlib import import_module
import pandas as pd
import torch
from torch.utils.data import DataLoader
from dataset import TestDataset, MaskBaseDataset
def load_model(saved_model, num_classes, device):
model_cls = getattr(import_module("model"), args.model)
model = model_cls(
num_classes=num_classes
)
# tarpath = os.path.join(saved_model, 'best.tar.gz')
# tar = tarfile.open(tarpath, 'r:gz')
# tar.extractall(path=saved_model)
model_path = os.path.join(saved_model, 'best.pth')
model.load_state_dict(torch.load(model_path, map_location=device))
return model
@torch.no_grad()
def inference_cutmix(data_dir, model_dir, output_dir, args):
"""
"""
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
num_classes = MaskBaseDataset.num_classes # 18
model = load_model(model_dir, num_classes, device).to(device)
model.eval()
img_root = os.path.join(data_dir, 'images')
info_path = os.path.join(data_dir, 'info.csv')
info = pd.read_csv(info_path)
img_paths = [os.path.join(img_root, img_id) for img_id in info.ImageID]
dataset = TestDataset(img_paths, args.resize)
loader = torch.utils.data.DataLoader(
dataset,
batch_size=args.batch_size,
num_workers=multiprocessing.cpu_count() // 2,
shuffle=False,
pin_memory=use_cuda,
drop_last=False,
)
print("Calculating inference results..")
preds = []
with torch.no_grad():
for idx, images in enumerate(loader):
images = images['image'].to(device)
pred = model(images)
pred = pred.argmax(dim=-1)
preds.extend(pred.cpu().numpy())
info['ans'] = preds
save_path = os.path.join(output_dir, f'output.csv')
info.to_csv(save_path, index=False)
print(f"Inference Done! Inference result saved at {save_path}")
@torch.no_grad()
def inference_multi_label(data_dir, model_dir, output_dir, args):
"""
"""
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
num_classes = MaskBaseDataset.num_classes # 18
num_classes = 11
model = load_model(model_dir, num_classes, device).to(device)
model.eval()
img_root = os.path.join(data_dir, 'images')
info_path = os.path.join(data_dir, 'info.csv')
info = pd.read_csv(info_path)
img_paths = [os.path.join(img_root, img_id) for img_id in info.ImageID]
dataset = TestDataset(img_paths, args.resize)
loader = torch.utils.data.DataLoader(
dataset,
batch_size=args.batch_size,
num_workers=multiprocessing.cpu_count() // 2,
shuffle=False,
pin_memory=use_cuda,
drop_last=False,
)
print("Calculating inference results..")
preds = []
with torch.no_grad():
for idx, images in enumerate(loader):
images = images['image'].to(device)
#pred = model(images)
outs = model(images)
(mask_outs, gender_outs, age_outs) = torch.split(outs, [3, 2, 6], dim=1)
mask_preds = torch.argmax(mask_outs, dim=-1)
gender_preds = torch.argmax(gender_outs, dim=-1)
age_probs = torch.nn.functional.softmax(age_outs)
age_probs = torch.transpose(age_probs,0,1)
age_probs1 = torch.transpose(torch.unsqueeze(torch.sum(age_probs[:2], dim=0), 0),0,1)
age_probs2 = torch.transpose(torch.unsqueeze(torch.sum(age_probs[2:5], dim=0), 0),0,1)
age_probs3 = torch.transpose(age_probs[5:],0,1)
age_add_probs = torch.cat((age_probs1, age_probs2, age_probs3), -1)
age_preds = torch.argmax(age_add_probs, dim=-1)
age_preds = age_preds.to(device)
pred = age_preds + 3*gender_preds + 6*mask_preds
#pred = pred.argmax(dim=-1)
preds.extend(pred.cpu().numpy())
info['ans'] = preds
save_path = os.path.join(output_dir, f'output.csv')
info.to_csv(save_path, index=False)
print(f"Inference Done! Inference result saved at {save_path}")
def age_converter(age):
if age<2: return 0
if age<5: return 1
return 2
@torch.no_grad()
def inference_cutmix_multi_label(data_dir, model_dir, output_dir, args):
"""
"""
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
num_classes = MaskBaseDataset.num_classes # 18
num_classes = 11
model = load_model(model_dir, num_classes, device).to(device)
model.eval()
img_root = os.path.join(data_dir, 'images')
info_path = os.path.join(data_dir, 'info.csv')
info = pd.read_csv(info_path)
img_paths = [os.path.join(img_root, img_id) for img_id in info.ImageID]
dataset = TestDataset(img_paths, args.resize)
loader = torch.utils.data.DataLoader(
dataset,
batch_size=args.batch_size,
num_workers=multiprocessing.cpu_count() // 2,
shuffle=False,
pin_memory=use_cuda,
drop_last=False,
)
print("Calculating inference results..")
preds = []
with torch.no_grad():
for idx, images in enumerate(loader):
images = images['image'].to(device)
outs = model(images)
r_outs = torch.permute(outs, (1,0))
mask_outs = torch.permute(torch.tensor(r_outs[:3]), (1,0))
gender_outs = torch.permute(torch.tensor(r_outs[3:5]), (1,0))
age_outs = torch.permute(torch.tensor(r_outs[5:]), (1,0))
mask_preds = torch.argmax(mask_outs, dim=-1)
gender_preds = torch.argmax(gender_outs, dim=-1)
age_preds = torch.argmax(age_outs, dim=-1)
age_preds = torch.tensor([age_converter(age) for age in age_preds])
age_preds = age_preds.to(device)
pred = mask_preds * 6 + gender_preds * 3 + age_preds
#pred = pred.argmax(dim=-1)
preds.extend(pred.cpu().numpy())
info['ans'] = preds
save_path = os.path.join(output_dir, f'output.csv')
info.to_csv(save_path, index=False)
print(f"Inference Done! Inference result saved at {save_path}")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Data and model checkpoints directories
parser.add_argument('--batch_size', type=int, default=128, help='input batch size for validing (default: 128)')
parser.add_argument('--resize', type=tuple, default=(224, 224), help='resize size for image when you trained (default: (224, 224))')
parser.add_argument('--model', type=str, default='TinyVit_224', help='model type (default: TinyVit_224)')
# Container environment
parser.add_argument('--data_dir', type=str, default=os.environ.get('SM_CHANNEL_EVAL', './data/eval'))
parser.add_argument('--model_dir', type=str, default=os.environ.get('SM_CHANNEL_MODEL', './model/exp'))
parser.add_argument('--output_dir', type=str, default=os.environ.get('SM_OUTPUT_DATA_DIR', './output'))
parser.add_argument('--inference_mode', type=str, default='train_cutmix') # inference_cutmix, inference_multi_label,inference_cutmix_multi_label
args = parser.parse_args()
warnings.filterwarnings(action='ignore')
data_dir = args.data_dir
model_dir = args.model_dir
output_dir = args.output_dir
inference_mode = args.inference_mode
os.makedirs(output_dir, exist_ok=True)
# stratifiedkfold_tta의 경우 train이 끝나면 이어서 inference
if inference_mode=='inference_cutmix':
inference_cutmix(data_dir, model_dir, output_dir, args)
elif inference_mode=='inference_multi_label':
inference_multi_label(data_dir, model_dir, output_dir, args)
else:
inference_cutmix_multi_label(data_dir, model_dir, output_dir, args)