akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
How i can measure pnsr for multiychannel iamges

anoud_abdalziz: <p>how I can measure pnsr and SSIM for multichannel  MRIimages   ?</p>

eqy: <p>You might want to take a look at the implementations in <code>scikit-image</code> which are good examples:<br>
<a href="https://github.com/scikit-image/scikit-image/blob/main/skimage/metrics/simple_metrics.py#L111-L163" rel="noopener nofollow ugc">scikit-image/simple_metrics.py at main · scikit-image/scikit-image (github.com)</a></p>
<p><a href="https://github.com/scikit-image/scikit-image/blob/main/skimage/metrics/_structural_similarity.py#L16-L248" rel="noopener nofollow ugc">scikit-image/_structural_similarity.py at main · scikit-image/scikit-image (github.com)</a></p>

anoud_abdalziz: <p>Thank you for your response,<br>
I know that very well and I asked for comparing with two with depth dimensions for images shape (265,265,130) ,  how [_structural_similarity.] measure ? which compares multichannel</p>
Import dataset without labels

InfT: <p>Hi everyone!<br>
I’m trying to import the AITEX Dataset that hasn’t got labels because in the folder there are just images. The structure of the folder content is:</p>
<ul>
<li>AITEX<br>
– Defect_images → it contains images<br>
– Mask_images → it contains images<br>
– NODefect_images → it contains subdirectories with images</li>
</ul>
<p>How can i write class CustomTextDataset(Dataset)?</p>
Indexing the topk to pairwise list

GabbyChan: <p>Hi,<br>
I am looking for an effective way to convert the indices from topk() into a pairwise array without using any for loop…or the most runtime efficient way possible…?</p>
<p>For example,</p>
<pre><code class="lang-auto">import torch
import pdb

x_ = torch.randn(10,1)  
key_dist_s = torch.cdist(x_, x_, p=2) 

pdb.set_trace()

idx = torch.topk(key_dist_s, k=3, largest=True) 
idx_indices =  idx.indices
</code></pre>
<p>I want to reshape the following <code>idx_indices</code></p>
<pre><code class="lang-auto">tensor([[7, 5, 9],
        [4, 0, 8],
        [7, 4, 5],   
        [4, 1 8]])
</code></pre>
<p>into something like this without using for-loop…</p>
<pre><code class="lang-auto">tensor([[7, 5], 
           [7,9]
          [4, 0],
          [4, 8],
          [7, 4],
          [7,5], 
          [4, 1]... ]) 
</code></pre>
<p>Can anyone please give a solution ?</p>

eqy: <p>Do you mean a solution with no <em>python</em> for loops? I think any solution will have some underlying for loops, so hopefully native loops are fast enough here <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<pre><code class="lang-auto">import torch
import pdb

x_ = torch.randn(10,1)
key_dist_s = torch.cdist(x_, x_, p=2)

idx = torch.topk(key_dist_s, k=3, largest=True)
idx_indices =  idx.indices
a = idx_indices[:,0:2]
b = idx_indices[:,[0,2]]
print(a)
print(b)
temp = torch.cat((a, b), dim=1)
total_len = temp.size(0)*2
out = temp.reshape(total_len, -1)
print(out)
</code></pre>
<pre><code class="lang-auto">tensor([[3, 4],
        [5, 3],
        [5, 6],
        [5, 6],
        [5, 6],
        [3, 4],
        [3, 4],
        [5, 3],
        [3, 5],
        [5, 3]])
tensor([[3, 2],
        [5, 4],
        [5, 0],
        [5, 0],
        [5, 0],
        [3, 2],
        [3, 2],
        [5, 4],
        [3, 4],
        [5, 4]])
tensor([[3, 4],
        [3, 2],
        [5, 3],
        [5, 4],
        [5, 6],
        [5, 0],
        [5, 6],
        [5, 0],
        [5, 6],
        [5, 0],
        [3, 4],
        [3, 2],
        [3, 4],
        [3, 2],
        [5, 3],
        [5, 4],
        [3, 5],
        [3, 4],
        [5, 3],
        [5, 4]])
