Hi Andrew!


I am fairly new to Pytorch and I’m currently working on a project that needs to perform classification on images. However, it’s not a binary classification.
The outputs of the neural network are real numbers.


A quick note on terminology:  To me, at least, the term
“classification” has the connotation of assigning input
samples to discrete classes, that is, labelling them with
integer class labels.  So I might call what you’re talking
about something like continuous value estimation, rather
than “classification.”


For instance the classification I’m looking the neural network to provide is as such:
Reads Image
says that the image has attribute A at a value of 1200 and another attribute B at a value of 8.
The image data that’s fed into this neural net usually has a value range of 1200 - 1800 and another attribute range of 4 - 20. The main goal is to train the network to give an estimated analog value based on the image fed into it.


This seems perfectly reasonable to attempt with a pytorch
neural network (depending on the details of your problem).


The classes provided by Pytorch seem to favor binary classification, such as the Dataloader and ImageFolder classes.


This is an overstatement.  Pytorch certainly supports binary
classification, but it supports lots of other things, too.


However, I cannot provide the class folder structure that is often used since I do not have set binary classes:


Yes, while you can certainly encode binary class labels (and
multi-class labels) in a directory structure, you can’t very
well encode real-number attributes that way.
So the straightforward approach would be to have a data file
that has the real-number attributes in it.  It could be as
simple as containing a sequence of (pairs of) real numbers,
with the understanding that the order in which you read in
your images will (somehow) match the order of the attribute
values in your file.  Or your data file could have a character
string for the filename of each image file, followed by two
numbers for the values of the two attributes.


I want to discuss what possible structures of neural networks can support a problem like this. Including activation functions and what Pytorch might have to offer.


Since you’re analyzing images your network would probably
start with a couple of convolutional layers, and then switch
over to a couple of fully-connected layers, with the last
layer having two (floating-point number) outputs as the
predictions for your two attributes.  (There are many
additional techniques that could improve the performance
of your network – things like nn.MaxPool2d or
nn.Dropout2d – but I would recommend starting simple
and adding refinements as the need arises.  Or start with
a prepackaged network.)
Rectified linear units (nn.ReLU) are commonly used as
(non-linear) activation functions.


What type of loss functions and optimizers in Pytorch may best serve this type of problem?


For your loss function, you would probably simply use the squared
error between your predictions and actuals (nn.MSELoss).
That is:
loss = (predicted_A - actual_A)^2 + (predicted_B - actual_B)^2.

You might want to weight the two terms so they have comparable
size lest you preferentially optimize for accuracy in A at the
cost of B.
I always like to start simple, so I would recommend starting
with plain-vanilla (stochastic) gradient descent (optim.SGD).
You can also add momentum to gradient descent (supported by
optim.SGD), and pytorch offers more sophisticated optimizers,
such as the commonly-used optim.Adam (“adaptive moments”).


Are there implementations out there that I haven’t found on the web yet?


Undoubtedly!  There are lots of (semi-) prepackaged architectures
out there for image processing, but I don’t know much about them
and don’t have any concrete suggestions.  But if you give us more
detail about your specific problem (and post some sample images!),
some of the experts here will likely have good advice.


Should I even be considering neural networks to perform this task in the first place?


Likely yes, but it really depends on your problem.  (So post
details and sample images!)  For example, if the two attributes
you are trying to “predict” are the intensity and and saturation
averaged over the pixels in a color image, you’d be much better
off just calculating them directly.
Best.
K. Frank