高斯混合模型适合Python与sklearn太慢 - 任何替代?

时间:2016-05-18 13:12:23

标签: 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'或'sphere',时间确实减少了一点,但结果更糟糕,时间仍然不够好,甚至没有接近。

我想知道是否有其他我可以使用的库,或者是否值得将函数从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))

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以尝试使用'对角线'或球形协方差矩阵而不是满。  covariance_type='diag' 要么  covariance_type='spherical'

我相信它会快得多。