简体   繁体   English

从 .pb 文件加载 TensorFlow 模型

[英]Load TensorFlow model from .pb file

Background: I have my TensorFlow model stored in a directory with the folders/files: assets, variables, and saved_model.pb.背景:我将我的 TensorFlow 模型存储在包含文件夹/文件的目录中:资产、变量和saved_model.pb。 All I need to do with this model is make predictions (no training) so I do not need the assets and variables folders.我需要对这个模型做的就是进行预测(没有训练),所以我不需要资产和变量文件夹。 The saved_model.pb file is much smaller than the variables folder and storage is extremely important to me. saved_model.pb 文件比 variables 文件夹小得多,存储对我来说非常重要。

Question: How could I load my TensorFlow model from the saved_model.pb file only (no variables or assets folders) in Tensorflow 2.3?问题:如何在 Tensorflow 2.3 中仅从 saved_model.pb 文件(无变量或资产文件夹)加载我的 TensorFlow 模型?

This is my file structure:这是我的文件结构:

saved_models
├── model_1
|   ├── assets
|   |   └── (this is an empty folder)
|   ├── saved_model.pb
|   ├── variables
|   |   ├── variables.data-00000-of-00001
|   |   └── variables.index

The saved_model.pb file only contains a description of the structure of the network. saved_model.pb文件只包含网络结构的描述。 You need the variables as they contain the weights for each of the network layers, without them you would be doing prediction with a blank network.您需要变量,因为它们包含每个网络层的权重,没有它们,您将使用空白网络进行预测。

You could try saving the model in .h5 format.您可以尝试以 .h5 格式保存模型。 This just generates a single file .这只会生成一个文件。 I am not sure if it is smaller than the .pb size but give it a try and find out.我不确定它是否小于 .pb 大小,但试一试并找出答案。 Documentation is here.文档在这里。

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

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