</code></pre>

GabbyChan: <p>Hi <a href="https://discuss.pytorch.org/u/eqy">eqy</a>, thanks …</p>
<p>Eventually, I did something like this…</p>
<pre><code class="lang-auto">topk = 3
idx_indices =  idx.indices 

sources_sig    = idx_indices[ :, 0].unsqueeze(-1).float()
targets_sig    = idx_indices[ :, 1:].float()

ones_like      = torch.ones( 1, topk-1, device=key_s_gt.device).float()

sources_sig     = torch.matmul(sources_sig, ones_like)

sources_sig_long = sources_sig.long().view(-1)
targets_sig_long = targets_sig.long().view(-1)

pairwise_sig_st = torch.stack([ sources_sig_long, targets_sig_long], dim=1)
</code></pre>
<p>Anyway, please feel free to comment for the better alternatives <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> …</p>
How to feed changed output of a layer to network?

students: <p>hello, I change output one layer and I want this change again feed to network (input this change to network). I write below code but no changes are applied to the network !!! please help me.</p>
<pre><code class="lang-auto">model.conv3.register_forward_hook(get_activation('conv3'))

x, labels = next(iter(test_loader))

a = activation['conv3']

a[9,0,1,:]=a[9,0,2,:]

a[9,0,2,:]=a[9,0,3,:]

a[9,0,3,:]=a[9,0,4,:]

activation['conv3']=a

output = model(x)

plt.subplot(1, 3, 1)

plt.imshow(output[9][0].detach().numpy(), cmap="gray") 

#print(activation['conv3'])

plt.subplot(1, 3, 2)

plt.imshow(activation['conv3'][9][0], cmap="gray") 

plt.subplot(1, 3, 3)

plt.imshow(x[9][0], cmap="gray")</code></pre>

students: <p>please guide me for this problem</p>

students: <p>We have taken the output of a layer. We want to make changes to this output and restart the network. What process should we use?</p>

ptrblck: <p>I don’t fully understand the use case of changing an activation and “restart” the network, as the activation wouldn’t be used, if you reset the model training.<br>
In any case, you should manipulate the activation in the forward hook, if you want to pass the changed activation to the next layers.</p>

students: <aside class="quote no-group quote-post-not-found" data-username="students" data-post="5" data-topic="125162">
<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/s/82dd89/48.png" class="avatar"> students:</div>
<blockquote>
<p>I manipulate the output of one layer and then this changed output feed to following layers. I used this code but I can’t replace this change to output of layer in model.</p>
</blockquote>
</aside>
<p>I manipulate the output of one layer and then this changed output feed to following layers. I used this code but I can’t replace this change to output of layer in model.</p>
<pre><code class="lang-auto">model.conv3.register_forward_hook(get_activation('conv3'))

x, labels = next(iter(test_loader))

a = activation['conv3']

a[9,0,1,:]=a[9,0,2,:]

a[9,0,2,:]=a[9,0,3,:]

a[9,0,3,:]=a[9,0,4,:]

activation['conv3']=a

output = model(x)

plt.subplot(1, 3, 1)

plt.imshow(output[9][0].detach().numpy(), cmap="gray") 

#print(activation['conv3'])

plt.subplot(1, 3, 2)</code></pre>

ptrblck: <p>It seems you are still manipulating the activation after the forward pass finished, so you could take a look at this example to see how to manipulate them inside the hook:</p>
<pre><code class="lang-python">model = nn.Sequential(
    nn.Linear(1, 1),
    nn.ReLU())
model[0].register_forward_hook(lambda m, input, output: output + 1000.)

x = torch.ones(1, 1)
out = model(x)
print(out)
&gt; tensor([[1000.5511]], grad_fn=&lt;ReluBackward0&gt;)
</code></pre>
How can I apply group normalization after a full-connection layer?

