小编Har*_*ngh的帖子

为什么与 Matlab 相比,在 python 中运行的 CNN 非常慢?

我在 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) 

Run Code Online (Sandbox Code Playgroud)

当尝试使用此代码对同一测试集进行分类时,它似乎对图像进行了正确分类,但速度非常慢,并且在某些点达到高达 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. …
Run Code Online (Sandbox Code Playgroud)

python matlab tensorflow onnx cnn

8
推荐指数
1
解决办法
432
查看次数

标签 统计

cnn ×1

matlab ×1

onnx ×1

python ×1

tensorflow ×1