CV-部署芯片接续-CV全流程部署-TF版本

1 单个CNN算子

import cv2
import numpy as np
import tensorflow as tf
import os
from tensorflow.python.framework import graph_util # 参考连接 https://blog.csdn.net/tensorflowforum/article/details/112352764 代码
# 参考连接 参数详解:https://blog.csdn.net/weixin_43529465/article/details/124721583
# https://blog.csdn.net/rain6789/article/details/78754516 class SingleCnn(tf.keras.Model):
def __init__(self):
super(SingleCnn, self).__init__()
# filters=1 卷积核数目,相当于卷积核的channel
self.conv = tf.keras.layers.Conv2D(filters=1,
kernel_size=[1, 1],
# valid表示不填充, same表示合理填充
padding='valid',
# data_format='channels_last',-> 表示HWC,输入可以定义批次
data_format='channels_last',
use_bias=False,
kernel_initializer=tf.keras.initializers.he_uniform(seed=None),
name="conv") def call(self, inputs):
x = self.conv(inputs)
return x
if __name__ == "__main__":
# 图像数据
imagefile = r"catanddog\cat\5.JPG"
img = cv2.imread(imagefile)
img = cv2.resize(img, (64, 64))
img = np.expand_dims(img, axis=0)
print(img.shape, type(img), img.dtype)
img = img.astype(np.uint8)
singlecnn = SingleCnn() output = singlecnn(img)

2 图片导入

    imagefile = r"catanddog\cat\5.JPG"
img = cv2.imread(imagefile)
img = cv2.resize(img, (64, 64))
img = np.expand_dims(img, axis=0)
print(img.shape, type(img), img.dtype)
img = img.astype(np.uint8)

  

3 推理时报错

output = singlecnn(img)
Value for attr 'T' of uint8 is not in the list of allowed values: half, bfloat16, float, double, int32
; NodeDef: {{node Conv2D}}; Op<name=Conv2D; signature=input:T, filter:T -> output:T; attr=T:type,allowed=[DT_HALF, DT_BFLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32]; attr=strides:list(int); attr=use_cudnn_on_gpu:bool,default=true; attr=padding:string,allowed=["SAME", "VALID", "EXPLICIT"]; attr=explicit_paddings:list(int),default=[]; attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]; attr=dilations:list(int),default=[1, 1, 1, 1]> [Op:Conv2D] Call arguments received by layer 'conv' (type Conv2D):
• inputs=tf.Tensor(shape=(1, 64, 64, 3), dtype=uint8)

  

已解决:

    # 未量化的model不支持int32和int8
# img = img.astype(np.int32)
img = tf.convert_to_tensor(img, np.float32)
print(img.shape, type(img), img.dtype)

  

4 保存为PB文件

不是ckpt文件

    # =========== ckpt保存 with session的写法tf2 已不再使用 ===========
# with tf.Session(graph=tf.Graph()) as sess:
# constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['op_to_store']) # 保存参考 https://zhuanlan.zhihu.com/p/146243327
# save_format='tf' 代表保存pb
singlecnn.save('./pbmodel/singlecnn.pb', save_format='tf') # 加载模型 验证可以加载
new_model = tf.keras.models.load_model('./pbmodel/singlecnn.pb', compile=False)
output_ = new_model(img)
# print(output_.shape, output_[0][2:6][2:6])
print(output_.shape)

  

出现问题  保存的pb 文件是一个目录 里面有多个pb文件不知道 用哪个部署 尝试单独使用某一个pb部署 都会报错。

所以需要合一的pb文件。

tf.keras.saving.save_model  |  TensorFlow v2.11.0

pb是protocol(协议) buffer(缓冲)的缩写

最新文章

  1. 重度使用示波器进行优化分析&mdash;&mdash;一个DSDA项目回顾
  2. Meteor + node-imap(nodejs) + mailparser(nodejs) 实现完整收发邮件
  3. Android studio 运行设置
  4. Java并发编程核心方法与框架-phaser的使用
  5. Flex条件判断中注意事项
  6. ubuntu SecureCRT破解
  7. SDRAM总结
  8. jquery easyui防止超出浏览器边界
  9. CSS3 字体
  10. Sqlparameter防SQL注入
  11. ASP.NET开发,简化与封装
  12. 2016NEFU集训第n+3场 G - Tanya and Toys
  13. php 好用的函数
  14. Redis入门 (CentOS7 + Redis-3.2.1)
  15. 微软URLRewriter.dll的url重写在目标框架.Net Framework2.0、4.0和应用程序池经典模式、集成模式下的配置
  16. mac上Android环境变量配置
  17. iOS - 常用的CG结构体
  18. Android Studio安装和配置的方法 &amp; 错误解决
  19. ConEmu配置task的脚本
  20. 9.11 Django视图 view和路由

热门文章

  1. CCRD_TOC_2008年第2期
  2. 第一个webpack例子demo1
  3. ImGui引入深度测试的方法
  4. 第二周作业N67044-张铭扬
  5. Word13 《经费联审结算单》模板office真题
  6. Pyodide 中实现网络请求的 3 种方法
  7. csp201503-2(数字排序)
  8. JS学习-Web Workers API接口
  9. 函数:3ds max 给选择对象设置轴心点
  10. M1芯片使用pod报错 *** for architecture arm64