gpt4 book ai didi

opencv - EM::read() 在 OpenCV 中保存模型后得到不同的 EM::predict() 结果

转载 作者:太空宇宙 更新时间:2023-11-03 21:46:27 24 4
gpt4 key购买 nike

我是 OpenCV 和 C++ 的新手,我正在尝试使用 OpenCV 中的高斯混合模型构建分类器。我弄清楚了它是如何工作的,并让它工作了……也许吧。我现在得到了这样的东西:

如果我在模型训练和保存后立即对训练样本进行分类,我得到了我想要的结果。但是,当我使用 read() 对我的训练数据进行重新分类时,其中一个集群丢失了,这意味着我从同一个 GMM 模型中得到了不同的集群结果。我现在不明白,因为我想要的集群不见了,我无法再次重现分类,直到我使用相同的数据重新训练模型。我检查了运行时的代码, 返回的 Vec2d 中的 result 值从未分配过 predict()到 1(我设置了 3 个簇)。

可能有错误或者我做错了什么?

附注我在 VS2013 中使用 2.4.8

我的程序是这样的:

训练部分

void GaussianMixtureModel::buildGMM(InputArray _src){
//use source to train GMM and save the model

Mat samples, input = _src.getMat();
createSamples(input, samples);
bool status = em_model.train(samples);

saveModel();
}

保存/加载模型

FileStorage fs(filename, FileStorage::READ);
if (fs.isOpened()) // if we have file with parameters, read them
{
const FileNode& fn = fs["StatModel.EM"];
em_model.read(fn);
fs.release();
}

FileStorage fs_save(filename, FileStorage::WRITE);
if (fs_save.isOpened()) // if we have file with parameters, read them
{
em_model.write(fs_save);
fs_save.release();
}

预测部分

vector<Mat> GaussianMixtureModel::classify(Mat input){
/// samples is a matrix of channels x N elements, each row is a set of feature
Mat samples;
createSamples(input, samples);
for (int k = 0; k < clusterN; k++){
masks[k] = Mat::zeros(input.size(), CV_8UC1);
}
int idx = 0;
for (int i = 0; i < input.rows; i++){
for (int j = 0; j < input.cols; j++){
//process the predicted probability
Mat probs(1, clusterN, CV_64FC1);
Vec2d response = em_model.predict(samples.row(idx++), probs);
int result = cvRound(response[1]);
for (int k = 0; k < clusterN; k++){
if (result == k){
// change to the k-th class's picture
masks[k].at<uchar>(i, j) = 255;
}
...
// something else
}
}
}
}

最佳答案

我想我的回答为时已晚,但由于我遇到了同样的问题,所以我找到的解决方案可能对其他人有用。

通过分析源代码,我注意到在 EM::COV_MAT_DIAGONAL 的情况下,协方差矩阵的特征值(源代码中的 covsEigenValues)是在加载保存的数据后通过 SVD 获得的。但是,SVD 计算奇异值(在我们的例子中为特征值)并按升序存储它。为了防止这种情况,我只是直接提取 covsEigenValues 中加载的协方差矩阵的对角线元素以保持良好的顺序。

关于opencv - EM::read() 在 OpenCV 中保存模型后得到不同的 EM::predict() 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23485982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com