当前位置:   article > 正文

TensorFlow 分类模型的转换和使用 OpenCV Python 启动_cv.dnn.readnetfromtensorflow

cv.dnn.readnetfromtensorflow

介绍

让我们简要回顾一下使用 OpenCV API 进行 TensorFlow 模型转换的管道中涉及的关键概念。将 TensorFlow 模型转换为 cv.dnn.Net 的第一步是获取冻结的 TF 模型图。冻结图定义了模型图结构与所需变量(例如权重)的保留值的组合。通常冻结的图形保存在 protobuf () 文件中。模型文件生成后,可以使用 cv.dnn.readNetFromTensorflow 函数读取它。.pb.pb

要求

为了能够试验以下代码,您需要安装一组库。为此,我们将使用 python3.7+ 的虚拟环境:

virtualenv -p /usr/bin/python3.7 <env_dir_path>
源<env_dir_path>/bin/activate

对于从源代码构建 OpenCV-Python,请遵循 OpenCV 简介中的相应说明。

在开始安装库之前,可以自定义要求 .txt,排除或包含(例如)某些依赖项。以下行将需求安装启动到先前激活的虚拟环境中:opencv-python

pip 安装 -r 要求.txt

实践

在这一部分中,我们将介绍以下几点:

  1. 创建 TF 分类模型转换管道并提供推理
  2. 评估和测试 TF 分类模型

如果只想运行评估或测试模型管道,可以跳过“模型转换管道”教程部分。

模型转换管道

本子章中的代码位于模块中,可以使用以下行执行:dnn_model_runner

python -m dnn_model_runner.dnn_conversion.tf.classification.py_to_py_mobilenet

以下代码包含下面列出的步骤的说明:

  1. 实例化 TF 模型
  2. 创建TF冻结图
  3. 使用 OpenCV API 读取 TF 冻结图
  4. 准备输入数据
  5. 提供推理
# 初始化 TF MobileNet 模型
original_tf_model = MobileNet(
include_top=真,
权重=“imagenet”
)
# 获取 TF 冻结图路径
full_pb_path = get_tf_model_proto(original_tf_model)
# 使用 OpenCV API 读取冻结图
opencv_net = cv2.dnn.readNetFromTensorflow(full_pb_path)
print(“OpenCV 模型已成功读取。模型层:\n“, opencv_net.getLayerNames())
# 获取预处理后的图片
input_img = get_preprocessed_img(“../数据/squirrel_cls.jpg“)
# 获取 ImageNet 标签
imagenet_labels = get_imagenet_labels(“../data/dnn/classification_classes_ILSVRC2012.txt“)
# 获取 OpenCV DNN 预测
get_opencv_dnn_prediction(opencv_net、input_img、imagenet_labels)
# 获取TF模型预测
get_tf_dnn_prediction(original_tf_model, input_img, imagenet_labels)

为了提供模型推理,我们将使用下面对应于 ImageNet 类 ID 335 的松鼠照片(在 CC0 许可下):

狐狸松鼠、东方狐狸松鼠、黑狐狸

squirrel_cls.jpg

分类模型输入图像

对于所获得的预测的标签解码,我们还需要文件,其中包含 ImageNet 类的完整列表。imagenet_classes.txt

让我们以预训练的 TF MobileNet 为例,更深入地了解每个步骤:

  • 实例化 TF 模型:
# 初始化 TF MobileNet 模型
original_tf_model = MobileNet(
include_top=真,
权重=“imagenet”
)
  • 创建TF冻结图
