Sakaggi
Sakaggi

Reputation: 95

Error in converting keras model to core ml model

I have been trying to convert the Keras model to the core mlmodel. However, while doing so, I get multiple inputs and outputs. I just want the following as input and output.

name: "input"
type {  
    imageType {    
        width: 256    
        height: 64    
        colorSpace: GRAYSCALE  }
}

name: "output"
type {
  dictionaryType {
    stringKeyType {
    }
  }
}

The current model I get.

type {
  imageType {
    width: 256
    height: 64
    colorSpace: GRAYSCALE
  }
}
, name: "lstm1_h_in"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm1_c_in"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm1_h_in_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm1_c_in_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm2_h_in"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm2_c_in"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm2_h_in_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
, name: "lstm2_c_in_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
  isOptional: true
}
]

[name: "output"
type {
  dictionaryType {
    stringKeyType {
    }
  }
}
, name: "lstm1_h_out"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm1_c_out"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm1_h_out_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm1_c_out_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm2_h_out"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm2_c_out"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm2_h_out_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "lstm2_c_out_rev"
type {
  multiArrayType {
    shape: 256
    dataType: DOUBLE
  }
}
, name: "classLabel"
type {
  stringType {
  }
}
]

I am using this code to convert:

from keras.models import load_model
import coremltools as ct

class_labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
                "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "-"]

tf_keras_model = load_model("HTRAdam.h5")
mlmodel = ct.converters.keras.convert('HTRAdam.h5', input_names=['image'], output_names=['output'],
                                                   class_labels=class_labels, image_input_names='image')
mlmodel.save('HTR.mlmodel')

When I enter the image in this, I don't get any output. I mean image output is nil. I step into debug mode, but cannot find any error from model prediction.

I am guessing the issue might be with the compiling model but I have no idea about how to resolve it. Thank you for your help.

Upvotes: 1

Views: 194

Answers (1)

Matthijs Hollemans
Matthijs Hollemans

Reputation: 7892

If your model contains an LSTM layer, Core ML expects you to pass in the LSTM state manually, which is why you get all these extra inputs and outputs. Keras manages this state for you but Core ML does not.

Upvotes: 1

Related Questions