Show Seg Mask in opencv or pltshow

sissico: <p>I am working on a tensor model. I have trained it and now testing and evaluating it. Img_score with the prediction is working correctly. I want also to print the seg_mask image (seg_out in the code), but when I save it with opencv or plot it is all black. The values of the seg_out image are all negatives which I think it is related to the normalization.<br>
The seg_out shape is [1,80,29].<br>
Here the seg_out values:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fc6f54ca49c3d9802e0986d40ea7a60bc23a0c9.jpeg" alt="seg_out value" data-base62-sha1="mNsbos34X8o0alMjd2fjACoCs1b" width="546" height="111"><br>
What can I do to show the correct image?<br>
Here is what I am doing:</p>
<pre><code class="lang-auto">img = cv2.imread(img_path) if INPUT_CHANNELS == 3 else cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) #original image
img = cv2.resize(img, (INPUT_WIDTH, INPUT_HEIGHT))
print(img.shape) 
img = np.transpose(img, (2, 0, 1)) if INPUT_CHANNELS == 3 else img[np.newaxis]
img_t = torch.from_numpy(img)[np.newaxis].float() / 445400  # must be [BATCH_SIZE x CHANNELS x HEIGHT x WIDTH]
dec_out, seg_out = model(img_t)
img_score = torch.sigmoid(dec_out)
print(img_score) 

seg_out2=seg_out[0].detach().numpy().transpose(1, 2, 0)
cv2.imwrite("seg_out.jpg", seg_out2) 

</code></pre>

ptrblck: <p>I guess the image is blackas the values are negative and OpenCV might map it to a black pixel value.<br>
I don’t know what the values should represent but you might want to normalize them to <code>[0, 1]</code> as floating point values or <code>[0, 255]</code> as <code>uint8</code> values first before calling <code>imwrite</code>.</p>

sissico: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Yes the image should be a black/white mask image, a one channel image. The white pixels should be in the area in which the scretch is.<br>
How can I do to show the image in this way? Thanks</p>

ptrblck: <aside class="quote no-group" data-username="sissico" data-post="3" data-topic="147149">
<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/sissico/48/47833_2.png" class="avatar"> sissico:</div>
<blockquote>
<p>Yes the image should be a black/white mask image</p>
</blockquote>
</aside>
<p>This doesn’t fit the posted values, as I would assume to see binary values (e.g. zeros and ones) for a black/white image while your current <code>seg_out</code> tensor contains negative floating point values.</p>
<aside class="quote no-group" data-username="sissico" data-post="3" data-topic="147149">
<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/sissico/48/47833_2.png" class="avatar"> sissico:</div>
<blockquote>
<p>The white pixels should be in the area in which the scretch is.</p>
</blockquote>
</aside>
<p>I’m unsure what this means.</p>
<aside class="quote no-group" data-username="sissico" data-post="3" data-topic="147149">
<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/sissico/48/47833_2.png" class="avatar"> sissico:</div>
<blockquote>
<p>How can I do to show the image in this way?</p>
</blockquote>
</aside>
<p>You could try to normalize the tensor and then maybe binarize it (e.g. via a threshold), but since I’m ot familiar with your use case I’m unsure if this suggestion is valid.</p>

AaronYeh03: <p>Hi, I currently working on this paper too. (Mixed supervision for surface-defect detection_from weakly to fully supervised learning)<br>
Maybe we can discuss some implementation detail future more</p>
<p>Here’s my email: <a href="mailto:aaronye086@gmail.com">aaronye086@gmail.com</a></p>