Create undateable CoreML model from Keras

25 Views Asked by At

I have a simple Keras model. After training I would like to export it as a coreml model using coremltools with the dense layer set to updatable so that it can be refined on device. However, when I try to export the model I get this runtime error

RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "compiler error: Encountered an error while compiling a neural network model: Espresso exception: "Unsupported configuration": inner_product needs at least 2 inputs for gradients, got 1"

If I change the activation function of the output layer to sigmoid the error goes away, but I'd like to keep it linear. I'd like to understand the error and get a sense of how to resolve it without changing the activation function

Network

    model = tf.keras.Sequential([
        tf.keras.layers.Dense(2, input_shape=(2,), name="xy"),
        tf.keras.layers.Dense(256, name="dense_1"),
        tf.keras.layers.Dense(2)
    ])

    model.compile(loss='mean_squared_error', optimizer='adam')
...

    model.fit(input_tensor, label_tensor, epochs=epochs)

Conversion code

    model_spec = builder.spec
    builder.make_updatable(['sequential/dense_1/BiasAdd'])
    feature = ('Identity', datatypes.Array(1, 2))
    builder.set_mean_squared_error_loss(name='lossLayer', input_feature=feature)
    builder.make_updatable(['sequential/dense_1/BiasAdd'])
    model_spec.description.input[
        0].shortDescription = 'The XY coordinate indicating where the system thinks a point is located'
    model_spec.description.output[0].shortDescription = 'The XY coordinate of the corrected output'
    builder.set_adam_optimizer(AdamParams(lr=0.01, batch=32))

CoreML model inspection

[Id: 2], Name: Identity (Type: innerProduct)
          Updatable: False
          Input blobs: ['sequential/dense_1/BiasAdd']
          Output blobs: ['Identity']
[Id: 1], Name: sequential/dense_1/BiasAdd (Type: innerProduct)
          Updatable: True
          Input blobs: ['sequential/xy/BiasAdd']
          Output blobs: ['sequential/dense_1/BiasAdd']
[Id: 0], Name: sequential/xy/BiasAdd (Type: innerProduct)
          Updatable: False
          Input blobs: ['xy_input']
          Output blobs: ['sequential/xy/BiasAdd']
0

There are 0 best solutions below