• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

It is necessary to observe the performance in real time during Keras training network. Mean IOU is not the evaluation function of Keras, and TF thinks it is not useful, so I wrote one by myself. After testing, there is no problem.

Calculate the IoU

The ground truth calculated by NUMpy is used as IoU’s test:

 def iou_numpy(y_true,y_pred):
     
     intersection = np.sum(np.multiply(y_true.astype('bool'),y_pred == 1))
     union = np.sum((y_true.astype('bool')+y_pred.astype('bool'))>0)    
     
     return intersection/union
Copy the code

keras metric IoU

def iou_keras(y_true, y_pred): """ Return the Intersection over Union (IoU). Args: y_true: the expected y values as a one-hot y_pred: the predicted y values as a one-hot or softmax output Returns: the IoU for the given label """ label = 1 # extract the label values using the argmax operator then # calculate equality  of the predictions and truths to the label y_true = K.cast(K.equal(y_true, label), K.floatx()) y_pred = K.cast(K.equal(y_pred, label), K.floatx()) # calculate the |intersection| (AND) of the labels intersection = K.sum(y_true * y_pred) # calculate the |union| (OR) of the labels union = K.sum(y_true) + K.sum(y_pred) - intersection # avoid divide by zero - if the union is Zero, return 1 # otherwise, return the intersection over union return K.witch (k.qual (union, 0), 1.0, intersection / union)Copy the code

Calculating mean IoU

Mean IoU for simplicity, (0,1,0.05) was selected as different IoU thresholds to calculate the average IoU

Numpy true value calculation

Def mean_iou_numpy(y_true,y_pred): iou_list = [] for thre in list(np.arange(0.0000001,0.99,0.05)): y_pred_temp = y_pred >= thre iou = iou_numpy(y_true, y_pred_temp) iou_list.append(iou) return np.mean(iou_list)Copy the code

Keras mean IoU

def mean_iou_keras(y_true, y_pred): """ Return the mean Intersection over Union (IoU). Args: y_true: the expected y values as a one-hot y_pred: the predicted y values as a one-hot or softmax output Returns: the mean IoU """ label = 1 # extract the label values using the argmax operator then # calculate equality of the predictions and truths to the label y_true = K.cast(K.equal(y_true, label), K. loatx()) mean_iou = k. ariable(0) thre_list = list(np.arange(0.0000001,0.99,0.05)) for thre in thre_list: y_pred_temp = K.cast(y_pred >= thre, K.floatx()) y_pred_temp = K.cast(K.equal(y_pred_temp, label), K.floatx()) # calculate the |intersection| (AND) of the labels intersection = K.sum(y_true * y_pred_temp) # calculate the |union| (OR) of the labels union = K.sum(y_true) + K.sum(y_pred_temp) - intersection iou = K.switch(K.equal(union, Mean_iou = mean_iou + iou return mean_iou/len(thre_list)Copy the code

test

# print(f 'iou:) # print(f 'iou:) # print(f 'iou:) {iou_numpy(y_true_np, y_pred_np)}') print(f' mean_iou_numpy : {mean_iou_numpy(y_true_np, }') y_true = tf.Variable(y_true_np) y_pred = tf.Variable(y_pred_np) ## compute node iou_res = iou_keras (y_true, y_pred) m_iou_res = mean_iou_keras (y_true, Init_op = tf.global_variables_initializer() with tf.session () as sess: Result = sess.run(iou_res) print(f'result: {result} \nsame with ground truth: {abs(iou_numpy(y_true_np, y_pred_NP) -result)< 0.0000001}') result = sess.run(m_iou_res) print(f'result: {result} \nsame with ground truth: {abs(mean_iou_numpy(y_true_np, y_pred_NP) - result) < 0.0000001}')Copy the code

Output:

Iou: 0.0mean_iou_numpy: 0.5295 result: 0.0same with ground truth: True result: 0.5295000076293945 Same with ground truth: TrueCopy the code

Download the source code

Github.com/zywvvd/Pyth…