akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
How to dump Custom Dataset into ImageFolder readable format?

username: sanjass
cooked: <p>I have a <code>CustomDataset</code> that subclasses <code>torch.utils.data.Dataset</code>.<br>
Due to some third-party library specs, I have an unusual situation where I need the data in <code>CustomDataset</code> to be stored in a directory in an <code>ImageFolder</code> readable format.</p>
<p>What’s the easiest/cleanest way to achieve this?<br>
Many thanks</p>

username: ptrblck
cooked: <p>If you need to store the data in the “folder layout” expected by <code>ImageFolder</code>, you could either move the data to the root and subfolders or create symbolic links instead.</p>

username: sanjass
cooked: <p>Can you explain what do you mean by move the data to the root and subfolders? The CustomDataset contains custom data that I did not load from a folder but created from scratch</p>

username: ptrblck
cooked: <p>In that case I don’t understand the question correctly.<br>
Could you explain this part a bit more:</p>
<aside class="quote no-group" data-username="sanjass" data-post="1" data-topic="117992">
<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/d26b3c/48.png" class="avatar"> sanjass:</div>
<blockquote>
<p>I have an unusual situation where I need the data in <code>CustomDataset</code> to be stored in a directory in an <code>ImageFolder</code> readable format.</p>
</blockquote>
</aside>
<p>If you are creating the data from scratch, then you might want to save it to the expected folder structure.</p>

username: sanjass
cooked: <p>Yes, I am creating the data from scratch and have it in the form of a pytorch Dataset and I was wondering if there’s a simple way to “export” it into an ImageFolder compatible format.<br>
I imagine I can do this “manually” myself, but I wanted to know if there’s some existing functionality that can easily dump a Dataset into the desired format, as the ImageFolder format is common.</p>

username: ptrblck
cooked: <p>Thanks for clarifying. No, I don’t think there is an automatic way to export the images.</p>
How to use Avg 2d Pooling as a dataset transform?

username: RylanSchaeffer
cooked: <p>I have a dataset of 2D images (or alternatively, 1 channel images) and I’d like to apply average 2D pooling as a transform. How can one do this? The following does not work:</p>
<pre><code class="lang-auto">    omniglot_dataset = torchvision.datasets.Omniglot(
        root=data_dir,
        download=True,
        transform=torchvision.transforms.Compose([
            torchvision.transforms.ToTensor(),
            torchvision.transforms.CenterCrop((80, 80)),
            # torchvision.transforms.Resize((10, 10))
            torch.nn.functional.avg_pool2d(kernel_size=3, strides=1),
        ])
    )

</code></pre>

username: ptrblck
cooked: <p>You can use <code>transforms.Lambda</code> to call the functional API:</p>
<pre><code class="lang-python">transform=torchvision.transforms.Compose([
    torchvision.transforms.CenterCrop((80, 80)),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Lambda(lambda x: torch.nn.functional.avg_pool2d(x, kernel_size=3, stride=1)),
])

img = transforms.ToPILImage()(torch.randn(3, 224, 224))
out = transform(img)
</code></pre>
TorchAudio : __init__() got an unexpected keyword argument 'center'

username: HitsujiAura
cooked: <p>Hello,</p>
<p>I was passing some mel arguments into the torchaudio.transforms.MFCC function but it doesn’t seem to recognize the argument “center”. Center is a parameter to the Mel Spectorgram so I don’t understand why it’s not working.</p>
<p>Code:</p>
<pre><code class="lang-auto">melkwargs = {
    "center": False,
    "n_fft": n_fft,
    "hop_length": hop_length,
    "power": 2,
    "f_min": f_min,
    "f_max": f_max
}

mfcc_module = torchaudio.transforms.MFCC(sample_rate=sample_rate, n_mfcc=20, melkwargs=melkwargs)
torch_mfcc = mfcc_module(torch.tensor(waveform))
</code></pre>
<p>Error:</p>
<blockquote>
<p>TypeError: <strong>init</strong>() got an unexpected keyword argument ‘center’</p>
</blockquote>
<p>Any help would be appreciated.</p>

username: ptrblck
cooked: <p>You might need to update <code>torchaudio</code> to <code>0.8.0</code>, as the <code>center</code> argument doesn’t seem to be available in previous versions.</p>
Torch::cuda::is_available() is throwing compilation error

username: vicki
cooked: <p>I am trying out the c++ frontend with a simple program.<br>
Compilation of this code block is failing -</p>
<pre><code class="lang-cpp">    torch::DeviceType device_type;
    if (torch::cuda::is_available())
    {
        std::cout &lt;&lt; "CUDA is available!" &lt;&lt; std::endl;
        device_type = torch::kCUDA;
    }

</code></pre>
<p>And throws this error -</p>
<pre><code class="lang-cpp">error: ‘is_available’ is not a member of ‘at::cuda’
     if (torch::cuda::is_available())
</code></pre>
<p>Any idea what might be wrong here?</p>

username: mhubii
cooked: <p>hm that is strange. Can you please provide your <code>CMakeLists.txt</code> and the command with which you are trying to build your code? Where did you get your libtorch from?</p>

username: vicki
cooked: <p>CMakeList is a minimal:</p>
<pre><code class="lang-auto">project(custom_op)

find_package(Torch REQUIRED)

add_executable(jit_minimal jit_minimal.cpp)
target_link_libraries(jit_minimal "${TORCH_LIBRARIES}")
set_property(TARGET jit_minimal PROPERTY CXX_STANDARD 11)
</code></pre>
<p>Command used for build -</p>
<pre><code class="lang-auto">mkdir -p build/
cd build/
# cmake -DCMAKE_PREFIX_PATH=~/libs/libtorch/ ..
cmake -DCMAKE_PREFIX_PATH=~/libs/nightly/libtorch/ ..
make
</code></pre>
<p>And I got the libtorch using the download url on pytorch website.</p>

