温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何部署TensorFlow模型

发布时间:2021-07-30 18:13:17 来源:亿速云 阅读:295 作者:Leah 栏目:大数据

如何部署TensorFlow模型,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

TensorFlow模型部署

1. docker镜像拉取

# Download the TensorFlow Serving Docker image and repo
docker pull tensorflow/serving

2. 保存模型

# 保存模型的时候需要设置模型的版本号字段,不然部署到docker后会报找不到模型版本的错误
model.save("./data/models/zsh_test/1")

3. 部署模型

3.1 单个模型部署

# 映射restapi端口8501,grpc端口8500
# 映射模型定义的路径
# 定义模型名称
docker run -p 8501:8501 \
  -v /path/to/model/models/zsh_test:/models/zsh_test/ \
  -e MODEL_NAME=zsh_test -t tensorflow/serving
# 模型结构
~/PycharmProjects/pytorch-study/data/models
❯ tree
.
└── zsh_test
    └── 1
        ├── assets
        ├── saved_model.pb
        └── variables
            ├── variables.data-00000-of-00001
            └── variables.index

4 directories, 3 files

3.2 多个模型部署

  • 目录结构

    在模型保存文件夹models下保存多个模型,并添加models.config配置文件,配置模型基本信息。

    ~/PycharmProjects/pytorch-study/data
    ❯ tree
    .
    ├── exp_all_data.csv
    ├── exp_all_result.csv
    └── models
        ├── models.config
        ├── zsh_test
        │   └── 1
        │       ├── assets
        │       ├── saved_model.pb
        │       └── variables
        │           ├── variables.data-00000-of-00001
        │           └── variables.index
        └── zsh_test1
            └── 1
                ├── assets
                ├── saved_model.pb
                └── variables
                    ├── variables.data-00000-of-00001
                    └── variables.index
    
    9 directories, 9 files


  • 配置文件

    model_config_list:{
        config:{
          name:"zsh_test",
          base_path:"/models/zsh_test",
          model_platform:"tensorflow"
        },
        config:{
          name:"zsh_test1",
          base_path:"/models/zsh_test1",
          model_platform:"tensorflow"
        }
    }


# 部署多个模型
docker run -p 8501:8501 \
-v /path/to/model/models/:/models/ \
-t tensorflow/serving --model_config_file=/models/models.config

4. 测试API

 # http://ip:prot/version/models/model_name
 # 查看模型信息
 curl http://localhost:8501/v1/models/zsh_test
 
 response:
 {
 "model_version_status": [
  {
   "version": "1",
   "state": "AVAILABLE",
   "status": {
    "error_code": "OK",
    "error_message": ""
   }
  }
 ]
}

# 调用模型
curl --location --request POST 'http://localhost:8501/v1/models/zsh_test:predict' \
--header 'Content-Type: application/json' \
--data '{"instances": [test_data]
}'

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI