'tuple' object has no attribute 'size'

Hi i am working on images dataset and facing this isssue when i run the follwing code .help me to figure it out plz

class NSSADNN(nn.Module):
def init(self,kernels1=1,extra_kernels=0):
super(NSSADNN, self).init()

    self.conv1 = nn.Conv2d(in_channels=1, out_channels=50, kernel_size=5,padding = (1,1),stride=1)#, ,kernels=kernels1)
    self.conv2 = nn.Conv2d(in_channels=50, out_channels=50, kernel_size=3, stride=1, padding=0)
    self.conv3 = nn.Conv2d(in_channels=50, out_channels=50, kernel_size=3, stride=1, padding=1)
    self.conv4 = nn.Conv2d(in_channels=50, out_channels=25, kernel_size=3, stride=1, padding=0)
    self.conv5 = nn.Conv2d(in_channels=25, out_channels=25, kernel_size=3, stride=1, padding=0)
    self.conv6 = nn.Conv2d(in_channels=25, out_channels=25, kernel_size=3, stride=1, padding=1)

    self.fc1_1 = nn.Linear(2500, 1024)
    self.fc2_1 = nn.Linear(1024, 36)

    self.fc1_2 = nn.Linear(2500, 1024)
    self.fc2_2 = nn.Linear(2048, 1024)
    self.fc3_2 = nn.Linear(1024, 1)


def forward(self, x):
    # print('x', x.size())#x torch.Size([128, 1, 32, 32])
    x_distort = x.view(-1, x.size(-3), x.size(-2), x.size(-1))
    # print('input size:' + str(x_distort.size()))#input size:torch.Size([1, 1, 32, 32])

    x1 = F.relu(self.conv1(x_distort))# print('x1:' + str(x1.size()))#x1:torch.Size([1, 50, 28, 28])
    x2 = F.relu(self.conv2(x1))# print('x2:' + str(x2.size()))#x2:torch.Size([1, 50, 26, 26])
    x3 = F.relu(self.conv3(x2))# print('x3:' + str(x3.size()))#x3:torch.Size([1, 50, 26, 26])

    #x3 = torch.add(x3, x2 )# print('x3:' + str(x3.size()))#x3:torch.Size([1, 50, 26, 26])
    x3 = F.max_pool2d(x3, (2, 2), stride=2) # print('x3:' + str(x3.size()))#x3:torch.Size([1, 50, 13, 13])

    x4 = F.relu(self.conv4(x3)) # print('x4:' + str(x4.size()))#x4:torch.Size([1, 25, 11, 11])
    x5 = F.relu(self.conv5(x4)) # print('x5:' + str(x5.size()))#x5:torch.Size([1, 25, 9, 9])
    x6 = F.relu(self.conv6(x5)) # print('x6:' + str(x6.size()))#x6:torch.Size([1, 25, 9, 9])

    ##x6 = torch.add(x6, x5)

    #fc = x6.view(-1, self.num_flat_features(x6)) # #fc torch.Size([1, 2025])
    fc = x6.view (x6.size(0),-1)
    fc1_1 = self.fc1_1(fc) # print('fc1_1', fc1_1.size())#fc1_1 torch.Size([1, 1024])
    fc2_1 = self.fc2_1(fc1_1) # print('fc2_1', fc2_1.size())#fc2_1 torch.Size([1, 36])

    fc1_2 = self.fc1_2(fc) # print('fc1_2', fc1_2.size())#fc1_2 torch.Size([1, 1024])
    cat = F.relu(torch.cat((fc1_2, fc1_1), 1)) # print('cat', cat.size())#cat torch.Size([1, 2048])
    fc2_2 = self.fc2_2(cat)
    quality = self.fc3_2(fc2_2)

    # print('quality', quality.size())#quality torch.Size([128, 1])
    # print('fc2_1', fc2_1.size())#torch.Size([128, 36])

    return quality, fc2_1

ERROR:ignite.engine.engine.Engine:Current run is terminating due to exception: ‘tuple’ object has no attribute ‘size’
ERROR:ignite.engine.engine.Engine:Engine run is terminating due to exception: ‘tuple’ object has no attribute ‘size’
Traceback (most recent call last):