username: mhubii
cooked: <p>that should work, please provide your full <code>jit_minimal.cpp</code><br>
what version of CUDA do you have?</p>

username: kitterive
cooked: <p>I met this error too, any idea?</p>

username: pratyush911
cooked: <p>Including the following should solve it.<br>
<span class="hashtag">#include</span> &lt;torch/torch.h&gt;</p>
<p>You are possibly only including <span class="hashtag">#include</span> &lt;torch/script.h&gt;</p>

username: Martin_JH
cooked: <p>Thanks <a class="mention" href="/u/pratyush911">@pratyush911</a>. That was the solution.</p>

username: Schuyler
cooked: <p>Thanks for the information. It is really helpful for us. Please keep suggesting such a content.</p>
<p><a href="https://www.myaarpmedicare.review/" rel="noopener nofollow ugc">www.myaarpmedicare.com</a></p>
SyntaxError: index out of range

username: Mehedi_Hasan_Bijoy
cooked: <pre><code class="lang-auto">epochs = 10
for epoch in range(1, epochs+1):
    print(f'Epoch: {epoch}/{epochs}')

    model.train()
    train_losses = []
    for images, labels in train_loader:
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad()
        outputs = model(images).to(torch.float64)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        train_losses.append(loss.cpu().detach().numpy())
    train_loss = np.sum(train_losses) / len(train_losses)
    print(train_loss)
</code></pre>
<pre><code class="lang-auto">Epoch: 1/10
  File "&lt;string&gt;", line unknown
SyntaxError: index out of range
</code></pre>
<p><strong>why i am getting this error? can you please help?</strong><br>
<strong>I have tried this using 10000 images but when i tried full dataset (51000) then i got this error.</strong></p>
<pre><code class="lang-auto">class LoadDataset(Dataset):
    def __init__(self, df, root_dir, transform=None):
        self.annotations = df
        self.root_dir = root_dir
        self.transform = transform
    
    def __len__(self):
        return len(self.annotations)
    
    def __getitem__(self, index):
        img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
        image = io.imread(img_path)
        y_label = torch.tensor(self.annotations.iloc[index, 1:], dtype = torch.float64, requires_grad = True)
        
        if self.transform:
            image = self.transform(image)
            
        return (image, y_label)


dataset = LoadDataset(df = new_df, root_dir = root_dir, transform = transform_img)

no_of_training_images = round(len(dataset) * .8)
remaining_images = len(dataset) - no_of_training_images

train_data, valid_data = torch.utils.data.random_split(dataset, [no_of_training_images, remaining_images])

train_loader = DataLoader(dataset = train_data, batch_size = 16, shuffle = True)
valid_loader = DataLoader(dataset = valid_data, batch_size = 16, shuffle = True)
</code></pre>

username: ptrblck
cooked: <p>The error message is a bit hard to understand, but it usually raised by <code>PIL</code>, e.g. if the image file is broken.<br>
You could debug it further by checking which image is causing this issue (e.g. print the <code>index</code> in <code>Dataset.__getitem__</code> and try to load the image directly) and either fix the image (download it again) or remove it.</p>

username: Mehedi_Hasan_Bijoy
cooked: <p><strong>Update: the problem is solved.</strong><br>
I just created a list of problematic(NoneType) images and removed those images.</p>
<pre><code class="lang-auto">problematic_images = []
for img in new_df.iloc[:, 0]:
    img_path = os.path.join(root_dir, img)
    img_array = cv2.imread(img_path)
    if type(img_array) == type(None):
        problematic_images.append(img_path)
</code></pre>
Handling high pixel class imbalance in biomedical image segmentation

username: YASJAY
cooked: <p>Hi,</p>
<p>This post is to understand how high class imbalance is handled in biomedical image segmentation and detection problems especially in problems like cell detection and where the annotations are at dot/pixel level. This leads to very high number of background (bg) pixels. The model should ideally detect cells along with non-cells (bg pixels).</p>
<p>While trying to perform a pixel-level classification, the high number of bg pixels would have a biased learning favoring only the bg pixels.</p>
<p>Weighted loss function is one way, what are the other ways?</p>
Bilinear Interpolation at random locations in an image

username: mrmoody
cooked: <p>Hi everyone,</p>
<p>Let’s say I have data consisting of batches of images, resulting in a shape of <code>[batch_size, channels, h, w]</code>. I also have batches of x-y coordinates, which are not integer values -&gt; <code>[batch_size, num_points, 2]</code>.</p>
<p>What I want to do now is to get a resulting tensor of size <code>[batch_size, channels, num_points]</code> which are the bilinear interpolated values for the given float x-y coordinates.</p>
<p>Is there a respective function in pytorch? I have only found sampling methods, but this is not what I want.</p>
<p>Thanks a lot!</p>

username: ptrblck
cooked: <p>I think <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.functional.grid_sample">grid_sample</a> should be suitable. Could you try that out and see if it’s working for you?</p>

username: liwenssss
cooked: <p>Hi, did you solve it? I hanve the same quesiton…</p>
How to solve this problem: statements of "__all__" without "Dataset"

username: Robin_lyq
cooked: <p>This inspection warns if a protected member is accessed outside the class, a descendant of the class where it’s defined or a module.  It occurred when I use the following sentence:from torch.utils.data import Dataset</p>

username: ptrblck
cooked: <p>I don’t get this warning when running <code>from torch.utils.data import Dataset</code>. Which PyTorch version are you using?</p>

username: Robin_lyq
cooked: <p>My version is PyTorch 1.7.1</p>

username: Robin_lyq
cooked: <p>Thanks, I found it, my dataset.py has some mistakes in defining</p>
Conv1d on grayscale image. How to improve?

