TypeError: __init__() takes 1 positional argument but 2 were given

i am trying to pass single image to the network but got error .

loader = transforms.Compose([ transforms.ToTensor()])
def image_loader(image_name):
    """load image, returns cuda tensor"""
    image = Image.open(image_name)
    image = loader(image).float()
    #image = Variable(image, requires_grad=True)
    image = image.unsqueeze(0)  #this is for VGG, may not be needed for ResNet
    return image
image=image_loader('Dataset/local/corn0.jpg')
model(image)

my network

class mandal(nn.Module):
    def __init__(self):
        super(mandal,self).__init__()
        
        #layer1 inpit (136,136 *3) output (136,136)
        self.layer1=nn.Conv2d(3,16,3,stride=1,padding=1)
        
        #layer2 input (136,136*16) output (136,136*32)
        self.layer2=nn.Conv2d(16,32,3,stride=1,padding=1)
        
        #layer3 input(136,136*32) output(640,480*64)
        self.layer3=nn.Conv2d(32,64,3,stride=1,padding=1)
        
        #layer4 input(136,136*64) output(66,66*64)
        self.layer4=nn.MaxPool2d(2,2)
        
        #layer5 input(66,66*64) output (33,33*128)
        self.layer5=nn.Conv2d(64,128,3,stride=2,padding=2)
        
        #layeer6 input(33,33*128) output(17,17*128)
        self.layer6=nn.MaxPool2d(2,2)
        
        #layer7 input(17,17*128) output(8,8*192)
        self.layer7=nn.Conv2d(128,192,3,stride=2,padding=2)
        
        #layer8 input(8,8*192) output(8,8*120)
        self.layer8=nn.Conv2d(192,120,1,stride=1,padding=0)
        
        #layer9 input(8,8*94) output(8,8*64)
        self.layer9=nn.Conv2d(120,64,1,stride=1,padding=0)
        
        #layer10 input(8,8*64) output(8,8*32)
        self.layer10=nn.Conv2d(64,32,1,stride=1,padding=0)
        
        #layer11 input(8,8*32) output(8,8*16)
        self.layer11=nn.Conv2d(32,10,1,stride=1,padding=0)
        self.falten=nn.Flatten()
        #layer12 input(8,8*16) output(1024)
        self.layer12=nn.Linear(32*9*9,500)
        
        #layer13 input(1024) 0utput(500)
        self.layer13=nn.Linear(500,80) 
        
        #layer14 input(80) output(10)
        self.layer14=nn.Linear(80,10)
        
        #layer15 input(10) output(3)
        self.layer15=nn.Linear(10,2)
        
        #layer12 input(8,8*16) output(1024)
        self.layer16=nn.Linear(32*9*9,500)
        
        #layer13 input(1024) 0utput(500)
        self.layer17=nn.Linear(500,80)
        
        #layer14 input(80) output(10)
        self.layer18=nn.Linear(80,10)
        
        #layer15 input(10)  output(3)
        self.layer19=nn.Linear(10,4)
        #dropout p=0.20
        
        self.dropout=nn.Dropout(0.20)
        
        
    def forward(self,x):
        x = F.relu(self.layer1(x))
        x = F.relu(self.layer2(x))
        x = F.relu(self.layer3(x))
        x = self.layer4(x)
        x = F.relu(self.layer5(x))
        
        x = F.relu(self.layer7(x))
        x = F.relu(self.layer8(x))
        x = F.relu(self.layer9(x))
        x = F.relu(self.layer10(x))
        x = self.dropout(x)
        x = self.layer6(x)
        x = self.falten(x)
        y = x
        x = F.relu(self.layer12(x))
        x = self.dropout(x)
        x = F.relu(self.layer13(x))
        x = self.dropout(x)
        x = F.relu(self.layer14(x))
        x = self.dropout(x)
        x = F.relu(self.layer15(x))
        y = F.relu(self.layer16(y))
        y = self.dropout(x)
        y = F.relu(self.layer17(y))
        y = self.dropout(x)
        y = F.relu(self.layer18(y))
        y = self.dropout(x)
        y = F.relu(self.layer19(y))
        
        return x,y
        
model=mandal()

Hi,

Can you give the full stack trace of the error to know where it comes from?

Also I edited your message to make the code look better, you can do so using triple backticks: ```

albanD thank you for your response

and this the full error

TypeError Traceback (most recent call last)
in
8 return image
9 image=image_loader(‘Dataset/local/corn0.jpg’)
—> 10 mandal(image)

TypeError: init() takes 1 positional argument but 2 were given

The problem is that you use mandal here, which is the class that defines your model, and not model which is an instance of that class.
I would recommend you use capital letters for classes and lower letter for instances to avoid such confusions.

2 Likes

Alband thank you for your response it helps me to solve this problem