-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils2.py
217 lines (179 loc) · 7.51 KB
/
utils2.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
from __future__ import print_function
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
# 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)
# Replaces a select value in an array with a new value
def replace_val_in_array(input_array, original_val, replace_val = sys.maxsize - 1):
for index, item in enumerate(input_array):
if item == original_val:
input_array[index] = replace_val
return input_array
# Replace NaNs with a value
def replaces_nan_in_array(input_array, replace_val=1.0):
for index, item in enumerate(input_array):
if math.isnan(item):
input_array[index] = replace_val
return input_array
# 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))
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)
# 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]):
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)
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_avg_accuracy(y_pred, y_true):
w = y_true.shape[0]
h = y_true.shape[1]
total = w*h
count = 0.0
for i in range(w):
for j in range(h):
if y_pred[i, j] == y_true[i, j]:
count = count + 1.0
return count / total
# Compute the class-specific segmentation accuracy
def compute_class_accuracies(y_pred, y_true, num_classes=12):
w = y_true.shape[0]
h = y_true.shape[1]
flat_image = np.reshape(y_true, w*h)
total = []
for val in range(num_classes):
total.append((flat_image == val).sum())
count = [0.0] * num_classes
for i in range(w):
for j in range(h):
if y_pred[i, j] == y_true[i, j]:
count[int(y_pred[i, j])] = count[int(y_pred[i, j])] + 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
# Compute precision
def precision(pred, label):
TP = np.float(np.count_nonzero(pred * label)) + 1.0
FP = np.float(np.count_nonzero(pred * (label - 1))) + 1.0
prec = TP / (TP + FP)
return prec
# Compute recall
def recall(pred, label):
TP = np.float(np.count_nonzero(pred * label)) + 1.0
FN = np.float(np.count_nonzero((pred - 1) * label)) + 1.0
rec = TP / (TP + FN)
return rec
# Compute f1 score
def f1score(pred, label):
prec = precision(pred, label)
rec = recall(pred, label)
f1 = np.divide(2 * prec * rec, (prec + rec))
return f1
def compute_mean_iou(pred, label):
w = label.shape[0]
h = label.shape[1]
unique_classes = np.unique(label)
iou_list = list([0]) * len(unique_classes)
for index, curr_class in enumerate(unique_classes):
pred_mask = pred[:, :] == curr_class
label_mask = label[:, :] == curr_class
# TP = np.float(np.count_nonzero(pred_mask * label_mask))
# FP = np.float(np.count_nonzero(pred_mask * (label_mask - 1)))
# FN = np.float(np.count_nonzero((pred_mask - 1) * label_mask))
iou_and = np.float(np.sum(np.logical_and(pred_mask, label_mask)))
iou_or = np.float(np.sum(np.logical_or(pred_mask, label_mask)))
iou_list[index] = iou_and / iou_or
mean_iou = np.mean(iou_list)
return mean_iou
def median_frequency_balancing(labels_dir, num_classes=12):
'''
Perform median frequency balancing on the image files, given by the formula:
f = Median_freq_c / total_freq_c
Where median_freq_c is the median frequency of the class for all pixels of C that appeared in images
and total_freq_c is the total number of pixels of c in the total pixels of the images where c appeared.
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.
'''
#Initialize all the labels key with a list value
image_files = [os.path.join(labels_dir, file) for file in os.listdir(labels_dir) if file.endswith('.png')]
label_to_frequency_dict = {}
for i in range(num_classes):
label_to_frequency_dict[i] = []
for n in range(len(image_files)):
image = imread(image_files[n])
unique_labels = list(np.unique(image))
#For each image sum up the frequency of each label in that image and append to the dictionary if frequency is positive.
for i in unique_labels:
class_mask = np.equal(image, i)
class_mask = class_mask.astype(np.float32)
class_frequency = np.sum(class_mask)
if class_frequency != 0.0:
index = unique_labels.index(i)
label_to_frequency_dict[index].append(class_frequency)
class_weights = []
print(class_frequency)
#Get the total pixels to calculate total_frequency later
total_pixels = 0
for frequencies in label_to_frequency_dict.values():
total_pixels += sum(frequencies)
for i, j in label_to_frequency_dict.items():
j = sorted(j) #To obtain the median, we've got to sort the frequencies
median_frequency = np.median(j) / sum(j)
total_frequency = sum(j) / total_pixels
median_frequency_balanced = median_frequency / total_frequency
class_weights.append(median_frequency_balanced)
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 use:', memoryUse)