I'm making an iOS app that uses a Deep Learning Model developed in Keras and converted to a CoreML model. I was using the Resnet50 CoreML Model downloaded from Apple's website and the app was working flawlessly, but when I implemented the model I developed an error occurred when I chose an image from the photo library or took a picture with the camera within the app. This is the code associated with the CoreML Model and the error I'm getting: This is the code used to convert the Keras Model into a CoreML model:
output_labels = ['0', '1']
coreml_model = coremltools.converters.keras.convert('Costume.h5', input_names=['image'],
class_labels=output_labels, image_input_names='image',
output_names=['output'])
print(coreml_model) # Check that input type is imageType
# Metadata for XCode
coreml_model.author = 'Author'
coreml_model.short_description = 'Some Description'
coreml_model.input_description['image'] = 'Takes as input an image'
coreml_model.output_description['output'] = 'Prediction of image'
coreml_model.save('Costume.mlmodel')
I have no idea how to fix this error, thanks in advance! This is my Keras Model:
model = Sequential()
# Hidden Layer 1
model.add(Conv2D(128, 3, 3, border_mode='same', input_shape=input_shape, activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Hidden Layer 2
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Hidden Layer 3
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Hidden Layer 4
model.add(Conv2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=0.0001),
metrics=['accuracy'])
model.summary()
I found a solution to my problem so I'm posting it in case anyone has the same problem in the future. You need to change the following:
model.add(Dense(1))
model.add(Activation('sigmoid'))
to
model.add(Dense(2))
model.add(Activation('softmax'))
and also remember to change the loss from binary_crossentropy
to categorical_crossentropy
.