username: HashRocketSyntax
cooked: <p>Posting this partially as a working example for people that are struggling, and partially for feedback as I am brand new to torch.</p>
<hr>
<p>I have individual images of shape: <em>width x height</em>. There is no channel (aka single channel) because it’s grayscale.<br>
<code>sample.shape==[160, 120]</code></p>
<p>With a batch size of 5, I get the shape: <em>sample x width x height</em>.<br>
<code>batch.shape==[5, 160, 120]</code> ← width will serve as 1d input channel</p>
<hr>
<pre><code class="lang-python">def fn_build(): # wrapped just to try init strategies. i know functional api exists.

    model = nn.Sequential(
        #Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')
        nn.Conv1d(
            in_channels=160 # running with `in_channels` as the width of the image.
            , out_channels=56 # arbitrary number. treating this as network complexity.
            , kernel_size=3
            , padding=1
        )
        , nn.MaxPool1d(kernel_size=2, stride=2)
        #, nn.BatchNorm1d(56,56) # stuck at local minimum (0.68)
        , nn.ReLU() #wasnt learning with tanh
        , nn.Dropout(p=0.4)

        , nn.Conv1d(
            in_channels=56, out_channels=128, # expand to get more granular featurespace.
            kernel_size=3, padding=1
        )
        , nn.MaxPool1d(kernel_size=2, stride=2)
        #, nn.BatchNorm1d(128,128) # stuck at local minimum (0.68)
        , nn.ReLU() #wasnt learning with tanh
        , nn.Dropout(p=0.4)
        # output [5x1440]

        , nn.Flatten()
        , nn.Linear(3840, 3840)
        , nn.BatchNorm1d(3840, 3840)
        , nn.ReLU()
        , nn.Dropout(p=0.4)

        , nn.Linear(3840, 1)
        , nn.Sigmoid()
    )

# ------ Tried init but it didn't learn (0.50 flatline) ------
#     def initialize_layerz(m):
#         if type(m) == nn.Linear:
#             nn.init.uniform_(m.weight)
#         if type(m) == nn.Conv1d:
#             nn.init.normal_(m.weight)
            
#     model.apply(initialize_layerz)

    return model
</code></pre>
<ul>
<li>Padding is an obvious area for improvement, but I’m more concerned with topology at this stage in learning.</li>
<li>Is there a way I can use less neurons (3840) in my receiving Linear layer?</li>
<li>Would a multidimensional shape of linear layer be faster/ possible?</li>
<li>Is there anything obviously wrong with those init lines?</li>
<li>All criticism welcome.</li>
</ul>
Runtime error shape invalid for input size

username: fani
cooked: <p>Hello, could someone help me please? Im using tacotron training notebook on google collab and I have a error :</p>
<h1>
<a name="hparams-to-tune" class="anchor" href="#hparams-to-tune"></a>hparams to Tune</h1>
<p><span class="hashtag">#These</span> two are the most important</p>
<p>hparams.batch_size = 10 # Controls how fast the model trains. Don’t set this too high, or else it will GPU will OOM (out of memory). 30-ish is usually a good number if you have a bigger dataset. If the number of audio files is more than/about the same as this number, it won’t train properly, and you won’t be able to use it.</p>
<p>hparams.epochs = 500 # Maxmimum epochs (number of times the AI looks through the dataset) to train</p>
<p><span class="hashtag">#The</span> rest aren’t that important</p>
<p>hparams.p_attention_dropout=0.1</p>
<p>hparams.p_decoder_dropout=0.1</p>
<p>hparams.decay_start = 15000         # wait till decay_start to start decaying learning rate</p>
<p>hparams.A_ = 5e-4                   # Start/Max Learning Rate</p>
<p>hparams.B_ = 8000                   # Decay Rate</p>
<p>hparams.C_ = 0                      # Shift learning rate equation by this value</p>
<p>hparams.min_learning_rate = 1e-5    # Min Learning Rate</p>
<p>generate_mels = True # Don’t change</p>
<p>hparams.show_alignments = True</p>
<p>alignment_graph_height = 600</p>
<p>alignment_graph_width = 1000</p>
<p>hparams.load_mel_from_disk = True</p>
<p>hparams.ignore_layers = [] # Layers to reset (None by default, other than foreign languages this param can be ignored)</p>
<p>torch.backends.cudnn.enabled = hparams.cudnn_enabled</p>
<p>torch.backends.cudnn.benchmark = hparams.cudnn_benchmark</p>
<p>output_directory = ‘/content/drive/My Drive/colab/outdir’ # Location to save Checkpoints</p>
<p>log_directory = ‘/content/tacotron2/logs’ # Location to save Log files locally</p>
<p>log_directory2 = ‘/content/drive/My Drive/colab/logs’ # Location to copy log files (done at the end of each epoch to cut down on I/O)</p>
<p>checkpoint_path = output_directory+(r’/’)+model_filename</p>
<hr>
<p>if generate_mels:</p>
<pre><code>create_mels()
</code></pre>
<p>RuntimeError: shape ‘[1, 1, 83256]’ is invalid for input of size 166512</p>

username: ptrblck
cooked: <p>A shape mismatch error is often created by a wrongly shaped input.<br>
I don’t know what the shape of your current input is and what shape is expected, but you might want to check the reference implementations and what inputs are used in the original code.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
Conv2d vs ConvTranspose2d

username: Kev1nZheng
cooked: <p>I have a tensor a.shape = [50,64,64]. First, I shrink the size to [20,64,64] by a Con2d layer. Then I want to reconstruct it from [20,64,64] to [50,64,64].<br>
Is there any difference between using:<br>
conv1 = nn.Conv2d(in_channels = 20,out_channels = 50)<br>
conv2 = nn.ConvTranspose2d(in_channels = 20,out_channels = 50)</p>

