Interpret model predictions

username: Schroter
cooked: <p>Hi All,<br>
I am trying to create an image classifier using <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="noopener nofollow ugc">this</a>  tutorial. My target is to save the model and used the saved model to predict test images. The loss function is binary cross entropy. Please find the code below for the inference. Note: The transforms are identical to the validation transforms.</p>
<pre><code class="lang-auto"># Get the list of files in the directory
file_names = os.listdir(test_img_path)
results = []
filenames = []
# Print the file names
for file_name in file_names:
    file_path = test_img_path + '/' + file_name
    ds = pydicom.dcmread(file_path)
    img = ds.pixel_array
    image1 = (np.maximum(img,0) / img.max()) * 255.0
    image1 = image1.astype(np.uint8)
    test_image = cv2.cvtColor(image1, cv2.COLOR_GRAY2RGB)
    test_image = Image.fromarray(test_image)
    test_image = transforms.Resize(256)(test_image)
    test_image = transforms.CenterCrop(224)(test_image)
    test_image = transforms.ToTensor()(test_image)
    test_image = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(test_image)
    test_image = torch.unsqueeze(test_image, axis = 0)
    result = model_conv(test_image)
    result = str(result[0].reshape(1,-1).flatten().tolist()).replace('[', '').replace(']', '')
    results.append(result)
    filenames.append(file_name[:-4])
print(filenames,results)
</code></pre>
<p>The result I get is a bit confusing:</p>
<pre><code class="lang-auto">['736471439', '361203119', '68070693', '1591370361'] ['-5.878069877624512, 4.447736740112305', '-0.44018930196762085, 0.5596869587898254', '-7.579869747161865, 7.123885154724121', '-5.736744403839111, 5.130640029907227']
</code></pre>
<p>In the above result the first list is the image names and the second is the output from the model.<br>
For each image two outputs are given. Which looks like a range. I would like to know how to interprit this and whether the results are as meant to be. Also, is there a better way to write the above code (inference part) Furthermore, If I could visualise the images with the predictions would also be great.<br>
Would anyone be able to help me in this matter.</p>
<p>Thanks &amp; Best Regards<br>
AMJS</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Schroter" data-post="1" data-topic="174081">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/schroter/48/57964_2.png" class="avatar"> Schroter:</div>
<blockquote>
<p>The loss function is binary cross entropy.</p>
</blockquote>
</aside>
<p>This sounds like a binary classification use case, but:</p>
<aside class="quote no-group" data-username="Schroter" data-post="1" data-topic="174081">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/schroter/48/57964_2.png" class="avatar"> Schroter:</div>
<blockquote>
<p>and the second is the output from the model.<br>
For each image two outputs are given.</p>
</blockquote>
</aside>
<p>this now sounds like a multi-label classification.<br>
If your use case is indeed a binary classification, the model should return a single output logit.<br>
The predicted class can then be computed by comparing the output logit to a threshold, e.g.:</p>
<pre data-code-wrap="python"><code class="lang-python">out = model(input)
preds = out &gt; 0.0
</code></pre>