Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q1, Q2: Featured extraction done and prior probability estimation done. #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Dealine**: 01.04.2021

Please put your name here:
**Name:** .......
**Name:** Chirag Vaghela, Akarawint Chawalitanont
## Foreword
### Implementation of a Minimal Classification System

Expand Down
Binary file added data/renders/solution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/renders/test_gt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/renders/test_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions src/Bayes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ CBayes::CBayes(byte nStates, word nFeatures)
void CBayes::addFeatureVec(const Mat & featureVector, byte gt)
{
// --- PUT YOUR CODE HERE ---




for (word f = 0; f < m_nFeatures; f++) {
byte feature = featureVector.at<byte>(f, 0);
m_vPDF[f * m_nStates + gt]->addPoint(feature);
}

m_pPrior->addPoint(gt);
}

Mat CBayes::getPotentials(const Mat & featureVector) const
Expand All @@ -30,15 +30,15 @@ Mat CBayes::getPotentials(const Mat & featureVector) const
for (byte s = 0; s < m_nStates; s++)
res.at<float>(s, 0) = static_cast<float>(m_pPrior->getDensity(s));

// --- PUT YOUR CODE HERE ---
for (byte s = 0; s < m_nStates; s++)
for (word f = 0; f < m_nFeatures; f++)
res.at<float>(s, 0) = m_vPDF[f * m_nStates + s]->isEstimated() ? res.at<float>(s, 0) * m_vPDF[f * m_nStates + s]->getDensity(featureVector.at<byte>(f, 0)): 0;



return res;
return res;
}

void CBayes::printPriorProbabilities(void) {
for (byte s = 0; s < m_nStates; s++)
printf("%.1f%%\t", 100 * m_pPrior->getDensity(s));
printf("\n");
}
}
47 changes: 38 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,33 @@ int main(int argc, char *argv[])
for (int y = 0; y < imgSize.height; y++) {
for (int x = 0; x < imgSize.width; x++) {
// --- PUT YOUR CODE HERE ---

featureVector.row(0) = train_fv.at<Vec3b>(y, x)[0];
featureVector.row(1) = train_fv.at<Vec3b>(y, x)[1];
featureVector.row(2) = train_fv.at<Vec3b>(y, x)[2];


if (x == 0 && y == 0)
std::cout << featureVector << std::endl;

byte gt = train_gt.at<byte>(y, x);

classifier->addFeatureVec(featureVector, gt);
} // x
} // y
Timer::stop();

classifier->printPriorProbabilities();
// 17.2% 0.4% 59.5% 9.9% 13.0% 0.0%
// Car class is not represented in training image because 0.0% probability

// ========================= Testing =========================
Timer::start("Testing... ");
for (int y = 0; y < imgSize.height; y++) {
for (int x = 0; x < imgSize.width; x++) {
// --- PUT YOUR CODE HERE ---

featureVector.row(0) = test_fv.at<Vec3b>(y, x)[0];
featureVector.row(1) = test_fv.at<Vec3b>(y, x)[1];
featureVector.row(2) = test_fv.at<Vec3b>(y, x)[2];


// get potentials
Expand All @@ -61,21 +68,39 @@ int main(int argc, char *argv[])
// find the largest potential
byte classLabel = 0;
// --- PUT YOUR CODE HERE ---



double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
minMaxLoc( potentials, &minVal, &maxVal, &minLoc, &maxLoc );
// float maxV = 0.0;
// float maxL = 0;
// for (int i = 0; i < potentials.rows; i++) {
// std::cout << potentials.at<float>(i) << " ";
// if (potentials.at<float>(i) > maxV) {
// maxV = potentials.at<float>(i);
// maxL = i;
// }
// }
// std::cout << "MaxV:" << maxVal << "maxL:" << " " << maxLoc.y;

classLabel = (byte)maxLoc.y;
solution.at<byte>(y, x) = classLabel;
}
// cout << "\n";
}
Timer::stop();

// ====================== Evaluation =======================
Timer::start("Evaluation... ");
double accuracy = 0;
for (int y = 0; y < imgSize.height; y++)
for (int x = 0; x < imgSize.width; x++)
if (solution.at<byte>(y, x) == test_gt.at<byte>(y, x))
accuracy++;
for (int y = 0; y < imgSize.height; y++) {
for (int x = 0; x < imgSize.width; x++){
if (solution.at<byte>(y, x) == test_gt.at<byte>(y, x)) {
accuracy++;
}
}
}
accuracy /= (imgSize.height * imgSize.width);
Timer::stop();
printf("Accuracy = %.2f%%\n", accuracy * 100);
Expand All @@ -87,6 +112,10 @@ int main(int argc, char *argv[])
imshow("Test image", test_img);
imshow("groundtruth", test_gt);
imshow("solution", solution);

imwrite(dataPath + "renders/test_img.png", test_img);
imwrite(dataPath + "renders/test_gt.png", test_gt);
imwrite(dataPath + "renders/solution.png", solution);
waitKey();

return 0;
Expand Down