Convert Efficientnet to Core ML [There is a converted model]

MLBoy
1 min readAug 11, 2020

--

Convert the latest model of image classification to Core ML format.

Converted CoreML Model (GitHub)

Steps

1,Build a model by downloading it from the TensorFlow Hub model collection.

m = tf.keras.Sequential([
hub.KerasLayer("https://tfhub.dev/tensorflow/efficientnet/b0/classification/1")
])
m.build([1, 224, 224, 3])
m.summary()

2,Load the class label.

(This time, it is used as it is in 1000 class of ImageNet. TensorFlowHub has also released a version that can transfer learning.)

import urllib
label_url = 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt'
class_labels = urllib.request.urlopen(label_url).read().splitlines()
class_labels = class_labels[1:] # remove the first class which is background
assert len(class_labels) == 1000

# make sure entries of class_labels are strings
for i, label in enumerate(class_labels):
if isinstance(label, bytes):
class_labels[i] = label.decode("utf8")

3,Convert with CoreMLTools 4.0.

import coremltools as ct

image_input = ct.ImageType(shape=(1, 224, 224, 3,),
scale=1/255)

input_name = m.inputs[0].name.split(':')[0]
print(input_name) #Check input_name.
keras_output_node_name = m.outputs[0].name.split(':')[0]
graph_output_node_name = keras_output_node_name.split('/')[-1]
mlmodel = ct.convert(m,
inputs=[image_input],
classifier_config=classifier_config,
)
mlmodel.save('./efficientnet.mlmodel')

We send information related to machine learning.

Twitter

contact:

rockyshikoku@gmail.com

--

--