forked from MaybeShewill-CV/lanenet-lane-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_model_utils.py
85 lines (67 loc) · 2.47 KB
/
evaluate_model_utils.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 19-1-21 上午11:17
# @Author : MaybeShewill-CV
# @Site : https://github.com/MaybeShewill-CV/lanenet-lane-detection
# @File : evaluate_model_utils.py
# @IDE: PyCharm
"""
Calculate model's fp fn and precision
"""
import tensorflow as tf
def calculate_model_precision(input_tensor, label_tensor):
"""
calculate accuracy acc = correct_nums / ground_truth_nums
:param input_tensor: binary segmentation logits
:param label_tensor: binary segmentation label
:return:
"""
logits = tf.nn.softmax(logits=input_tensor)
final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)
idx = tf.where(tf.equal(final_output, 1))
pix_cls_ret = tf.gather_nd(label_tensor, idx)
accuracy = tf.count_nonzero(pix_cls_ret)
accuracy = tf.divide(
accuracy,
tf.cast(tf.shape(tf.gather_nd(label_tensor, tf.where(tf.equal(label_tensor, 1))))[0], tf.int64))
return accuracy
def calculate_model_fp(input_tensor, label_tensor):
"""
calculate fp figure
:param input_tensor:
:param label_tensor:
:return:
"""
logits = tf.nn.softmax(logits=input_tensor)
final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)
idx = tf.where(tf.equal(final_output, 1))
pix_cls_ret = tf.gather_nd(final_output, idx)
false_pred = tf.cast(tf.shape(pix_cls_ret)[0], tf.int64) - tf.count_nonzero(
tf.gather_nd(label_tensor, idx)
)
return tf.divide(false_pred, tf.cast(tf.shape(pix_cls_ret)[0], tf.int64))
def calculate_model_fn(input_tensor, label_tensor):
"""
calculate fn figure
:param input_tensor:
:param label_tensor:
:return:
"""
logits = tf.nn.softmax(logits=input_tensor)
final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)
idx = tf.where(tf.equal(label_tensor, 1))
pix_cls_ret = tf.gather_nd(final_output, idx)
label_cls_ret = tf.gather_nd(label_tensor, tf.where(tf.equal(label_tensor, 1)))
mis_pred = tf.cast(tf.shape(label_cls_ret)[0], tf.int64) - tf.count_nonzero(pix_cls_ret)
return tf.divide(mis_pred, tf.cast(tf.shape(label_cls_ret)[0], tf.int64))
def get_image_summary(img):
"""
Make an image summary for 4d tensor image with index idx
:param img:
"""
if len(img.get_shape().as_list()) == 3:
img = tf.expand_dims(img, -1)
image = img - tf.reduce_min(img)
image /= tf.reduce_max(img) - tf.reduce_min(img)
image *= 255
return image