-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
50 lines (41 loc) · 1.53 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
import cv2
import numpy as np
import pandas as pd
from utils import post_process
from dataset import mask2rle
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 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[image_id % 4][0],
class_params[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)