1

I'm trying to figure out how to solve the really simple problem of counting how many pixels in an image are white.
I have 20x20 pixel images (zeros matrix) with 1 to 20 pixels randomly set to 1.

Checking some tutorials Im able to solve this problem via a classification approach with the model using CrossEntropyLoss() and Adam optimizer:

 nn.Linear(20*20, 100),
 nn.ReLU(),
 nn.Linear(100, 30),            
 nn.ReLU(),
 nn.Linear(30, 20)```

Here my output is a vector of size 20 with the probabilities that the image contains 1 to 20 white pixels (ones) Using argmax() I can simply retreive the most possible "class"

What I wanna do now is to solve the same problem but not as a classification one, but like a "counter", where, wiht the image as imput, the NN can estimate the number of white pixels as one single Integer output.

I've made some changes to the code using one neuron as output and MSELoss() and other functions as loss functions, but still cannot get the output to be an integer (1 to 20) instead of a real number

At the end, what I try to understand is how to see a NN as a function, that, in this case, goes from Rn to R

Any ideas?

Using one neuron as output, the predictions are real numbers and the accuracy is always around 4.9% with a loss that keeps quite constant across epochs

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • If you treat the problem as a regression one, accuracy is no longer meaningful as a metric; accuracy is applicable only to classification problems: https://stackoverflow.com/questions/48775305/what-function-defines-accuracy-in-keras-when-the-loss-is-mean-squared-error-mse/48788577#48788577 – desertnaut Apr 01 '22 at 01:15

1 Answers1

1

Keep in mind that you probably have to normalize the outputs. So your model should still output something between 0 and 1 where 0 means 0 white pixels, 1 means 20 white pixels and 0.5 means 10 and so on. Therefore use sigmoid on the output neuron and mulltiply it by 20 to get the estimated amount of white pixels.

Theodor Peifer
  • 3,097
  • 4
  • 17
  • 30
  • Thx Theodor It was a good idea to take into account. But unfortunately the thing is not about the accuracy, but the loss staying almost similar across the batchs-epochs. I cant see an improvement after each backpropagation – Felipe Bayona Apr 04 '22 at 13:59
  • At the end the problem is simple as a NN that sums a vector of zeros and ones. ... – Felipe Bayona Apr 04 '22 at 14:09