username: albanD
cooked: <p>Hi,</p>
<p>The transpose or not refers to how spatial dimensions are handled, not channel dimensions.<br>
If you only want to change the number of channels, you can use conv2d.<br>
If you want the opposite spatial connectivity, then you need to use the transposed version.</p>

username: anu
cooked: <p>Hello <a class="mention" href="/u/alband">@albanD</a></p>
<p>Could you explain a bit more detailed by saying opposite spatial connectivity?</p>
<p>Thanks in advance.</p>

username: albanD
cooked: <p>Hi,</p>
<p>The visualizations in this blogpost might be clearer than words: <a href="https://medium.com/apache-mxnet/transposed-convolutions-explained-with-ms-excel-52d13030c7e8" class="inline-onebox">Transposed Convolutions explained with… MS Excel! | by Thom Lane | Apache MXNet | Medium</a></p>

username: anu
cooked: <p>I read the whole series. Helped a lot, thanks!!</p>
Create a Linear Layer outputting a 128*7*7 tensor

username: twister9458
cooked: <p>Hello all;</p>
<p>Could you help me creating an equivalent layer of Keras Dense (128,7,7), hence outputting a 3D (128,7,7) layer ? thank you very much.<br>
I’ve tempted <code>nn.Linear(in_features=100, out_features=(128,7,7), bias=True)</code>, but I don’t know if this is correct.<br>
I’m working on DCGAN, and my input z is of 100 size.<br>
Thank you very much<br>
Habib</p>

username: ptrblck
cooked: <p>Could you explain, how this layer is applied, i.e. what input shape is expected and how is the weight parameter applied to the input?<br>
I can’t find any information in the Keras docs about these multi-dimensional shapes.</p>

username: twister9458
cooked: <p>Sorry, I’m still new to using Keras and Pytorch.<br>
My input layer is of 100 dim, the output should be of <code>128*7*7</code> dimensions (tensor), I’ve tempted <code>nn.Linear(in_features=100, out_features=128*7*7, bias=True)</code> which seemed working, I saw it in other notebooks, but I can’t figure out this syntax, <code>128*7*7</code>. Is this a correct syntax ? Why isn’t it interpreted as <code>6272</code> ?</p>

username: ptrblck
cooked: <p>In your code snippet, <code>128*7*7</code> is interpreted as <code>6272</code>.<br>
The way to write the input shape as a multiplication makes instead of the result directly makes it easier to understand the shape.</p>
<p>E.g. when this linear layer is preceded by conv layers, you would flatten the conv activation, so that it’ll have the shape <code>[batch_size, channels*height*width]</code>. In this case it’s clear to define <code>in_features</code> as <code>channels*height*width</code> or in your case <code>128*7*7</code>.</p>
<p>However, in the Keras code these numbers are not multiplied, but seem to be passed as a multi-dimensional shape?<br>
Note that I’m not deeply familiar with Keras, so my interpretation might be wrong.</p>

username: twister9458
cooked: <p>Thank you very much !</p>
Pytorch neural network giving the same output always

username: Philip_George
cooked: <p>Hi All,</p>
<p>I have defined a neural network class like below<br>
class MLP(nn.Module):</p>
<pre><code>def __init__(self,n_classes=5):
    super(MLP,self).__init__()
    self.fc1=nn.Linear(784,256)
    self.fc2=nn.Linear(256,64)
    self.fc3=nn.Linear(64,16)
    
    self.clf=nn.Linear(16,n_classes)
    
def forward(self,x):
    x=x.view(-1,784)
    x=F.relu(self.fc1(x))
    x=F.relu(self.fc2(x))
    x=F.relu(self.fc3(x))
    out=self.clf(x)
    
    return out
</code></pre>
<p>I have trained this model and stored in a location.</p>
<p>Now I am trying to predict the class for a new image.</p>
<p>model_MLP=MLP(5)<br>
model_MLP.load_state_dict(torch.load("&lt;model.pt&gt;"))</p>
<p>x_data contains the 28x28 pixel image<br>
images = x_data.float()<br>
output=model_MLP(images)</p>
<p>The output class I get for the first time after training is the same output that I get always if I try with other images. I see that the output values don’t change at all for the new images I provide as input to the class.</p>
<p>How can I resolve this issue?</p>
<p>Regards.<br>
Philip</p>

username: Kushaj
cooked: <p>There is nothing wrong with the code. Can you provide the exact inference code you are using. Along with output values.</p>

username: siva_sankar
cooked: <p>I have exactly same issue. Solution plss</p>
Concatenation of gru and lstm layers in pytorch

username: asifdadra
cooked: <p>Hello Everyone, I am trying to concatenate the gru and lstm layers in pytorch. the example code is below. does anyone help?<br>
self.rnn_encoder_gru = nn.GRU(din, dhid, bidirectional=True, batch_first=True)<br>
self.rnn_encoder_lstm = nn.LSTM(din, dhid, bidirectional=True, batch_first=True)<br>
self.rnn_encoder = concatenate (self.rnn_encoder_lstm, self.rnn_encoder_gru)</p>

username: ptrblck
cooked: <p>You cannot concatenate modules, but could concatenate their output tensors.<br>
Once you’ve created the modules, you could pass a tensor to them and concatenate the output or hidden states afterwards.</p>

username: asifdadra
cooked: <p>Ok. Thanks. I got answer</p>
Dataloader collate fn throws `RuntimeError: stack expects each tensor to be equal size` in response to variable number of bounding boxes

