AttributeError: 'dict' object has no attribute 'self'

sugh7020: <p>I have dataset class and dataloader as:</p>
<pre><code class="lang-auto">class SomeDataset(Dataset):
    def __init__(self, usage='val', dataset_pickle_file='./some.pkl', skip_every_n_image=1):
        super(SomeDataset, self).__init__()

        self.to_tensor = transforms.ToTensor()

        with open(dataset_pickle_file, 'rb') as file:
            self.data_info = pickle.load(file)[usage]
        self.idx = [i for i in range(0, len(self.data_info[0]), skip_every_n_image)]
        self.data_len = len(self.idx)

    def __len__(self):
        return self.data_len

    def __getitem__(self, index):  #1
        instanceNum = self.idx[index]
        color_img = self.data_info[0][instanceNum]
        color_imgPIL = Image.open(color_img)
        color_tensor = self.to_tensor(np.array(color_imgPIL))
        d_img = self.data_info[1][instanceNum] #2
        d_imgPIL = Image.open(d_img)
        d_tensor = self.to_tensor(np.array(d_imgPIL))
        output = {'image': color_tensor, 'dt': d_tensor}
        return output
</code></pre>
<pre><code class="lang-auto">train_dataloader = DataLoader(train_dataset, shuffle=True,
                                  batch_size=args.batch_size,
                                  num_workers=1)
d_net = ExtendedDnet().cuda()
optimizer = torch.optim.Adam(depth_net.parameters(), lr=args.learning_rate)
for epoch in range(2):
        d_net.train()
        for train_idx, train_data in enumerate(train_dataloader):
                 image = train_data['image'].cuda(non_blocking=True)
</code></pre>
<p>I am running the code in debug mode on PyCharm.<br>
Just at the last line I am getting this error:</p>
<blockquote>
<p>File “~/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_resolver.py”, line 215, in resolve<br>
return getattr(dict, key)<br>
AttributeError: ‘dict’ object has no attribute ‘self’</p>
</blockquote>
<p>Can you all please suggest what might be the issue? Thanks for the help!</p>

Valerio_Biscione: <p>Can you give the full error stack trace?</p>
<p>Anyway, I think your error is in  <code>color_img = self.data_info[0][instanceNum]</code> this line, but it’s difficult to say without knowing what data_info contains</p>

sugh7020: <p>Hi Valerio, Thanks for helping!</p>
<pre><code class="lang-auto">self.data_info[0][instanceNum]
</code></pre>
<p>contains:</p>
<blockquote>
<p>‘/media/storage2/~/train/color_007431.png’</p>
</blockquote>
<p>So basically self.data_info[0] contains location of RGB images, self.data_info[1] contains depth images in local system and usage refers to train or validation.</p>
<p>Can you please mention how I can obtain the full error stack trace?<br>
I just see this in the console:</p>
<blockquote>
<p>File “~/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_resolver.py”, line 215, in resolve<br>
return getattr(dict, key)<br>
AttributeError: ‘dict’ object has no attribute ‘self’</p>
</blockquote>

Valerio_Biscione: <p>Weird, I have tried a simplified version of your script and it works for me:</p>
<pre><code class="lang-auto">import pickle
import torchvision.transforms as transforms
from torch.utils.data import Dataset
from PIL import Image
import numpy as np
from torch.utils.data import DataLoader


class SomeDataset(Dataset):
    def __init__(self, usage='val', dataset_pickle_file='./some.pkl', skip_every_n_image=1):
        super(SomeDataset, self).__init__()

        self.to_tensor = transforms.ToTensor()
        self.data_info = {}
        self.data_info[0] = ['./data/Leek2D/Renderings/S1_DD2.bmp', './data/Leek2D/Renderings/S1_DS2.bmp']
        self.data_info[1] = ['./data/Leek2D/Renderings/S1_DD2.bmp', './data/Leek2D/Renderings/S1_DS2.bmp']

        self.idx = [i for i in range(0, len(self.data_info[0]), skip_every_n_image)]
        self.data_len = len(self.idx)

    def __len__(self):
        return self.data_len

    def __getitem__(self, index):  #1
        instanceNum = self.idx[index]
        color_img = self.data_info[0][instanceNum]
        color_imgPIL = Image.open(color_img)
        color_tensor = self.to_tensor(np.array(color_imgPIL))
        d_img = self.data_info[1][instanceNum] #2
        d_imgPIL = Image.open(d_img)
        d_tensor = self.to_tensor(np.array(d_imgPIL))
        output = {'image': color_tensor, 'dt': d_tensor}
        return output


train_dataloader = DataLoader(SomeDataset(), shuffle=True,
                                  batch_size=1,
                                  num_workers=0)

for epoch in range(2):
        # d_net.train()
        for train_idx, train_data in enumerate(train_dataloader):
                 image = train_data['image']
</code></pre>
<p>Can you try it? (Change the self.data_info into some image you have on disk)</p>