TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not Sequential

I’m trying to create my own custom model by using resnet50 as the body and defining my own head

class stdConv(nn.Module):

def __init__(self, nin, nout, stride, drop=0.1):
    super().__init__()
    self.conv1 = nn.Conv2d(nin, nout, kernel_size = 3, stride = stride, padding = 1) 
    self.bn = nn.BatchNorm2d(nout)
    self.drop = nn.Dropout(drop)

def forward(self, x):
    x = F.relu(self.conv1(x))
    x = self.bn(x)
    x = self.drop(x)
    return x

flattening method

def flatten_conv(x, k):
bs, nf, gx, gy = x.size()
x = x.permute(0, 2, 3, 1).contiguous()
return x.view(bs, -1, nf//k)

class outConv(nn.Module):

def __init__(self, k, nin, bias):
    super().__init__()
    self.oconv1 = nn.Conv2d(nin, (len(cat)+1)*k, 3, padding = 1) 
    self.oconv2 = nn.Conv2d(nin,     4*k,      3, padding = 1) 
    
    self.oconv1.bias.data.zero_().add(bias)


def forward(self, x):
      return [flatten_conv(self.oconv1(x), self.k),
                 flatten_conv(self.oconv2(x), self.k)])

a class that defines the architecture head

class myHead(nn.Module):

def __init__(self, bias, k):
    
    super().__init__()
    self.drop = nn.Dropout(0.25)
    self.sconv1 = stdConv(nin = 2048, nout = 1024,  stride = 2) 
    self.sconv2 = stdConv(nin = 1024, nout = 512,  stride = 2) 
    self.sconv3 = stdConv(nin = 512,  nout = 256,  stride = 2) 
    self.sconv4 = stdConv(nin = 256,  nout = 256,  stride = 2)
    
    self.oconv = outConv(k, 256, bias) 
    

def forward(self, x):

x = self.drop(F.relu(x))

    print(x)
    x = self.sconv1(x)
    x = self.sconv2(x)
    x = self.sconv3(x)
    x = self.sconv4(x)
    
    x = self.oconv(x)
    
    return x

class myModel(Module):

def __init__(self, encoder, head):
    self.encoder, self.head = encoder, head
    

def forward(self, x):
    
    return self.head(self.encoder)

create the head of the model

myhead = myHead(3, 1)

create the body of the model

encoder = create_body(resnet50, n_in=3, pretrained=True, cut=-2)

merge the body and the head together to create the model

modely = myModel(encoder, myhead)

learn = Learner(dls = dl, model = modely)

return one batch from the training set

d, i, z = dl.one_batch()

But then i get this error. please help

Are you found a solution to your error, please?

His issue here was in the forward function, it should be like so:

class myModel(Module):
  def __init__(self, encoder, head):
      self.encoder, self.head = encoder, head
    

  def forward(self, x):
    
      return self.head(self.encoder(x)) # WE NEED TO PASS X

The input was never passed through the model

1 Like

In my case I have

` else:
     in_rgb = self.model[0:3]
     print("in_rgb",in_rgb)

     in_ther = self.model[0:3]
     print("in_ther",in_ther)

     output = torch.nn.Sequential(*(list(in_rgb)+list(in_ther)))
     print("output",output)

     m = self.model[3:]
     print("model",m)

     out = torch.nn.Sequential(*(list(output)+list(m)))
     print("out",out)

     

    # out = {0:torch.cat([in_rgb, in_ther], 1), 1:torch.cat([output, m], 1)}

     return self.forward_once(out, profile)  # single-scale inference, train`

and i have the same error :

` Traceback (most recent call last):

File “train.py”, line 622, in
train(hyp, opt, device, tb_writer, wandb)
File “train.py”, line 79, in train
model = Model(opt.cfg or ckpt[‘model’].yaml, ch=6, nc=nc).to(device) # create
File “/content/drive/My Drive/yolov3_v0/models/yolo.py”, line 103, in init
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, 3, s, s),torch.zeros(1, 3, s, s))]) # forward
File “/content/drive/My Drive/yolov3_v0/models/yolo.py”, line 190, in forward
return self.forward_once(out, profile) # single-scale inference, train
File “/content/drive/My Drive/yolov3_v0/models/yolo.py”, line 206, in forward_once
out = m(out) # run
File “/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/content/drive/My Drive/yolov3_v0/models/common.py”, line 37, in forward
return self.act(self.bn(self.conv(x)))
File “/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py”, line 399, in forward
return self._conv_forward(input, self.weight, self.bias)
File “/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py”, line 396, in _conv_forward
self.padding, self.dilation, self.groups)
TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not Sequential `