tensorflow - 将预训练的保存模型从 NCHW 转换为 NHWC,使其与 Tensorflow Lite 兼容
问题描述
我已将模型从 PyTorch 转换为 Keras,并使用后端提取 tensorflow 图。由于 PyTorch 的数据格式是 NCHW,所以提取和保存的模型也是这样。将模型转换为 TFLite 时,由于格式为 NCHW,无法转换。有没有办法将整个图转换为 NHCW?
解决方案
最好有一个与 TFLite 匹配的数据格式的图,以便更快地推理。一种方法是手动将转置操作插入图中,例如: How to convert the CIFAR10 tutorial to NCHW
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as session:
kernel = tf.ones(shape=[5, 5, 3, 64])
images = tf.ones(shape=[64,24,24,3])
imgs = tf.transpose(images, [0, 3, 1, 2]) # NHWC -> NCHW
conv = tf.nn.conv2d(imgs, kernel, [1, 1, 1, 1], padding='SAME', data_format = 'NCHW')
conv = tf.transpose(conv, [0, 2, 3, 1]) # NCHW -> NHWC
print("conv=",conv.eval())
推荐阅读
- ios - iOS 14、mobileconfig、DNS over HTTPS,支持 DNSDomainMatch 白名单
- visual-c++ - 如何在 MSVC 中启用 SSE4.1 和 SSE3(但不是 AVX)
- json - 通过宏定义时未正确推断特征实现
- dbus - 使用 busctl 读取 openbmc 中的传感器值
- javascript - 某些 PDF 在使用 Base64 字符串显示时显示为空白
- c# - NET Core RazorPages 中实现 PageModel 的抽象类
- excel - 函数“如果”在结果的情况下是一个变量
- spring-boot - 在 Springboot 中输入代理设置后分级未知主机“services.gradle.org”错误
- c# - 如何在C# for loop Logic中将三分为三
- css - 任何自定义 CSS 来删除 Slick Slider 轮播中图像上的灰色叠加层?