# 定义 .pb model 的目录
pb_model_path = “模型”
# 定义 .pb 模型的名称
pb_model_name =“移动网.pb”
# 为进一步转换的模型创建目录
os.makedirs(pb_model_path, exist_ok=True)
# 获取模型 TF 图
tf_model_graph = tf.function(lambda x: tf_model(x))
# 获取具体函数
tf_model_graph = tf_model_graph.get_concrete_function(
tf 中。TensorSpec(tf_model.inputs[0].shape, tf_model.inputs[0].dtype))
# 获取冻结混凝土函数
frozen_tf_func = convert_variables_to_constants_v2(tf_model_graph)
# 获取冻结图
frozen_tf_func.graph.as_graph_def()
# 保存完整的 TF 模型
tf.io.write_graph(graph_or_graph_def=frozen_tf_func.graph,
logdir=pb_model_path,
名称=pb_model_name,
as_text=False)

在上述代码成功执行后,我们将在 中得到一个冻结的图。models/mobilenet.pb

# 获取 TF 冻结图路径
full_pb_path = get_tf_model_proto(original_tf_model)
  • 使用 cv2.dnn.blobFromImage 函数准备输入数据:
# 读取镜像
input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
input_img = input_img.astype(np.float32)
# 定义预处理参数
均值 = np.array([1.0, 1.0, 1.0]) * 127.5
比例 = 1 / 127.5
# 准备输入 blob 以适应模型输入:
# 1.减去平均值
# 2.缩放以设置从 0 到 1 的像素值
input_blob = cv2.dnn.blobFromImage(
image=input_img,
scalefactor=scale,
size=(224, 224), # img 目标大小
mean=平均值,
swapRB=True, # BGR -> RGB
crop=True # 中心裁剪
)
print(“输入 blob 形状:{}\n”.format(input_blob.shape))

请注意 cv2.dnn.blobFromImage 函数中的预处理顺序。首先,减去平均值,然后将像素值乘以定义的刻度。因此,为了重现 TF mobilenet.preprocess_input 函数的图像预处理管道,我们将 乘以 。mean127.5

结果,获得了 4 维:input_blob

Input blob shape: (1, 3, 224, 224)

# 设置 OpenCV DNN 输入
opencv_net.setInput(preproc_img)
# OpenCV DNN 推理
out = opencv_net.forward()
print(“OpenCV DNN 预测:\n”)
print(“* 形状: ”, out.shape)
# 获取预测的类 ID
imagenet_class_id = np.argmax(输出)
# 获得信心
置信度 = out[0][imagenet_class_id]
print(“* 类 ID: {}, label: {}”.format(imagenet_class_id, imagenet_labels[imagenet_class_id]))
print(“* 置信度:{:.4f}\n”.format(置信度))

在上述代码执行后,我们将得到以下输出:

OpenCV DNN 预测:
* 形状:(1,1000)
*类ID:335,标签:狐狸松鼠、东方狐狸松鼠、黑狐狸
* 置信度:0.9525
  • 提供 TF MobileNet 推理:
# 推理
preproc_img = preproc_img.转置(0, 2, 3, 1)
print(“TF 输入 blob 形状:{}\n”.format(preproc_img.shape))
输出 = original_net(preproc_img)
print(“\nTensorFlow 模型预测: \n”)
print(“* 形状: ”, out.shape)
# 获取预测的类 ID
imagenet_class_id = np.argmax(输出)
print(“* 类 ID: {}, label: {}”.format(imagenet_class_id, imagenet_labels[imagenet_class_id]))
# 获得信心
置信度 = out[0][imagenet_class_id]
print(“* 置信度:{:.4f}”.format(置信度))

为了适应 TF 模型输入,进行了转置:input_blob

TF 输入 blob 形状:(1, 224, 224, 3)

TF 推理结果如下:

TensorFlow 模型预测:
* 形状:(1,1000)
*类ID:335,标签:狐狸松鼠、东方狐狸松鼠、黑狐狸
* 置信度:0.9525

从实验中可以看出,OpenCV和TF的推理结果是相等的。

模型评估

建议的模块允许在 ImageNet 数据集上运行完整的评估管道,并测试以下 TensorFlow 分类模型的执行:dnn/samplesdnn_model_runner

  • VGG16型
  • VGG19型
  • resnet50的
  • resnet101的
  • resnet152
  • densenet121
  • densenet169
  • 密集网201
  • inceptionresnetv2
  • 开创V3
  • 移动网
  • 移动网络v2
  • nasnetlarge大
  • 纳斯网移动
  • Xception(Xception)

