-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_big_submission_tta.py
141 lines (117 loc) · 4.21 KB
/
test_big_submission_tta.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
"""
Created on Fri Jun 15 09:15:18 2018
Sript that creates file to submit
@author: pablo
"""
import numpy as np
import pandas as pd
import os
from skimage.io import imread
from skimage.transform import resize
from skimage.color import gray2rgb
from scipy import ndimage
from skimage.morphology import dilation
from keras.models import load_model
import cv2
from RLE import *
from utils import *
# Constant variables
TEST_PATH = '/home/pablo/Documents/NucleiCompetition/Nuclei/Data/stage2_test_final' #Path to the test images
OUTPUT_PATH = '/home/pablo/Documents/NucleiCompetition/Nuclei/Models/validation_models' #Path to the model
SUBMISSION_PATH = '/home/pablo/Documents/NucleiCompetition/Nuclei/Submissions/' #Path to the submissions folder
H = 128
W = 128
C = 3
test_names_all = os.listdir(TEST_PATH)
thresh_lab = 0.6
thresh_cont = 0.5
for j in range(0,6):
if j == 0:
r = range(0,500)
s = 0
e = 500
elif j == 1:
r = range(500,1000)
s = 500
e = 1000
elif j == 2:
r = range(1000,1500)
s = 1000
e = 1500
elif j == 3:
r = range(1500,2000)
s = 1500
e = 2000
elif j == 4:
r = range(2000,2500)
s = 2000
e = 2500
else:
r = range(2500,len(test_names_all))
s = 2500
e = len(test_names_all)
test_number = len(r)
test_names = test_names_all[s:e]
X_test = [0] * test_number
pads_h = [0] * test_number
pads_w = [0] * test_number
for i in np.arange(0,test_number):
path = os.path.join(TEST_PATH, test_names[i], 'images', test_names[i] + '.png')
im = imread(path, as_grey=False)
if im.ndim < 3:
im = gray2rgb(im)
else:
im = im[:,:,:C]
X_test[i],pads_h[i],pads_w[i] = pad_test(im)
print('Loading: '+str(1000*j + i)+' / '+str(len(test_names_all)))
Y = [0] * test_number
model = load_model(os.path.join(OUTPUT_PATH,'model_30_2.h5'))
for i in range(0,test_number):
print(str(i)+' / '+str(test_number))
if X_test[i].shape[1] < 600:
im = doTTA(X_test[i])
temp_lab, temp_cont = model.predict(im,verbose=0)
temp_lab = temp_lab[:,:,:,0]
temp_cont = temp_cont[:,:,:,0]
Y_lab_test = undoTTA(temp_lab)
Y_cont_test = undoTTA(temp_cont)
Y_lab_test = unpad_test(Y_lab_test,pads_h[i],pads_w[i])
Y_cont_test = unpad_test(Y_cont_test,pads_h[i],pads_w[i])
else:
im = X_test[i]
temp_lab, temp_cont = model.predict(np.expand_dims(X_test[i],axis=0),verbose=0)
temp_lab = temp_lab[:,:,:,0]
temp_cont = temp_cont[:,:,:,0]
Y_lab_test = unpad_test(temp_lab[0],pads_h[i],pads_w[i])
Y_cont_test = unpad_test(temp_cont[0],pads_h[i],pads_w[i])
Y[i] = (Y_lab_test >= thresh_lab) & (Y_cont_test <= thresh_cont)
Y[i] = Y[i].astype(np.int16)
Y[i] = ndimage.binary_fill_holes(Y[i])
Y[i] = label(Y[i])
Y[i] = dilation(Y[i])
if len(np.unique(Y[i])) == 1:
Y[i] = (Y_lab_test >= thresh_lab-0.1) & (Y_cont_test <= thresh_cont)
Y[i] = Y[i].astype(np.int16)
Y[i] = ndimage.binary_fill_holes(Y[i])
Y[i] = label(Y[i])
Y[i] = dilation(Y[i])
if len(np.unique(Y[i])) == 1:
Y[i] = (Y_lab_test >= thresh_lab-0.2) & (Y_cont_test <= thresh_cont)
Y[i] = Y[i].astype(np.int16)
Y[i] = ndimage.binary_fill_holes(Y[i])
Y[i] = label(Y[i])
Y[i] = dilation(Y[i])
prepareSubmission_new(test_names,Y,SUBMISSION_PATH+'gen'+str(j+1)+'.csv')
results = pd.read_csv(SUBMISSION_PATH+'gen1.csv')
for i in range(2,7):
results = results.append(pd.read_csv(SUBMISSION_PATH+'gen'+str(i)+'.csv'))
ids = results['ImageId']
ids = ids.tolist()
unique_ids = unique(ids)
non_app = findNonAppeared(test_names_all,unique_ids)
sub = pd.DataFrame()
sub['ImageId'] = non_app
sub['EncodedPixels'] = '1 1'
sub.to_csv(SUBMISSION_PATH + 'gen_nonapp.csv', index=False)
results.append(pd.read_csv(SUBMISSION_PATH+'gen_nonapp.csv'))
results.to_csv(SUBMISSION_PATH+'final.csv',index=False)