Man*_*ion 5 keras tensorflow tensor
我正在研究一个 U-net 架构,以在 10 个类中执行分段。我想在每个时代之后计算每个班级的骰子系数。
我的网络的输出是一个形状为每个类的分割掩码堆栈(b_size, rows, cols, num_classes)。通过此输出,我按以下方式计算每个类的骰子系数:
def dice_metric(ground_truth, prediction):
# initialize list with dice scores for each category
dice_score_list = list()
# get list of tensors with shape (rows, cols)
ground_truth_unstacked = reshape_ground_truth(ground_truth)
prediction_unstacked = tf.unstack(prediction, axis=-1)
for (ground_truth_map, prediction_map) in zip(ground_truth_unstacked, prediction_unstacked):
# calculate dice score for every class
dice_i = dice_score(ground_truth_map, prediction_map)
dice_score_list.append(dice_i)
return tf.reduce_mean(dice_score_list, axis=[0])
Run Code Online (Sandbox Code Playgroud)
有什么方法可以打印骰子分数列表而不是平均值。所以在每个时期的输出是:
Epoch 107/200
- 13s - loss: 0.8896 - dice_metric: [dice_class_1, ... dice_class_10] - val_loss: 3.3417 - val_dice_metric: [val_dice_class_1, ... val_dice_class_10]
Run Code Online (Sandbox Code Playgroud)
关于自定义指标的Keras 文档仅考虑单个张量值(即,“自定义指标可以在编译步骤传递。该函数需要(y_true, y_pred)
作为参数并返回单个张量值。 ”
是否有任何方法/解决方法可以输出具有多个值的指标?
为了让 keras 输出所有通道,每个通道需要一个指标。您可以创建一个采用索引并仅返回所需类的包装器:
#calculates dice considering an input with a single class
def dice_single(true,pred):
true = K.batch_flatten(true)
pred = K.batch_flatten(pred)
pred = K.round(pred)
intersection = K.sum(true * pred, axis=-1)
true = K.sum(true, axis=-1)
pred = K.sum(pred, axis=-1)
return ((2*intersection) + K.epsilon()) / (true + pred + K.epsilon())
def dice_for_class(index):
def dice_inner(true,pred):
#get only the desired class
true = true[:,:,:,index]
pred = pred[:,:,:,index]
#return dice per class
return dice_single(true,pred)
return dice_inner
Run Code Online (Sandbox Code Playgroud)
那么模型中的指标将是“metrics = [dice_for_class(i) for i in range(10)]
提示:除非绝对必要,否则不要迭代。
没有迭代的十个类别的骰子示例
def dice_metric(ground_truth, prediction):
#for metrics, it's good to round predictions:
prediction = K.round(prediction)
#intersection and totals per class per batch (considers channels last)
intersection = ground_truth * prediction
intersection = K.sum(intersection, axis=[1,2])
ground_truth = K.sum(ground_truth, axis=[1,2])
prediction = K.sum(prediciton, axis=[1,2])
dice = ((2 * intersection) + K.epsilon()) / (ground_truth + prediction + K.epsilon())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1411 次 |
| 最近记录: |