'bool' object is not callable, when launch dataloader

Ruslan_Salikhzianov: <p>Hello. I’m trying to build a dataloader for the object detection task. I have the following class:</p>
<pre><code class="lang-auto">class TrafficLightsDataset(Dataset):
    def __init__(self, df, transforms=None):
        super().__init__()

        # Image_ids will be the "Filename" here
        self.image_ids = df.image_id.unique()
        self.df = df
        self.transforms = transforms

    def __len__(self):
        return self.image_ids.shape[0]

    def __getitem__(self, index):
        image_id = self.image_ids[index]
        records = self.df[self.df.image_id == image_id]

        # Reading Image
        image = cv2.imread(image_id)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)
        image /= 255.0

        # Bounding Boxes
        boxes = records[['xmin','ymin','xmax','ymax']].values
        boxes = torch.as_tensor(boxes.astype(int),dtype=torch.float32)

        # Area of the bounding boxes
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        area = torch.as_tensor(area, dtype=torch.float32)

        # Labels of the object detected
        labels = torch.as_tensor(records.label.values.astype(int), dtype=torch.int64)

        iscrowd = torch.zeros_like(labels, dtype=torch.int64)

        target = {}
        target['boxes'] = boxes
        target['labels'] = labels
        target['image_id'] = torch.tensor([index])
        target['area'] = area
        target['iscrowd'] = iscrowd

        if self.transforms:
            sample = {
                'image': image,
                'bboxes': target['boxes'],
                'labels': labels
            }

            sample = self.transforms(**sample)
            image = sample['image']

            target['boxes'] = torch.as_tensor(sample['bboxes'],dtype=torch.float32)
            target['labels'] = torch.as_tensor(sample['labels'])

        print(f'TARGET: {target}')
        print(f'IMAGE: {image}')
        return image, target
</code></pre>
<p>And initialize the dataloader as follows:</p>
<pre><code class="lang-auto">train_dataset = TrafficLightsDataset(train_df, transform)
train_dataLoader = DataLoader(
    train_dataset,
    batch_size=BATCH_SIZE,
    shuffle=True,
    num_workers=NUM_WORKERS,
    collate_fn=utils.collate_fn
)
</code></pre>
<p>when I try to get a batch:<br>
<code>images, targets = next(iter(train_dataLoader))</code></p>
<p>I get the following error without explanation:</p>
<blockquote>
<hr>
<p>TypeError                                 Traceback (most recent call last)<br>
 in &lt;cell line: 1&gt;()<br>
----&gt; 1 images, targets = next(iter(train_dataLoader))<br>
TypeError: ‘bool’ object is not callable</p>
</blockquote>
<p>I think it’s not about the class because I can print the result of the function “<strong>getitem</strong>”</p>
<pre><code class="lang-auto">        print(f'TARGET: {target}')
        print(f'IMAGE: {image}')
</code></pre>
<p>and I get the following (batch=2):<br>
<code>images, targets = next(iter(train_dataLoader))</code></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/2/521bd415b637adeda8f4569083f969b5e5c5fc9a.png" data-download-href="https://discuss.pytorch.org/uploads/default/521bd415b637adeda8f4569083f969b5e5c5fc9a" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/521bd415b637adeda8f4569083f969b5e5c5fc9a_2_690x426.png" alt="image" data-base62-sha1="bImM2dfSsMres9aDPdQnQbMfUFA" width="690" height="426" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/521bd415b637adeda8f4569083f969b5e5c5fc9a_2_690x426.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/521bd415b637adeda8f4569083f969b5e5c5fc9a_2_1035x639.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/521bd415b637adeda8f4569083f969b5e5c5fc9a_2_1380x852.png 2x" data-dominant-color="3E3E3E"><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">image</span><span class="informations">2160×1334 369 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><strong>The function result looks correct and I don’t understand where the error is. I ask for your help in finding the error.</strong></p>

ptrblck: <p>Could you post a minimal and executable code snippet reproducing the issue as it’s unclear which line of code fails. E.g. your <code>transforms</code> argument could be a <code>bool</code> while transformations are expected.</p>

Ruslan_Salikhzianov: <p>Will it be convenient if I provide access to the colab: <a href="https://colab.research.google.com/drive/1XCJJ7tbhnhzeLogmJjXa5CXHZzHIGkGU?usp=sharing" class="inline-onebox" rel="noopener nofollow ugc">Google Colab</a>? There the code is conveniently divided into blocks</p>

Ruslan_Salikhzianov: <p>I found the problem. The fact is that I defined the “next” variable at the data preparation stage as a bool value, which is why the reserved “next()” function was erased.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a80595c8c72abea8eb33729b77c784d327110a0.png" data-download-href="https://discuss.pytorch.org/uploads/default/5a80595c8c72abea8eb33729b77c784d327110a0" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a80595c8c72abea8eb33729b77c784d327110a0_2_690x73.png" alt="image" data-base62-sha1="cUBXscWDnNgitmd99LpwGYqmoKI" width="690" height="73" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a80595c8c72abea8eb33729b77c784d327110a0_2_690x73.png, https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a80595c8c72abea8eb33729b77c784d327110a0.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a80595c8c72abea8eb33729b77c784d327110a0.png 2x" data-dominant-color="323334"><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">image</span><span class="informations">980×104 9.83 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>Be careful when defining variable names.</p>