简体   繁体   English

如何在 AWS Lambda 上使用 Tensorflow Lite

[英]How to use Tensorflow Lite on AWS Lambda

I'm trying to host a small model I have compiled down to a .tflite on AWS Lambda.我正在尝试在 AWS Lambda 上托管一个我编译为 .tflite 的小模型。 Using either the python 3.6 or python 3.7 tflite wheel files available on the tensorflow website I zip up my packages/code, and upload to S3 and link to lambda with space to spare.使用 tensorflow 网站上提供的 python 3.6 或 python 3.7 tflite 轮文件,我压缩了我的包/代码,然后上传到 S3 并链接到 lambda 并留出空间。 However, when I test my function it crashes when trying to load tflite.但是,当我测试我的函数时,它在尝试加载 tflite 时崩溃了。 Initially, it couldn't load shared object files.最初,它无法加载共享对象文件。 This was the error这是错误

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_predict': No module named '_interpreter_wrapper')

I found this shared object file and moved it up into the local directory, and then got another error我找到了这个共享对象文件并将其移动到本地目录中,然后又出现了另一个错误

Unable to import module 'lambda_predict': /lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/_interpreter_wrapper.so)

My base system is Ubuntu (Bionic Beaver) Both these errors come from importing tflite我的基本系统是 Ubuntu (Bionic Beaver) 这两个错误都来自导入 tflite

Okay I solved this today.好的,我今天解决了这个问题。

Complete solution and compiled dependencies for amazonlinux/aws lambda on my github: https://github.com/tpaul1611/python_tflite_for_amazonlinux我的 github 上 amazonlinux/aws lambda 的完整解决方案和编译依赖项: https : //github.com/tpaul1611/python_tflite_for_amazonlinux

So the problem is that aws lambda runs on amazonlinux which apparently needs a different compilation of the tflite _interpreter_wrapper than the one tensorflow is currently providing on their website.所以问题是 aws lambda 在 amazonlinux 上运行,这显然需要与 tensorflow 目前在其网站上提供的不同的 tflite _interpreter_wrapper编译。 https://www.tensorflow.org/lite/guide/python https://www.tensorflow.org/lite/guide/python

My solution was to compile it natively on amazonlinux using docker and the script that tensorflow is providing in their git repo.我的解决方案是使用 docker 和 tensorflow 在其 git repo 中提供的脚本在 amazonlinux 上本地编译它。 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/pip_package https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/pip_package

I created a Dockerfile:我创建了一个 Dockerfile:

FROM amazonlinux

WORKDIR /tflite

RUN yum groupinstall -y development
RUN yum install -y python3.7
RUN yum install -y python3-devel
RUN pip3 install numpy wheel
RUN git clone --branch v2.2.0-rc0 https://github.com/tensorflow/tensorflow.git
RUN sh ./tensorflow/tensorflow/lite/tools/pip_package/build_pip_package.sh
RUN pip3 install tensorflow/tensorflow/lite/tools/pip_package/gen/tflite_pip/python3/dist/tflite_runtime-2.2.0rc0-cp37-cp37m-linux_x86_64.whl

CMD tail -f /dev/null

and then ran the following commands:然后运行以下命令:

docker build -t tflite_amazonlinux .
docker run -d --name=tflite_amazonlinux tflite_amazonlinux
docker cp tflite_amazonlinux:/usr/local/lib64/python3.7/site-packages .
docker stop tflite_amazonlinux

These commands output a folder called site-packages which contain the corretly compiled tflite python dependencies for amazonlinux and therefore also aws lambda.这些命令输出一个名为site-packages的文件夹,其中包含为 amazonlinux 正确编译的 tflite python 依赖项,因此也包含 aws lambda。

If you currently are trying to zip it up locally and upload the zip, your binary files may not run on the same OS that lambda runs on.如果您当前正尝试在本地将其压缩并上传 zip,则您的二进制文件可能无法在运行 lambda 的同一操作系统上运行。

You may want to try SAM as @Yann suggests as it can build your deployment package for you ;您可能想按照@Yann 的建议尝试 SAM,因为它可以为您构建部署包 however, I am not sure if this can get the proper binary you need.但是,我不确定这是否可以获得您需要的正确二进制文件。

What I normally do to get the right binary for lambda is launch an ec2 instance with the same ami as what lambda uses under the hood and download it there.我通常为 lambda 获得正确的二进制文件所做的是启动一个 ec2 实例,该实例与 lambda 在后台使用的 ami 相同,然后在那里下载。 You can then export it to s3, download it locally, and then package it.然后就可以导出到s3,下载到本地,然后打包。

Info on using binaries on lambda: https://aws.amazon.com/premiumsupport/knowledge-center/lambda-linux-binary-package/关于在 lambda 上使用二进制文件的信息: https : //aws.amazon.com/premiumsupport/knowledge-center/lambda-linux-binary-package/

Info on what ami to use for ec2: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html有关用于 ec2 的 ami 的信息: https : //docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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