username: kareemamr
cooked: <p>I am using <code>torchvision.models.detection.retinanet_resnet50_fpn</code> which expects the labels to be a list of dictionaries each containing:</p>
<ol>
<li>boxes (<code>FloatTensor[N, 4]</code>): the ground-truth boxes in <code>[x1, y1, x2, y2]</code> format, with <code>0 &lt;= x1 &lt; x2 &lt;= W</code> and <code>0 &lt;= y1 &lt; y2 &lt;= H</code>.</li>
<li>labels (<code>Int64Tensor[N]</code>): the class label for each ground-truth box</li>
</ol>
<p>So normally each dict which belongs to a unique training sample would contain a different number <code>boxes</code> and <code>labels</code>.</p>
<p>In my custom <code>Dataset</code> class:</p>
<pre><code class="lang-auto">class COCODataset(Dataset):
    def __init__(self, root, annFile, transforms):
        self.root = Path(root)
        self.annFile = Path(annFile)
        self.transforms = transforms
        self._parse_annotations()
       
    def __len__(self):
        return len(self.imgs)

    def __getitem__(self, idx):
        img_path = self.root / self.imgs[idx]
        img = Image.open(img_path).convert("RGB")
        target = self.targets[idx]

        target["boxes"] = torch.as_tensor(target["boxes"], dtype=torch.float32)
        target["labels"] = torch.as_tensor(target["labels"], dtype=torch.int64)

        if self.transforms is not None:
            img = self.transforms(img)

        return img, target
</code></pre>
<p>When trying to retrieve a sample directly from the custom dataset it works fine and returns <code>x</code> and <code>y</code> as:</p>
<pre><code class="lang-auto">(tensor([[[0.5977, 0.6082, 0.6157,  ..., 0.5798, 0.5562, 0.5738],
          ...,
          [0.4960, 0.6187, 0.6154,  ..., 0.9062, 0.9111, 0.9098]]]),

 {'boxes': tensor([[250.8200, 168.2600, 320.9300, 233.1400],
          [285.5500, 370.5600, 297.6200, 389.7700]]),
  'labels': tensor([1, 1])})
</code></pre>
<p>However when wrapping the dataset in a <code>Dataloader</code> and it throws <code>RuntimeError: stack expects each tensor to be equal size, but got [10, 4] at entry 0 and [3, 4] at entry 1</code>. Any idea how to deal with this?</p>

username: kareemamr
cooked: <p>However in this official <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#writing-a-custom-dataset-for-pennfudan" rel="noopener nofollow ugc">tutorial</a> I believe they are using a variable number of bounding boxes as well when building the custom dataset so it must be possible.</p>

username: kareemamr
cooked: <p>This issue was solved by passing this <a href="https://github.com/pytorch/vision/blob/512ea299d4b2d2bbac3498a75a2d8c0190cfcb39/references/detection/utils.py#L235" rel="noopener nofollow ugc">collate_fn</a> to the <code>DataLoader</code>.</p>
Resize multiple times with torchvision

