Siamese Tutorial - Custom data block as input to Learner

Hi guys,

I really love!! That’s why I chose it to work on my Master Thesis experiments :slight_smile:
Unfortunately, I got stuck when following the Siamese network tutorial…

I want to build a similar architecture to the one shown in the Siamese tutorial.

Now the tutorial is structured as follows:

  1. First we see how it’s done with the Mid-level API.
  2. Then the Custom data block approach is introduced.
  3. But then for the training we revert back to the Mid-level API approach and grab the data like this.

However, I want to use the Custom data block (dataloader) from step 2 directly.

siamese = DataBlock(
blocks=(ImageTupleBlock, CategoryBlock),
get_x=get_x, get_y=get_y,

dls = siamese.dataloaders(files)

I tried:

learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), splitter=siamese_splitter, metrics=accuracy)

But I get the following error:
TypeError: forward() missing 1 required positional argument: ‘x2’

When I want to run

Any idea why this might happen? What did I miss? Is there an intermediate step that is crucial that is not automatically applied by simply passing the dls object to the Learner?

I tried to find a similar topic in the forum, but couldn’t find someone who ran into the same kind of problem / error.

I would guess that the forward function expects x1 and x2 as separate inputs, but you pass both as a tuple. So the tuple (x1, x2) is passed as x1 to the model, and x2 is empty.
You could try to adapt the forward function in the model or pass two inputs in the DataLoader.

1 Like

Thank you :grinning:

I changed it to this and now it works:

class SiameseModel(Module):
    def __init__(self, encoder, head):
        self.encoder,self.head = encoder,head
    def forward(self, x):
        ftrs =[self.encoder(x[0]), self.encoder(x[1])], dim=1)
        return self.head(ftrs)