Dataloader throwing randint argument missing error

Hi, this is my code for training an image dataset.

batch_size=20

    data_loader = data.DataLoader(dataset, batch_size,
                                  num_workers=args.num_workers,
                                  shuffle=True,
                                  collate_fn=collate_fn,
                                  pin_memory=False)
    
for step,(img_pre, img_next, boxes_pre, boxes_next, labels, valid_pre, valid_next) in enumerate(data_loader):
        if args.cuda:
            print("cuda is true..")
            img_pre = Variable(img_pre.cuda())
            img_next = Variable(img_next.cuda())
            boxes_pre = Variable(boxes_pre.cuda())
            boxes_next = Variable(boxes_next.cuda())
            valid_pre = Variable(valid_pre.cuda(), volatile=True)
            valid_next = Variable(valid_next.cuda(), volatile=True)
            labels = Variable(labels.cuda(), volatile=True)

        else:
            img_pre = Variable(img_pre)
            img_next = Variable(img_next)
            boxes_pre = Variable(boxes_pre)
            boxes_next = Variable(boxes_next)
            valid_pre = Variable(valid_pre)
            valid_next = Variable(valid_next)
            labels = Variable(labels, volatile=True)


        # forward
        t0 = time.time()
        out = net(img_pre, img_next, boxes_pre, boxes_next, valid_pre, valid_next)

        optimizer.zero_grad()
        loss_pre, loss_next, loss_similarity, loss, accuracy_pre, accuracy_next, accuracy, predict_indexes = criterion(out, labels, valid_pre, valid_next)

        loss.backward()
        optimizer.step()
        t1 = time.time()

        all_epoch_loss += [loss.data.cpu()]

I am getting an error after one iteration :

TypeError                                 Traceback (most recent call last)
<ipython-input-25-418aca395a0e> in <module>()
   327 
   328 if __name__ == '__main__':
--> 329     train()

<ipython-input-25-418aca395a0e> in train()
   154     #img_pre, img_next, boxes_pre, boxes_next, labels, valid_pre, valid_next=next(batch_iterator)
   155 
--> 156     for step,(img_pre, img_next, boxes_pre, boxes_next, labels, valid_pre, valid_next) in enumerate(data_loader):
   157         if args.cuda:
   158             print("cuda is true..")

~/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
   334                 self.reorder_dict[idx] = batch
   335                 continue
--> 336             return self._process_next_batch(batch)
   337 
   338     next = __next__  # Python 2 compatibility

~/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
   355         self._put_indices()
   356         if isinstance(batch, ExceptionWrapper):
--> 357             raise batch.exc_type(batch.exc_msg)
   358         return batch
   359 

TypeError: Traceback (most recent call last):
 File "/home/shounak/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 106, in _worker_loop
   samples = collate_fn([dataset[i] for i in batch_indices])
 File "/home/shounak/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 106, in <listcomp>
   samples = collate_fn([dataset[i] for i in batch_indices])
 File "<ipython-input-8-101613e4c615>", line 45, in __getitem__
   return self.transform(current_image, next_image, current_box, next_box, labels)
 File "<ipython-input-16-7e912b97874a>", line 558, in __call__
   return self.augment(img_pre, img_next, boxes_pre, boxes_next, labels)
 File "<ipython-input-16-7e912b97874a>", line 45, in __call__
   img_pre, img_next, boxes_pre, boxes_next, labels =                 t(img_pre, img_next, boxes_pre, boxes_next, labels)
 File "<ipython-input-16-7e912b97874a>", line 408, in __call__
   im_pre, im_next, boxes_pre, boxes_next, labels =             self.rand_brightness(im_pre, im_next, boxes_pre, boxes_next, labels)
 File "<ipython-input-16-7e912b97874a>", line 184, in __call__
   if random.randint(2):
TypeError: randint() missing 1 required positional argument: 'b

Can you please help ?

It seems you are using a custom collate_fn with an if-condition:

if random.randint(2)

The random.randint method takes two arguments: the lower bound a and the upper bound b.
If you would like to sample in [0, 2], you can use random.randint(0, 2) instead.
Note that the Python randint method will sample with an inclusive upper bound, so you’ll get an integer in [0, 1, 2]!

1 Like