AttributeError: 'tuple' object has no attribute 'size'

idontknow: <p>Hi, I am working on omniglot images and I am struggling with this error. I believe the problem is with my dataset generation. Please help me figure it out. Here is the dataset code:</p>
<pre><code class="lang-auto">import os
from PIL import Image
import numpy as np
from torch.utils.data import Dataset
import torch
from torchvision import datasets, transforms
class Dataset(Dataset):
    def __init__(self,data_txt,transformm):
        location_file = open(data_txt, 'r')
        locations = location_file.read().split()
        self.filenames = locations
        self.labels = []
        self.images = []
        self.transform = transformm
        self.path = os.getcwd()
        for address in locations:
            #label creation
            label = address.split('/')[0]
            self.labels.append(label)


    def __len__(self):
        return len(self.filenames)

    def __getitem__(self, idx):
        image = Image.open(os.path.join(self.path,'images_background', self.filenames[idx]))
        image = self.transform(image)
        return image, self.labels[idx]
</code></pre>
<p>Here is the full traceback<br>
Traceback (most recent call last):<br>
File “-/Documents/Code/AI/AI_Learning/Omniglot2/omniglot.py”, line 79, in <br>
loss = crtieria(outputs, labels)<br>
File “-AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”, line 541, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “-\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\loss.py”, line 916, in forward<br>
ignore_index=self.ignore_index, reduction=self.reduction)<br>
File “-\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py”, line 2009, in cross_entropy<br>
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)<br>
File “-\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py”, line 1834, in nll_loss<br>
if input.size(0) != target.size(0):<br>
AttributeError: ‘tuple’ object has no attribute ‘size’</p>

ptrblck: <p>Could you please post the stack trace so that we can have a better look at your issue?<br>
I cannot find anything obviously wrong in the current code snippet.</p>

idontknow: <p>I edited the original post you should be able to see it. Do you need the Network code aswell?</p>

ptrblck: <p>Could you check the <code>type</code> of <code>outputs</code> and <code>labels</code>?<br>
I guess <code>labels</code> is passed as a <code>tuple</code> instead of a tensor.<br>
If so, you could return <code>labels</code> in your <code>__getitem__</code> as:</p>
<pre><code class="lang-python">torch.tensor(self.labels[idx])
</code></pre>

idontknow: <p>I still have a question, when I make my labels to a tensor like that it gives me error about labels being a string(I know that they are) and I am suppose to make them integers right? Labels must be integers?<br>
Do you have any suggestions for it?</p>

ptrblck: <p>Yes, <code>nn.CrossEntropyLoss</code> expects the target to be a <code>LongTensor</code> containing the class indices in the range <code>[0, nb_classes-1]</code>.<br>
If your current labels are stored as string, you might want to use a <code>dict</code> and map these strings to the corresponding indices.</p>

nickvu: <p>I have a similar issue to this <a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/idontknow">@idontknow</a><br>
I have a custom image dataset modeled closely after this official <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener"> pytorch tutorial</a>.</p>
<p>The dataset passes a PIL image to a transform function:</p>
<pre><code class="lang-auto">class Resize(object):
    """Resize the image in a sample to a given size.
    Args:
        output_size (int): Desired size in pixels. Yields output_size by output_size image
    """

    def __init__(self, output_size):
        assert isinstance(output_size, int)
        self.output_size = output_size

    def __call__(self, sample):
        image = sample

        resized_image = image.resize((self.output_size, self.output_size))

        return resized_image
