TensorFlow .pb: Save and Display Models

Paddle Lite端侧AI部署:架构解析与实战优化 端侧AI部署是解决移动端和物联网设备实时推理需求的关键技术,其核心在于平衡计算效率和模型精度。通过轻量化框架如Paddle Lite,开发者可以实现模型在ARM CPU、Mali GPU等异构硬件上的高效运行。该技术采用模块化设计、内存复用等机制,显著降低延迟和功耗,适用于智能门锁、工业质检等隐私敏感和实时性要求高的场景。其中模型量化技术(如INT8动态量化)和硬件适配层(支持NPU加速)是提升性能的关键,典型应用可实现200ms内的人脸识别响应。 阅读详情

What is the use of a *.pb file in TensorFlow and how does it work? - Stack Overflow

Basics

pb stands for protobuf. In TensorFlow, the protbuf file contains the graph definition as well as the weights of the model. Thus, a pb file is all you need to be able to run a given trained model.

Given a pb file, you can load it as follow.

def load_pb(path_to_pb):
    with tf.gfile.GFile(path_to_pb, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='')
        return graph

Once you have loaded the graph, you can basically do anything. For instance, you can retrieve tensors of interest with

input = graph.get_tensor_by_name('input:0')
output = graph.get_tensor_by_name('output:0')

and use regular TensorFlow routine like:

sess.run(output, feed_dict={input: some_data})

Explanation

The .pb format is the protocol buffer (protobuf) format, and in Tensorflow, this format is used to hold models. Protobufs are a general way to store data by Google that is much nicer to transport, as it compacts the data more efficiently and enforces a structure to the data. When used in TensorFlow, it's called a SavedModel protocol buffer, which is the default format when saving Keras/ Tensorflow 2.0 models. More information about this format can be found here and here.

For example, the following code (specifically, m.save), will create a folder called my_new_model, and save in it, the saved_model.pb, an assets/ folder, and a variables/ folder.

# first download a SavedModel from TFHub.dev, a website with models
m = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4")
])
m.build([None, 224, 224, 3])  # Batch input shape.
m.save("my_new_model") # defaults to save as SavedModel in tensorflow 2

In some places, you may also see .h5 models, which was the default format for TF 1.X. source


Extra information: In TensorFlow Lite, the library for running models on mobile and IoT devices, instead of protocol buffers, flatbuffers are used. This is what the TensorFlow Lite Converter converts into (.tflite format). This is another Google format which is also very efficient: it allows access to any part of the message without deserialization (unlike json, xml). For devices with less memory (RAM), it makes more sense to load what you need from the model file, instead of loading the entire thing into memory to deserialize it.


Loading SavedModels in TensorFlow 2

I noticed BiBi's answer to show loading models was popular, and there is a shorter way to do this in TF2:

import tensorflow as tf
model_path = "/path/to/directory/inception_v1_224_quant_20181026"
model = tf.saved_model.load(model_path)

Note,

  • the directory (i.e. inception_v1_224_quantBasis _20181026) has to have a saved_model.pb or saved_model.pbtxt, otherwise the code will crash. You cannot specify the .pb path, specify the directory.
  • you might get TypeError: 'AutoTrackable' object is not callable for older models, fix here.