username: snowball
cooked: <p>I’m looking to resize the MNIST dataset to a 8x8 image, and then resize the 8x8 image, back to its original dimensions. During this process, the image should lose quality (since we are resizing from 8x8 to 28x28.</p>
<p>Currently, the code is as follows</p>
<pre><code class="lang-auto">dataset = torchvision.datasets.MNIST('./', train=True, download=False,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.Resize(8) ]))
upscale = transforms.Compose([transforms.Resize(28)])
pil, label = dataset[0] #Take first image in dataset
result = upscale(pil)
result.show()
</code></pre>
<p>But this does not save the result into the MNIST object (i.e. the variable named "dataset’). I would also need a nasty for loop to loop through all the images.</p>
<p>Is there a better way to accomplish this?</p>

username: ptrblck
cooked: <p>Since the MNIST data is preloaded completely, you could directly access it via <code>dataset.data</code> and apply the resizing on this tensor directly.<br>
However, if you are working with a dataset, which uses lazy loading, you would have to iterate the dataset, as each sample is loaded in the corresponding iteration.</p>
Batched bounding box coordinate normalization code?

username: Felix_Labelle
cooked: <p>I inherited some code that normalizes bounding boxes coordinates (BATCH_SIZE x NUM_BOUNDING_BOXES x FOUR_COORDINATES) based on batched image sizes (BATCH_SIZE x X_Y_DIMS). The problem is that when the batchsize (B in the code below) is higher than 1 the code no longer functions properly, either not doing the correct operation or failing due to dimension mismatches for B &gt; 2.</p>
<p>In the snippet below tensor_a represents the batched bounding boxes and tensor_b the image dimensions, NOTE: they may not all be the same size (it was simpler to check the code this way). How could I do this operation for any batch size?</p>
<pre><code class="lang-auto">B = 1
tensor_a = torch.ones([B, 36, 4])
tensor_b = torch.ones([B, 2])
tensor_b[:,0] = 4
test_res1 = tensor_a[:, :, (0, 2)] / tensor_b[:, 1]
test_res2 = tensor_a[:, :, (1, 3)] / tensor_b[:, 0]
test_res1, test_res2
</code></pre>
CUDA/GCC problem when building from source

username: PersonalFinance004
cooked: <p>I get the following error message when trying to build PyTorch:</p>
<pre><code class="lang-auto">unsupported GNU version! gcc versions later than 8 are not supported!
</code></pre>
<p>According to <a href="https://stackoverflow.com/questions/6622454/cuda-incompatible-with-my-gcc-version" rel="noopener nofollow ugc">this stackoverflow thread</a> I need to make sure that I use the max supported GCC version for the CUDA version I have installed.</p>
<pre><code class="lang-auto">nvidia-smi
</code></pre>
<p>gives me</p>
<pre><code class="lang-auto">NVIDIA-SMI 460.32.03    Driver Version: 460.32.03    CUDA Version: 11.2
</code></pre>
<p>and</p>
<pre><code class="lang-auto">gcc --version
</code></pre>
<p>gives me</p>
<pre><code class="lang-auto">gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
</code></pre>
<p>and</p>
<pre><code class="lang-auto">nvcc -V
</code></pre>
<p>gives</p>
<pre><code class="lang-auto">Cuda compilation tools, release 11.2, V11.2.152
</code></pre>
<p>According to the thread linked above, CUDA 11 supports GCC &lt;= 10.<br>
What am I missing here?</p>

username: PersonalFinance004
cooked: <p>For some reason, it worked after I tried building inside an Anaconda environment instead of a venv virtual-environment. I do not know why.</p>
`TypeError: Cannot handle this data type: (1, 1, 1), |u1` when using `torchvision.utils.draw_bounding_boxes`

username: kareemamr
cooked: <p>I’m trying to draw a bounding box over an image using the <code>draw_bounding_boxes</code> function but am faced with this error.<br>
Here is the code:</p>
<pre><code class="lang-auto">img = cv2.imread(img_path, 0)
img = torch.unsqueeze(torch.Tensor(img), 0)
img = img.type(torch.uint8)
res = draw_bounding_boxes(img, bbox)
</code></pre>
<p>Can anyone help with this?</p>

username: ptrblck
cooked: <p>Could you check the type of <code>bbox</code>? Based on the <a href="https://pytorch.org/vision/master/utils.html#torchvision.utils.draw_bounding_boxes">docs</a> a tensor is expected and the error message points towards a wrong type in a numpy array, if I’m not mistaken.</p>

username: kareemamr
cooked: <p>Yes I verified that <code>bbox</code> was of type <code>Tensor</code></p>

username: ptrblck
cooked: <p>Could you post an executable code snippet (using random tensors) so that we could reproduce this issue, please?</p>
Meaning of `grad_outputs` in `torch.autograd.grad` for complex inout

username: mfkasim
cooked: <p>For real-valued tensors, I can understand providing <code>grad_outputs</code> in <code>torch.autograd.grad</code>, e.g. <code>torch.autograd.grad(out, inp, grad_outputs=g)</code> is like calculating the expression <code>g^T @ jac(out, inp)</code>. What is the expression if all variables are complex?</p>
<p>N.B.: I also posted this question on <a href="https://ai.stackexchange.com/questions/27292/meaning-of-grad-outputs-in-torch-autograd-grad-for-complex-input-and-output" rel="noopener nofollow ugc">AI StackExchange</a> because they have support for math expression.</p>
Tensor multiplication by multiple matrices multiplication

username: kfiros
cooked: <p>Hi,</p>
<p>I have 2D tensor of matrices, i.e I have matrices_tensor in size NxNx3x3 such that for index (i,j), matrices_tensor[i,j,…] yields matrix in size of 3x3.</p>
<p>I would like to do the following:<br>
For target Nx3x1, I would like to get output in size Nx3 such that</p>
<blockquote>
<p>output = torch.zeros((N,3))<br>
for i in range(N):<br>
for j in range(N):<br>
output[i,:] += matrices_tensor[i,j] @ target[j,:]</p>
</blockquote>
<p>where @ is matrix multiplication operator.<br>
I want to do in tensor operation and not in loop in order to leverage the performance in gpu.<br>
The matrix should be sparse, but for now, I’m looking for solution of dense matrix.</p>
<p>p.s:<br>
It’s not necessary to know the application but if someone wants to know, the purpose is optimizing non rigid transformation for mesh deformation and regularize with smoothness of the rotated vertices.</p>

username: KFrank
cooked: <p>Hi kfiros!</p>
<aside class="quote no-group" data-username="kfiros" data-post="1" data-topic="117936" data-full="true">
<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/kfiros/48/33714_2.png" class="avatar"> kfiros:</div>
<blockquote>
<p>I want to do in tensor operation and not in loop</p>
</blockquote>
</aside>
<p><a href="https://pytorch.org/docs/1.8.1/generated/torch.einsum.html#torch.einsum" rel="noopener nofollow ugc">torch.einsum()</a> is your go-to tool when you want to sum multiplied slices<br>
(“contract indices”) of tensors.</p>
<p>Here is an example script:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
print (torch.__version__)

_ = torch.manual_seed (2021)

N = 5
m = 3

matrices_tensor = torch.randn (N, N, m, m)
target = torch.randn (N, m, 1)

output_einsum = torch.einsum ('ijkl, jlm -&gt; ik', matrices_tensor, target)

output = torch.zeros((N, m))
for i in range (N):
    for j in range (N):
        output[i, :] += (matrices_tensor[i, j] @ target[j, :]).squeeze()

print ('output_einsum.shape =', output_einsum.shape)
print ('output.shape =', output.shape)
print (torch.allclose (output_einsum, output))
</code></pre>
<p>And here is its output:</p>
<pre><code class="lang-plaintext">1.7.1
output_einsum.shape = torch.Size([5, 3])
output.shape = torch.Size([5, 3])
True
</code></pre>
<p>(Note that your <code>target</code> has a trailing singleton dimension, while your<br>
<code>output</code> does not so I had to <code>squeeze()</code> the result of the matrix<br>
multiplication to make your code work.)</p>
<p>Best.</p>
<p>K. Frank</p>
Torch.where to change output in a customised loss function

username: MauroGentile
cooked: <p>Hi,<br>
in my customised Loss function, I need to change the value of net output in positions where a condition is met. And I must use operations that guarantee a correct  back propagation.</p>
<p>The code:<br>
<code>output[target != 1] = 1 - output[target != 1] </code><br>
throws an error: “RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation”</p>
<p>I am wandering if I can bypass this error by using bultin torch functions.</p>
<p>I tried to use:</p>
<pre><code class="lang-auto">output = torch.where(target != 1, 1 - output[target != 1] , output)
</code></pre>
<p>but I now get the error:</p>
<p>“RuntimeError: The size of tensor a (800) must match the size of tensor b (3686385) at non-singleton dimension 3”</p>
<p>I think this is due to the fact that in torch.where(condition, a, b)<br>
a and b must be scalars. They cannot be matrices. Is this correct?</p>
<p>Any suggestion for solving my problem?</p>

username: theevann
cooked: <p>You can very probably bypass the error by cloning output before changing it inplace:</p>
<pre><code class="lang-python">output = output.clone()
output[target != 1] = 1 - output[target != 1] 
</code></pre>
<p>The <code>where</code> function works with matrices but the sizes of the parameters have to be the same (or scalar). Here <code>1 - output[target != 1] </code> and <code>output</code> have different sizes. You should do:</p>
<pre><code class="lang-auto">output = torch.where(target != 1, 1 - output, output)
</code></pre>

username: MauroGentile
cooked: <aside class="quote no-group" data-username="MauroGentile" data-post="1" data-topic="117877">
<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/maurogentile/48/36952_2.png" class="avatar"> MauroGentile:</div>
<blockquote>
<p>“RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation”</p>
</blockquote>
</aside>
<p>But if I operate on a clone, does this operation back propagate?</p>
How to implement self-feedback in computational graph?

username: chatterboy
cooked: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/9/694500e6f591a535c63a32fa32bcd5c2592359a8.png" alt="image" data-base62-sha1="f1fYn9F7HtXpDRRIuCDXvHNRgSc" width="307" height="137"><br>
Computational graph can represent expressions as graphs. For example, <code>a+b</code> can be represented like the above figure. When we consider an expression with self-feedback, how can it be represented as a graph? For example, <code>dv(t)/dt=v(t)+i(t)</code> can be solved as <code>v(t)=v(t-1)f(t)+i(t)</code>. It is possible to construct a graph with right-hand side. But, we also have to store the final result to a state variable <code>v</code>. How to handle this issue?</p>
<p>Thank you.</p>

username: albanD
cooked: <p>I am not sure to understand what you’re trying to do here. Could you describe what you want to compute?</p>
Uniform sampling in multi-label classification problem

username: mwosinski
cooked: <p>Hi,</p>
<p>I work on a dataset with images and associated one-hot vectors of labels (code below). I would like to uniformly sample a label from the labels set which is consisted of 300 unique labels and then randomly pick an image whose one-hot vector possesses selected label. E.g. in first step I sample index=2 and in the second step I sample image with one-hot vector label = [0,0,1,0,1,…]. If I understand the documentation of built-in samplers correctly, they can be used only in map-style datasets. Should I write a custom dataloader or rather custom sampler for this kind of problem? Any help appreciated.</p>
<pre><code class="lang-auto">class datasetShop(Dataset):
    
    # df_csv - df with data description
    # root_dir - directory to the folder with images
    # transform - data transformation function
    
    def __init__(self, df_csv, root_dir, transform=None):
        
        self.df_csv = df_csv  
        self.root_dir = root_dir
        self.transform = transform
        
    def __len__(self):
        return len(self.df_csv)
    
    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
        
        img_name = self.df_csv['ID'].iloc[idx] + '.jpg'
        img_name = os.path.join(self.root_dir, img_name)
        
        image = Image.open(img_name)
    
        one_hot = torch.Tensor(self.df_csv['one_hot'].iloc[idx])
        
        if self.transform:
            image = self.transform(image)            
        
        sample = {'image':image, 'label':one_hot}

        return sample
</code></pre>

username: Hafplo
cooked: <p>I am dealing with a similar, but simpler issue.<br>
I would like to sample by label, without one-hot encoding.<br>
the labels are stored in some “df_csv”, and served by “<strong>getitem</strong>()”<br>
how can we point the sampler to the correct column in df_csv or otherwise sample according to items’ labels?<br>
<a class="mention" href="/u/ptrblck">@ptrblck</a></p>

username: ptrblck
cooked: <p>The sampler would be responsible to pass the sample indices to the <code>Dataset.__getitem__</code>, where you would be loading and processing the data.<br>
Inside <code>__getitem__</code> you could use the <code>index</code> and access the <code>df_csv</code> as needed. E.g. you could index it via <code>pandas</code>.</p>
Training slow down as epoch progress

username: cbd
cooked: <p>I have define some variables inside forward pass. Input is 3-dimension and its different channel is assigned to the different variable.</p>
<p>One channel is assigned as self.channel_1=self.input[:,0,:,:]</p>
<p>Second and third channel is assigned after initialization like below.<br>
self.channel_2 = torch.ones(1,1,256,256).to(self.device)<br>
self.channel_3 = torch.ones(1,1,256,256).to(self.device)</p>
<p>self.channel_2 = self.input[0,1,:,:]<br>
self.channel_3= self.input[0,2,:,:]</p>
<p>As epoch is progressed, training slow down.</p>
<p>(1) What is the difference between two assignment above? Is it the cause of slow down?<br>
(2) Can i use “torch.cuda.empty_cache()” at end of epoch to speed up next epoch?</p>
<p>I am using torch 1.4.0.</p>

username: ptrblck
cooked: <ol>
<li>
<p>I’m not sure which difference you are pointing to. But the issue sounds as if you are storing the computation graph in each iterations, which could slow down your code and would also be visible in an increasing memory usage.</p>
</li>
<li>
<p>No, <code>torch.cuda.empty_cache()</code> will potentially only slow down your code further since synchronizing <code>cudaMalloc</code> calls would be needed to re-allocate the memory, and won’t save any memory.</p>
</li>
</ol>

username: cbd
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="117814">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>I’m not sure which difference you are pointing to.</p>
</blockquote>
</aside>
<p>torch.ones(1,1,256,256).to(self.device) is not created for “self.channel_1” before assignment while for “self.channel_2” and “self.channel_3” it is created before assignment.</p>

username: ptrblck
cooked: <p>Thanks for explaining this.<br>
You could directly pass the <code>device</code> to the tensor creation: <code>torch.ones(size, device=device)</code>, which might be faster than creating these values on the CPU and push them to the GPU.<br>
Were you able to check the memory usage and did you see an increased usage?</p>
Multi-label binary classification: result type Float can't be cast to the desired output type Long

username: BramVanroy
cooked: <p>Doing multi-label binary classification with BCEWithLogitsLoss, but I get a RunTimeError “RuntimeError: result type Float can’t be cast to the desired output type Long”.  Running 1.8.1 on Windows but the problem also occurred on 1.4. Am I doing something wrong with respect to multi-labels, perhaps?</p>
<pre><code class="lang-python">from torch import FloatTensor, LongTensor
from torch.nn import BCEWithLogitsLoss


criterion = BCEWithLogitsLoss()

logits = FloatTensor([[ 0.1144, -0.3313, -0.1003],
                      [-0.0207, -0.2222, -0.0905],
                      [-0.1814, -0.0793, -0.1249],
                      [ 0.0079, -0.3403,  0.0057]])
labels = LongTensor([[0, 1, 0],
                     [0, 0, 1],
                     [0, 0, 1],
                     [0, 1, 1]])

loss = criterion(logits, labels)
</code></pre>

username: BramVanroy
cooked: <p>Seems like the solution is to explicitly cast <code>labels.float()</code> although that seems quite counter-intuitive for class labels.</p>

username: KFrank
cooked: <p>Hi Bram!</p>
<p>Two comments:</p>
<aside class="quote no-group" data-username="BramVanroy" data-post="2" data-topic="117915" data-full="true">
<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/bramvanroy/48/28628_2.png" class="avatar"> BramVanroy:</div>
<blockquote>
<p>Seems like the solution is to explicitly cast <code>labels.float()</code></p>
</blockquote>
</aside>
<p>First, as you’ve seen, <code>BCEWithLogitsLoss</code> requires its <code>target</code> to<br>
be a <code>float</code> tensor, not <code>long</code> (or a <code>double</code> tensor, if the <code>input</code> is<br>
<code>double</code>).  And yes, converting to <code>float</code> (<code>labels.float()</code>) is the<br>
correct solution.</p>
<aside class="quote no-group">
<blockquote>
<p>that seems quite counter-intuitive for class labels.</p>
</blockquote>
</aside>
<p>Second, as to <em>why:</em>  Unlike pytorch’s <code>CrossEntropyLoss</code>,<br>
<code>BCEWithLogitsLoss</code> supports labels that are probabilities (sometimes<br>
called “soft” labels).  Thus, a label could be <code>0.333</code>.  This would indicate<br>
that the sample has a 33.3% of being in the “1”-class (or “yes”-class)<br>
and therefore a 66.7% change of being in the “0”-class (“no”-class).<br>
So this is probably a “no”, but a value of <code>0.00</code> would be a “hard”<br>
(non-probabilistic, fully-certain) “no.”</p>
<p>(Note that probabilistic labels make perfect sense for cross entropy,<br>
as well.  It’s just that pytorch’s <code>CrossEntropyLoss</code> chooses not to<br>
support them (although perhaps it should).  Doing so would require<br>
<code>CrossEntropyLoss</code> to accept a <code>target</code> with a different shape, namely,<br>
with a class dimension.)</p>
<p>Best.</p>
<p>K. Frank</p>
Annotating Semantic Segmentation Pytorch

username: urbeckmd
cooked: <p>I am trying to follow Pytorch Semantic Segmentation documentation on my own dataset (link to documentation: <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 1.8.1+cu102 documentation</a>). When using the PennDataset that the tutorial uses, I get a functioning model. I’ve made two datasets of completely different images for two different applications. I annotated the first set of images myself using Labelme, and the model functioned correctly. But when I annotated the second dataset the same way I get the error: ‘ValueError: zero-size array to reduction operation minimum which has no identity’. I am using the exact same model file for all three datasets, so I know it’s not the model that is wrong; it must be the data. I changed the masks to have pixel values equal to the identity of the object in the image (1 for first object, 2 for second, etc). I cant figure out why the same model works for two datasets but not the third. The annotation process was very long, so is there a simpler way to annotate the images with only a few steps?</p>

username: urbeckmd
cooked: <p>Solution: I needed to convert the mask to “L” mode using .convert(“L”).</p>
Replacing elements in a tensor?

username: scheon
cooked: <p>Hi, I want to replace elements in a tensor according to certain standard<br>
for example, let’s say a tensor has elements ranging from 0 to 10<br>
and I want to replace values 0~3 to 0, value 4~7 to 1, and 8~10 to 2.<br>
Is there any function that can do this job?</p>

username: Eta_C
cooked: <pre><code class="lang-python">tensor = torch.arange(11).float()
tensor = torch.floor(tensor / 4)
</code></pre>
How configure the size of dataset?

username: sangheonlee
cooked: <p>Hi,</p>
<p>I was running GNMTv2 using pytorch. But I just want to check the 1 iteration time(Forward ~ Optimize). Now If data was loaded, It automatically grabs the size of dataset and it runs many times.</p>
<p>I want to know how can I change the dataset size.</p>
<p>Thanks for reading this.</p>

username: ptrblck
cooked: <p>The length of a <code>Dataset</code> is defined in it’s <code>__len__()</code> method and you could override it with a custom method returning your desired length or alternatively implement a new custom <code>Dataset</code> with the desired length.</p>

username: sangheonlee
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks to helping me.</p>