我想在TensorFlow中实现CRF损失。 假设我的代码如下所示:
# input to the model and ground truth segmentation mask
image = tf.placeholder(tf.float32, [None, N, M, 1])
gt_mask = tf.placeholder(tf.float32, [None, N, M, n_classes])
# prediction:
pred_mask = my_cnn(image)
pred_mask = tf.nn.softmax(pred_mask) # has shape = [None, N, M, n_classes]
# compute CRF loss
crf_loss = ... ?
尤其是,crf_loss
公式必须是可微的,以便我可以使用SGD来训练模型。
我发现this论文说我们可以使用Potts模型的二次松弛(等式4,第4页)。在我的示例中,关于如何在TensorFlow中实现所有这些的任何想法?
谢谢您的帮助:)
答案 0 :(得分:0)
在更多文献中,我发现CRF通常作为RNN在完全卷积网络中实现,如Zheng等人的ICCV 2015 paper所述。
他们还共享了一个实现here,而另一个实现(应该更加灵活)已经共享here。
@Jindřich指出,也应该有一些有用的代码here。