-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
92 lines (76 loc) · 2.96 KB
/
predict.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
import cv2
import numpy as np
import pandas as pd
from utils import post_process
from dataset import mask2rle
from tqdm import tqdm
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def predict(loaders=None,
runner=None,
class_params: dict = None,
path: str = '',
sub_name: str = ''):
"""
Args:
loaders:
runner:
class_params:
path:
sub_name:
Returns:
"""
encoded_pixels = []
image_id = 0
for _, test_batch in tqdm(enumerate(loaders['test'])):
runner_out = runner.predict_batch({"features": test_batch[0].cuda()})['logits']
for _, batch in (enumerate(runner_out)):
for probability in batch:
probability = probability.cpu().detach().numpy()
if probability.shape != (350, 525):
probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR)
prediction, num_predict = post_process(sigmoid(probability), class_params[str(image_id % 4)][0],
class_params[str(image_id % 4)][1])
if num_predict == 0:
encoded_pixels.append('')
else:
r = mask2rle(prediction)
encoded_pixels.append(r)
image_id += 1
sub = pd.read_csv(f'{path}/sample_submission.csv')
sub['EncodedPixels'] = encoded_pixels
sub.to_csv(f'submissions/submission_{sub_name}.csv', columns=['Image_Label', 'EncodedPixels'], index=False)
def predict_blend(loaders=None,
runner=None,
class_params: dict = None,
path: str = '',
sub_name: str = ''):
"""
Args:
loaders:
runner:
class_params:
path:
sub_name:
Returns:
"""
encoded_pixels = []
image_id = 0
for _, test_batch in tqdm(enumerate(loaders['test'])):
runner_out = runner.predict_batch({"features": test_batch[0].cuda()})['logits']
for _, batch in (enumerate(runner_out)):
for probability in batch:
probability = probability.cpu().detach().numpy()
if probability.shape != (350, 525):
probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR)
prediction, num_predict = post_process((probability), class_params[str(image_id % 4)][0],
class_params[str(image_id % 4)][1])
if num_predict == 0:
encoded_pixels.append('')
else:
r = mask2rle(prediction)
encoded_pixels.append(r)
image_id += 1
sub = pd.read_csv(f'{path}/sample_submission.csv')
sub['EncodedPixels'] = encoded_pixels
sub.to_csv(f'submissions/submission_{sub_name}.csv', columns=['Image_Label', 'EncodedPixels'], index=False)