When I train my network, The same number appears periodically on my my training acc ， I don't konow the error come from

username: fyy
cooked: <p><strong>this is my train.py</strong>  I use crosssentropy loss for my network , <strong>outputs</strong>  size is [4,2,224,224] where 4 means batchsize, 2 means channels, 224 means h and w .output_c1 size is [4,224,224] ,labels size is [4,224,224] too.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from model import U_net
import visdom
from dataset import driveDateset
from torch import optim
from Dice_loss import DiceLoss
from Dice_loss import MulticlassDiceLoss
import matplotlib.pylab as plt
import numpy as np
import time

if __name__ == '__main__':
    DATA_DIRECTORY = "F:\\experiment_code\\U-net\\DRIVE\\training"
    DATA_LIST_PATH = "F:\\experiment_code\\U-net\DRIVE\\training\\images_id.txt"
    Batch_size = 4
    epochs = 100
    dst = driveDateset(DATA_DIRECTORY, DATA_LIST_PATH)

    # Initialize model
    device = torch.device("cuda")
    model = U_net()
    model.to(device)
    optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
    criteon = nn.CrossEntropyLoss() #reduce=False
    best_acc, best_epoch =0, 0
    global_step = 0
    start_time = time.time()
    viz = visdom.Visdom()
   for epoch in range(epochs):
        running_corrects = 0
        since_epoch = time.time()
        trainloader = torch.utils.data.DataLoader(dst, batch_size=Batch_size) #,shuffle =True
        for step, data in enumerate(trainloader):
            imgs, labels, _, _ = data
            imgs, labels = imgs.to(device), labels.to(device)
            labels = labels.long()
            model.train()
            outputs = model(imgs)   # output  B * C * H *W
            output_c1 = outputs[:,0,:,:] # C are 2 channels ,I choose the second channel
            Rounding_output_c1 = torch.round(output_c1)
            Rounding_output_c11 = torch.stack([(Rounding_output_c1 == i).float() for i in range(256)]) #[4,224,224]-&gt;[256,4,224,224] 256 is the number of classes, means pixel from 0-255
            Rounding_output_c11 = Rounding_output_c11.permute(1,0,2,3) #[256,4,224,224]-&gt;[4,256,224,224]
            loss = criteon(Rounding_output_c11,labels)
            loss.requires_grad = True
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            viz.line([loss.item()],[global_step], win='loss', update='append',opts=dict(title='train_loss'))
            labels_float = labels.float()
            running_corrects = torch.sum(Rounding_output_c1 == labels_float).float()
            labels_size = labels.size(1) * labels.size(2) * 4
            training_acc = running_corrects / labels_size
            time_elapsed_epoch = time.time() - since_epoch
            print('epoch :', epoch, '\t', 'loss:', loss.item(),'\t','training_acc',training_acc,'\t','{:.0f}m {:.0f}s'.format(time_elapsed_epoch // 60, time_elapsed_epoch % 60))
            global_step += 1
</code></pre>
<p><strong>I test Data incoming has no problem ,every training data has Traversed. training data and label like this</strong><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/3/13d3a37b4cd5741cc40a0fe7557569d3670233b1.png" data-download-href="https://discuss.pytorch.org/uploads/default/13d3a37b4cd5741cc40a0fe7557569d3670233b1" title="HcLxo"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/3/13d3a37b4cd5741cc40a0fe7557569d3670233b1_2_551x500.png" alt="HcLxo" data-base62-sha1="2PovlkhweKcLHsEAuvWaZCfJ4vn" width="551" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/3/13d3a37b4cd5741cc40a0fe7557569d3670233b1_2_551x500.png, https://discuss.pytorch.org/uploads/default/original/3X/1/3/13d3a37b4cd5741cc40a0fe7557569d3670233b1.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/3/13d3a37b4cd5741cc40a0fe7557569d3670233b1.png 2x" data-dominant-color="DEDCE4"><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">HcLxo</span><span class="informations">638×578 39.4 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><br>
<strong>but when I train my network ,output picture like this ,</strong><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/1/5/1509e13a2db1c53eaae38fb076fb6ef263532408.png" alt="LbUEh" data-base62-sha1="307c2EVEZzfXY5IeGCk0qT3Qive" width="603" height="427"><br>
<strong>and some result like this :</strong></p>
<pre><code class="lang-auto">epoch : 0 	 loss: 5.2415571212768555 	 training_acc tensor(0.3103, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.228370666503906 	 training_acc tensor(0.3235, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.224219799041748 	 training_acc tensor(0.3276, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.222436428070068 	 training_acc tensor(0.3294, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.2202887535095215 	 training_acc tensor(0.3316, device='cuda:0') 	 0m 2s
epoch : 1 	 loss: 5.2415571212768555 	 training_acc tensor(0.3103, device='cuda:0') 	 0m 0s
epoch : 1 	 loss: 5.22836971282959 	 training_acc tensor(0.3235, device='cuda:0') 	 0m 0s
epoch : 1 	 loss: 5.224219799041748 	 training_acc tensor(0.3276, device='cuda:0') 	 0m 0s
epoch : 1 	 loss: 5.222436428070068 	 training_acc tensor(0.3294, device='cuda:0') 	 0m 1s
epoch : 1 	 loss: 5.2202887535095215 	 training_acc tensor(0.3316, device='cuda:0') 	 0m 1s
epoch : 2 	 loss: 5.2415571212768555 	 training_acc tensor(0.3103, device='cuda:0') 	 0m 0s
epoch : 2 	 loss: 5.22836971282959 	 training_acc tensor(0.3235, device='cuda:0') 	 0m 0s
epoch : 2 	 loss: 5.224219799041748 	 training_acc tensor(0.3276, device='cuda:0') 	 0m 0s
epoch : 2 	 loss: 5.222436428070068 	 training_acc tensor(0.3294, device='cuda:0') 	 0m 1s
epoch : 2 	 loss: 5.2202887535095215 	 training_acc tensor(0.3316, device='cuda:0') 	 0m 1s
epoch : 3 	 loss: 5.2415571212768555 	 training_acc tensor(0.3103, device='cuda:0') 	 0m 0s
epoch : 3 	 loss: 5.22836971282959 	 training_acc tensor(0.3235, device='cuda:0') 	 0m 0s
epoch : 3 	 loss: 5.224219799041748 	 training_acc tensor(0.3276, device='cuda:0') 	 0m 0s
epoch : 3 	 loss: 5.222436428070068 	 training_acc tensor(0.3294, device='cuda:0') 	 0m 1s
epoch : 3 	 loss: 5.2202887535095215 	 training_acc tensor(0.3316, device='cuda:0') 	 0m 1s

</code></pre>
<p>I don’t konw how to solve this problem</p>

username: KFrank
cooked: <p>Hi fyy!</p>
<p>I believe that your network isn’t actually training.</p>
<aside class="quote no-group" data-username="fyy" data-post="1" data-topic="74237" data-full="true">
<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/f/eb8c5e/48.png" class="avatar"> fyy:</div>
<blockquote>
<pre><code class="lang-auto">        trainloader = torch.utils.data.DataLoader(dst, batch_size=Batch_size) #,shuffle =True
</code></pre>
</blockquote>
</aside>
<p>Note, that because you <em>do not</em> shuffle your dataset, you will run<br>
on the exact same batches in each epoch.</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">            Rounding_output_c1 = torch.round(output_c1)
</code></pre>
</blockquote>
</aside>
<p>Although technically differentiable, the derivative of <code>torch.round()</code><br>
is zero (almost) everywhere.  So the gradients that flow back to<br>
your model parameters will be zero, and <code>optimizer.step()</code><br>
won’t actually do anything.</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">            loss.requires_grad = True
</code></pre>
</blockquote>
</aside>
<p>This is a red flag.  Did you just put this in for good luck?  Or did<br>
you observe that <code>loss.grad == False</code>?  If the latter, something<br>
upstream of <code>loss</code> in your processing is breaking / detaching<br>
your computation graph (and would prevent gradients, zero or<br>
not, from flowing back to your model parameters).</p>
<aside class="quote no-group">
<blockquote>
<p><strong>and some result like this :</strong></p>
<pre><code class="lang-auto">epoch : 0 	 loss: 5.2415571212768555 	 training_acc tensor(0.3103, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.228370666503906 	 training_acc tensor(0.3235, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.224219799041748 	 training_acc tensor(0.3276, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.222436428070068 	 training_acc tensor(0.3294, device='cuda:0') 	 0m 2s
epoch : 0 	 loss: 5.2202887535095215 	 training_acc tensor(0.3316, device='cuda:0') 	 0m 2s
epoch : 1 	 loss: 5.2415571212768555 	 training_acc tensor(0.3103, device='cuda:0') 	 0m 0s
epoch : 1 	 loss: 5.22836971282959 	 training_acc tensor(0.3235, device='cuda:0') 	 0m 0s
epoch : 1 	 loss: 5.224219799041748 	 training_acc tensor(0.3276, device='cuda:0') 	 0m 0s
epoch : 1 	 loss: 5.222436428070068 	 training_acc tensor(0.3294, device='cuda:0') 	 0m 1s
epoch : 1 	 loss: 5.2202887535095215 	 training_acc tensor(0.3316, device='cuda:0') 	 0m 1s
...
</code></pre>
</blockquote>
</aside>
<p>I see that the loss ad accuracy repeats itself exactly* from epoch<br>
to epoch.  So it appears that your model is not training.</p>
<p>*)  The second loss in epoch 0 is slightly different than the second<br>
loss in subsequent epochs.  I’ll choose to attribute that to a slightly<br>
different order of operations in the gpu and therefore differing<br>
round-off error.</p>
<p>So my working hypothesis is that your model isn’t changing at all.<br>
You get different results, of course, for different batches within an<br>
epoch, but when you analyze the <em>same</em> batch in a subsequent,<br>
you get the same result.</p>
<p>The only thing that doesn’t fit with this explanation is that your loss<br>
is going down systematically from batch to batch (within an epoch),<br>
and your accuracy is going up.  That makes it look like your model<br>
<em>is</em> training.  But I don’t see anything in your code that would reset<br>
your model from one epoch to the next.  It is hard for me to attribute<br>
the decreasing loss to random luck, although it could be due to<br>
some structure in the (unshuffled) order of samples in your training<br>
set.</p>
<p>Step 1:  Get rid of <code>torch.round()</code> (and any other zero-derivative<br>
functions) leading up to your loss function.</p>
<p>Step 2:  Why are you calling <code>loss.requires_grad = True</code>?<br>
(And why isn’t it throwing an error?  What version of pytorch are<br>
you using?)</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: fyy
cooked: <p><a class="mention" href="/u/kfrank">@KFrank</a> ，Thank you for your kind help，</p>
<p><strong>1</strong>.I use <code>Rounding_output_c1 = torch.round(output_c1)</code><br>
because I want to make my network’s output is integer,<br>
and use these code to caculate my training_acc<br>
<code> labels_float = labels.float()  #labels is not Decimal, labels size is [4,224,224]</code><br>
<code>running_corrects = torch.sum(Rounding_output_c1 == labels_float).float()</code><br>
<code>labels_size = labels.size(1) * labels.size(2) * 4</code><br>
<code>training_acc = running_corrects / labels_size</code></p>
<p>and if I don’t use <code>Rounding_output_c1 = torch.round(output_c1)</code>,  <strong>training_acc will be 0.</strong></p>
<p><strong>2</strong> I use <code>loss.requires_grad = True</code>   because when I debug my code I observed that  loss requires_grad is False if I don’t use this code It will get an error .<br>
<code>Traceback (most recent call last):</code><br>
<code> File "F:/experiment_code/U-net/train_2.py", line 85, in &lt;module&gt;</code><br>
<code> loss.backward()</code><br>
<code> File "D:\Anaconda3\lib\site-packages\torch\tensor.py", line 107, in backward</code><br>
<code>torch.autograd.backward(self, gradient, retain_graph, create_graph)</code><br>
<code>File "D:\Anaconda3\lib\site-packages\torch\autograd\__init__.py", line 93, in backward</code><br>
<code> allow_unreachable=True)  # allow_unreachable flag</code><br>
<code>RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</code></p>
<p>my pytorch version is <img src="https://discuss.pytorch.org/uploads/default/original/3X/7/2/7275d2ec7a3243480eb94f84f7582a0654480e15.png" alt="QQ图片20200325085636" data-base62-sha1="gkyRPqw5Em6ynXUREF9McMirkPz" width="588" height="97"></p>

username: KFrank
cooked: <p>Hi fyy!</p>
<p>First, I don’t think the overall structure of what you are doing is<br>
correct.  More on that, below.</p>
<p>Some specific comments, in line:</p>
<aside class="quote no-group quote-modified" data-username="fyy" data-post="3" data-topic="74237" data-full="true">
<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/f/eb8c5e/48.png" class="avatar"> fyy:</div>
<blockquote>
<p><strong>1</strong>.I use <code>Rounding_output_c1 = torch.round(output_c1)</code><br>
because I want to make my network’s output is integer,<br>
and use these code to caculate my training_acc<br>
…<br>
and if I don’t use <code>Rounding_output_c1 = torch.round(output_c1)</code>,  <strong>training_acc will be 0.</strong></p>
</blockquote>
</aside>
<p>Accuracy and loss are two different things.  Accuracy is the count<br>
of how many right answers you get, and doesn’t need to be<br>
differentiable.</p>
<p>Loss is the quantity the optimizer attempts to minimize while<br>
training, and <em>does</em> need to be differentiable in order for pytorch’s<br>
gradient-descent optimization algorithms to work.</p>
<p>Of course, your loss function should have some relationship to<br>
your accuracy for it to make sense, but they aren’t the same.</p>
<p>Even if it did make sense to use <code>torch.round()</code> to calculate<br>
your accuracy (and I don’t think it does in your case), you can’t<br>
use it for your loss function because it’s not usefully differentiable.</p>
<aside class="quote no-group">
<blockquote>
<p><strong>2</strong> I use <code>loss.requires_grad = True</code>   because when I debug my code I observed that  loss requires_grad is False</p>
</blockquote>
</aside>
<p>This means that something in your code is breaking your autograd<br>
computation graph.  (Although <code>torch.round()</code> won’t work for<br>
your loss function, it is technically differentiable, and I don’t think<br>
it breaks the computation graph.)</p>
<p>I’m not sure, but maybe this <code>for</code> loop breaks the graph?</p>
<pre><code class="lang-auto">Rounding_output_c11 = torch.stack([(Rounding_output_c1 == i).float() for i in range(256)]) #[4,224,224]-&gt;[256,4,224,224] 256 is the number of classes, means pixel from 0-255
</code></pre>
<p>Perhaps <a class="mention" href="/u/ptrblck">@ptrblck</a> might be able to spot where the graph is getting<br>
broken.</p>
<p>Some comments about your overall approach:</p>
<aside class="quote no-group" data-username="fyy" data-post="1" data-topic="74237">
<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/f/eb8c5e/48.png" class="avatar"> fyy:</div>
<blockquote>
<p>I use crosssentropy loss for my network , <strong>outputs</strong> size is [4,2,224,224] where 4 means batchsize, 2 means channels, 224 means h and w .output_c1 size is [4,224,224] ,labels size is [4,224,224] too.</p>
</blockquote>
</aside>
<p>From this I speculate that you are performing multi-class image<br>
segmentation, that is, that you are classifying each pixel in an<br>
image, assigning them <code>nClass</code> different class labels.</p>
<p>The question is what is <code>nClass</code>, that is, what are the possible<br>
values of each element of your <code>[nBatch, 244, 244]</code> <code>labels</code><br>
tensor?</p>
<p>You do say <code># 256 is the number of classes</code>, but that seems<br>
rather large.  It is the number of <em>possible</em> values of an 8-bit pixel, but<br>
do all such values really occur in your <code>labels</code> tensor?</p>
<p>Anyway, let’s call it <code>nClass</code>, and assume that each value in <code>labels</code><br>
is an integer class label in the range <code>[0, nClass - 1]</code>, inclusive.</p>
<p>(I am assuming that <code>nClass != 2</code>, that is, that this is not a binary<br>
segmentation / classification problem.)</p>
<p>For this you want to use <code>CrossEntropyLoss</code>, and structure your<br>
network output to have shape <code>[nBatch, nClass, 244, 244]</code>.<br>
(These are your <em>predictions</em> and are the <code>input</code> to the loss function.)<br>
Note that your number of channels (2) does <em>not</em> show up in the<br>
shape your your output (nor in the shape of <code>labels</code>).</p>
<p>Your <code>labels</code>, again, have shape <code>[nBatch, 244, 244]</code> and are<br>
the <code>target</code> passed to <code>CrossEntropyLoss</code>.  (<code>nClass</code> enters into<br>
<code>labels</code> not in its shape, but  in the fact the values in <code>labels</code><br>
range from <code>0</code> to <code>nClass - 1</code>.)</p>
<p>If the above makes sense for your use case, then to calculate your<br>
accuracy you would test <code>input.argmax (dim = 1)</code> for equality<br>
against <code>labels</code> (the <code>target</code> you pass to <code>CrossEntropyLoss</code>),<br>
and count the matches.</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="KFrank" data-post="4" data-topic="74237">
<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/k/ecb155/48.png" class="avatar"> KFrank:</div>
<blockquote>
<p>I’m not sure, but maybe this <code>for</code> loop breaks the graph?</p>
</blockquote>
</aside>
<p>Yes, I think you nailed it down.<br>
Here is a small dummy example:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(10, requires_grad=True)
y = torch.stack([(x == i).float() for i in range(256)])
print(y.requires_grad)
&gt;  False
</code></pre>
<p><a class="mention" href="/u/kfrank">@KFrank</a> explained the usage of <code>nn.CrossEntropyLoss</code> really well, so I don’t have anything to add. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

username: fyy
cooked: <p><a class="mention" href="/u/kfrank">@KFrank</a>，Thank you for your answer。 <code>Rounding_output_c11 = torch.stack([(Rounding_output_c1 == i).float() for i in range(256)]) #[4,224,224]-&gt;[256,4,224,224] 256 is the number of classes, means pixel from 0-255])</code>   <strong>is truthly break the graph</strong>, I just want to use this code to use one-hot encoding to change <code>output_c1</code>'s shape ,<code> nClass</code> is pixel value from 0 to 255.<br>
the values of each element of my <code>[nBatch, 244, 244]</code>   <code>labels</code> tensor is from 0 to 255.<br>
my network’s output is <code>output_c1</code> which size is [4,2,224,224] ,It value from 0-255.<br>
I want to use<br>
<code>output_c1 = outputs[:,0,:,:]  # outputs [4,2,224,224] -&gt;outputs_c1[4,224,2224]</code><br>
to choose the second channel to extract Foreground image<br>
If I want to use <code>CrossEntropyLoss</code> ,I don’t konw  how to change my outputs’s shape and labels’s shape to correctly calculate the loss . <img src="https://discuss.pytorch.org/images/emoji/apple/sob.png?v=9" title=":sob:" class="emoji" alt=":sob:"></p>

username: fyy
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> .thank you for your help ,from your small dummy example,  I find  <code>Rounding_output_c11 = torch.stack([(Rounding_output_c1 == i).float() for i in range(256)])</code> breaks the graph. <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>

username: KFrank
cooked: <p>Hi fyy!</p>
<aside class="quote no-group" data-username="fyy" data-post="6" data-topic="74237" data-full="true">
<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/f/eb8c5e/48.png" class="avatar"> fyy:</div>
<blockquote>
<p>If I want to use <code>CrossEntropyLoss</code> ,I don’t konw  how to change my outputs’s shape and labels’s shape to correctly calculate the loss .</p>
</blockquote>
</aside>
<p>I still don’t think that you’re going about this the right way.  And I<br>
doubt that you really have a 256-class classification problem.</p>
<p>I assume that the input to your model is some sort of image.<br>
What is the conceptual meaning of such an image, and what<br>
is its shape?</p>
<p>If you successfully train your model, what, at a high, conceptual<br>
level, is your model supposed to tell you about an input image?</p>
<p>What do your labels <em>mean?</em>  To be concrete, you say that your<br>
batch-of-labels tensor has shape <code>[4, 244, 244]</code>.  What does<br>
the value of a specific element of that tensor mean.  That is,<br>
<code>labels[0, 17, 128]</code> is some number.  Conceptually, what<br>
is that number telling us?</p>
<p>Best.</p>
<p>K. Frank</p>

username: fyy
cooked: <p><a class="mention" href="/u/kfrank">@KFrank</a>,<br>
Hi, KFrank!<br>
My dataset is Fundus image. I try to use U-net model to perform medical image segmentation</p>
<pre><code>        for step, data in enumerate(trainloader):
            imgs, labels, _, _ = data
            model.train()
            outputs = model(imgs)   #   B * C * H *W  outputs [4,2,224,224]
            output_c1 = outputs[:,1,:,:]  # output_c1 [4,224,224]
            labels_show =labels.cpu().detach().numpy().astype(np.uint8)
            img_show = imgs.cpu().detach().numpy().astype(np.uint8)
            plt.figure()
            plt.subplot(4, 2, 1)
            plt.imshow(labels_show[0,:,:]),plt.axis('off')
            plt.subplot(4, 2, 2)
            plt.imshow(img_show[0,1,:,:]),plt.axis('off')
            plt.subplot(4, 2, 3)
            plt.imshow(labels_show[1,:,:]),plt.axis('off')
            plt.subplot(4, 2, 4)
            plt.imshow(img_show[1,1,:,:]),plt.axis('off')
            plt.subplot(4, 2, 5)
            plt.imshow(labels_show[2,:,:]),plt.axis('off')
            plt.subplot(4, 2, 6)
            plt.imshow(img_show[2,1,:,:]),plt.axis('off')
            plt.subplot(4, 2, 7)
            plt.imshow(labels_show[3,:,:]),plt.axis('off')
            plt.subplot(4, 2, 8)
            plt.imshow(img_show[3,1,:,:]),plt.axis('off')
            plt.pause(0.5) 
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/2/225e4092cfa62b3fad43fe891c238384e9e0d43c.png" data-download-href="https://discuss.pytorch.org/uploads/default/225e4092cfa62b3fad43fe891c238384e9e0d43c" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/225e4092cfa62b3fad43fe891c238384e9e0d43c_2_560x500.png" alt="image" data-base62-sha1="4U2arFwFAq1EoiRPF20uyLhJTgU" width="560" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/225e4092cfa62b3fad43fe891c238384e9e0d43c_2_560x500.png, https://discuss.pytorch.org/uploads/default/original/3X/2/2/225e4092cfa62b3fad43fe891c238384e9e0d43c.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/2/225e4092cfa62b3fad43fe891c238384e9e0d43c.png 2x" data-dominant-color="DDDDE4"><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">image</span><span class="informations">639×570 53.8 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><br>
Left column is<strong>Fundus vessels pictures</strong> and also is  <strong><code>labels</code></strong>, right colum is  <strong>Fundus image</strong> and also is <strong><code> imgs</code></strong> .<br>
<code>labels</code> information is here<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/c/cc48eb93cb46ef2f4873f4531ace25f47409fd69.png" alt="image" data-base62-sha1="t9bDmhXdLjobLSdhuFPaSSj0SYp" width="305" height="438"><br>
<strong><code>labels</code>[0,17,128]  tell us The first picture in a batch that Pixel values in row 17 and column 128</strong><br>
and <code>imgs</code> information is here<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2ce4793902fb2b22562df12985e2aa010e0d975c.png" alt="image" data-base62-sha1="6p8vGaWuhX25uraHTnb4kOjxnS4" width="303" height="442"><br>
<code>outputs</code> information is here<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/f/e/fe51f771b7f6e8a6d20d20120082744a727d0376.png" alt="image" data-base62-sha1="AhOTbsSEXlvTtbYLrxFso4u9On4" width="352" height="446"><br>
<code>output_c1</code>  information is here<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/7/b7d7dcbca48d69dd3593ad960f660e2bf40568dc.png" alt="image" data-base62-sha1="qelRjdinuVbTWuvTN3ENVtEqPtW" width="352" height="437"></p>

username: KFrank
cooked: <p>Hello fyy!</p>
<aside class="quote no-group quote-modified" data-username="fyy" data-post="9" data-topic="74237" data-full="true">
<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/f/eb8c5e/48.png" class="avatar"> fyy:</div>
<blockquote>
<p><a class="mention" href="/u/kfrank">@KFrank</a>,<br>
I try to use U-net model to perform medical image segmentation</p>
</blockquote>
</aside>
<p>At this point, the best I can offer you is some general advice.</p>
<p>First:  Independent of u-net or pytorch or machine learning, you<br>
need to understand the problem you are trying to solve.  You<br>
should <em>look</em> at the data you are working with.  Print out some<br>
images and try to segment them by hand (without a computer).<br>
Draw the segmentation in with a pencil, and see how well you<br>
can do.</p>
<p>With all due respect, you replied:</p>
<aside class="quote no-group">
<blockquote>
<p><strong><code>labels</code>[0,17,128]  tell us The first picture in a batch that Pixel values in row 17 and column 128</strong></p>
</blockquote>
</aside>
<p>to my question:</p>
<aside class="quote no-group" data-username="KFrank" data-post="8" data-topic="74237">
<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/k/ecb155/48.png" class="avatar"> KFrank:</div>
<blockquote>
<p>What do your labels <em>mean?</em> To be concrete, you say that your<br>
batch-of-labels tensor has shape <code>[4, 244, 244]</code> . What does<br>
the value of a specific element of that tensor mean. That is,<br>
<code>labels[0, 17, 128]</code> is some number. Conceptually, what<br>
is that number telling us?</p>
</blockquote>
</aside>
<p>Your data has <em>meaning.</em>  It’s virtually impossible to do any kind<br>
of worthwhile data analysis (machine learning or not) without<br>
<em>understanding</em> your data.</p>
<p>Second:  You need to learn the basics of pytorch before tackling a<br>
more substantive problem.  I suggest that you work though some of<br>
the pytorch tutorials to learn the framework and tools.  In particular<br>
the <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py" rel="noopener nofollow ugc">Training a Classifier</a> tutorial should be useful because your<br>
segmentation problem is a kind of classification problem.</p>
<p>A hint:  I’m convinced that you are working on a <em>binary</em> segmentation<br>
(classification) problem.  Even though you posted them in (false) color,<br>
when I look at your sample labels images, I see black-and-white (that<br>
is, binary, rather than grayscale) images.</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: fyy
cooked: <p><a class="mention" href="/u/kfrank">@KFrank</a>  .thank you for your help.</p>