【发布时间】:2019-11-06 04:00:16
【问题描述】:
我有一个 CNN 对象检测模型,它有两个头(输出),张量名称为 'classification' 和 'regression'。
我想定义一个同时接受两个输出的度量函数,以便它查看回归预测来决定保留和使用哪些索引这些索引从分类预测中选择张量并计算一些指标。
我在 this link 的帮助下定义的当前度量函数:
from tensorflow.python.keras.metrics import MeanMetricWrapper
class Accuracy2(MeanMetricWrapper):
def __init__(self, name='dummyAccuracy', dtype=None):
super(Accuracy2, self).__init__(metric_calculator_func, name, dtype=dtype)
self.true_positives = self.add_weight(name='lol', initializer='zeros')
@classmethod
def from_config(cls, config):
if 'fn' in config:
config.pop('fn')
return super(Accuracy2, cls).from_config(config)
def update_state(self, y_true, y_pred, sample_weight=None):
print("==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===")
print("Y-True {}".format(y_true))
print("Y-Pred {}".format(y_pred))
print("==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===")
update_ops = [self.true_positives.assign_add(1.0)]
return tf.group(update_ops)
def result(self):
return self.true_positives
def reset_states(self):
# The state of the metric will be reset at the start of each epoch.
self.true_positives.assign(0.)
我将模型编译期间称为:
training_model.compile(
loss={
'regression' : regression_loss(),
'classification': classification_loss()
},
optimizer=keras.optimizers.Adam(lr=lr, clipnorm=0.001),
metrics=[Accuracy2()]
)
tf.estimator.train_and_evaluate 期间的屏幕日志为:
INFO:tensorflow:loss = 0.0075738616,步长 = 31(11.941 秒)
INFO:tensorflow:global_step/sec: 4.51218
INFO:tensorflow:loss = 0.01015341,步长 = 36(1.108 秒)
INFO:tensorflow:将 40 的检查点保存到 /tmp/tmpcla2n3gy/model.ckpt。
INFO:tensorflow:调用model_fn。 ==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@=== 张量("IteratorGetNext:1", shape=(?, 120087, 5), dtype=float32, device=/device:CPU:0) 张量("regression/concat:0", shape=(?, ?, 4), dtype=float32) ==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@=== ==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@=== 张量("IteratorGetNext:2", shape=(?, 120087, 2), dtype=float32, device=/device:CPU:0) 张量(“分类/连接:0”,形状=(?,?,1),dtype=float32) ==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===
INFO:tensorflow:Done 调用 model_fn。
INFO:tensorflow:2019-06-24T08:20:35Z 开始评估 INFO:tensorflow:Graph 已完成。 2019-06-24 13:50:36.457345:我 tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] 添加可见 gpu 设备:0 2019-06-24 13:50:36.457398:我 tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 设备互连 StreamExecutor 与强度 1 边缘矩阵: 2019-06-24 13:50:36.457419:我 tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0 2019-06-24 13:50:36.457425: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0: N 2019-06-24 13:50:36.457539:我 tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 创建了 TensorFlow 设备(/job:localhost/replica:0/task:0/device:GPU:0 和 9855 MB 内存)-> 物理 GPU(设备:0,名称:GeForce RTX 2080 Ti,pci 总线 ID:0000:01:00.0,计算能力:7.5)
INFO:tensorflow:从 /tmp/tmpcla2n3gy/model.ckpt-40 恢复参数
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op。
INFO:tensorflow:Evaluation [10/100]
INFO:tensorflow:Evaluation [20/100]
信息:张量流:评估 [30/100]
信息:张量流:评估 [40/100]
信息:张量流:评估 [50/100]
信息:张量流:评估 [60/100]
信息:张量流:评估 [70/100]
信息:张量流:评估 [80/100]
信息:张量流:评估 [90/100]
INFO:tensorflow:Evaluation [100/100]
INFO:tensorflow:2019-06-24-08:20:44 完成评估
INFO:tensorflow:为全局步骤 40 保存字典:_focal = 0.0016880237, _smooth_l1 = 0.0, dummyAccuracy = 100.0, global_step = 40, loss = 0.0016880237
这一行:
==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===
Tensor("IteratorGetNext:1", shape=(?, 120087, 5), dtype=float32, device=/device:CPU:0)
Tensor("regression/concat:0", shape=(?, ?, 4), dtype=float32)
==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===
==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===
Tensor("IteratorGetNext:2", shape=(?, 120087, 2), dtype=float32, device=/device:CPU:0)
Tensor("classification/concat:0", shape=(?, ?, 1), dtype=float32)
==@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@===
表明Accuracy2() 被调用了两次,第一次用于回归,然后用于分类。
但我希望它被调用一次,将 regression 和 classification 一起输入它
【问题讨论】:
-
关于我的解决方案的任何 cmet 吗?
标签: python-3.x tensorflow keras deep-learning