</code></pre>
<p>At the point where PIL Image.resize() is called, I get this error:</p>
<p>File “test.py”, line 52, in <br>
for samples in train_dataloader:<br>
File “/Users/nick/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 345, in <strong>next</strong><br>
data = self._next_data()<br>
File “/Users/nick/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 385, in _next_data<br>
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
File “/Users/nick/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/Users/nick/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/Users/nick/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataset.py”, line 207, in <strong>getitem</strong><br>
return self.datasets[dataset_idx][sample_idx]<br>
File “/Users/nick/Projects/Personal_Projects/CNN/datasets/LDataset.py”, line 52, in <strong>getitem</strong><br>
sample = self.transform(sample)<br>
File “/Users/nick/opt/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py”, line 70, in <strong>call</strong><br>
img = t(img)<br>
File “/Users/nick/Projects/Personal_Projects/CNN/datasets/LTransforms.py”, line 24, in <strong>call</strong><br>
resized_image = image.resize((self.output_size, self.output_size))<br>
AttributeError: ‘tuple’ object has no attribute ‘resize’</p>

nickvu: <p>OK, this was the problem. Following the <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener">pytorch tutorial</a> for making a custom dataset, my dataset would pass a sample as a dictionary: <code>{'image': image, 'label': label}</code>.<br>
Then the custom transforms would unpack this and access the image as sample[‘image’].</p>
<p>Interestingly, before being put in the dictionary, type(image) would be &lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=341x341 at 0x12D907210&gt; as expected. However, when retrieving it from sample[‘image’] it becomes: (&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=341x341 at 0x12D907210&gt;,).</p>
<p>So it got converted into a tuple for some reason. Accessing the 0th element of the tuple and passing it along to transform fixes the issue.</p>

hsynakcay: <p><span class="hashtag">#KÜTÜPHANELER</span></p>
<p>import os<br>
from pathlib import Path<br>
from tqdm import tqdm<br>
from easydict import EasyDict as edict</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
import torchvision.utils as vutils<br>
from torchvision import transforms as trans</p>
<p>from data.ms1m import get_train_loader<br>
from data.lfw import LFW</p>
<p>from backbone.arcfacenet import SEResNet_IR<br>
from margin.ArcMarginProduct import  ArcMarginProduct</p>
<p>from util.utils import save_checkpoint, test<br>
<span class="hashtag">#KONFİGÜRASYON</span></p>
<p>conf = edict()<br>
conf.train_root = “dataset/MS1M/MS1M”<br>
conf.lwf_test_root = “dataset/lfw_aligned_112/lfw_aligned_112”<br>
conf.lwf_file_list =“dataset/lfw_pair.txt”</p>
<p>conf.mode =“se_ir”   <span class="hashtag">#model</span><br>
conf.depth = 50            <span class="hashtag">#derinlik</span><br>
conf.margin_type = “ArcFace”<br>
conf.feature_dim = 512<br>
conf.scale_siz = 32.0<br>
conf.batch_size =96 # bir seferde 96 resim yüklenecek<br>
conf.lr = 0.01 <span class="hashtag">#modele</span> daha az resim verilirse lr’ düşürülmeli<br>
conf.milestones = [8,10,12]<br>
conf.total_epoch = 14 #  model 14 defa resimlerin üstünde geçecek 14 iterasyon sayısı</p>
<p>conf.save_folder="./saved"<br>
conf.save_dir = os.path.join(conf.save_folder ,conf.mode +’_’ + str(conf.depth))</p>
<p>conf.device = torch.device (“cuda:0” if torch.cuda.is_available() else “cpu”) # modelin nerede çalışacağını söylüyoruz</p>
<p>conf.num_workers = 4<br>
conf.pin_memory = True</p>
<p>os.makedirs(conf.save_dir,exist_ok=True)</p>
<p><span class="hashtag">#VERİ</span> YÜKLEME</p>
<p>transform = trans.Compose([<br>
trans.ToTensor(),<br>
trans.Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5))<br>
])<br>
trainloader = class_num = get_train_loader(conf)</p>
<p>print("number of id ",class_num)</p>
<p>print(trainloader.dataset)</p>
<p>################################################################<br>
Traceback (most recent call last):<br>
File “C:/Users/hsyna/Desktop/ArcFace/train.py”, line 59, in <br>
print(trainloader.dataset[0])<br>
AttributeError: ‘tuple’ object has no attribute ‘dataset’</p>
<p>I have an error too, how do I fix it?</p>

