Search code examples
pythonmachine-learningzipconv-neural-networkpytorch

How to iterate over two dataloaders simultaneously using pytorch?


I am trying to implement a Siamese network that takes in two images. I load these images and create two separate dataloaders.

In my loop I want to go through both dataloaders simultaneously so that I can train the network on both images.

for i, data in enumerate(zip(dataloaders1, dataloaders2)):

    # get the inputs
    inputs1 = data[0][0].cuda(async=True);
    labels1 = data[0][1].cuda(async=True);

    inputs2 = data[1][0].cuda(async=True);
    labels2 = data[1][1].cuda(async=True);

    labels1 = labels1.view(batchSize,1)
    labels2 = labels2.view(batchSize,1)

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    outputs1 = alexnet(inputs1)
    outputs2 = alexnet(inputs2)

The return value of the dataloader is a tuple. However, when I try to use zip to iterate over them, I get the following error:

OSError: [Errno 24] Too many open files
Exception NameError: "global name 'FileNotFoundError' is not defined" in <bound method _DataLoaderIter.__del__ of <torch.utils.data.dataloader._DataLoaderIter object at 0x7f2d3c00c190>> ignored                           

Shouldn't zip work on all iterable items? But it seems like here I can't use it on dataloaders.

Is there any other way to pursue this? Or am I approaching the implementation of a Siamese network incorrectly?


Solution

  • I see you are struggling to make a right dataloder function. I would do:

    class Siamese(Dataset):
    
    
        def __init__(self, transform=None):
        
           #init data here
        
        def __len__(self):
            return   #length of the data
    
        def __getitem__(self, idx):
            #get images and labels here 
            #returned images must be tensor
            #labels should be int 
            return img1, img2 , label1, label2