Created
January 13, 2019 06:02
-
-
Save peune/e3f815d118e8650934a60251de032668 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import backend as K | |
def my_loss( y_true, y_pred ): | |
loss_conf, loss_bbox, loss_cls = K.variable(value=0), K.variable(value=0), K.variable(value=0) | |
for i in range(y_pred.shape[-2]): | |
true_bbox, true_conf, true_cls = y_true[..., i,:4], y_true[..., i,4], y_true[..., i,5:] | |
pred_bbox, pred_conf, pred_cls = y_pred[..., i,:4], y_pred[..., i,4], y_pred[..., i,5:] | |
pred_conf = K.sigmoid(pred_conf) | |
pred_bbox = K.sigmoid(pred_bbox) | |
pred_cls = K.softmax(pred_cls) | |
loss_conf = loss_conf + K.binary_crossentropy(true_conf, pred_conf) | |
loss_bbox = loss_bbox + true_conf*K.mean( (true_bbox-pred_bbox) ** 2 ) # mse | |
loss_cls = loss_cls + true_conf*K.categorical_crossentropy(true_cls, pred_cls) | |
return loss_conf + loss_bbox + loss_cls | |
model.compile(loss=my_loss, optimizer='adam') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment