Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Error when loading LDA from yaml file

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.

Error when loading LDA from yaml file

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.

Error when loading LDA from yaml fileLDA loaded does not work

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.

click to hide/show revision 4
No.4 Revision

LDA loaded does not work

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.

LDA loaded does not work

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.

To clarify, I've been running std::cout on trainData and have 256 cols and 30 rows but still get the error.

LDA loaded does not work

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.

To clarify, I've been running std::cout on trainData and have to confirm it has 256 cols and 30 rows but still get the error.

image description

LDA loaded does not work

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly, perfectly (same code for processing the test images too!), so I can't understand why this error keeps occurring, unless I'm not loading the LDA correctly.occurring.

To clarify, I've been running std::cout on trainData and to confirm it has 256 cols and 30 rows but still get the error.error. This is driving me crazy because if I deliberately make testData an incorrectly sized matrix (not the 256 cols it's expecting) than I get the same answer!

image description

LDA loaded does not work

I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2);
lda.compute(trainData, trainLabels);
cv::Mat projected = lda.project(trainData);
lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2);
lda.load("tag_lda.yml");
cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly (same code for processing the test images too!), so I can't understand why this error keeps occurring.

To clarify, I've been running std::cout on trainData and to confirm it has 256 cols and 30 rows but still get the error. This is driving me crazy because if I deliberately make testData an incorrectly sized matrix (not the 256 cols it's expecting) than I get the same answer!

image description

The main section of code

cv::Mat testData;

for (int i=0; i < 30; i++)
{
    std::string filename = "/Users/u5305887/Desktop/tags/test/";
    filename = filename + std::to_string(i);
    filename = filename + ".jpg";
    cv::Mat image = cv::imread(filename, 0);
    cv::Mat flat_image = image.clone().reshape(0, 1);
    testData.push_back(flat_image);
}

cv::LDA lda;

lda.load("tag_lda.yml");

std::cout << "testData columns: " << testData.cols << std::endl;
std::cout << "testData rows: " << testData.rows << std::endl;

cv::Mat projected = lda.project(testData);

cv::Mat proj_float;
projected.convertTo(proj_float, CV_32FC1);