LCOV - code coverage report
Current view: top level - src - LayerOutput.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 20 22 90.9 %
Date: 2024-12-28 17:36:05 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "LayerOutput.h"
       2             : #include "Manager.h"
       3             : #include <cstddef>
       4             : #include <opencv2/opencv.hpp>
       5             : 
       6             : using namespace sipai;
       7             : 
       8          42 : void LayerOutput::computeErrors(const cv::Mat &expectedValues) {
       9          42 :   if (expectedValues.total() != total()) {
      10           0 :     throw std::invalid_argument("Invalid expected values size");
      11             :   }
      12             : 
      13          42 :   const float error_min = Manager::getConstInstance().network_params.error_min;
      14          42 :   const float error_max = Manager::getConstInstance().network_params.error_max;
      15          42 :   const float weightFactor = 0.5f; // Experiment with weight between 0 and 1
      16             : 
      17             :   // Create the errors matrix if not already allocated
      18          42 :   if (errors.empty()) {
      19           0 :     errors.create((int)size_y, (int)size_x, CV_32FC4);
      20             :   }
      21             : 
      22             :   // Iterate over all neurons in the layer
      23         168 :   for (int y = 0; y < (int)size_y; ++y) {
      24         504 :     for (int x = 0; x < (int)size_x; ++x) {
      25         378 :       const Neuron &neuron = neurons[y][x];
      26         378 :       cv::Vec4f &error = errors.at<cv::Vec4f>(y, x);
      27             : 
      28             :       // Compute the weighted sum of neighboring neuron values
      29         378 :       cv::Vec4f neighborSum = cv::Vec4f::all(0.0f);
      30        1386 :       for (const NeuronConnection &connection : neuron.neighbors) {
      31        2016 :         neighborSum += connection.weight.mul(values.at<cv::Vec4f>(
      32        2016 :             (int)connection.neuron->index_y, (int)connection.neuron->index_x));
      33             :       }
      34             : 
      35             :       // Compute and update the error
      36         378 :       const cv::Vec4f &currentValue = values.at<cv::Vec4f>(y, x);
      37         378 :       const cv::Vec4f &expectedValue = expectedValues.at<cv::Vec4f>(y, x);
      38         378 :       const cv::Vec4f newError = weightFactor * (currentValue - expectedValue) +
      39         756 :                                  (1.0f - weightFactor) * neighborSum;
      40         378 :       error = Common::clamp4f(newError, error_min, error_max);
      41             :     }
      42             :   }
      43          42 : }

Generated by: LCOV version 1.16