此列表还可以通过进一步适当的评估管道配置进行扩展。

评估模式

下行表示模块在评估模式下的运行:

python -m dnn_model_runner.dnn_conversion.tf.classification.py_to_py_cls --model_name <tf_cls_model_name>

从列表中选择的分类模型将被读入 OpenCV 对象。TF 和 OpenCV 模型的评估结果(精度、推理时间、L1)将写入日志文件。推理时间值也将在图表中描述,以概括获得的模型信息。cv.dnn_Net

必要的评估配置在test_config.py中定义,并可根据数据位置的实际路径进行修改:

@dataclass
类 TestClsConfig:
batch_size:int = 50
frame_size:int = 224
img_root_dir:str = “./ILSVRC2012_img_val”
# image-class 匹配的位置
img_cls_file:str = “./val.txt”
bgr_to_rgb:bool = True

可以根据所选模型自定义值。TestClsConfig

要启动对 TensorFlow MobileNet 的评估,请运行以下行:

python -m dnn_model_runner.dnn_conversion.tf.classification.py_to_py_cls --model_name mobilenet

脚本启动后,将在以下位置生成包含评估数据的日志文件:dnn_model_runner/dnn_conversion/logs

===== 使用以下参数运行模型评估:
* val 数据位置:./ILSVRC2012_img_val
* 日志文件位置:dnn_model_runner/dnn_conversion/logs/TF_mobilenet_log.txt
测试模式

以下行表示模块在测试模式下的运行,即它提供了模型推理的步骤:

python -m dnn_model_runner.dnn_conversion.tf.classification.py_to_py_cls --model_name <tf_cls_model_name> --test True --default_img_preprocess <True/False> --evaluate False

此处的 key 定义是要使用某些特定值参数化模型测试过程,还是使用默认值,例如 、 或 。default_img_preprocessscalemeanstd

测试配置在test_config.py类中表示:TestClsModuleConfig

@dataclass
类 TestClsModuleConfig:
cls_test_data_dir:str = “../数据”
test_module_name: str = “classification”
test_module_path:str = “classification.py”
input_img:str = os.path.join(cls_test_data_dir, “squirrel_cls.jpg”)
型号: str = “”
frame_height:str = str(TestClsConfig.frame_size)
frame_width:str = str(TestClsConfig.frame_size)
比例:str = “1.0”
mean: List[str] = field(default_factory=lambda: [“0.0”, “0.0”, “0.0”])
标准:List[str] = 字段(default_factory=list)
裁剪:str = “False”
rgb:str = “真”
rsz_height:str = “”
rsz_width:str = “”
类:str = os.path.join(cls_test_data_dir, “dnn”, “classification_classes_ILSVRC2012.txt”)

缺省图像预处理选项在 中定义。例如,对于 MobileNet:default_preprocess_config.py

tf_input_blob = {
“平均值”: [“127.5”, “127.5”, “127.5”],
“比例”:str(1 / 127.5),
“std”:[],
“crop”: “真”,
“rgb”: “真”
}

模型测试的基础在 samples/dnn/classification.py 中表示。 可以使用提供的转换模型和填充的 cv.dnn.blobFromImage 参数自主执行。classification.py--input

要从头开始重现“模型转换管道”中描述的 OpenCV 步骤,请执行以下行:dnn_model_runner

python -m dnn_model_runner.dnn_conversion.tf.classification.py_to_py_cls --model_name mobilenet --test True --default_img_preprocess True --evaluate False

网络预测显示在输出窗口的左上角:

tf_mobilenet_opencv_test_res.jpg

TF MobileNet OpenCV 推理输出

   在线教程

有需要的小伙伴,可以点击下方链接免费领取或者V扫描下方二维码免费领取

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/747191
推荐阅读
相关标签