LabelImage RandomForestImage::improveHistograms(const RGBDImage& image, const LabelImage& labelImage, const bool onGPU, bool useDepthImages) const { LabelImage prediction(image.getWidth(), image.getHeight()); const LabelType numClasses = getNumClasses(); if (treeData.size() != ensemble.size()) { throw std::runtime_error((boost::format("tree data size: %d, ensemble size: %d. histograms normalized?") % treeData.size() % ensemble.size()).str()); } cuv::ndarray<float, cuv::host_memory_space> hostProbabilities( cuv::extents[numClasses][image.getHeight()][image.getWidth()], m_predictionAllocator); //These offsets should have been used instead of traversing to the leaf again /* cuv::ndarray<unsigned int, cuv::dev_memory_space> nodeOffsets( cuv::extents[image.getHeight()][image.getWidth()], m_predictionAllocator); */ if (onGPU) { cuv::ndarray<float, cuv::dev_memory_space> deviceProbabilities( cuv::extents[numClasses][image.getHeight()][image.getWidth()], m_predictionAllocator); cudaSafeCall(cudaMemset(deviceProbabilities.ptr(), 0, static_cast<size_t>(deviceProbabilities.size() * sizeof(float)))); { utils::Profile profile("classifyImagesGPU"); for (const boost::shared_ptr<const TreeNodes>& data : treeData) { classifyImage(treeData.size(), deviceProbabilities, image, numClasses, data, useDepthImages); bool found_tree = false; //should be change to parallel for and add lock for (size_t treeNr = 0; treeNr < ensemble.size(); treeNr++) { if (data->getTreeId() == ensemble[treeNr]->getId()) { found_tree =true; const boost::shared_ptr<RandomTree<PixelInstance, ImageFeatureFunction> >& tree = ensemble[treeNr]->getTree(); //this should have been used and done before trying to classify the images, since it doesn't change //std::vector<size_t> leafSet; //tree->collectLeafNodes(leafSet); for (int y = 0; y < image.getHeight(); y++) for (int x = 0; x < image.getWidth(); x++) { LabelType label = labelImage.getLabel(x,y); if (!shouldIgnoreLabel(label)) { PixelInstance pixel(&image, label, x, y); //This should be changed. When classifying the image, the nodeoffsets should be returned and those used directly //instead of traversing again to the leaves. As a test, can check if the nodeoffset is the same as the one returned //by travertoleaf tree->setAllPixelsHistogram(pixel); } } } if (found_tree) break; } } } } //should also add the CPU code! return prediction; }
double calculatePixelAccuracy(const LabelImage& prediction, const LabelImage& groundTruth, const bool includeVoid, ConfusionMatrix* confusionMatrix) { size_t correct = 0; size_t wrong = 0; if (confusionMatrix) { LabelType numClasses = 0; for (int y = 0; y < groundTruth.getHeight(); ++y) { for (int x = 0; x < groundTruth.getWidth(); ++x) { numClasses = std::max(numClasses, groundTruth.getLabel(x, y)); } } numClasses++; if (confusionMatrix->getNumClasses() < numClasses) { confusionMatrix->resize(numClasses); confusionMatrix->reset(); } } for (int y = 0; y < prediction.getHeight(); ++y) { for (int x = 0; x < prediction.getWidth(); ++x) { const LabelType label = groundTruth.getLabel(x, y); if (!includeVoid && label == 0) { // skip void continue; } const LabelType predictedClass = prediction.getLabel(x, y); if (predictedClass == label) { correct++; } else { wrong++; } if (confusionMatrix) { confusionMatrix->increment(label, predictedClass); } } } size_t numPixels; if (includeVoid) { numPixels = prediction.getWidth() * prediction.getHeight(); } else { numPixels = correct + wrong; } return static_cast<double>(correct) / numPixels; }
double calculatePixelAccuracy(const LabelImage& prediction, const LabelImage& groundTruth, const bool includeVoid, const std::vector<LabelType>* ignoredLabels, ConfusionMatrix* confusionMatrix) { size_t correct = 0; size_t wrong = 0; if (confusionMatrix) { LabelType numClasses = 0; for (int y = 0; y < groundTruth.getHeight(); ++y) { for (int x = 0; x < groundTruth.getWidth(); ++x) { numClasses = std::max(numClasses, groundTruth.getLabel(x, y)); } } numClasses++; if (confusionMatrix->getNumClasses() < numClasses) { confusionMatrix->resize(numClasses); confusionMatrix->reset(); } } for (int y = 0; y < prediction.getHeight(); ++y) { for (int x = 0; x < prediction.getWidth(); ++x) { const LabelType label = groundTruth.getLabel(x, y); bool ignore = false; // assert(!includeVoid && ignoredLabels); if (!includeVoid && !ignoredLabels->empty()) for (LabelType ID: *ignoredLabels) if (ID == label) { ignore = true; break; } if (ignore) continue; const LabelType predictedClass = prediction.getLabel(x, y); if (predictedClass == label) { correct++; } else { wrong++; } if (confusionMatrix) { confusionMatrix->increment(label, predictedClass); } } } size_t numPixels; if (includeVoid) { numPixels = prediction.getWidth() * prediction.getHeight(); } else { numPixels = correct + wrong; } return static_cast<double>(correct) / numPixels; }