Python defines mydatasets to implement multi-channel input of different data modes

Time:2020-11-24

Recently, we are working on a project, which uses dual channel neural network. Each channel inputs different data for training and has the same label. At the beginning, I didn’t expect how to realize it. Many examples on the Internet are single channel. Even if we find a dual channel example, the input of the two channels is the same.

Finally, a solution came up. Multiple input and single input are actually the same, only need to be rewritten torch.utils.data . datasets. You need to rewrite init, len and getitem in class dataset

One example:

class MyDataset(data.Dataset):
  def __init__(self, data1,data2, labels):
    self.data1= data1
    self.data2= data2
    self.labels  =In my example, the label is the same, if you are different, you can add another one

  def __getitem__(self, index):  
    img1,img2, target = self.data1[index], self.data2[index], self.labels[index]
    return img1,img2, target

  def __len__(self):
    return len( self.data1 )In my case, len( self.data1 ) = len( self.data2 )

In the above article, python defines mydatasets to implement multi-channel input of different data, which is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.