ptrblck: <p>Your <code>get_train_loader</code> method seems to return a <code>tuple</code> instead of a <code>DataLoader</code> instance, so you would either have to change the return statement or index the returned <code>tuple</code>.<br>
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>

MANISH_GOYAL: <p>from advertorch.attacks import LinfPGDAttack<br>
import torch.nn as nn<br>
import numpy as np</p>
<p>use_cuda = torch.cuda.is_available()<br>
device = torch.device(“cuda” if use_cuda else “cpu”)</p>
<h1>
<a name="printuse_cuda" class="anchor" href="#printuse_cuda"></a>print(use_cuda)</h1>
<p>model.to(device)<br>
torch.cuda.empty_cache();</p>
<p>model = model.eval()</p>
<h1>
<a name="x-torchfrom_numpytrain_image0asnumpy" class="anchor" href="#x-torchfrom_numpytrain_image0asnumpy"></a>x = torch.from_numpy(train_image[0].asnumpy())</h1>
<h1>
<a name="y-torchfrom_numpyclass_ids0" class="anchor" href="#y-torchfrom_numpyclass_ids0"></a>y = torch.from_numpy(class_ids[0])</h1>
<p>x = torch.Tensor(batch[0][0].asnumpy())<br>
y = torch.Tensor(batch[6][0].asnumpy())<br>
print(type(x))</p>
<h1>
<a name="printx-batch00" class="anchor" href="#printx-batch00"></a>print("x = ",batch[0][0])</h1>
<h1>
<a name="printtypey" class="anchor" href="#printtypey"></a>print(type(y))</h1>
<h1>
<a name="printy-batch60" class="anchor" href="#printy-batch60"></a>print("y = ",batch[6][0])</h1>
<p>adversary = LinfPGDAttack(<br>
model, loss_fn=nn.BCEWithLogitsLoss(reduction=“none”), eps=0.3,<br>
nb_iter=40, eps_iter=0.01, rand_init=False, clip_min=0.0, clip_max=1.0,<br>
targeted=False)<br>
<span class="hashtag">#nn</span>.CrossEntropyLoss(reduction=“sum”)</p>
<p>x = torch.unsqueeze(x,0)</p>
<p>adv_untargeted = adversary.perturb(x, y)</p>
<p>Output:</p>
<p>&lt;class ‘torch.Tensor’&gt;</p>
<hr>
<p>AttributeError                            Traceback (most recent call last)<br>
 in <br>
