-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
executable file
·312 lines (254 loc) · 11.3 KB
/
helpers.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
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
from __future__ import print_function, division
import os,time,cv2, sys, math
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
import time, datetime
import os, random
from scipy.misc import imread
import ast
from sklearn.metrics import precision_score, \
recall_score, confusion_matrix, classification_report, \
accuracy_score, f1_score
import helpers
# Takes an absolute file path and returns the name of the file without th extension
def filepath_to_name(full_name):
file_name = os.path.basename(full_name)
file_name = os.path.splitext(file_name)[0]
return file_name
# Print with time. To console or file
def LOG(X, f=None):
time_stamp = datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
if not f:
print(time_stamp + " " + X)
else:
f.write(time_stamp + " " + X)
# Count total number of parameters in the model
def count_params():
total_parameters = 0
for variable in tf.trainable_variables():
shape = variable.get_shape()
variable_parameters = 1
for dim in shape:
variable_parameters *= dim.value
total_parameters += variable_parameters
print("This model has %d trainable parameters"% (total_parameters))
# Subtracts the mean images from ImageNet
def mean_image_subtraction(inputs, means=[123.68, 116.78, 103.94]):
inputs=tf.to_float(inputs)
num_channels = inputs.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=3, num_or_size_splits=num_channels, value=inputs)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=3, values=channels)
def _lovasz_grad(gt_sorted):
"""
Computes gradient of the Lovasz extension w.r.t sorted errors
See Alg. 1 in paper
"""
gts = tf.reduce_sum(gt_sorted)
intersection = gts - tf.cumsum(gt_sorted)
union = gts + tf.cumsum(1. - gt_sorted)
jaccard = 1. - intersection / union
jaccard = tf.concat((jaccard[0:1], jaccard[1:] - jaccard[:-1]), 0)
return jaccard
def _flatten_probas(probas, labels, ignore=None, order='BHWC'):
"""
Flattens predictions in the batch
"""
if order == 'BCHW':
probas = tf.transpose(probas, (0, 2, 3, 1), name="BCHW_to_BHWC")
order = 'BHWC'
if order != 'BHWC':
raise NotImplementedError('Order {} unknown'.format(order))
C = probas.shape[3]
probas = tf.reshape(probas, (-1, C))
labels = tf.reshape(labels, (-1,))
if ignore is None:
return probas, labels
valid = tf.not_equal(labels, ignore)
vprobas = tf.boolean_mask(probas, valid, name='valid_probas')
vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
return vprobas, vlabels
def _lovasz_softmax_flat(probas, labels, only_present=True):
"""
Multi-class Lovasz-Softmax loss
probas: [P, C] Variable, class probabilities at each prediction (between 0 and 1)
labels: [P] Tensor, ground truth labels (between 0 and C - 1)
only_present: average only on classes present in ground truth
"""
C = probas.shape[1]
losses = []
present = []
for c in range(C):
fg = tf.cast(tf.equal(labels, c), probas.dtype) # foreground for class c
if only_present:
present.append(tf.reduce_sum(fg) > 0)
errors = tf.abs(fg - probas[:, c])
errors_sorted, perm = tf.nn.top_k(errors, k=tf.shape(errors)[0], name="descending_sort_{}".format(c))
fg_sorted = tf.gather(fg, perm)
grad = _lovasz_grad(fg_sorted)
losses.append(
tf.tensordot(errors_sorted, tf.stop_gradient(grad), 1, name="loss_class_{}".format(c))
)
losses_tensor = tf.stack(losses)
if only_present:
present = tf.stack(present)
losses_tensor = tf.boolean_mask(losses_tensor, present)
return losses_tensor
def lovasz_softmax(probas, labels, only_present=True, per_image=False, ignore=None, order='BHWC'):
"""
Multi-class Lovasz-Softmax loss
probas: [B, H, W, C] or [B, C, H, W] Variable, class probabilities at each prediction (between 0 and 1)
labels: [B, H, W] Tensor, ground truth labels (between 0 and C - 1)
only_present: average only on classes present in ground truth
per_image: compute the loss per image instead of per batch
ignore: void class labels
order: use BHWC or BCHW
"""
probas = tf.nn.softmax(probas, 3)
labels = helpers.reverse_one_hot(labels)
if per_image:
def treat_image(prob, lab):
prob, lab = tf.expand_dims(prob, 0), tf.expand_dims(lab, 0)
prob, lab = _flatten_probas(prob, lab, ignore, order)
return _lovasz_softmax_flat(prob, lab, only_present=only_present)
losses = tf.map_fn(treat_image, (probas, labels), dtype=tf.float32)
else:
losses = _lovasz_softmax_flat(*_flatten_probas(probas, labels, ignore, order), only_present=only_present)
return losses
# Randomly crop the image to a specific size. For data augmentation
def random_crop(image, label, crop_height, crop_width):
if (image.shape[0] != label.shape[0]) or (image.shape[1] != label.shape[1]):
# print(image.shape[0])
# print(image.shape[1])
# print(label.shape[0])
# print(label.shape[1])
raise Exception('Image and label must have the same dimensions!')
if (crop_width <= image.shape[1]) and (crop_height <= image.shape[0]):
x = random.randint(0, image.shape[1]-crop_width)
y = random.randint(0, image.shape[0]-crop_height)
if len(label.shape) == 3:
return image[y:y+crop_height, x:x+crop_width, :], label[y:y+crop_height, x:x+crop_width, :]
else:
return image[y:y+crop_height, x:x+crop_width, :], label[y:y+crop_height, x:x+crop_width]
else:
raise Exception('Crop shape exceeds image dimensions!')
# Compute the average segmentation accuracy across all classes
def compute_global_accuracy(pred, label):
total = len(label)
count = 0.0
for i in range(total):
if pred[i] == label[i]:
count = count + 1.0
return float(count) / float(total)
# Compute the class-specific segmentation accuracy
def compute_class_accuracies(pred, label, num_classes):
total = []
for val in range(num_classes):
total.append((label == val).sum())
count = [0.0] * num_classes
for i in range(len(label)):
if pred[i] == label[i]:
count[int(pred[i])] = count[int(pred[i])] + 1.0
# If there are no pixels from a certain class in the GT,
# it returns NAN because of divide by zero
# Replace the nans with a 1.0.
accuracies = []
for i in range(len(total)):
if total[i] == 0:
accuracies.append(1.0)
else:
accuracies.append(count[i] / total[i])
return accuracies
def compute_mean_iou(pred, label):
unique_labels = np.unique(label)
num_unique_labels = len(unique_labels);
I = np.zeros(num_unique_labels)
U = np.zeros(num_unique_labels)
for index, val in enumerate(unique_labels):
pred_i = pred == val
label_i = label == val
I[index] = float(np.sum(np.logical_and(label_i, pred_i)))
U[index] = float(np.sum(np.logical_or(label_i, pred_i)))
mean_iou = np.mean(I / U)
return mean_iou
def evaluate_segmentation(pred, label, num_classes, score_averaging="weighted"):
flat_pred = pred.flatten()
flat_label = label.flatten()
global_accuracy = compute_global_accuracy(flat_pred, flat_label)
class_accuracies = compute_class_accuracies(flat_pred, flat_label, num_classes)
prec = precision_score(flat_pred, flat_label, average=score_averaging)
rec = recall_score(flat_pred, flat_label, average=score_averaging)
f1 = f1_score(flat_pred, flat_label, average=score_averaging)
iou = compute_mean_iou(flat_pred, flat_label)
return global_accuracy, class_accuracies, prec, rec, f1, iou
def evaluate_validation_accuracy(pred, label, num_classes, score_averaging="weighted"):
flat_pred = pred.flatten()
flat_label = label.flatten()
global_accuracy = compute_global_accuracy(flat_pred, flat_label)
class_accuracies = compute_class_accuracies(flat_pred, flat_label, num_classes)
prec = precision_score(flat_pred, flat_label, average=score_averaging)
rec = recall_score(flat_pred, flat_label, average=score_averaging)
f1 = f1_score(flat_pred, flat_label, average=score_averaging)
iou = compute_mean_iou(flat_pred, flat_label)
return np.float32(global_accuracy), np.float32(class_accuracies), np.float32(prec), np.float32(rec), np.float32(f1), np.float32(iou)
# return global_accuracy
# def compute_class_weights(labels_dir, label_values):
def compute_class_weights(labels_dir):
'''
Arguments:
labels_dir(list): Directory where the image segmentation labels are
num_classes(int): the number of classes of pixels in all images
Returns:
class_weights(list): a list of class weights where each index represents each class label and the element is the class weight for that label.
'''
image_files = [os.path.join(labels_dir, file) for file in os.listdir(labels_dir) if file.endswith('.png')]
num_classes = len(label_values)
class_pixels = np.zeros(num_classes)
total_pixels = 0.0
for n in range(len(image_files)):
image = imread(image_files[n])
for index, colour in enumerate(label_values):
class_map = np.all(np.equal(image, colour), axis = -1)
class_map = class_map.astype(np.float32)
class_pixels[index] += np.sum(class_map)
print("\rProcessing image: " + str(n) + " / " + str(len(image_files)), end="")
sys.stdout.flush()
total_pixels = float(np.sum(class_pixels))
index_to_delete = np.argwhere(class_pixels==0.0)
class_pixels = np.delete(class_pixels, index_to_delete)
class_weights = total_pixels / class_pixels
class_weights = class_weights / np.sum(class_weights)
return class_weights
# Compute the memory usage, for debugging
def memory():
import os
import psutil
pid = os.getpid()
py = psutil.Process(pid)
memoryUse = py.memory_info()[0]/2.**30 # Memory use in GB
print('Memory usage in GBs:', memoryUse)
def calculate_validation_metrics(input_image_batch, gt_image_batch, num_classes):
accuracy_list = []
class_accuracies_list = []
prec_list = []
rec_list = []
f1_list = []
iou_list = []
for index in range(input_image_batch.shape[0]):
output_image = np.array(input_image_batch[index,:,:,:])
output_image = helpers.reverse_one_hot(output_image)
gt_image_acc = np.array(gt_image_batch[index,:,:])
accuracy, class_accuracies, prec, rec, f1, iou = evaluate_validation_accuracy(pred=output_image, label=gt_image_acc, num_classes=num_classes)
accuracy_list.append(accuracy)
class_accuracies_list.append(class_accuracies)
prec_list.append(prec)
rec_list.append(rec)
f1_list.append(f1)
iou_list.append(iou)
return np.mean(accuracy_list, dtype=np.float32),np.mean(class_accuracies_list,dtype=np.float32),\
np.mean(prec_list, dtype=np.float32),np.mean(rec_list, dtype=np.float32),np.mean(f1_list, dtype=np.float32),\
np.mean(iou_list, dtype=np.float32)