-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
238 lines (190 loc) · 7.67 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
227
228
229
230
231
232
233
234
235
236
237
import argparse
import multiprocessing
import os
from importlib import import_module
import pandas as pd
import torch
from torch.utils.data import DataLoader
from dataset_mask import TestDataset
def load_model(saved_model, num_classes, device, task_num):
tasks = ['model_mask', 'model_gender', 'model_age']
model_name = None
if task_num == 0:
model_name = args.model_mask
elif task_num == 1:
model_name = args.model_gender
else:
model_name = args.model_age
model_cls = getattr(import_module(tasks[task_num]), model_name)
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(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.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_mask(data_dir, model_dir, args):
"""
"""
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
num_classes = 3
model = load_model(model_dir, num_classes, device, 0).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.to(device)
pred = model(images)
pred = pred.argmax(dim=-1)
preds.extend(pred.cpu().numpy())
info['ans'] = preds
return info
@torch.no_grad()
def inference_gender(data_dir, model_dir, args):
"""
"""
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
num_classes = 2
model = load_model(model_dir, num_classes, device, 1).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.to(device)
pred = model(images)
pred = pred.argmax(dim=-1)
preds.extend(pred.cpu().numpy())
info['ans'] = preds
return info
@torch.no_grad()
def inference_age(data_dir, model_dir, args):
"""
"""
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
num_classes = 3
model = load_model(model_dir, num_classes, device, 2).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.to(device)
pred = model(images)
pred = pred.argmax(dim=-1)
preds.extend(pred.cpu().numpy())
info['ans'] = preds
return info
def merge_result(info_mask, info_gender, info_age, output_dir):
tmp = []
for i,j,k in zip(info_mask['ans'], info_gender['ans'], info_age['ans']):
tmp.append(i*6 + j*3 + k)
info_mask['ans'] = tmp
save_path = os.path.join(output_dir, args.output_name+'.csv')
info_mask.to_csv(save_path, index=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Data and model checkpoints directories
parser.add_argument('--batch_size', type=int, default=1000, help='input batch size for validing (default: 1000)')
parser.add_argument('--resize', type=tuple, default=(128, 96), help='resize size for image when you trained (default: (96, 128))')
parser.add_argument('--model_mask', type=str, default='ModelMask', help='model type (default: BaseModel)')
parser.add_argument('--model_gender', type=str, default='ModelGender', help='model type (default: BaseModel)')
parser.add_argument('--model_age', type=str, default='ModelAge', help='model type (default: BaseModel)')
# Container environment
parser.add_argument('--data_dir', type=str, default=os.environ.get('SM_CHANNEL_EVAL', '/opt/ml/input/data/eval'))
parser.add_argument('--model_mask_dir', type=str, default=os.environ.get('SM_CHANNEL_MODEL', './model/exp_mask'))
parser.add_argument('--model_gender_dir', type=str, default=os.environ.get('SM_CHANNEL_MODEL', './model/exp_gender'))
parser.add_argument('--model_age_dir', type=str, default=os.environ.get('SM_CHANNEL_MODEL', './model/exp_age'))
parser.add_argument('--output_dir', type=str, default=os.environ.get('SM_OUTPUT_DATA_DIR', './output'))
parser.add_argument('--output_name', type=str, default='output')
args = parser.parse_args()
data_dir = args.data_dir
mask_model_dir = args.model_mask_dir
gender_model_dir = args.model_gender_dir
age_model_dir = args.model_age_dir
output_dir = args.output_dir
os.makedirs(output_dir, exist_ok=True)
info_mask = inference_mask(data_dir, mask_model_dir, args)
info_gender = inference_gender(data_dir, gender_model_dir, args)
info_age = inference_age(data_dir, age_model_dir, args)
merge_result(info_mask, info_gender, info_age, output_dir)