如何 运行 在 gpu 上伪造量化图?

How to run a fake quantized graph on a gpu?

我有一个从here下载的量化mobilenet,这个图包含训练期间的假量化节点,以模拟测试时间输出。我想从该网络的最后一个逐点卷积层收集输出。

量化冻结模型包含额外的 fc、softmax 等层,这些层对我的应用程序没有用。

我有以下加载图表的代码。

def load_graph(frozen_graph_filename):
    # We load the protobuf file from the disk and parse it to retrieve the
    # unserialized graph_def
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

with tf.Graph().as_default() as graph:
    # The name var will prefix every op/nodes in your graph
    # Since we load everything in a new graph, this is not needed
    tf.import_graph_def(graph_def, name="prefix")
return graph

graph1 = load_graph("./quantized_fake.pb")

input = graph1.get_tensor_by_name('prefix/input:0')
output = graph1.get_tensor_by_name('prefix/MobilenetV1/MobilenetV1/Conv2d_13_pointwise/Conv2D_Fold:0')

然后 运行 使用 sess.run() ,但是我观察到卷积层的输出没有量化(8 位),因为它应该是 运行ning在手机上。

如何在 运行在我的电脑上运行代码时生成与在移动设备上生成的相同的输出。

tflite 文件可以用于 pc 上的推理吗?

TensorFlow 伪量化图实际上并未量化,它插入了 FakeQuantization operations 以模拟量化。这些仅由 TensorFlow Lite 转换为完全量化的操作。这就是为什么 运行 使用 TensorFlow 伪量化图只会产生浮点值而不是量化值。

TensorFlow Lite 量化目前 CPU-only,可以在 CPU PC 上 运行。这是 example,介绍了如何在您的 PC 上 运行 调用 TFLite 解释器。