4 outputs labels for image classification

username: DanTereson
cooked: <p>Hi folks,<br>
How are you today?</p>
<p>I’m doing research at audio technology. But I have happened to utilize CNN to classify sounds.</p>
<p>I have 8000 audio samples with 4 labels for each. Each label is just a float number.<br>
I want to build a model to classify the new audio for which the labels belongs.<br>
How can I create a CNN and predict the 4 multiple labels as output? What kind of layers do I need?</p>
<p>Cheers</p>
<p>[Update]<br>
Here is how my metaData looks like:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/5/252bdaa525ecbb8210752191ff7b6f3cd1d6a88d.png" data-download-href="https://discuss.pytorch.org/uploads/default/252bdaa525ecbb8210752191ff7b6f3cd1d6a88d" title="Screenshot 2022-05-30 at 23.13.14"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/252bdaa525ecbb8210752191ff7b6f3cd1d6a88d_2_690x198.png" alt="Screenshot 2022-05-30 at 23.13.14" data-base62-sha1="5iPCE8yBywdazCmxuSbWBZGn4VD" width="690" height="198" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/252bdaa525ecbb8210752191ff7b6f3cd1d6a88d_2_690x198.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/252bdaa525ecbb8210752191ff7b6f3cd1d6a88d_2_1035x297.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/252bdaa525ecbb8210752191ff7b6f3cd1d6a88d_2_1380x396.png 2x" data-dominant-color="DCDCDC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2022-05-30 at 23.13.14</span><span class="informations">1454×418 87.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The image spectra are based on those 4 parameters: A, Fw, P, and Fi. The generated audio samples iare produced through a group of algorithm, but each time we only choose one underlying algorithm. So any trained model is a reflection of that algorithm.<br>
My working flow is: audio → spectrum → training model → evaluating.<br>
At the end, I want to create a model which can predict a new audio sample generated by the same algorithm.</p>

username: eqy
cooked: <p>Are the labels discrete classes or intended to be continuous values? In either case you likely do not need to do anything special as a first attempt using a standard classification back bone as they will output continuous logits by default. For a multilabel objective you could use e.g., <a href="https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html" class="inline-onebox" rel="noopener nofollow ugc">BCEWithLogitsLoss — PyTorch 1.11.0 documentation</a> and for a regression objective you could use e.g., <a href="https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html" class="inline-onebox" rel="noopener nofollow ugc">MSELoss — PyTorch 1.11.0 documentation</a></p>

username: DanTereson
cooked: <p>Hello eqy,<br>
Thanks for your very early response to me.<br>
In this project, the audio samples have been transformed to spectra. Actually, each audio is generated by a group of algorithms where the 4 labels are the factors.<br>
So, I wish I could build a CNN and feed the trained model with a new bunch of audio and hope the model could give the predictions(4 labels in float) back for me.</p>
<p>The 4 labels for each audio and according spectrum are randomly generated.</p>
<p>The first layer of the model should be conv2d, and so on. But how can I define the last linear lawyer as the labels are randomly generated. On the control, if we are training a speech audio, our labels is easy as [yes, no, cat, dog]. But in my case, the labels are unpredictable.</p>
<p>So, can you give a a simple demo to address the model?</p>
<p>Cheers</p>

username: eqy
cooked: <p>I’m a bit confused here, could you give an example of what the randomly generated labels would look like? If they are actually “unpredictable,” then I’m wondering if it would make sense to frame it as a learning task?</p>

username: DanTereson
cooked: <p>Hi eqy,</p>
<p>Here it is.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/9/39c5e1af8b900f2d1b059638d1093cd29adb1c6e.png" data-download-href="https://discuss.pytorch.org/uploads/default/39c5e1af8b900f2d1b059638d1093cd29adb1c6e" title="Screenshot 2022-05-30 at 23.45.16"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39c5e1af8b900f2d1b059638d1093cd29adb1c6e_2_690x196.png" alt="Screenshot 2022-05-30 at 23.45.16" data-base62-sha1="8f5aRiy10b1HSkEHHaZgMOqaJrw" width="690" height="196" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39c5e1af8b900f2d1b059638d1093cd29adb1c6e_2_690x196.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39c5e1af8b900f2d1b059638d1093cd29adb1c6e_2_1035x294.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39c5e1af8b900f2d1b059638d1093cd29adb1c6e_2_1380x392.png 2x" data-dominant-color="DCDCDC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2022-05-30 at 23.45.16</span><span class="informations">1442×410 87.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>First, I use a group of algorithms to generate the audio samples but each time a certain algorithm. So the trained model should be the reflection of that algorithm.<br>
Next, the 4 labels are randomly selected within a given range, and then the audio samples are based on those 4 labels and the algorithm.<br>
So, what I want to implement is to create a model and feed the trained model a new audio sample generated by the same selected algorithm, and hope the model give me the prediction(4 labels) back.</p>
<p>How can I define the CNN model class and implement the forward function?</p>
<p>Best</p>

username: eqy
cooked: <p>Please take a look at the MNIST and ImageNet examples linked above for a recipe involving defining your own model or using a preexisting architecture.</p>
<p>From the table linked it looks like the labels are “continuous,” so you might want to start off with MSE and tune things from there (e.g., normalizing the labels etc. if they are varied in scale, etc.).</p>

username: DanTereson
cooked: <p>Hi, eqy,<br>
Thanks for your message.</p>
<p>Oh, that’s good.<br>
But in my case, the labels are digits generated randomly from a given range, like [20, 50] or so.<br>
I looked at the MNIST example, and torch.nn.CrossEntropyLoss() was used.<br>
I used the same loss function as well, but have several runtime errors.</p>
<p>So,</p>
<ul>
<li>What loss function do you suggest?</li>
<li>How can I define my output layer since there are uncountable outputs in theory. It’s not simply going down on 2 or 3 categories. The output labels should be a random guess from the same range.</li>
</ul>
<p>Dan.</p>

username: eqy
cooked: <p>As mentioned previously, you can try <a href="https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html" class="inline-onebox" rel="noopener nofollow ugc">MSELoss — PyTorch 1.11.0 documentation</a> as a starting point</p>