简体   繁体   中英

Fatal Error when implementing a Keras Model converted to a CoreML model in an iOS app

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: 与 CoreML 模型相关的代码 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM