forked from SarderLab/Collagen_Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollagenSegUtils.py
More file actions
347 lines (230 loc) · 10.9 KB
/
Copy pathCollagenSegUtils.py
File metadata and controls
347 lines (230 loc) · 10.9 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
Utilities included here for collagen segmentation task.
This includes:
output figure generation,
metrics calculation,
etc.
"""
import torch
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from PIL import Image
from Segmentation_Metrics_Pytorch.metric import BinaryMetrics
from skimage.transform import resize
from skimage.color import rgb2gray, rgb2lab, lab2rgb
def back_to_reality(tar):
# Getting target array into right format
classes = np.shape(tar)[-1]
dummy = np.zeros((np.shape(tar)[0],np.shape(tar)[1]))
for value in range(classes):
mask = np.where(tar[:,:,value]!=0)
dummy[mask] = value
return dummy
def apply_colormap(img):
n_classes = np.shape(img)[-1]
if n_classes==2:
image = img[:,:,1]
else:
image = img[:,:,0]
for cl in range(1,n_classes):
image = np.concatenate((image, img[:,:,cl]),axis = 1)
return image
def visualize_multi_task(images,output_type):
n = len(images)
if output_type=='comparison':
fig = plt.figure(constrained_layout = True)
subfigs = fig.subfigures(1,3)
image_keys = list(images.keys())
for outer_ind,subfig in enumerate(subfigs.flat):
current_key = image_keys[outer_ind]
subfig.suptitle(current_key)
if len(images[current_key].shape)==4:
img = images[current_key][0,:,:,:]
else:
img = images[current_key]
if np.shape(img)[0]<np.shape(img)[-1]:
img = np.moveaxis(img,source=0,destination=-1)
img = np.float32(img)
if image_keys[outer_ind]=='Image':
img_ax = subfig.add_subplot(1,1,1)
img_ax.imshow(img)
else:
neg_img = np.uint8(255*np.round(img[:,:,0]))
coll_img = np.uint8(255*img[:,:,1])
axs = subfig.subplots(1,2)
titles = ['Continuous','Binary']
sub_imgs = [coll_img,neg_img]
cmaps = ['jet','jet']
for innerind,ax in enumerate(axs.flat):
ax.set_title(current_key+'_'+titles[innerind])
ax.set_xticks([])
ax.set_yticks([])
ax.imshow(sub_imgs[innerind],cmap=cmaps[innerind])
elif output_type=='prediction':
pred_mask = images['Pred_Mask']
if len(np.shape(pred_mask))==4:
pred_mask = pred_mask[0,:,:,:]
pred_mask = np.float32(pred_mask)
if np.shape(pred_mask)[0]<np.shape(pred_mask)[-1]:
pred_mask = np.moveaxis(pred_mask,source=0,destination = -1)
neg_output = 255*np.round(pred_mask[:,:,0])
coll_output = 255*pred_mask[:,:,1]
#print(f'Collagen min/max: {np.min(coll_output)},{np.max(coll_output)}')
#print(f'Negative image min/max: {np.min(neg_output)},{np.max(neg_output)}')
fig = [coll_output,neg_output]
return fig
def visualize_continuous(images,output_type):
if output_type=='comparison':
n = len(images)
for i,key in enumerate(images):
plt.subplot(1,n,i+1)
plt.xticks([])
plt.yticks([])
plt.title(key)
if len(np.shape(images[key])) == 4:
img = images[key][0,:,:,:]
else:
img = images[key]
img = np.float32(img)
if np.shape(img)[0]<np.shape(img)[-1]:
img = np.moveaxis(img,source=0,destination=-1)
if key == 'Pred_Mask' or key == 'Ground_Truth':
img = apply_colormap(img)
plt.imshow(img,cmap='jet')
else:
plt.imshow(img)
output_fig = plt.gcf()
elif output_type=='prediction':
pred_mask = images['Pred_Mask']
if len(np.shape(pred_mask))==4:
pred_mask = pred_mask[0,:,:,:]
pred_mask = np.float32(pred_mask)
if np.shape(pred_mask)[0]<np.shape(pred_mask)[-1]:
pred_mask = np.moveaxis(pred_mask,source=0,destination = -1)
output_fig = apply_colormap(pred_mask)
return output_fig
def get_metrics(pred_mask,ground_truth,img_name,calculator,target_type):
metrics_row = {}
if target_type=='binary':
edited_gt = ground_truth[:,1,:,:]
edited_gt = torch.unsqueeze(edited_gt,dim = 1)
edited_pred = pred_mask[:,1,:,:]
edited_pred = torch.unsqueeze(edited_pred,dim = 1)
#print(f'edited pred_mask shape: {edited_pred.shape}')
#print(f'edited ground_truth shape: {edited_gt.shape}')
#print(f'Unique values prediction mask : {torch.unique(edited_pred)}')
#print(f'Unique values ground truth mask: {torch.unique(edited_gt)}')
acc, dice, precision, recall,specificity = calculator(edited_gt,torch.round(edited_pred))
metrics_row['Accuracy'] = [round(acc.numpy().tolist(),4)]
metrics_row['Dice'] = [round(dice.numpy().tolist(),4)]
metrics_row['Precision'] = [round(precision.numpy().tolist(),4)]
metrics_row['Recall'] = [round(recall.numpy().tolist(),4)]
metrics_row['Specificity'] = [round(specificity.numpy().tolist(),4)]
#print(metrics_row)
elif target_type == 'nonbinary':
square_diff = (ground_truth.numpy()-pred_mask.numpy())**2
mse = np.mean(square_diff)
norm_mse = (square_diff-np.min(square_diff))/np.max(square_diff)
norm_mse = np.mean(norm_mse)
metrics_row['MSE'] = [round(mse,4)]
metrics_row['Norm_MSE']=[round(norm_mse,4)]
elif target_type == 'multi_task':
bin_gt = ground_truth[:,0,:,:]
bin_gt = torch.squeeze(bin_gt)
bin_pred = pred_mask[0,:,:]
acc, dice, precision, recall, sensitivity = calculator(bin_gt,torch.round(bin_pred))
metrics_row['Accuracy'] = [round(acc.numpy().tolist(),4)]
metrics_row['Dice'] = [round(dice.numpy().tolist(),4)]
metrics_row['Precision'] = [round(precision.numpy().tolist(),4)]
metrics_row['Recall'] = [round(recall.numpy().tolist(),4)]
metrics_row['Specificity'] = [round(specificity.numpy().tolist(),4)]
metrics_row['Sensitivity'] = [round(sensitivity.numpy().tolist(),4)]
reg_gt = ground_truth[:,1,:,:]
reg_gt = torch.squeeze(reg_gt)
reg_pred = pred_mask[1,:,:]
square_diff = (reg_gt.numpy()-reg_pred.numpy())**2
mse = np.mean(square_diff)
norm_mse = (square_diff-np.min(square_diff))/np.max(square_diff)
norm_mse = np.mean(norm_mse)
metrics_row['MSE'] = [round(mse,4)]
metrics_row['Norm_MSE'] = [round(norm_mse,4)]
metrics_row['ImgLabel'] = img_name
return metrics_row
# Function to resize and apply any condensing transform like grayscale conversion
def resize_special(img,output_size,transform):
# multi-image input transform
if 'multi_input' in transform:
if transform =='multi_input_invbf':
# Inverting brightfield channels
img = resize(img,output_shape=(output_size))
f_img = img[:,:,0:3]
f_img = f_img/np.sum(f_img,axis=-1)[:,:,None]
b_img = 255-img[:,:,2:5]
b_img = b_img/np.sum(b_img,axis=-1)[:,:,None]
img = np.concatenate((f_img,b_img),axis=-1)
elif transform =='multi_input_green_invbf':
# Green channels, inverting bf
#img = resize(img, output_shape = (output_size))
f_img = img[:,:,1]
f_img = (f_img - np.min(f_img))/np.ptp(f_img)
b_img = 255-img[:,:,4]
b_img = (b_img - np.min(b_img))/np.ptp(b_img)
img = np.concatenate((f_img[:,:,None],b_img[:,:,None]),axis=-1)
#img = resize(img,output_shape = (output_size))
elif transform == 'multi_input_mean_invbf':
# Mean of color channels, inverting bf
f_img = np.mean(img[:,:,0:3],axis=-1)
f_img = (f_img - np.min(f_img))/np.ptp(f_img)
b_img = 255-np.mean(img[:,:,2:5],axis=-1)
b_img = (b_img - np.min(b_img))/np.ptp(b_img)
img = np.concatenate((f_img[:,:,None],b_img[:,:,None]),axis=-1)
elif transform=='multi_input_green':
# Grabbing green channels without inverting
f_img = img[:,:,1]
b_img = img[:,:,4]
img = np.concatenate((f_img[:,:,None],b_img[:,:,None]),axis=-1)
elif transform == 'multi_input_mean':
# Grabbing mean of brightfield and fluorescent images and concatenating them
f_img = np.mean(img[:,:,0:3])
b_img = np.mean(img[:,:,2:5])
img = np.concatenate((f_img[:,:,None],b_img[:,:,None]),axis=-1)
else:
if transform=='mean':
img = np.mean(img,axis = -1)
img = img[:,:,np.newaxis]
elif transform in ['red','green','blue']:
color_list = ['red','green','blue']
img = img[:,:,color_list.index(transform)]
img = img[:,:,np.newaxis]
elif transform == 'rgb2gray':
img = rgb2gray(img)
img = img[:,:,np.newaxis]
elif transform == 'rgb2lab':
img = rgb2lab(img)
elif type(transform)==dict:
# Determining non-tissue regions to mask out prior to scaling/conversion
# For BF images the non-tissue regions are closer to white whereas with fluorescence images they
# are closer to black
lab_img = rgb2lab(img)
scaled_img = (lab_img-np.nanmean(lab_img))/np.nanstd(lab_img)
for i in range(3):
scaled_img[:,:,i] = scaled_img[:,:,i]*transform['norm_std'][i]+transform['norm_mean'][i]
# converting back to rgb
img = (scaled_img-np.nanmean(scaled_img))/np.nanstd(scaled_img)
elif transform == 'invert_bf_intensity':
# Grabbing the green channel from both the fluorescence and brightfield images
f_green_img = img[:,:,1]
f_green_img = np.divide(f_green_img,np.sum(img[:,:,0:3],axis=-1),where=(np.sum(img[:,:,0:3],axis=-1)!=0))
# Inverting brightfield channels
b_green_inv_img = 255-img[:,:,3]
b_green_inv_img = np.divide(b_green_inv_img,np.sum(255-img[:,:,2:5],axis=-1),where=(np.sum(255-img[:,:,2:5],axis=-1)!=0))
img = np.concatenate((f_green_img[:,:,None],b_green_inv_img[:,:,None]),axis=-1)
elif transform == 'invert_bf_01norm':
inv_bf = 255-img[:,:,2:5]
inv_bf_norm = np.divide(inv_bf,np.sum(inv_bf,axis=-1)[:,:,None],where=(np.sum(inv_bf,axis=-1)[:,:,None]!=0))
f_img = img[:,:,0:3]
f_norm = np.divide(f_img,np.sum(f_img,axis=-1)[:,:,None],where=(np.sum(f_img,axis=-1)[:,:,None]!=0))
img = np.concatenate((f_norm,inv_bf_norm),axis=-1)
img = np.float32(resize(img,output_size))
return img