python - 使用 sklearn 在 Python 中拟合的高斯混合模型太慢 - 还有其他选择吗?

标签 python performance opencv gaussian mixture-model

我需要在 RGB 图像上使用高斯混合模型,因此数据集非常大。这需要实时运行(来自网络摄像头)。我首先用 Matlab 编写了这个代码,我能够为 1729∆×∆866 的图像实现 0.5 秒的运行时间。最终应用程序的图像会更小,因此时间会更快。

但是,我需要为最终应用程序使用 Python 和 OpenCV 来实现它(我需要它在嵌入式板上运行)。我翻译了所有代码并使用 sklearn.mixture.GMM 替换了 Matlab 中的 fitgmdist。计算 GMM 模型本身的代码行仅用了 7.7e-05 秒,但拟合模型的代码行却用了 19 秒。我尝试过其他类型的协方差,例如“diag”或“spherical”,时间确实减少了一点,但结果更糟,时间仍然不够好,甚至不接近。

我想知道是否有任何其他库可以使用,或者将函数从 Matlab 转换为 Python 是否值得。

这是我的例子:

import cv2
import numpy as np
import math
from sklearn.mixture import GMM

im = cv2.imread('Boat.jpg');

h, w, _ = im.shape;       # Height and width of the image

# Extract Blue, Green and Red
imB = im[:,:,0]; imG = im[:,:,1]; imR = im[:,:,2];

# Reshape Blue, Green and Red channels into single-row vectors
imB_V = np.reshape(imB, [1, h * w]);
imG_V = np.reshape(imG, [1, h * w]);
imR_V = np.reshape(imR, [1, h * w]);

# Combine the 3 single-row vectors into a 3-row matrix
im_V =  np.vstack((imR_V, imG_V, imB_V));

# Calculate the bimodal GMM
nmodes = 2;
GMModel = GMM(n_components = nmodes, covariance_type = 'full', verbose = 0, tol = 1e-3)
GMModel = GMModel.fit(np.transpose(im_V))

非常感谢您的帮助

最佳答案

您可以尝试使用“对角线”或球形协方差矩阵而不是完整协方差矩阵进行拟合。 covariance_type='诊断' 或者 covariance_type='spherical'

我相信它会快得多。

关于python - 使用 sklearn 在 Python 中拟合的高斯混合模型太慢 - 还有其他选择吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37300698/

相关文章:

sql - 多列(每种类型一列)与单个 TEXT/Clob 列

database - 我应该最大限度地分解关系数据库中的表吗?

Java JNI 调用比预期慢(至少 2 毫秒/调用)

c++ - C++中的mtcnn人脸对齐

python - 星型递归 Python

python - Django 中 AbstractUser 和 AbstractBaseUser 的区别?

javascript - 使用 Javascript 将页面打印为 PDF(使用 Django)

python - Tkinter 与 Python 3.3 : Change colour of button on click

python - 在Jupyter笔记本中使用OpenCV查看tif图像文件

python - 如何在 ActivePython 2.7 的 OSX 10.5 32 位上安装 OpenCV 2.2?