前言:
现在看官们对“googlenet keras”大体比较重视,看官们都想要分析一些“googlenet keras”的相关内容。那么小编同时在网上网罗了一些有关“googlenet keras””的相关知识,希望大家能喜欢,姐妹们一起来了解一下吧!本篇文章译自英文文档 Compile Keras Models tvm 0.13.dev0 documentation
作者是 Yuwei Hu
更多 TVM 中文文档可访问 →Apache TVM 是一个端到端的深度学习编译框架,适用于 CPU、GPU 和各种机器学习加速芯片。 | Apache TVM 中文站
本文介绍如何用 Relay 部署 Keras 模型。
首先安装 Keras 和 TensorFlow,可通过 pip 快速安装:
pip install -U keras --userpip install -U tensorflow --user
或参考官网:
import tvmfrom tvm import teimport tvm.relay as relayfrom tvm.contrib.download import download_testdataimport kerasimport tensorflow as tfimport numpy as np加载预训练的 Keras 模型
加载 Keras 提供的预训练 resnet-50 分类模型:
if tuple(keras.__version__.split(".")) < ("2", "4", "0"): weights_url = "".join( [ ";, "download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5", ] ) weights_file = "resnet50_keras_old.h5"else: weights_url = "".join( [ " ;, "resnet/resnet50_weights_tf_dim_ordering_tf_kernels.h5", ] ) weights_file = "resnet50_keras_new.h5"weights_path = download_testdata(weights_url, weights_file, module="keras")keras_resnet50 = tf.keras.applications.resnet50.ResNet50( include_top=True, weights=None, input_shape=(224, 224, 3), classes=1000)keras_resnet50.load_weights(weights_path)加载测试图像
这里使用的还是先前猫咪的图像:
from PIL import Imagefrom matplotlib import pyplot as pltfrom tensorflow.keras.applications.resnet50 import preprocess_inputimg_url = ";img_path = download_testdata(img_url, "cat.png", module="data")img = Image.open(img_path).resize((224, 224))plt.imshow(img)plt.show()# 预处理输入data = np.array(img)[np.newaxis, :].astype("float32")data = preprocess_input(data).transpose([0, 3, 1, 2])print("input_1", data.shape)
输出结果:
input_1 (1, 3, 224, 224)使用 Relay 编译模型
将 Keras 模型(NHWC 布局)转换为 Relay 格式(NCHW 布局):
shape_dict = {"input_1": data.shape}mod, params = relay.frontend.from_keras(keras_resnet50, shape_dict)# 编译模型target = "cuda"dev = tvm.cuda(0)# TODO(mbs):opt_level=3 导致 nn.contrib_conv2d_winograd_weight_transform# 很可能由于潜在的错误,最终出现在 cuda 上的内存验证失败的模块中。# 注意:只能在 evaluate() 中传递 context,它不被 create_executor() 捕获。with tvm.transform.PassContext(opt_level=0): model = relay.build_module.create_executor("graph", mod, dev, target, param).evaluate()在 TVM 上执行
dtype = "float32"tvm_out = model(tvm.nd.array(data.astype(dtype)))top1_tvm = np.argmax(tvm_out.numpy()[0])查找分类集名称
在 1000 个类的分类集中,查找分数最高的第一个:
synset_url = "".join( [ ";, "4d0b62f3d01426887599d4f7ede23ee5/raw/", "596b27d23537e5a1b5751d2b0481ef172f58b539/", "imagenet1000_clsid_to_human.txt", ])synset_name = "imagenet1000_clsid_to_human.txt"synset_path = download_testdata(synset_url, synset_name, module="data")with open(synset_path) as f: synset = eval(f.read())print("Relay top-1 id: {}, class name: {}".format(top1_tvm, synset[top1_tvm]))# 验证 Keras 输出的正确性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]))
输出结果:
Relay top-1 id: 285, class name: Egyptian catKeras top-1 id: 285, class name: Egyptian cat
标签: #googlenet keras