'bool' object is not callable, when launch dataloader

Hello. I’m trying to build a dataloader for the object detection task. I have the following class:

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

And initialize the dataloader as follows:

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
)

when I try to get a batch:
images, targets = next(iter(train_dataLoader))

I get the following error without explanation:


TypeError Traceback (most recent call last)
in <cell line: 1>()
----> 1 images, targets = next(iter(train_dataLoader))
TypeError: ‘bool’ object is not callable

I think it’s not about the class because I can print the result of the function “getitem

        print(f'TARGET: {target}')
        print(f'IMAGE: {image}')

and I get the following (batch=2):
images, targets = next(iter(train_dataLoader))

The function result looks correct and I don’t understand where the error is. I ask for your help in finding the error.

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

Will it be convenient if I provide access to the colab: Google Colab? There the code is conveniently divided into blocks

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.

Be careful when defining variable names.