-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
153 lines (113 loc) · 4.85 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
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
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
import numpy as np
import cv2
import json
import numpy as np
import tqdm
import torch
import warnings
warnings.filterwarnings(action='ignore')
from modules.utils import seed_everything
def select_contour(contours):
# contour가 2개 이상일 경우
# 가장 point 수가 많은 contour를 mask로 사용
largest_contour_id = 0
max_point_counts = 0
for contour_id, contour in enumerate(contours):
if len(contour) > max_point_counts:
largest_contour_id = contour_id
max_point_counts = len(contour)
else:
pass
return largest_contour_id
def contour_to_mask(polygon):
temp = []
for point in polygon:
temp.append(tuple(point.values()))
mask_img = Image.new('L', (512, 512), 'black')
ImageDraw.Draw(mask_img).polygon(temp, outline='white', fill='white')
return mask_img
def set_form(contour):
polygon1 = []
for _, coord in enumerate(contour):
x = coord[0,0]
y = coord[0,1]
polygon1.append({"x":int(x), "y":int(y)})
return polygon1
def get_iou(mask1, mask2):
union = np.logical_or(mask1, mask2)
intersection = np.logical_and(mask1, mask2)
iou_score = np.sum(intersection) / np.sum(union)
return iou_score
def compare_contour(sample_pred):
contours1, _ = cv2.findContours(sample_pred,
cv2.RETR_EXTERNAL,
#cv2.CHAIN_APPROX_SIMPLE)
cv2.CHAIN_APPROX_NONE) # 516개 point 생성 -> 총 json 파일 용량이 200MB를 초과
# cv2.CHAIN_APPROX_TC89_L1) # 171개 point 생성 -> 52MB 초과
# cv2.CHAIN_APPROX_TC89_KCOS) #130개 point 생성
contours2, _ = cv2.findContours(sample_pred,
cv2.RETR_EXTERNAL,
# cv2.CHAIN_APPROX_SIMPLE)
# cv2.CHAIN_APPROX_NONE) # 516개 point 생성 -> 총 json 파일 용량이 200MB를 초과
# cv2.CHAIN_APPROX_TC89_L1) # 171개 point 생성 -> 52MB 초과
cv2.CHAIN_APPROX_TC89_KCOS) #130개 point 생성
if len(contours1) >= 2:
largest_contour_id1 = select_contour(contours1)
else:
largest_contour_id1 = 0
if len(contours2) >= 2:
largest_contour_id2 = select_contour(contours2)
else:
largest_contour_id2 = 0
contour1 = contours1[largest_contour_id1]
contour2 = contours2[largest_contour_id2]
polygon1 = set_form(contour1)
polygon2 = set_form(contour2)
mask1 = contour_to_mask(polygon1)
mask2 = contour_to_mask(polygon2)
diff = get_iou(mask1, mask2)
return diff , polygon1, polygon2
if __name__ == "__main__":
DATA_PATH = "/DATA/Final_DATA"
with open(DATA_PATH + "/task02_test/sample_submission.json", "r") as json_file:
labels = json.load(json_file)
seed_everything(1015)
with torch.no_grad():
PATH_1 = "./model/first_model.pth"
model_1 = torch.load(PATH_1, map_location = "cuda")
PATH_2 = "./model/second_model.pth"
model_2 = torch.load(PATH_2, map_location = "cuda")
diff_array = []
polygon1s = []
polygon2s = []
for idx, item_name in tqdm.tqdm(enumerate(labels['annotations']),
total = len(labels['annotations'])):
sample_img = Image.open(DATA_PATH + "/task02_test/images/" + item_name['file_name'])
sample_img = np.array(sample_img) / 255
sample_img = sample_img.transpose(2, 0, 1).astype('float32')
sample_tensor_1 = torch.cuda.FloatTensor(sample_img[np.newaxis,...],
device = "cuda")
sample_tensor_2 = torch.cuda.FloatTensor(sample_img[np.newaxis,...],
device = "cuda")
sample_pred_1 = model_1(sample_tensor_1).cpu().detach().numpy()[0,0,...]
sample_pred_2 = model_2(sample_tensor_2).cpu().detach().numpy()[0,0,...]
sample_pred = (sample_pred_1 + sample_pred_2) / 2
sample_pred = (sample_pred > 0.5) * 1
sample_pred = sample_pred.astype(np.uint8)
diff, polygon1, polygon2 = compare_contour(sample_pred)
diff_array.append(diff)
polygon1s.append(polygon1)
polygon2s.append(polygon2)
threshold = np.quantile(diff_array, 0.04).round(5)
print(threshold)
for idx, item_name in tqdm.tqdm(enumerate(labels['annotations']),
total = len(labels['annotations'])):
if diff_array[idx] < threshold:
polygon = polygon1s[idx]
else:
polygon = polygon2s[idx]
labels['annotations'][idx]['polygon1'] = polygon
with open("./output/submission.json", "w") as json_file:
json.dump(labels, json_file)