30 x = torch.unsqueeze(x,0)<br>
31<br>
—&gt; 32 adv_untargeted = adversary.perturb(x, y)</p>
<p>~.conda\envs\final\lib\site-packages\advertorch\attacks\iterative_projected_gradient.py in perturb(self, x, y)<br>
181                 x + delta.data, min=self.clip_min, max=self.clip_max) - x<br>
182<br>
 → 183         rval = perturb_iterative(<br>
184             x, y, self.predict, nb_iter=self.nb_iter,<br>
185             eps=self.eps, eps_iter=self.eps_iter,</p>
<p>~.conda\envs\final\lib\site-packages\advertorch\attacks\iterative_projected_gradient.py in perturb_iterative(xvar, yvar, predict, nb_iter, eps, eps_iter, loss_fn, delta_init, minimize, ord, clip_min, clip_max, l1_sparsity)<br>
64     for ii in range(nb_iter):<br>
65         outputs = predict(xvar + delta)<br>
—&gt; 66         loss = loss_fn(outputs, yvar)<br>
67         if minimize:<br>
68             loss = -loss</p>
<p>~.conda\envs\final\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
720             result = self._slow_forward(*input, **kwargs)<br>
721         else:<br>
 → 722             result = self.forward(*input, **kwargs)<br>
723         for hook in itertools.chain(<br>
724                 _global_forward_hooks.values(),</p>
<p>~.conda\envs\final\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)<br>
626<br>
627     def forward(self, input: Tensor, target: Tensor) → Tensor:<br>
 → 628         return F.binary_cross_entropy_with_logits(input, target,<br>
629                                                   self.weight,<br>
630                                                   pos_weight=self.pos_weight,</p>
<p>~.conda\envs\final\lib\site-packages\torch\nn\functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)<br>
2535         reduction_enum = _Reduction.get_enum(reduction)<br>
2536<br>
 → 2537     if not (target.size() == input.size()):<br>
2538         raise ValueError(“Target size ({}) must be the same as input size ({})”.format(target.size(), input.size()))<br>
2539</p>
<p>AttributeError: ‘tuple’ object has no attribute ‘size’</p>
<p>Please Help ! <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

ptrblck: <p>Based on the error message it seems you are also passing the model output or target as a <code>tuple</code> while a tensor is expected. Check the <code>type()</code> of both objects and make sure they are valid tensors before passing it to the criterion.</p>

MANISH_GOYAL: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/d/dd2f00231ac4060fb3adb07ff97c3cd9b329adfb.png" data-download-href="https://discuss.pytorch.org/uploads/default/dd2f00231ac4060fb3adb07ff97c3cd9b329adfb" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/d/dd2f00231ac4060fb3adb07ff97c3cd9b329adfb.png" alt="Capture" data-base62-sha1="vyGdAqapMZpokT0uhm1ENv2UIDp" width="353" height="500" data-dominant-color="F2F2F2"><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">Capture</span><span class="informations">586×830 20 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></p>
<p>This is the value of x and y and their types that i’m passing. x being the input image and y as labels. It seems they are both tensors as needed.</p>
<p>Thank You so much for a quick response sir <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>In that case the model output is most likely creating the issue. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

MANISH_GOYAL: <p>Thank You Sir ! I’ll check that <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

MANISH_GOYAL: <p>The function is an inbuilt function in advertorch library of pytorch and the attack that I’m trying to do using that is PGD. Would you be able to explain a little what type of output should I expect from my model. Sorry, I am new to pytorch and tensorflows.</p>

ptrblck: <p>I’m not familiar with <code>advertorch</code> and don’t know which outputs are expected, so you could either check the implementation of the model in their repository as well as the documentation or check the output type manually.<br>
I’m still unsure, if the error is raised by <code>LinfPGDAttack</code> or if your model just returns a <code>tuple</code> while tensors are expected, so you might also want to check the return type of it.</p>

MANISH_GOYAL: <p>Ok Sir ! Thank You ! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

bowen_wu: <p>Hi, I’m using custom dataset inherited from torch.utils.data.Dataset and DataLoader.<br>
Here is the code snippet and the output.</p>
<p>Note there is a change of type before and after variable assignment.</p>
<pre><code class="lang-auto">def __getitem__(self, idx):
        chunk = self.chunks[idx]

        print("type:", type(chunk["gesture"][:self.t_prev_20fps+self.window_len_20fps].T))
        gesture = chunk["gesture"][:self.t_prev_20fps+self.window_len_20fps].T,
        print("assigned type:", type(gesture))
        assert 0

Output:
type: &lt;class 'numpy.ndarray'&gt;
assigned type: &lt;class 'tuple'&gt;
</code></pre>
<p>The variable ‘gesture’ was unintendedly modified to be a tuple. Though using index 0 fixes the issue, could anyone tell me why this is happening?</p>
<p>Is this relevant to the return type of dict?</p>

ptrblck: <p>You are appending a comma after the <code>gesture</code> definition:</p>
<pre><code class="lang-python">gesture = chunk["gesture"][:self.t_prev_20fps+self.window_len_20fps].T,
</code></pre>
<p>Remove it and the <code>tuple</code> shouldn’t be created.</p>