使用Openfaas部署AI模型进行Serverless函数模型推理

本文主要是从基于已经安装部署好openfaas后的环境,来教大家如何部署AI模型进行函数推理。
如果你还未能安装部署openfaas,请openfaas官网进行安装部署。

创建适用于model inference的函数模板:

1. 拿到现有模板:

faas-cli template pull
faas-cli new --list

2. 复制python3-debain模板,命名为python3-model

修改里面的template/python3-model/dockerfile文件:
具体内容为:

FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/classic-watchdog:0.2.1 as watchdog
FROM --platform=${TARGETPLATFORM:-linux/amd64} python:3

ARG TARGETPLATFORM
ARG BUILDPLATFORM

# Allows you to add additional packages via build-arg
ARG ADDITIONAL_PACKAGE

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

# 替换为阿里云的源
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list

# 更新apt-get并安装一些常用工具
RUN apt-get update && apt-get install -y \
    vim \
    git \
    wget \
    curl \
    && rm -rf /var/lib/apt/lists/*

# 替换为清华大学pip源
RUN mkdir -p ~/.pip \
    && echo '[global]' > ~/.pip/pip.conf \
    && echo 'index-url = https://pypi.tuna.tsinghua.edu.cn/simple' >> ~/.pip/pip.conf \
    && echo 'trusted-host = pypi.tuna.tsinghua.edu.cn' >> ~/.pip/pip.conf

# 安装PyTorch和其他依赖项
RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu && \
    pip3 install Pillow \
    && rm -rf /var/lib/apt/lists/
#    pip install numpy && \
#    pip install matplotlib && \
#    pip install scikit-learn && \
#    pip install scipy && \

#    pip install opencv-python-headless && \
#    pip install requests && \
#    pip install tqdm


#RUN apt-get update \
#    && apt-get install -y ca-certificates ${ADDITIONAL_PACKAGE} \
#    && rm -rf /var/lib/apt/lists/

# Add non root user
RUN groupadd app && useradd -r -g app app

WORKDIR /home/app/

COPY index.py           .
COPY requirements.txt   .

RUN chown -R app /home/app && \
    mkdir -p /home/app/python && chown -R app /home/app
USER app
ENV PATH=$PATH:/home/app/.local/bin:/home/app/python/bin/
ENV PYTHONPATH=$PYTHONPATH:/home/app/python

RUN pip install -r requirements.txt --target=/home/app/python

RUN mkdir -p function
RUN touch ./function/__init__.py

WORKDIR /home/app/function/
COPY function/requirements.txt	.

RUN pip install -r requirements.txt --target=/home/app/python

WORKDIR /home/app/

USER root

COPY function           function

# Allow any user-id for OpenShift users.
RUN chown -R app:app ./ && \
    chmod -R 777 /home/app/python

USER app

ENV fprocess="python3 index.py"
EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]

3.创建函数

faas-cli new --lang python3-model resnet18

编写函数代码(随机生成图片,测试使用,可以自行里面的逻辑)
resnet18/handler.py

import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image

# Load pre-trained ResNet18 model
model = models.resnet18(pretrained=True)

def handle(req):
    """handle a request to the function
    Args:
        req (str): request body
    """

    # 转换输入图像
    transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    # 加载并预处理随机生成的图片
    img = Image.new('RGB', (224, 224))
    input_tensor = transform(img)

    # 添加一个批次维度
    input_batch = input_tensor.unsqueeze(0)

    # 将模型设置为评估模式
    model.eval()

    # 使用模型进行预测
    with torch.no_grad():
        output = model(input_batch)

    # 获取预测结果中的最大值及其索引
    _, predicted = torch.max(output.data, 1)

    # 打印预测结果
    print(predicted.item())

    return req+"inference done"

4. 修改添加所需的requestment.txt依赖

resnet18/requestment.txt

按需添加: eg. requests

5. 编译并部署

faas-cli build -f ./resnet18.yml
faas-cli deploy -f ./resnet18.yml

6. 调用

通过 http://127.0.0.1:8080下 ui 或者 curl进行函数调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud Function是一个开源项目,它可以让你使用Spring框架构建无服务器(Serverless)函数Serverless函数可以在云平台上执行,例如AWS Lambda,Azure Functions或Google Cloud Functions。 使用Spring Cloud Function构建Serverless函数非常简单,只需要定义一个函数接口并实现它即可。这个函数接口可以是Java 8函数接口,也可以是Spring框架的Function接口。 下面是一个简单的使用Spring Cloud Function构建Serverless函数的示例: ```java import java.util.function.Function; import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler; public class MyFunction implements Function<String, String> { public String apply(String input) { return "Hello " + input; } public static void main(String[] args) throws Exception { SpringBootRequestHandler<String, String> handler = new SpringBootRequestHandler<>(MyFunction.class); System.out.println(handler.handleRequest("World", null)); } } ``` 在这个示例中,我们定义了一个MyFunction类,它实现了Function接口。apply方法接收一个String类型的参数并返回一个String类型的结果。 在main方法中,我们使用SpringBootRequestHandler类创建一个处理器,并将MyFunction类作为参数传递给它。然后,我们调用handleRequest方法,将参数"World"传递给它。handleRequest方法会调用MyFunction的apply方法,并返回结果。 当我们将这个函数部署到AWS Lambda上时,我们只需要将MyFunction打包成一个Jar文件并上传到AWS Lambda上。然后,我们可以在AWS Lambda控制台上配置触发器,让这个函数在某个事件发生时执行。 使用Spring Cloud Function构建Serverless函数非常简单,它可以让你使用Spring框架构建高效、可扩展、易于维护的Serverless函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值