File “D:\GIT HUB Downloads\CNNIQA\main.py”, line 416, in
run(args.batch_size, args.epochs, args.lr, args.weight_decay, config, args.exp_id,

File “D:\GIT HUB Downloads\CNNIQA\main.py”, line 358, in run
trainer.run(train_loader, max_epochs=epochs)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 704, in run
return self._internal_run()

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 783, in _internal_run
self._handle_exception(e)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception
raise e

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 753, in _internal_run
time_taken = self._run_once_on_dataset()

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 854, in _run_once_on_dataset
self._handle_exception(e)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception
raise e

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 840, in _run_once_on_dataset
self.state.output = self._process_function(self, self.state.batch)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine_init_.py”, line 103, in update
loss = loss_fn(y_pred, y)

File “D:\GIT HUB Downloads\CNNIQA\main.py”, line 48, in loss_fn
return F.l1_loss(y_pred, y[0])

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\torch\nn\functional.py”, line 3238, in l1_loss
if not (target.size() == input.size()):

AttributeError: ‘tuple’ object has no attribute ‘size’

I guess you might be trying to pass the model outputs (which is a tuple) directly to the loss function, which would fail as a tensor is expected.
However, that’s just my guess based on the forward implementation and the raised error, so check the stacktrace and make sure the loss function receives the right types.

but when i change the architecture and run the code its excuting properly , not knowing whats wrong with the above code .

any idea sir .can you help me out.

Could you post a minimal, executable code snippet which would reproduce the issue, assuming my previous post was pointing to the wrong root cause?

class Network(nn.Module):
def init(self,kernels1=1,extra_kernels=0):
super().init()
self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 64, kernel_size = 5, padding = (1,1), stride=1)#kernels=kernels1
self.bn1 = nn.BatchNorm2d(64,eps=1e-05,momentum=0.15)
self.cnn1_drop = nn.Dropout2d(p=0.15)
self.conv2 = nn.Conv2d (in_channels = 64, out_channels = 128, kernel_size = 4, padding = (1,1))
self.cnn2_drop = nn.Dropout2d(p=0.15)
self.bn2 = nn.BatchNorm2d(128,eps=1e-05,momentum=0.25)
self.conv3 = nn.Conv2d(in_channels = 128, out_channels = 256, kernel_size = 3, padding = (1,1))
self.bn3 = nn.BatchNorm2d(256,eps=1e-05,momentum=0.25)
self.cnn3_drop = nn.Dropout2d(p=0.25)
self.relu=nn.ELU(alpha=6.0)
self.fc1 = nn.Linear(in_features =2304, out_features = 1000)
self.bn4 = nn.BatchNorm1d(1000)
self.cnn4_drop = nn.Dropout2d(p=0.25)
self.fc2 = nn.Linear(in_features = 1000, out_features = 100)
self.bn5 = nn.BatchNorm1d(100)
self.cnn5_drop = nn.Dropout2d(p=0.25)
self.out = nn.Linear(in_features = 100, out_features = 1)
#self.cnn6_drop = nn.Dropout2d(p=0.25)

def forward(self, t):
    t = t.view(-1, t.size(-3), t.size(-2), t.size(-1))
    #print('t', str(t.size()))
    t = self.relu(self.bn1(self.conv1(t)))
    t = F.max_pool2d(t, kernel_size = 2, stride = 2)
    t = self.cnn1_drop(t)
    #t = self.conv1(t)
   # t = F.max_pool2d(t,(t.size(-2), t.size(-1)))
   # t1 = -F.max_pool2d(-t,(t.size(-2), t.size(-1)))
    #t  = torch.cat((t, t1),1)
    #t = t.squeeze(3).squeeze(2)
    #t = t.view (t.size(0),-1)
    #t  = t.unsqueeze(3).unsqueeze(2)
    t = self.relu(self.bn2(self.conv2(t)))
    t = F.max_pool2d(t, kernel_size = 2, stride = 2)
    t =self.cnn2_drop(t)
    t = self.relu(self.bn3(self.conv3(t)))
    t = F.max_pool2d(t, kernel_size = 2, stride = 2)
    t =self.cnn3_drop(t)
    t = t.view (t.size(0),-1)
    t = self.relu(self.bn4(self.fc1(t)))
    t =self.cnn4_drop(t)
    t = self.relu(self.bn5(self.fc2(t)))
    t =self.cnn5_drop(t)
    t = self.out(t)
    #t =self.cnn6_drop(t)
    
    return t

