Regression(MSELoss())/images/Inference

Mukesh1729: <p>**I am using MSE loss() for a regression problem but with images. I want to be able to predict the outcome from a test set of images using my saved model. (Given below is a sample code i used but didnt work well). Also i am unsure if it is the right method to go about it.<br>
How can i do that? (Sample code would be helpful)</p>
<p>During training my my training and validation loss doesnt improve much. Thanks**</p>
<p><span class="hashtag">#load</span> saved model</p>
<p>device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)</p>
<p>model = UNet1()</p>
<p>checkpoint=torch.load(‘DB_model.pth’)</p>
<p>model.load_state_dict(checkpoint[‘state_dict’])</p>
<p>model.eval()</p>
<p><span class="hashtag">#load</span> image and mask</p>
<p>image = cv2.imread("/content/image0/img2.png",0)</p>
<p>image = np.expand_dims(image, axis=0)</p>
<p>image = np.expand_dims(image, axis=1)</p>
<p>mask = cv2.imread("/content/wrapped/img2.png",0)</p>
<p>images = torch.from_numpy(image).float()</p>
<p><span class="hashtag">#Predictions</span></p>
<p>_mask = model(images)</p>
<p><span class="hashtag">#_</span>, _mask = torch.max(_mask, dim=1)</p>
<p>print(_mask.shape )</p>
<p>fig = plt.figure(figsize=(20, 12))</p>
<h1>
<a name="setting-values-to-rows-and-column-variables-1" class="anchor" href="#setting-values-to-rows-and-column-variables-1"></a>setting values to rows and column variables</h1>
<p>rows = 1</p>
<p>columns = 3</p>
<h1>
<a name="adds-a-subplot-at-the-1st-position-2" class="anchor" href="#adds-a-subplot-at-the-1st-position-2"></a>Adds a subplot at the 1st position</h1>
<p>fig.add_subplot(rows, columns, 1)</p>
<p>plt.imshow(images.squeeze(0).permute(1,2,0).squeeze(2).numpy(), cmap=‘gray’)</p>
<p>plt.axis(‘off’)</p>
<p>plt.title(“Image”)</p>
<h1>
<a name="adds-a-subplot-at-the-2nd-position-3" class="anchor" href="#adds-a-subplot-at-the-2nd-position-3"></a>Adds a subplot at the 2nd position</h1>
<p>fig.add_subplot(rows, columns, 2)</p>
<p>plt.imshow(_mask.squeeze(0).squeeze(0).detach().cpu().numpy(), cmap=‘gray’)</p>
<p>plt.axis(‘off’)</p>
<p>plt.title(“Prediction”)</p>
<h1>
<a name="adds-a-subplot-at-the-3rd-position-4" class="anchor" href="#adds-a-subplot-at-the-3rd-position-4"></a>Adds a subplot at the 3rd position</h1>
<p>fig.add_subplot(rows, columns, 3)</p>
<p>plt.imshow(mask, cmap=‘gray’)</p>
<p>plt.axis(‘off’)</p>
<p>plt.title(“Mask”)</p>

ptrblck: <aside class="quote no-group" data-username="Mukesh1729" data-post="1" data-topic="130658">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/90db22/48.png" class="avatar"> Mukesh1729:</div>
<blockquote>
<p>During training my my training and validation loss doesnt improve much</p>
</blockquote>
</aside>
<p>I would recommend to scale down the use case and try to overfit a small dataset (e.g. just 10 samples) by playing around with some hyperparameters and make sure your model is able to learn them. Once this is done you could scale up the use case again.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.</p>

Mukesh1729: <p>Hey thanks for getting back. If I am doing inference on an image for a regression problem, is it similar to doing with a classification problem or just pass the image though the model and get the outputs.<br>
I am using the mse loss function</p>
<p>Ex: train a classifier example from pytorch on cifar</p>

ptrblck: <aside class="quote no-group" data-username="Mukesh1729" data-post="3" data-topic="130658">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/90db22/48.png" class="avatar"> Mukesh1729:</div>
<blockquote>
<p>If I am doing inference on an image for a regression problem, is it similar to doing with a classification problem or just pass the image though the model and get the outputs.</p>
</blockquote>
</aside>
<p>Yes, you can pass the input to the model and use the output as the regression prediction. Since you would usually not update any parameters during evaluation, you could also wrap the forward pass into a <code>with torch.no_grad()</code> guard to save memory.</p>

Mukesh1729: <p>For inference:</p>
<p>I tried torch.argmax(model(outputs), dim=1) with the torch.no grad wrapper. I plotted the outcome and got zeros</p>
<p>Model(outputs) has values when I plot them though without argrmax function</p>

ptrblck: <p>A regression task usually uses floating point outputs, while <code>torch.argmax</code> will return the index in the specified dimension containing the max. value and is thus commonly used to get the class predictions for a multi-class classification use case.</p>

Mukesh1729: <p>What should I use then for regression? An example would help</p>

ptrblck: <p>You can use the output of the last layer directly:</p>
<pre><code class="lang-python">model = nn.Sequential(
    nn.Linear(10, 10),
    nn.ReLU(),
    nn.Linear(10, 10)
)
x = torch.randn(1, 10)
regression_output = model(x)
</code></pre>

Mukesh1729: <p>Thanks for the immediate response and help.</p>