3

I was trying to write masked MSE loss:

def mae_loss_masked(mask):
    def loss_fn(y_true, y_pred):
        abs_vec = tf.multiply(tf.abs(y_pred-y_true), mask)
        loss = tf.reduce_mean(abs_vec)
        return loss
    return loss_fn

My model:

def MobileNet_v1():
    # MobileNet with dense layer on top

    # Keras 2.1.6
    mobilenet = MobileNet(input_shape=(config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS),
                          alpha=1.0,
                          depth_multiplier=1,
                          include_top=False,
                          weights='imagenet'
                          )

    x = Flatten()(mobilenet.output)
    x = Dropout(0.5)(x)
    x = Dense(config.N_LANDMARKS * 2, activation='linear')(x)

    # -------------------------------------------------------

    model = Model(inputs=mobilenet.input, outputs=x)
    optimizer = Adadelta()
    model.compile(optimizer=optimizer, loss=mae_loss_masked)

    model.summary()
    import sys
    sys.exit()

    return model

But it give an error: TypeError: mae_loss_masked() takes 1 positional argument but 2 were given

Also a question how batch generator output should look like in this case.

Milo Lu
  • 3,176
  • 3
  • 35
  • 46
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • Your loss function takes 1 argument, while you are actually giving it 2. using mae_loss_masked(some_mask) will get you the actual loss function you need: https://stackoverflow.com/questions/46858016/keras-custom-loss-function-to-pass-arguments-other-than-y-true-and-y-pred , batch should still be (x,y) , or optionally (x,y, weights) – Dinari Nov 19 '18 at 07:31

0 Answers0