335621: <aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/68286001/how-can-i-apply-group-normalization-after-a-full-connection-layer">
  <header class="source">

      <a href="https://stackoverflow.com/questions/68286001/how-can-i-apply-group-normalization-after-a-full-connection-layer" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/13482179/frank" target="_blank" rel="noopener nofollow ugc">
    <img alt="Frank" src="https://www.gravatar.com/avatar/9f91dd26c535bb5c28ae9f48d1da95ab?s=128&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="128" height="128">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/68286001/how-can-i-apply-group-normalization-after-a-full-connection-layer" target="_blank" rel="noopener nofollow ugc">How can I apply group normalization after a full-connection layer?</a>
</h4>

<div class="tags">
  <strong>python, machine-learning, deep-learning, pytorch</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/13482179/frank" target="_blank" rel="noopener nofollow ugc">
    Frank
  </a>
  on <a href="https://stackoverflow.com/questions/68286001/how-can-i-apply-group-normalization-after-a-full-connection-layer" target="_blank" rel="noopener nofollow ugc">12:23PM - 07 Jul 21 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>How can I apply Group Normalization after a full-connection layer? Say the output of the full-connection layer is 1024. And the group normalization layer is using 16 groups.</p>
<pre><code class="lang-auto">self.gn1 = nn.GroupNorm(16, hidden_size)
h1 = F.relu(self.gn1(self.fc1(x))))
</code></pre>
<p>Am I right? How should we understand the group normalization if it is applied to the output of a full-connection layer?</p>
RandomAdjustSharpness gives IndexError: tuple index out of range

desert_ranger: <p>While using RandomAdjustSharpness, my code throws the following error - IndexError: tuple index out of range</p>
<p>Here is my code -</p>
<pre><code class="lang-auto">import math, random
from sklearn.datasets import load_sample_images
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
import torchvision.transforms as transforms
import matplotlib.pyplot as plt


def random_crop(imgs):


    imgs = torch.tensor(imgs)
    change = torch.nn.Sequential(
        transforms.RandomCrop(427),
        transforms.RandomAdjustSharpness(1, p=1)
    )
    imgs = change(imgs).numpy()
    return imgs

###Obtaining a random image and preprocessing it!##
dataset = load_sample_images()
first_img_data = dataset.images[0]
first_img_data  = first_img_data.reshape(-1, 427, 640)
first_img_data = first_img_data[1, :, :]
#first_img_data = first_img_data[0:84, 0:84].reshape(-1, 84,84)
# first_img_data = torch.tensor(first_img_data)
plt.figure()
plt.imshow(np.squeeze(first_img_data))

foo = random_crop(first_img_data)
plt.figure()

plt.imshow(np.squeeze(foo))
plt.show()
</code></pre>

ptrblck: <p>I cannot reproduce the error using:</p>
<pre><code class="lang-python">def random_crop(imgs):
    imgs = torch.tensor(imgs)
    change = torch.nn.Sequential(
        transforms.RandomCrop(427),
        transforms.RandomAdjustSharpness(1, p=1)
    )
    imgs = change(imgs).numpy()
    return imgs


first_img_data = torch.randn(3, 500, 500)
foo = random_crop(first_img_data)
plt.figure()
plt.imshow(np.squeeze(foo.transpose(1, 2, 0)))
plt.show()
</code></pre>

desert_ranger: <p>Oh, that’s surprising as I thought that the example put up could be directly executed (it uses scikit-learn to import images). However, I got the answer from here - <a href="https://stackoverflow.com/questions/68273819/randomadjustsharpness-gives-indexerror-tuple-index-out-of-range/68274576#68274576" class="inline-onebox" rel="noopener nofollow ugc">pytorch - RandomAdjustSharpness gives IndexError: tuple index out of range - Stack Overflow</a></p>
<p>Thanks a lot for your help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>