【发布时间】:2018-10-25 21:22:42
【问题描述】:
在这个损失函数中,我需要根据批量图像的数量和图像的大小来创建完整的指标。但是,我可以从 y_pred 获取图像大小,但不能获取批量大小,因为它在初始化图形时显示为 None。
def focal_loss(content, label_remap, gamma_=2, w_d=1e-4):
def focal_loss_fixed(y_true, y_pred):
num_classes = len(content.keys())
print("y_true_b", y_true.get_shape().as_list())
cv_eqation = K.constant([0.114, 0.587, 0.299])
y_true = tf.multiply(y_true, cv_eqation)
y_true = tf.reduce_sum(y_true, axis=3)
y_true = tf.cast(y_true, dtype=tf.uint8)
lbls_resized = y_true
logits_train = y_pred
b, c, w, h = K.int_shape(y_pred)
batch = K.constant(b)
channel = K.variable(c)
width = K.variable(w)
high = K.variable(h)
with tf.variable_scope("loss"):
......
# make the labels one-hot for the cross-entropy
onehot_mat = tf.reshape(tf.one_hot(lbls_resized, num_classes), (-1, num_classes))
# focal loss p and gamma
gamma = np.full((high * width * batch, channel), fill_value=gamma_)
print("gamma", gamma.shape)
.........
return loss
return focal_loss_fixed
另外,我尝试了另一种方法,使用 onehot_mat 形状,但它的形状没有任何价值。
【问题讨论】:
标签: python tensorflow neural-network keras