<h2 dir="auto"><g-emoji class="g-emoji" alias="rocket" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f680.png">🚀</g-emoji> Feature</h2>
<p dir="auto">I would like to see a PyTorch feature to generate a dataset, ideally Pandas DataFrame with the image name as index and lists of labels for each image.</p>
<h2 dir="auto">Motivation</h2>
<p dir="auto">For multi-label image classification, it is important to generate the dataset from scratch given a custom datasets. Most likely, the images would be duplicated in multiple folder if the image has multiple label, similar to how <code class="notranslate">ImageFolder</code> works. Unfortunately, I have not seen any of such features offered in most PyTorch implementation.</p>
<h2 dir="auto">Pitch</h2>
<p dir="auto">Multi-label image classification is definitely an important topic as most images can be classified with multiple labels. It is also much easier to implement compared to object detection if such level of detection is not required.</p>
<h2 dir="auto">Alternatives</h2>
<p dir="auto">@SpandanMadan had proposed a <a href="https://discuss.pytorch.org/t/multi-label-classification-in-pytorch/905/26" rel="nofollow">working solution</a>. I would like to clean it up and make it consistent with PyTorch implementation.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/fmassa/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/fmassa">@fmassa</a></p>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">When adding a tensor to another tensor <code class="notranslate">c</code> at some <strong>non-unique indices</strong>, we end up with an incorrect tensor. The output is stochastic (demonstrated below).</p>
<p dir="auto">This bug is also there when writing to <code class="notranslate">c</code> (instead of adding); I.e., <code class="notranslate">c1[b] = a; c2[b] = a</code>.</p>
<h2 dir="auto">To Reproduce</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
import numpy as np

def torch_indexing_test(device):
    a = torch.arange(10000).float().to(device)
    b = torch.tensor(np.random.choice(100, 10000)).to(device).long()
    
    c1 = torch.zeros(100).to(device)
    c2 = torch.zeros(100).to(device)

    c1[b] += a
    c2[b] += a

    print(torch.max(c1-c2)) # should be zero

if __name__ == '__main__':
    torch_indexing_test(device=&quot;cpu&quot;)
    torch_indexing_test(device=&quot;cuda:0&quot;)

    # print output:
    # tensor(0.)
    # tensor(9307., device='cuda:0')"><pre class="notranslate"><code class="notranslate">import torch
import numpy as np

def torch_indexing_test(device):
    a = torch.arange(10000).float().to(device)
    b = torch.tensor(np.random.choice(100, 10000)).to(device).long()
    
    c1 = torch.zeros(100).to(device)
    c2 = torch.zeros(100).to(device)

    c1[b] += a
    c2[b] += a

    print(torch.max(c1-c2)) # should be zero

if __name__ == '__main__':
    torch_indexing_test(device="cpu")
    torch_indexing_test(device="cuda:0")

    # print output:
    # tensor(0.)
    # tensor(9307., device='cuda:0')
</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">I would expect the printed output for the code when ran on both cpu and gpu to be <code class="notranslate">0</code> (meaning no stochasticity), but the output is non-zero for the gpu code.</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">PyTorch version: 1.4.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 10.1</p>
<p dir="auto">OS: Microsoft Windows 10 Home<br>
GCC version: (Rev5, Built by MSYS2 project) 5.3.0<br>
CMake version: Could not collect</p>
<p dir="auto">Python version: 3.7<br>
Is CUDA available: Yes<br>
CUDA runtime version: 10.1.105<br>
GPU models and configuration: GPU 0: GeForce GTX 1080 Ti<br>
Nvidia driver version: 441.22<br>
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\cudnn64_7.dll</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip] numpy==1.18.1<br>
[pip] torch==1.4.0<br>
[pip] torchvision==0.5.0<br>
[conda] blas                      1.0                         mkl<br>
[conda] libblas                   3.8.0                    14_mkl    conda-forge<br>
[conda] libcblas                  3.8.0                    14_mkl    conda-forge<br>
[conda] liblapack                 3.8.0                    14_mkl    conda-forge<br>
[conda] liblapacke                3.8.0                    14_mkl    conda-forge<br>
[conda] mkl                       2019.4                      245<br>
[conda] mkl-service               2.3.0            py37hb782905_0<br>
[conda] mkl_fft                   1.0.15           py37h14836fe_0<br>
[conda] mkl_random                1.1.0            py37h675688f_0<br>
[conda] pytorch                   1.4.0           py3.7_cuda101_cudnn7_0    pytorch<br>
[conda] torchvision               0.5.0                py37_cu101    pytorch</p>