-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputs_and_ground_truth_saving.py
282 lines (207 loc) · 14.8 KB
/
outputs_and_ground_truth_saving.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import tensorflow as tf
import keras
print(tf.__version__)
print(keras.__version__)
from keras.models import load_model
from scipy.stats import mode
import gc
import tifffile
from sklearn.metrics import jaccard_score, f1_score, accuracy_score
import matplotlib.pyplot as plt
#Make sure the GPU is available.
import tensorflow as tf
device_name = tf.test.gpu_device_name()
#Make sure the GPU is available.
import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
import segmentation_models_3D as sm
from skimage import io
from patchify import patchify, unpatchify
import numpy as np
from matplotlib import pyplot as plt
from keras import backend as K
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
more_data = True
vista_crop_dict = { 0:'NA' , 1: 'ALFALFA', 2: 'BEET', 3: 'CLOVER', 4: 'FLAX', 5: 'FLOWERING_LEGUMES', 6: 'FLOWERS', 7: 'FOREST', 8: 'GRAIN_MAIZE', 9: 'GRASSLAND', 10: 'HOPS', 11: 'LEGUMES', 12: 'VISTA_NA', 13: 'PERMANENT_PLANTATIONS', 14: 'PLASTIC', 15: 'POTATO', 16: 'PUMPKIN', 17: 'RICE', 18: 'SILAGE_MAIZE', 19: 'SOY', 20: 'SPRING_BARLEY', 21: 'SPRING_OAT', 22: 'SPRING_OTHER_CEREALS', 23: 'SPRING_RAPESEED', 24: 'SPRING_RYE', 25: 'SPRING_SORGHUM', 26: 'SPRING_SPELT', 27: 'SPRING_TRITICALE', 28: 'SPRING_WHEAT', 29: 'SUGARBEET', 30: 'SUNFLOWER', 31: 'SWEET_POTATOES', 32: 'TEMPORARY_GRASSLAND', 33: 'WINTER_BARLEY', 34: 'WINTER_OAT', 35: 'WINTER_OTHER_CEREALS', 36: 'WINTER_RAPESEED', 37: 'WINTER_RYE', 38: 'WINTER_SORGHUM', 39: 'WINTER_SPELT', 40: 'WINTER_TRITICALE', 41: 'WINTER_WHEAT'}
if more_data:
chosen_crop_types_list_list = [[1, 2, 3], [4, 5, 7], [8, 9, 10], [11, 12, 13], [14, 15, 16], [18, 19, 20], [21, 23, 27], [28, 30, 32], [33, 34, 35]]#, [36, 37, 40], [37, 40, 41]]
else:
chosen_crop_types_list_list = [[1, 2, 3], [4, 5, 7], [8, 9, 10], [11, 12, 13], [14, 15, 16], [18, 19, 20], [21, 23, 27], [28, 30, 32], [33, 34, 35], [36, 37, 40], [37, 40, 41]]
if more_data:
num_epochs = 450
else:
num_epochs = 1500
all_images_iou = []
all_images_f1 = []
for chosen_crop_types_list_indata in chosen_crop_types_list_list:
#chosen_crop_types_list = [1, 2, 3]
print("vista_crop_dict[chosen_crop_types_list[1]], vista_crop_dict[chosen_crop_types_list[1]], vista_crop_dict[chosen_crop_types_list[1]]", vista_crop_dict[chosen_crop_types_list_indata[0]], vista_crop_dict[chosen_crop_types_list_indata[1]], vista_crop_dict[chosen_crop_types_list_indata[2]])
all_input_img = []
all_input_mask = []
all_input_img_f = []
all_input_mask_f = []
sampling_group_fractions = [1.0, 1.0, 1.0]
counted = 0
for i in chosen_crop_types_list_indata:
input_img = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train_'+vista_crop_dict[i]+'.tif')
input_mask = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab_'+vista_crop_dict[i]+'.tif').astype(np.uint8)
if more_data:
#print("input_img.shape", input_img.shape)
#print("input_mask.shape", input_mask.shape)
input_img0 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train'+vista_crop_dict[i]+'n0.tif')
input_mask0 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab'+vista_crop_dict[i]+'n0.tif').astype(np.uint8)
#print("input_img0.shape", input_img0.shape)
#print("input_mask0.shape", input_mask0.shape)
input_img1 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train'+vista_crop_dict[i]+'n1.tif')
input_mask1 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab'+vista_crop_dict[i]+'n1.tif').astype(np.uint8)
#print("input_img1.shape", input_img1.shape)
#print("input_mask1.shape", input_mask1.shape)
input_img2 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train'+vista_crop_dict[i]+'n2.tif')
input_mask2 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab'+vista_crop_dict[i]+'n2.tif').astype(np.uint8)
#print("input_img2.shape", input_img2.shape)
#print("input_mask2.shape", input_mask2.shape)
input_img3 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train'+vista_crop_dict[i]+'n3.tif')
input_mask3 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab'+vista_crop_dict[i]+'n3.tif').astype(np.uint8)
#print("input_img3.shape", input_img3.shape)
#print("input_mask3.shape", input_mask3.shape)
input_img4 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train'+vista_crop_dict[i]+'n4.tif')
input_mask4 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab'+vista_crop_dict[i]+'n4.tif').astype(np.uint8)
#print("input_img4.shape", input_img4.shape)
#print("input_mask4.shape", input_mask4.shape)
input_img5 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/train'+vista_crop_dict[i]+'n5.tif')
input_mask5 = io.imread('/home/luser/stelar_3dunet/storage/per_crop_data_labels/'+vista_crop_dict[i]+'/lab'+vista_crop_dict[i]+'n5.tif').astype(np.uint8)
#print("input_img5.shape", input_img5.shape)
#print("input_mask5.shape", input_mask5.shape)
input_img_f = np.concatenate((input_img, input_img0, input_img1, input_img2, input_img3, input_img4, input_img5), axis=0)
input_mask_f = np.concatenate((input_mask, input_mask0, input_mask1, input_mask2, input_mask3, input_mask4, input_mask5), axis=0)
bis = int(len(input_img_f)*sampling_group_fractions[counted]) - 50
input_img_f = input_img_f[:bis]
input_mask_f = input_mask_f[:bis]
all_input_img_f.append(input_img_f)
all_input_mask_f.append(input_mask_f)
counted+=1
else:
all_input_img_f.append(input_img)
all_input_mask_f.append(input_mask)
input_img = np.concatenate((all_input_img_f), axis=0).reshape(-1, 64, 64, 64)
input_mask = np.concatenate((all_input_mask_f), axis=0).reshape(-1, 64, 64)
input_mask = np.repeat(input_mask[:, np.newaxis, :, :], repeats=64, axis=1)
#print("input_img.shape", input_img.shape)
#print("input_mask.shape", input_mask.shape)
unique_elements, element_counts = np.unique(input_mask, return_counts=True)
#clear memory
del all_input_img
del all_input_mask
lai_uniques = np.unique(input_img)
for n in range(len(lai_uniques)):
input_img[input_img==lai_uniques[n]]=n
input_img = input_img.astype(np.uint8)
# clear memory
del lai_uniques
n_classes=4
BACKBONE = 'vgg16' #Try vgg16, efficientnetb7, inceptionv3, resnet50
#BACKBONE = 'resnet50' #Try vgg16, efficientnetb7, inceptionv3, resnet50
preprocess_input = sm.get_preprocessing(BACKBONE)
def get_labels_in_color(groud_truth_image):
color_map = {
0: [0, 0, 0],1: [0, 255, 0], 2: [0, 0, 255], 3: [255, 255, 0], 4: [255, 165, 0], 5: [255, 0, 255], 6: [0, 255, 255],
7: [128, 0, 128], 8: [128, 128, 0], 9: [0, 128, 0], 10: [128, 0, 0], 11: [0, 0, 128], 12: [128, 128, 128], 13: [0, 128, 128],
14: [255, 0, 0], 15: [255, 255, 255], 16: [192, 192, 192], 17: [255, 0, 0], 18: [0, 255, 0], 19: [0, 0, 255], 20: [255, 255, 0],
21: [255, 165, 0], 22: [255, 0, 255], 23: [0, 255, 255], 24: [128, 0, 128], 25: [128, 128, 0], 26: [0, 128, 0],
27: [128, 0, 0], 28: [0, 0, 128], 29: [128, 128, 128], 30: [0, 128, 128], 31: [0, 0, 0], 32: [255, 255, 255],
33: [192, 192, 192], 34: [255, 0, 0], 35: [0, 255, 0], 36: [0, 0, 255], 37: [255, 255, 0], 38: [255, 165, 0],
39: [255, 0, 255], 40: [0, 128, 255], 41: [192, 192, 192] }
groud_truth_color_image = np.zeros(groud_truth_image.shape + (3,), dtype=np.uint8)
for i in range(groud_truth_image.shape[0]):
for j in range(groud_truth_image.shape[1]):
segment_id_gt = groud_truth_image[i, j]
groud_truth_color_image[i, j] = color_map[segment_id_gt]
return groud_truth_color_image
color_map = {
0: [0, 0, 0],1: [0, 255, 0], 2: [0, 0, 255], 3: [255, 255, 0], 4: [255, 165, 0], 5: [255, 0, 255], 6: [0, 255, 255],
7: [128, 0, 128], 8: [128, 128, 0], 9: [0, 128, 0], 10: [128, 0, 0], 11: [0, 0, 128], 12: [128, 128, 128], 13: [0, 128, 128],
14: [255, 0, 0], 15: [255, 255, 255], 16: [192, 192, 192], 17: [255, 0, 0], 18: [0, 255, 0], 19: [0, 0, 255], 20: [255, 255, 0],
21: [255, 165, 0], 22: [255, 0, 255], 23: [0, 255, 255], 24: [128, 0, 128], 25: [128, 128, 0], 26: [0, 128, 0],
27: [128, 0, 0], 28: [0, 0, 128], 29: [128, 128, 128], 30: [0, 128, 128], 31: [0, 0, 0], 32: [255, 255, 255],
33: [192, 192, 192], 34: [255, 0, 0], 35: [0, 255, 0], 36: [0, 0, 255], 37: [255, 255, 0], 38: [255, 165, 0],
39: [255, 0, 255], 40: [0, 128, 255], 41: [192, 192, 192] }
for test_img_number in range(30):
input_mask_1 = input_mask.copy()
train_img = np.stack((input_img,)*3, axis=-1)
ensambled_ground_truth = np.zeros((64, 64))
ensambled_result_image = np.zeros((64, 64))
ensambled_result_image_gen = np.zeros((64, 64, 64))
stacked_test_preds = []
nnn = 0
for chosen_crop_types_list in chosen_crop_types_list_list:
input_mask_1[input_mask_1<chosen_crop_types_list[0]]=0
input_mask_1[input_mask_1>chosen_crop_types_list[-1]]=0
for i in range(3):
input_mask_1[input_mask_1==chosen_crop_types_list[i]]=i+1
train_mask = np.expand_dims(input_mask_1, axis=4)
train_mask_cat = to_categorical(train_mask, num_classes=n_classes)
X_train, X_test, y_train, y_test_1 = train_test_split(train_img, train_mask_cat, test_size = 0.10, random_state = 0)
print("y_test_1.shape", y_test_1.shape)
del X_train
del train_mask_cat
del train_mask
del y_train
del input_mask_1
gc.collect()
if more_data:
my_model_1 = load_model('/home/luser/stelar_3dunet/storage/saved_model_bias_miti_class_weights_corrected_more_data/3D_unet_res_labels_'+vista_crop_dict[chosen_crop_types_list[0]]+'_'+vista_crop_dict[chosen_crop_types_list[1]]+'_'+vista_crop_dict[chosen_crop_types_list[2]]+'_num_epocs_'+str(num_epochs)+'.h5', compile=False)
else:
my_model_1 = load_model('/home/luser/stelar_3dunet/storage/saved_model/3D_unet_res_labels_'+vista_crop_dict[chosen_crop_types_list[0]]+'_'+vista_crop_dict[chosen_crop_types_list[1]]+'_'+vista_crop_dict[chosen_crop_types_list[2]]+'_num_epocs_'+str(num_epochs)+'.h5', compile=False)
test_img = X_test[test_img_number-1]
ground_truth_1 = y_test_1[test_img_number-1]
del y_test_1
ground_truth_argmax_1 = np.argmax(ground_truth_1, axis=3)
del ground_truth_1
test_img_input=np.expand_dims(test_img, 0)
test_img_input1 = preprocess_input(test_img_input, backend='tf')
del test_img_input
test_pred1 = my_model_1.predict(test_img_input1)
del test_img_input1
del my_model_1
gc.collect()
test_prediction1 = np.argmax(test_pred1, axis=4)
test_prediction1 = np.argmax(test_pred1, axis=4)[0,:,:,:]
for i in range(3):
test_prediction1[test_prediction1==i+1]= chosen_crop_types_list[i]
ground_truth_argmax_1[ground_truth_argmax_1==i+1]= chosen_crop_types_list[i]
#result_image1 = np.median(test_prediction1, axis=0).astype(np.uint8)
ground_truth_image1 = np.median(ground_truth_argmax_1, axis=0).astype(np.uint8)
for i in range(3):
ensambled_ground_truth[ground_truth_image1==chosen_crop_types_list[i]]=chosen_crop_types_list[i]
#ensambled_result_image[result_image1==chosen_crop_types_list[i]]=chosen_crop_types_list[i]
ensambled_result_image_gen[test_prediction1==chosen_crop_types_list[i]]=chosen_crop_types_list[i]
stacked_test_preds.append(ensambled_result_image_gen.copy())
ensambled_result_image_gen = np.zeros((64, 64, 64))
input_mask_1 = input_mask.copy()
nnn = nnn+1
stacked_test_preds = np.concatenate((stacked_test_preds), axis=0)
#ensambled_result_image_gen = np.median(stacked_test_preds, axis=0).astype(np.uint8)
test_mode = np.zeros((64, 64))
for i in range(64):
for j in range(64):
see = stacked_test_preds[:, i, j]
sum_see = np.sum(see)
if sum_see == 0:
test_mode[i, j] = 0
else:
local_mode = mode(see[see>0])[0]
test_mode[i, j]= local_mode
print("test_mode.shape", test_mode.shape)
print("ensambled_ground_truth.shape", ensambled_ground_truth.shape)
ground_truth = ensambled_ground_truth
prediction = test_mode
if more_data:
tifffile.imsave('/home/luser/stelar_3dunet/ensamble_results/iou_f1_more_data/ground_truth_'+str(test_img_number)+'_contains'+vista_crop_dict[chosen_crop_types_list_indata[0]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[1]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[2]]+'_.tif', ground_truth)
tifffile.imsave('/home/luser/stelar_3dunet/ensamble_results/iou_f1_more_data/prediction_'+str(test_img_number)+'_contains'+vista_crop_dict[chosen_crop_types_list_indata[0]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[1]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[2]]+'_.tif', prediction)
else:
tifffile.imsave('/home/luser/stelar_3dunet/ensamble_results/iou_f1/ground_truth_'+str(test_img_number)+'_contains'+vista_crop_dict[chosen_crop_types_list_indata[0]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[1]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[2]]+'_.tif', ground_truth)
tifffile.imsave('/home/luser/stelar_3dunet/ensamble_results/iou_f1/prediction_'+str(test_img_number)+'_contains'+vista_crop_dict[chosen_crop_types_list_indata[0]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[1]]+'_'+vista_crop_dict[chosen_crop_types_list_indata[2]]+'_.tif', prediction)