-
Notifications
You must be signed in to change notification settings - Fork 13
/
prediction.py
170 lines (137 loc) · 6.19 KB
/
prediction.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
import argparse
import json
import os
import warnings
import numpy as np
import pandas as pd
import torch
from tqdm import tqdm
warnings.filterwarnings("ignore")
from catalyst.dl.utils import load_checkpoint
import segmentation_models_pytorch as smp
from models import CustomNet
from utils import predict_batch
from utils.utils import mask2rle, post_process
from utils.config import load_config
from datasets import make_loader
from transforms import get_transforms
def run_cls(config_file_cls):
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# ------------------------------------------------------------------------------------------------------------
# 1. classification inference
# ------------------------------------------------------------------------------------------------------------
config = load_config(config_file_cls)
model = CustomNet(config.model.encoder, config.data.num_classes)
testloader = make_loader(
data_folder=config.data.test_dir,
df_path=config.data.sample_submission_path,
phase='test',
batch_size=config.test.batch_size,
num_workers=config.num_workers,
transforms=get_transforms(config.transforms.test),
num_classes=config.data.num_classes,
)
model.to(config.device)
model.eval()
checkpoint = load_checkpoint(f"{config.work_dir}/checkpoints/best.pth")
model.load_state_dict(checkpoint['model_state_dict'])
all_fnames = []
all_predictions = []
with torch.no_grad():
for i, (batch_fnames, batch_images) in enumerate(tqdm(testloader)):
batch_images = batch_images.to(config.device)
batch_preds = predict_batch(model, batch_images, tta=config.test.tta, task='cls')
all_fnames.extend(batch_fnames)
all_predictions.append(batch_preds)
all_predictions = np.concatenate(all_predictions)
np.save('all_preds', all_predictions)
df = pd.DataFrame(data=all_predictions, index=all_fnames)
df.to_csv('cls_preds.csv', index=False)
df.to_csv(f"{config.work_dir}/cls_preds.csv", index=False)
def run_seg(config_file_seg):
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# ------------------------------------------------------------------------------------------------------------
# 2. segmentation inference
# ------------------------------------------------------------------------------------------------------------
config = load_config(config_file_seg)
model = getattr(smp, config.model.arch)(
encoder_name=config.model.encoder,
encoder_weights=config.model.pretrained,
classes=config.data.num_classes,
activation=None,
)
if os.path.exists('cls_preds.csv'):
testloader = make_loader(
data_folder=config.data.test_dir,
df_path='cls_preds.csv',
phase='filtered_test',
batch_size=config.test.batch_size,
num_workers=config.num_workers,
transforms=get_transforms(config.transforms.test)
)
else:
testloader = make_loader(
data_folder=config.data.test_dir,
df_path=config.data.sample_submission_path,
phase='test',
batch_size=config.test.batch_size,
num_workers=config.num_workers,
transforms=get_transforms(config.transforms.test)
)
model.to(config.device)
model.eval()
checkpoint = load_checkpoint(f"{config.work_dir}/checkpoints/best.pth")
model.load_state_dict(checkpoint['model_state_dict'])
if os.path.exists(config.work_dir + '/threshold_search.json'):
with open(config.work_dir + '/threshold_search.json') as json_file:
data = json.load(json_file)
df = pd.DataFrame(data)
min_sizes = list(df.T.idxmax().values.astype(int))
print('load best threshold from validation:', min_sizes)
else:
min_sizes = config.test.min_size
print('load default threshold:', min_sizes)
predictions = []
with torch.no_grad():
for i, (batch_fnames, batch_images) in enumerate(tqdm(testloader)):
batch_images = batch_images.to(config.device)
batch_preds = predict_batch(model, batch_images, tta=config.test.tta)
for fname, preds in zip(batch_fnames, batch_preds):
if config.data.num_classes == 4:
for cls in range(preds.shape[0]):
mask = preds[cls, :, :]
mask, num = post_process(mask, config.test.best_threshold, min_sizes[cls])
rle = mask2rle(mask)
name = fname + f"_{cls + 1}"
predictions.append([name, rle])
else: # == 5
for cls in range(1, 5):
mask = preds[cls, :, :]
mask, num = post_process(mask, config.test.best_threshold, min_sizes[cls])
rle = mask2rle(mask)
name = fname + f"_{cls}"
predictions.append([name, rle])
# ------------------------------------------------------------------------------------------------------------
# submission
# ------------------------------------------------------------------------------------------------------------
df = pd.DataFrame(predictions, columns=['ImageId_ClassId', 'EncodedPixels'])
df.to_csv(config.work_dir + "/submission.csv", index=False)
def parse_args():
parser = argparse.ArgumentParser(description='Severstal')
parser.add_argument('--cls_config', dest='config_file_cls',
help='configuration file path',
default=None, type=str)
parser.add_argument('--seg_config', dest='config_file_seg',
help='configuration file path',
default=None, type=str)
return parser.parse_args()
def main():
args = parse_args()
if args.config_file_cls != None:
print('classification inference Severstal Steel Defect Detection.')
run_cls(args.config_file_cls)
if args.config_file_seg != None:
print('segmentation inference Severstal Steel Defect Detection.')
run_seg(args.config_file_seg)
if __name__ == '__main__':
main()