from_keras.py 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19 20 21
"""
Compile Keras Models
=====================
**Author**: `Yuwei Hu <https://Huyuwei.github.io/>`_

22
This article is an introductory tutorial to deploy keras models with Relay.
23 24 25 26 27

For us to begin with, keras should be installed.
Tensorflow is also required since it's used as the default backend of keras.

A quick solution is to install via pip
28 29 30 31 32 33

.. code-block:: bash

    pip install -U keras --user
    pip install -U tensorflow --user

34 35 36 37
or please refer to official site
https://keras.io/#installation
"""
import tvm
38
import tvm.relay as relay
39
from tvm.contrib.download import download_testdata
40 41 42 43 44 45 46 47 48 49
import keras
import numpy as np

######################################################################
# Load pretrained keras model
# ----------------------------
# We load a pretrained resnet-50 classification model provided by keras.
weights_url = ''.join(['https://github.com/fchollet/deep-learning-models/releases/',
                       'download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5'])
weights_file = 'resnet50_weights.h5'
50
weights_path = download_testdata(weights_url, weights_file, module='keras')
51
keras_resnet50 = keras.applications.resnet50.ResNet50(include_top=True, weights=None,
52
                                                      input_shape=(224, 224, 3), classes=1000)
53
keras_resnet50.load_weights(weights_path)
54 55 56 57 58 59 60 61 62

######################################################################
# Load a test image
# ------------------
# A single cat dominates the examples!
from PIL import Image
from matplotlib import pyplot as plt
from keras.applications.resnet50 import preprocess_input
img_url = 'https://github.com/dmlc/mxnet.js/blob/master/data/cat.png?raw=true'
63 64
img_path = download_testdata(img_url, 'cat.png', module='data')
img = Image.open(img_path).resize((224, 224))
65 66 67 68 69
plt.imshow(img)
plt.show()
# input preprocess
data = np.array(img)[np.newaxis, :].astype('float32')
data = preprocess_input(data).transpose([0, 3, 1, 2])
70
print('input_1', data.shape)
71 72

######################################################################
73 74 75 76
# Compile the model with Relay
# ----------------------------
# convert the keras model(NHWC layout) to Relay format(NCHW layout).
shape_dict = {'input_1': data.shape}
77
mod, params = relay.frontend.from_keras(keras_resnet50, shape_dict)
78 79
# compile the model
target = 'cuda'
80 81
ctx = tvm.gpu(0)
with relay.build_config(opt_level=3):
82
    executor = relay.build_module.create_executor('graph', mod, ctx, target)
83 84 85 86

######################################################################
# Execute on TVM
# ---------------
87
dtype = 'float32'
88
tvm_out = executor.evaluate()(tvm.nd.array(data.astype(dtype)), **params)
89
top1_tvm = np.argmax(tvm_out.asnumpy()[0])
90 91 92 93

#####################################################################
# Look up synset name
# -------------------
94
# Look up prediction top 1 index in 1000 class synset.
95 96 97 98
synset_url = ''.join(['https://gist.githubusercontent.com/zhreshold/',
                      '4d0b62f3d01426887599d4f7ede23ee5/raw/',
                      '596b27d23537e5a1b5751d2b0481ef172f58b539/',
                      'imagenet1000_clsid_to_human.txt'])
99 100 101
synset_name = 'imagenet1000_clsid_to_human.txt'
synset_path = download_testdata(synset_url, synset_name, module='data')
with open(synset_path) as f:
102
    synset = eval(f.read())
103
print('Relay top-1 id: {}, class name: {}'.format(top1_tvm, synset[top1_tvm]))
104 105 106 107
# confirm correctness with keras output
keras_out = keras_resnet50.predict(data.transpose([0, 2, 3, 1]))
top1_keras = np.argmax(keras_out)
print('Keras top-1 id: {}, class name: {}'.format(top1_keras, synset[top1_keras]))