This is the code that is getting executed.

Either your y_pred or y objects are a Tuple. Check each via type(y_pred) and type(y). Might just need to simply unpack them via [0]

Your code snippet is unfortunately not executable.
“Executable” here means that I can copy/paste the code and directly run it in my environment without running into any errors caused by undefined methods etc. and would be able to reproduce the error by just rerunning the code.

Thanks for ur reply ptrblck n sorry for the late reply , i couldnt get what u r explaning .can u plz tell me clearly .

Do u have any idea of how to implement spatial pyramid pooling in CNNs, i tried to implement it in the following Network , but stuck with an error plz help me out
Network
class CNNIQAnet(nn.Module):
def init(self, ker_size=7, n_kers=50, n1_nodes=800, n2_nodes=800):
self.output_num = [4,2,1]
super(CNNIQAnet, self).init()
self.conv1 = nn.Conv2d(1, n_kers, ker_size)

    self.fc1    = nn.Linear(2 * n_kers, n1_nodes)
    self.fc2    = nn.Linear(n1_nodes, n2_nodes)
    self.fc3    = nn.Linear(n2_nodes, 1)
    self.dropout = nn.Dropout()

def forward(self, x):
    x  = x.view(-1, x.size(-3), x.size(-2), x.size(-1))  #

    h  = self.conv1(x)
    spp = spatial_pyramid_pool(x,1,[int(x.size(2)),int(x.size(3))],self.output_num,5)

    # h1 = F.adaptive_max_pool2d(h, 1)
    # h2 = -F.adaptive_max_pool2d(-h, 1)
    h1 = F.max_pool2d(h, (h.size(-2), h.size(-1)))
    h2 = -F.max_pool2d(-h, (h.size(-2), h.size(-1)))
    h  = torch.cat((h1, h2), 1)  # max-min pooling
    h  = h.squeeze(3).squeeze(2)

    h  = F.relu(self.fc1(spp))
    h  = self.dropout(h)
    h  = F.relu(self.fc2(h))

    q  = self.fc3(h)
    return q

error
run(args.batch_size, args.epochs, args.lr, args.weight_decay, config, args.exp_id,

File “D:\GIT HUB Downloads\CNNIQA-Gaborconv2d\CNNIQA\main.py”, line 212, in run
trainer.run(train_loader, max_epochs=epochs)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 704, in run
return self._internal_run()

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 783, in _internal_run
self._handle_exception(e)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception
raise e

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 753, in _internal_run
time_taken = self._run_once_on_dataset()

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 854, in _run_once_on_dataset
self._handle_exception(e)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception
raise e

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 840, in _run_once_on_dataset
self.state.output = self._process_function(self, self.state.batch)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine_init_.py”, line 102, in update
y_pred = model(x)

File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py”, line 1130, in _call_impl
return forward_call(*input, **kwargs)

File “D:\GIT HUB Downloads\CNNIQA-Gaborconv2d\CNNIQA\main.py”, line 98, in forward
spp = spatial_pyramid_pool(x,1,[int(x.size(2)),int(x.size(3))],self.output_num,5)

File “D:\GIT HUB Downloads\CNNIQA-Gaborconv2d\CNNIQA\spp_layer.py”, line 12, in spatial_pyramid_pool
for i in range(len(out_pool_size)):

TypeError: object of type ‘int’ has no len()

The new error is raised in:

for i in range(len(out_pool_size)):

TypeError: object of type ‘int’ has no len()

as out_pool_size is an int and doesn’t have a len() operation, so it seems you want to use range(out_pool_size) instead.

Thanq for the reply :smile:

Can u give an example on how to use this spatial pyramid pooling in CNNs.