Normalize image pretrained resnet34 encoder U-net

Anastasia_Berlianna: <p>i use unet with pretrained resnet34 as encoder to segment diabetic retinopathy lession, before training, do i have to normalize the image with mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225] as its documentation? Because the result image get darken on the edge and i’m sure it’s affecting in generating the predicted mask, thank you for helping!</p>

ptrblck: <p>The <code>Normalize</code> transformation is used to create a zero-mean and unit-variance input to the model.<br>
The specified <code>mean</code> and <code>std</code> values were calculated from the <code>ImageNet</code> dataset and are often applied to images coming from a similar data distribution. However, since you are working with medical data I would assume the <code>mean</code> and <code>std</code> might differ and you might thus need to calculate these stats from your training dataset.</p>

Anastasia_Berlianna: <p>ah thankyou! since i’m newbie in pytorch, how can i calculate those value from my dataset?</p>

ptrblck: <p>You could either directly calculate the <code>mean</code> and <code>std</code> in case you are preloading the data or you could iterate the <code>DataLoader</code> and calculate these stats from each batch as seen in e.g. <a href="https://discuss.pytorch.org/t/mean-and-std-for-a-custom-dataset-olivetti/88793/7">this post</a>.</p>

Anastasia_Berlianna: <p>thankyou, i’ve tried to calculate mean and std and get the result, then i applied it on my dataset. after that, i checked one of my image dataset still get darken on the edge like this<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/1/a1ce683e285a7da3d440c85e6d63e210b652b818.png" alt="image" data-base62-sha1="n5p65c2Hnti52abUWB3knI0spq8" width="364" height="145"><br>
(left:fundus image, right:groundtruth)<br>
Is there any other way to normalize dataset beside using mean and std?</p>

ptrblck: <p>I don’t know what the issue is since normalizing the image might distort its visualization.<br>
Depending on the window you are using you might clip the image values so could you describe your concerns a bit more, please?</p>

Anastasia_Berlianna: <p>hm, i’m confused too, but here’s my code to show some of my dataset</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt

dataset = ProcessDataset(train_x, train_y)

fig, axs = plt.subplots(nrows=5, ncols=2, figsize=(8, 16))
for i, (data, target) in enumerate(dataset):
    if i &gt;= 5:
        break
    axs[i, 0].imshow(data.permute(1, 2, 0), cmap='gray')
    axs[i, 1].imshow(np.squeeze(target), cmap='gray')
    axs[i, 0].axis('off')
    axs[i, 1].axis('off')
plt.show()
</code></pre>
<p>and the output is like my previous answer, and talk about clip value, i got a warning</p>
<blockquote>
<p>WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0…1] for floats or [0…255] for integers).</p>
</blockquote>

ptrblck: <p>The warning indicates that the normalized image was clipped, which could distort the visualization. Do you see any other issue using these normalized images during training of your model?</p>

Anastasia_Berlianna: <p>yes, test loss and dice coefficient giving nan result<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/1/c1488e14eb66a82ba2ec4ca129a83c930fe0166c.png" alt="image" data-base62-sha1="rzRBd65dLQ30XHMPF3mrk9JThb6" width="262" height="238"></p>

ptrblck: <p>This seems to be related to <a href="https://discuss.pytorch.org/t/test-loss-and-dice-coefficient-giving-nan-result/175372">this topic</a> and thus I assume unrelated to the normalization?</p>

Anastasia_Berlianna: <p>oh so the normalization before training isn’t related with test loss and dice coefficient?</p>