【发布时间】:2020-08-01 10:31:39
【问题描述】:
我在 Matlab 2019b 中训练了一个 CNN,可将图像分类为三类。当这个 CNN 在 Matlab 中进行测试时,它运行良好,只需 10-15 秒即可对图像进行分类。我在 Maltab 中使用了 exportONNXNetwork 函数,以便可以在 Tensorflow 中实现我的 CNN。这是我在 python 中使用 ONNX 文件的代码:
import onnx
from onnx_tf.backend import prepare
import numpy as np
from PIL import Image
onnx_model = onnx.load('trainednet.onnx')
tf_rep = prepare(onnx_model)
filepath = 'filepath.png'
img = Image.open(filepath).resize((224,224)).convert("RGB")
img = array(img).transpose((2,0,1))
img = np.expand_dims(img, 0)
img = img.astype(np.uint8)
probabilities = tf_rep.run(img)
print(probabilities)
当尝试使用此代码对相同的测试集进行分类时,它似乎可以正确地对图像进行分类,但速度非常慢,并且在某些点达到高达 95+% 的高内存使用率时会冻结我的计算机。
我还注意到在分类时命令提示符会打印:
2020-04-18 18:26:39.214286: W tensorflow/core/grappler/optimizers/meta_optimizer.cc:530] constant_folding failed: Deadline exceeded: constant_folding exceeded deadline., time = 486776.938ms.
有什么方法可以让这个 python 代码分类更快?
【问题讨论】:
-
我建议您首先检查几件事: 1. 您是否将 GPU 与 python 和 Matlab 一起使用? 2.什么需要15秒(Matlab)或更多(python),是分类本身还是加载模型和图像处理? 3. 加载一张图片后,内存何时满?另外,您使用的是什么操作系统?
-
您是否尝试过运行分析器来查看您的瓶颈在哪里?见docs.python.org/3/library/profile.html#module-cProfile 和toucantoco.com/en/tech-blog/tech/…
-
“让python更快的最好方法就是少用它”
-
Matlab 是专有产品,物有所值
-
能贴出matlab代码吗?
标签: python matlab tensorflow conv-neural-network onnx