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

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(缓冲)的缩写

posted on 2023-02-16 16:09  lexn  阅读(88)  评论(0编辑  收藏  举报

导航