If you load a TF1 model, I found that I don't get any errors, but the loaded file doesn't behave as expected. (e.g. it doesn't have any functions on it, like predict)

More on Saved Models: Official Docs

https://www.tensorflow.org/guide/saved_model

includes: create, save, load and finetuning

View the Model on TensorBoard

tensorboard: view graph from saved_model.pb file [feature request] · Issue #8854 · tensorflow/tensorflow · GitHub

Soln1 (March 31st 2017)

As far as I know, you don't need to create any summaries to load the graph into Tensorboard. If you begin to create a summarywriter and then add the graph to it then you should see the graph appear in Tensorboard. I have some code that does that (as part of another project). I do however agree that being able to import something into Tensorboard would be handy.

I've quickly created a bit of code to load a graph into Tensorboard. See how that goes. Also available as a gist here

import tensorflow as tf
from tensorflow.python.platform import gfile
with tf.Session() as sess:
    model_filename ='PATH_TO_PB.pb'
    with gfile.FastGFile(model_filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        g_in = tf.import_graph_def(graph_def)
LOGDIR='YOUR_LOG_LOCATION'
train_writer = tf.summary.FileWriter(LOGDIR)
train_writer.add_graph(sess.graph)

 ==> the variation in .pb file and tf versions might cause the example scripts to fail, see from further discussions how to deal with these issues, such as:

Don't forget to flush ;) (Nov. 29th 2019)

train_writer.flush()

Soln 2 (Dec. 8th 2020)

New Readers/Visitors

Please note that my original solution was thrown together to temporarily answer the issue to help the user. I then PR'd a helper function into tf where much smarter people have taken it over and seemingly converted for 2.0. I've been fairly dormant on TF now too. Judging by the reactions on my original solution this is clearly a high traffic issue, but please ensure you checkout the latest version of tensorflow/import_pb_to_tensorboard.py at master · tensorflow/tensorflow · GitHub

You may be best off raising a new issue with that code explicitly (or other tb bits. Tb has become a lot more sophisticated since the early days when I was still working with tf regularly!). Do reference this issue still so those who land here can link over to your issue and find a better solution.

Also note that this method is most likely superseded by more accessible/superior ways of loading your model into TB ==> it seems so far loading the models is simplified but working with TB still requires crutches. I've not actually ever needed to use my own helper function, especially if you are using the Keras functionality, so I would recommend that you take some time to get your TB integration working in the best, native way possible so you can get this most out of it. I'll keep an eye on this to help where I can. Cheers

 ==> script usage example from Feb. 13th 2020:

https://colab.research.google.com/drive/13LAUUT9tEH2XeoNA_z9A7uE5omc-lzNv

local file management and import/load local models in colab:

https://blog.youkuaiyun.com/maxzcl/article/details/123731762

Midscene.js:基于AI视觉的跨平台自动化测试框架实战指南 自动化测试是现代软件工程保障质量的关键环节,其核心原理是通过脚本模拟用户操作,验证应用功能与界面。随着应用形态日益复杂,尤其是跨Web、桌面、移动及嵌入式等多终端场景,传统基于元素定位器(如XPath、CSS Selector)的测试方法面临维护成本高、脚本脆弱等挑战。在此背景下,融合计算机视觉(CV)与人工智能(AI)的**视觉驱动测试**技术应运而生,它通过识别屏幕上的文本、图标与控件图像来定位和操作,实现了测试脚本与底层UI实现的解耦,显著提升了**跨平台自动化测试**的稳定性和可维护性。这项技术尤其 阅读详情

相关推荐

tensorflow模型保存为pb的各种姿势

tensorflow模型保存的各种姿势 一,直接保存pb 1, 首先我们当然可以直接在tensorflow训练中直接保存为pb为格式,保存pb的好处就是使用场景是实现创建模型与使用模型的解耦,使得创建模型与使用模型的解耦,使得前向推导inference代码统一。另外的好处就是保存为pb的时候,模型的变量会变成固定的,导致模型的大小会大大减小。 这里稍稍解释下pb:是MetaGraph的pro...

Cutepingping的博客 2万+

python error “object is not callable“ 分析解决

最近在学习python相关的东西,查看别人的代码,有很多地方看不太懂,就写了一些简单的代码来debug 这个调用类的实例化的逻辑。当类中没有特殊函数call时,类的实例不可被调用,当有了call函数,则在调用类的实例的时候会执行call 函数中的内容。因为import的文件里面定义了一个类的对象,因此会调用Animal 类的构造函数进行一些初始化操作。修改主程序 Inherit_test.py,创建一个类调用主程序中的Main函数。会报错,因为Main函数里面调用了一个类的实例。

qq_42429142的博客 1215

PyTorch实现Pix2PixHD图像修复:划痕/遮挡/墨水渍三类破损精准修复

图像修复是计算机视觉中经典的条件生成任务,其核心在于利用已知像素约束重建缺失区域,兼顾结构保真与语义合理性。技术原理上依赖生成对抗网络(GAN)的判别引导能力,结合L1损失保障像素级精度、感知损失对齐高层语义、双尺度判别器协同优化全局构图与局部细节。此类方法在老照片修复、文档净化、屏幕划痕消除等场景具有明确工程价值。本文聚焦基于PyTorch的Pix2PixHD改进实现,特别强化对划痕、遮挡、墨水渍三类真实破损的建模能力——通过物理可解释的mask生成策略、带注释的多分支判别器代码、以及面向毕设与落地的可调

weixin_29094775的博客 301

Python常见异常处理之TypeError: ‘ ’ object is not callable

对于Python运行中console中出现: TypeError: ‘ ’ object is not callable 此语句的语义是: 某个对象不可调用 对于这种引号的参数的对象不可调用,可能存在的问题有: ··· 这个定义参数的变量名和该定义的函数名重名 ··· 此参数类型名写错,与实际不匹配 ··· 无返回return值 TypeError: ‘NoneType ’ object is not callable 可能存在的问题有 ··· def定义没返回return值 ··· return没调

Jason Zeng 3万+

【AI概念】迁移学习 vs. 域自适应 vs. 域泛化|定义、核心原理、数学表达、易混淆点与工程选择建议|实际案例、工程实现细节、未来研究趋势、实际应用场景

大家好,我是爱酱。本篇将会详细梳理迁移学习(Transfer Learning)、域自适应(Domain Adaptation)、域泛化(Domain Generalization)三大常被混淆的核心概念,结合数学公式、典型算法与实际应用场景,帮助你厘清它们的本质区别、联系与工程意义。 注:本文章含大量数学算式、详细例子说明及大量代码演示,大量干货,建议先收藏再慢慢观看理解。新频道发展不易,你们的每个赞、收藏跟转发都是我继续分享的动力!

AI人工智能爱酱~你的AI学习好帮手~ 2466

win11 使用 QEMU 配置龙芯 3A5000 虚拟环境

win11 使用 QEMU 配置龙芯 3A5000 虚拟环境

一朵花开的时间 6150

python出现类调用报错object is not callable,对象不可以被调用

后面有问题的代码又好了,不知道怎么回事,说这个问题的初衷就是万一遇到这种问题,可以用修改后的这个方案解决,多一个解决办法也不是坏事。,实例化对象后理论上可以调用__init__里面的类属性值。二、后面自己写了简单的几个例子,验证了没问题。# 连接log,创建日志器。直接把类的属性值单独创建个方法并返回。().info("进入登录界面")这里直接调用方法就可以了。连接log,创建日志器。

m0_74286016的博客 2837

YOLOv8魔改网络后训练全零?别慌,关掉AMP混合精度训练试试

本文探讨了YOLOv8魔改网络后训练出现全零指标的问题,指出AMP混合精度训练与自定义网络结构的不兼容性。通过分析梯度消失现象和AMP工作原理,提供了关闭AMP、调整学习率等解决方案,并建议预防性开发实践以避免类似问题。

weixin_28338005的博客 321

python中“'xxx' object is not callable”的说明以及解决方案

在python中常常遇到这种情况,‘xxx’ object is not callable,而且往往还发生在压根看不出错误的代码中。这也难怪,因为这个提示没有任何作用。这个提示真正的意思是 任何东西都不能身兼数职 什么意思? 看这串代码 def A(): while 1 : try: global A A=float(inpu...

weixin_45950542的博客 2万+

AHP层次分析法在项目风险评估中的实战应用(含Excel模板)

本文详细介绍了AHP层次分析法在项目风险评估中的实战应用。通过构建风险层次模型、利用判断矩阵量化主观判断,并结合Excel工具进行权重计算与一致性检验,将模糊的定性风险转化为清晰的量化排序。文章以新产品上线项目为例,提供了完整的分析流程和可操作的Excel模板,帮助项目经理高效识别并优先处理核心风险。

weixin_29061465的博客 570

32、运算放大器的工作原理与应用

本文详细介绍了运算放大器的工作原理与多种应用场景,涵盖开环与闭环放大器的基本概念、理想运算放大器的特性及其‘黄金规则’。文章深入解析了反相放大器、同相放大器、单位增益跟随器与反相器的配置和增益计算,并探讨了电压比较器及窗口比较器的工作机制。同时提供了运算放大器在实际应用中的选型流程、使用注意事项以及常见故障排查方法,帮助读者全面掌握运算放大器的设计与应用要点。

u5v6w7的博客 163

TypeError: ‘‘ object is not callable

python调用报错解决方案

Max_future_hust的博客 1342

【声纳技术】声呐时间反转镜技术:从被动聚焦到主动空间复用

时间反转镜技术以其独特的物理机制,为水声通信与声呐探测提供了一条绕过传统自适应均衡器的新路径。被动时间反转利用接收端的空间分集实现脉冲压缩,结构简洁、计算高效;主动时间反转则通过发射端预编码,将多径能量引导至目标位置,为多用户下行链路的空间复用奠定了物理基础。从Kuperman等人在海洋中首次验证声学时间反转镜,到TR-MIMO声呐的湖试验证,再到VTRM-CRK多用户通信系统的海试成功,该技术已从理论概念逐步走向工程实用。

u012133341的博客 55

Macro 中 Illustrator 编辑器浏览器夹具实战:用 Vite 与 Playwright 验证 ai-engine wasm 集成

本指南以仓库中的 [browser-test/README.md](https://link.gitcode.com/i/6377c5f6e8e98c11efb88adf4332ab1a) 为主线,讲解 Macro 仓库里 block-ai(Illustrator 风格编辑器)的浏览器夹具(fixture):它如何在不加载登录、文档存储与产品路由的前提下挂载真实的 `AiEditorView` 与

gitblog_00695的博客 390

STM32入门教程:智能马桶盖控制

通过编写相应的代码,您可以根据需要实现其他功能,例如座圈温度调节和液晶显示屏显示等。初始化配置 首先,使用STM32CubeMX创建一个新的工程,并选择您所使用的STM32单片机型号。智能马桶盖是一种集成了多种功能的智能家居设备,通过控制芯片来实现自动冲水、座圈加热、座圈温度调节等功能。在本教程中,我们将使用STM32系列单片机来控制智能马桶盖的各个功能。以上代码演示了如何通过STM32的GPIO控制引脚来控制智能马桶盖的冲水和座圈加热功能。写代码 接下来,我们将通过编写代码来控制智能马桶盖的各个功能。

2401_85258012的博客 657

倍福PLC--PERSISTENT断电保持数据和BOOT中各文件对应的解析

将D:\TC3程序\TwinCAT Project146\TwinCAT RT(x86)\Plc 文件夹的中三个替换文件复制到:G:\TwinCAT\3.1\Boot\Plc完成替换,如下图。(2) 找到程序所存的路径,编程电脑的D:\TC3程序\TwinCAT Project146路径下,并选择对应系统平台TwinCAT RT(x86)(3) 控制器断电,拔出内存卡,使用读卡器,在编程电脑中打开:(本次测试读卡器为G盘)(5) 开机完成后,TwinCAT自动运行,当前闪灯频率为50ms。

weixin_41883890的博客 2340

从AI音乐生成到文化概念驱动:实战构建概念融合生成引擎

AI生成技术正从通用内容创作迈向高度定制化与场景化应用。其核心原理在于利用深度学习模型(如VAE、Transformer)学习数据的潜在表示空间,并通过在该空间中进行向量操作来实现可控生成。这一技术价值在于能够将抽象的、非结构化的“文化概念”或“情感意象”量化为机器可理解的数学表示,从而赋能垂直领域的创意内容生产。在应用场景上,它特别适用于需要融合特定风格、情感或文化元素的领域,例如游戏配乐、品牌营销和互动艺术。本文以“哈萨维与枪炮玫瑰”的AI音乐生成实验为具体案例,详细拆解了如何构建一个从概念定义、数据标

weixin_30394669的博客 360

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

13.rk3576环境下机器人视觉——姿态获取

rk3576的机器人视觉在这里用的是传统算法求6d位姿,二维分割用了yolo,所以需要将分割算法的pt模型转为rknn模型。整个算法环境和模型转换步骤在后面详细说。以下环境没有单独建立conda环境,所有环境都安装在系统环境中(没有建立独立的conda环境)。

weixin_71719718的博客 294

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Python中的常见报错:‘xxx‘ object is not callable

【python】Error:‘xxx’ object is not callable ‘xxx’ object is not callable,“xxx”为函数,例如int,list,str。 当出现报错 ‘xxx’ is not callable的时候,通常都是函数名重用或者变量名重用。 网上有其他专业名词的解释,但华而不实,其本质通常都是函数名重用或者变量名重用。 当出现这个错误时查看报错语句中用到的变量名或者函数名在其他语句中是否重用。 例:我在这里用Bettery_number定义了一个属性,又用该

why0630的博客 1万+

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

关于“xxx”object is not callable的异常(转)

所谓callable对象是指一个后边可以加()的对象,比如函数, 所以这种异常肯定是某对象多加了(), 比如:把一个变量用了函数名来命名,结果再调这个函数的时候就会报这个异常。 ...

微电子学与固体电子学-俞驰 6756

python中 TypeError: ‘XXX‘ object is not callable报错原因

使用Python过程中遇到类似这种报错,是因为自定义了一个变量/函数,与python内置的关键字冲突了。自定义的变量会覆盖python内置函数,调用函数时就会出现TypeError报错 如: str = 'test' print(str(1)) 报错如下: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object is not callable

sinat_28228443的博客 2万+

Python中的常见报错:'xxx' object is not callable

【python】Error:’xxx’ object is not callable ‘xxx’ object is not callable,“xxx”为函数,例如int,list,str。 当出现报错 ‘xxx’ is not callable的时候,通常都是函数名重用或者变量名重用。 网上有其他专业名词的解释,但华而不实,其本质通常都是函数名重用或者变量名重用。 当出现这个错误时查看...

MX的博客 18万+

TypeError: 'module' object is not callable(“模块”对象不可调用)

运行结果 代码功能是生成器的调用,为什么报这个错误,“TypeError: ‘module’ object is not callable”从这串英文可以看出,模块调用出问题,在看代码调用了哪个模块,看头导入了什么模块,一个是greenlet模块一个是time模块,然而,我们导入的模块和自己程序文件命名一样,这里显然导入的是自己写的这个文件模块,不是引入的greenlet,解决办法,更改程序文...

一直在路上的JunGle 36万+

tensorflow, TypeError:'Tensor' object is not callable

解决办法:两个tensor相乘,需要加“*”。

weixin_34248118的博客 1万+

【编程55--代码share&debug14】Tensorflow,Pytorch 出现 ‘Tensor‘ object is not callable解决办法

呆呆象呆呆的所有博客目录 Tensorflow,Pytorch 出现 ‘Tensor‘ object is not callable解决办法 主要可能的原因: **出现问题的原因1:**可能是在你想调取张量的属性的时候,而写成了函数形式 # 原因1对应的代码 import torch tensor = torch.zeros((3,3)) print(tensor.shape) tensor() **出现问题的原因2:**可能是你想要索引的时候用的是小括号 # 原因2对应的代码 impo

呆呆象呆呆的博客 4859

python 遇到问题解决:object is not callable

解决TypeError: ‘NoneType’ object is not callable为什么会出现这个错误,原来是去掉pri函数的括号即可,因为do_twice()函数的形参不是函数必须去掉括号。程序如下: ...

shanliangxiao的博客 1万+
上一篇: Minifloats: FP Types for DNNs
下一篇: Communication Channel and Interference
EverNoob
博客等级 码龄7年 134粉丝 88原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值