Resnet50 to Apple CoreML


#1

Hi All, I am trying to train a model with Resnet50 class that is given in part-1. But, running into issue.

Training code :
resnet = Resnet50()
batches = resnet.get_batches(path+‘train’, batch_size=batch_size)
val_batches = resnet.get_batches(path+‘val’, batch_size=batch_size*2)
resnet.finetune(batches)
resnet.fit(batches, val_batches, nb_epoch=5)

Save :
resnet.model.save(‘resent50.h5’)

Convert to CoreML :

coreml_model = coremltools.converters.keras.convert( ‘resent50.h5’,
image_input_names=‘data’,
class_labels=‘labels.txt’)
coreml_model.save(‘resent50.mlmodel’)

Error :

138     model_config = f.attrs.get('model_config')
139     if model_config is None:

–> 140 raise ValueError(‘No model found in config file.’)
141 model_config = json.loads(model_config.decode(‘utf-8’))
142 model = model_from_config(model_config, custom_objects=custom_objects)

ValueError: No model found in config file.

----------->

Then I tried to load the model directly using keras, and I get a error there as well

model = keras.models.load_model(‘resent50.h5’)

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/utils/generic_utils.pyc in get_from_module(identifier, module_params, module_name, instantiate, kwargs)
123 if not res:
124 raise ValueError('Invalid ’ + str(module_name) + ': ’ +
–> 125 str(identifier))
126 if instantiate and not kwargs:
127 return res()

ValueError: Invalid core: vgg_preprocess

As anyone done conversion to CoreML successfully ?


#2

update: found a solution -

coremltools.converters.keras.convert also takes the model object directly. So instead of saving to .h5 I passed the object. But, I got the error Lambda layer is not supported for conversion. Then I commented out the lambda layer in Restnet50.create() and added it as part of preprocessing :

resnet = Resnet50()
gen = image.ImageDataGenerator(preprocessing_function=resnet.vgg_preprocess)
batches = resnet.get_batches(path+‘train’, gen, batch_size=batch_size)
val_batches = resnet.get_batches(path+‘val’,gen, batch_size=batch_size*2)
resnet.finetune(batches)
resnet.fit(batches, val_batches, nb_epoch=1)

Now I am able to export the model to coreml!


(ozgur sahin) #3

I have written a tutorial to fine-tune ResNet50 with Keras and convert it to CoreML. https://heartbeat.fritz.ai/how-to-fine-tune-resnet-in-keras-and-use-it-in-an-ios-app-via-core-ml-ee7fd84c1b26
Working Colab is here.