|
- # Copyright (c) Nanjing University, Vision Lab.
- # Last update: 2021.9.6
-
- #import tensorflow as tf
- import tensorlayer as tl
- import numpy as np
-
- #已测试通过
- def get_bce_loss(pred, label):
- """ (Weighted) Binary cross entropy loss.
- Input:GPU
- pred: [batch size, vsize, vsize, vsize, 1] float32
- label: must be 0 or 1, [batch size, vsize, vsize, vsize, 1] float32
- output:
- empty loss & full loss
- """
-
- # occupancy = pred
- occupancy = tl.ops.clip_by_value(tl.ops.sigmoid(pred), 1e-7, 1.0 - 1e-7)
- position_neg = tl.ops.cast(tl.ops.equal(tl.ops.reduce_max(label, axis=-1), 0), 'int8') #position_neg.shape (batch size, vsize, vsize, vsize)
- position_pos = tl.ops.cast(tl.ops.greater(tl.ops.reduce_max(label, axis=-1), 0), 'int8') #>0为1,其他为0
- # get position of pred
- position_neg = tl.ops.where(position_neg>0) #返回True元素的index
- position_pos = tl.ops.where(position_pos>0)
- # get occupancy value
- occupancy_neg = tl.ops.gather_nd(occupancy, position_neg) #按照position_neg的索引从occupancy中抽取切片
- occupancy_pos = tl.ops.gather_nd(occupancy, position_pos)
- # get empty loss
- empty_loss = tl.ops.reduce_mean((-1)*tl.ops.log(1.0 - occupancy_neg))
- full_loss = tl.ops.reduce_mean((-1)*tl.ops.log(occupancy_pos))
-
- return empty_loss, full_loss
-
- #测试通过
- def get_confusion_matrix(pred, label, th=0.):
- """confusion matrix:
- 1 0
- 1 TP FN
- 0 FP TN(option)
- input:
- pred, label: float32 [batch size, vsize, vsize, vsize, 1]
- output:
- TP(true position), FP(false position), FN(false negative);
- float32 [batch size, vsize, vsize, vsize];
- """
- pred = tl.ops.reshape(pred,(pred.shape[0],pred.shape[1],pred.shape[2],pred.shape[3]))
- label = tl.ops.reshape(label,(label.shape[0],label.shape[1],label.shape[2],label.shape[3]))
-
- pred = tl.ops.cast(tl.ops.greater(pred, th), tl.float32)
- label = tl.ops.cast(tl.ops.greater(label, th), tl.float32)
-
- TP = pred * label
- FP = pred * (1. - label)
- FN = (1. - pred) * label
- # TN = (1 - pred) * (1 - label)
-
- return TP, FP, FN
-
- #测试通过
- def get_classify_metrics(pred, label, th=0.):
- """Metrics for classification.
- input:
- pred, label; type : float32 tensor; shape: [batch size, vsize, vsize, vsize, 1]
- output:
- precision rate; recall rate; IoU;
- """
-
- TP, FP, FN = get_confusion_matrix(pred, label, th=th)
- TP = tl.ops.cast(tl.ops.ReduceSum()(TP), tl.float32)
- FP = tl.ops.cast(tl.ops.ReduceSum()(FP), tl.float32)
- FN = tl.ops.cast(tl.ops.ReduceSum()(FN), tl.float32)
-
- precision = TP / (TP + FP)
- recall = TP / (TP + FN)
- IoU = TP / (TP + FP + FN)
-
- return precision, recall, IoU #1个值的tensor
-
-
-
- if __name__=='__main__':
- np.random.seed(108)
- data = np.random.rand(2, 4, 4, 4, 1)* 10 - 5
- data = data.astype("float32")
- label = np.random.rand(2, 4, 4, 4, 1)
- label[label>=0.9] = 1
- label[label<0.9] = 0
- label = label.astype("float32")
-
- data = tl.convert_to_tensor(data)
- label = tl.convert_to_tensor(label)
- loss1, loss2 = get_bce_loss(data, label)
- print("loss1, loss2:",loss1, loss2)
- precision, recall, IoU = get_classify_metrics(data, label, 0.)
- print(precision.numpy(), recall.numpy(), IoU.numpy())
|