'tuple' object has no attribute 'size'

username: jyothi_sri
cooked: <p>Hi i am working on images dataset and facing this isssue when i run the follwing code .help me to figure it out plz</p>
<p>class NSSADNN(nn.Module):<br>
def <strong>init</strong>(self,kernels1=1,extra_kernels=0):<br>
super(NSSADNN, self).<strong>init</strong>()</p>
<pre><code>    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
</code></pre>
<p>ERROR:ignite.engine.engine.Engine:Current run is terminating due to exception: ‘tuple’ object has no attribute ‘size’<br>
ERROR:ignite.engine.engine.Engine:Engine run is terminating due to exception: ‘tuple’ object has no attribute ‘size’<br>
Traceback (most recent call last):</p>
<p>File “D:\GIT HUB Downloads\CNNIQA\main.py”, line 416, in <br>
run(args.batch_size, args.epochs, args.lr, args.weight_decay, config, args.exp_id,</p>
<p>File “D:\GIT HUB Downloads\CNNIQA\main.py”, line 358, in run<br>
trainer.run(train_loader, max_epochs=epochs)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 704, in run<br>
return self._internal_run()</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 783, in _internal_run<br>
self._handle_exception(e)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception<br>
raise e</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 753, in _internal_run<br>
time_taken = self._run_once_on_dataset()</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 854, in _run_once_on_dataset<br>
self._handle_exception(e)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception<br>
raise e</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 840, in _run_once_on_dataset<br>
self.state.output = self._process_function(self, self.state.batch)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine_<em>init</em>_.py”, line 103, in update<br>
loss = loss_fn(y_pred, y)</p>
<p>File “D:\GIT HUB Downloads\CNNIQA\main.py”, line 48, in loss_fn<br>
return F.l1_loss(y_pred, y[0])</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\torch\nn\functional.py”, line 3238, in l1_loss<br>
if not (target.size() == input.size()):</p>
<p>AttributeError: ‘tuple’ object has no attribute ‘size’</p>

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

username: jyothi_sri
cooked: <p>but when i change the architecture and run the code its excuting properly , not knowing whats wrong with the above code .</p>

username: jyothi_sri
cooked: <p>any idea sir .can you help me out.</p>

username: ptrblck
cooked: <p>Could you post a minimal, executable code snippet which would reproduce the issue, assuming my previous post was pointing to the wrong root cause?</p>

username: jyothi_sri
cooked: <p>class Network(nn.Module):<br>
def <strong>init</strong>(self,kernels1=1,extra_kernels=0):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 64, kernel_size = 5, padding = (1,1), stride=1)#kernels=kernels1<br>
self.bn1 = nn.BatchNorm2d(64,eps=1e-05,momentum=0.15)<br>
self.cnn1_drop = nn.Dropout2d(p=0.15)<br>
self.conv2 = nn.Conv2d (in_channels = 64, out_channels = 128, kernel_size = 4, padding = (1,1))<br>
self.cnn2_drop = nn.Dropout2d(p=0.15)<br>
self.bn2 = nn.BatchNorm2d(128,eps=1e-05,momentum=0.25)<br>
self.conv3 = nn.Conv2d(in_channels = 128, out_channels = 256, kernel_size = 3, padding = (1,1))<br>
self.bn3 = nn.BatchNorm2d(256,eps=1e-05,momentum=0.25)<br>
self.cnn3_drop = nn.Dropout2d(p=0.25)<br>
self.relu=nn.ELU(alpha=6.0)<br>
self.fc1 = nn.Linear(in_features =2304, out_features = 1000)<br>
self.bn4 = nn.BatchNorm1d(1000)<br>
self.cnn4_drop = nn.Dropout2d(p=0.25)<br>
self.fc2 = nn.Linear(in_features = 1000, out_features = 100)<br>
self.bn5 = nn.BatchNorm1d(100)<br>
self.cnn5_drop = nn.Dropout2d(p=0.25)<br>
self.out = nn.Linear(in_features = 100, out_features = 1)<br>
<span class="hashtag">#self</span>.cnn6_drop = nn.Dropout2d(p=0.25)</p>
<pre><code>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
</code></pre>
<p>This is the code that is getting executed.</p>

username: AlphaBetaGamma96
cooked: <p>Either your <code>y_pred</code> or <code>y</code> objects are a Tuple. Check each via <code>type(y_pred)</code> and <code>type(y)</code>. Might just need to simply unpack them via <code>[0]</code></p>

username: ptrblck
cooked: <p>Your code snippet is unfortunately not executable.<br>
“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.</p>

username: jyothi_sri
cooked: <p>Thanks for ur reply ptrblck n sorry for the late reply , i couldnt get what u r explaning .can u plz tell me clearly .</p>

username: jyothi_sri
cooked: <p>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<br>
<strong>Network</strong><br>
class CNNIQAnet(nn.Module):<br>
def <strong>init</strong>(self, ker_size=7, n_kers=50, n1_nodes=800, n2_nodes=800):<br>
self.output_num = [4,2,1]<br>
super(CNNIQAnet, self).<strong>init</strong>()<br>
self.conv1  = nn.Conv2d(1, n_kers, ker_size)</p>
<pre><code>    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
</code></pre>
<p><strong>error</strong><br>
run(args.batch_size, args.epochs, args.lr, args.weight_decay, config, args.exp_id,</p>
<p>File “D:\GIT HUB Downloads\CNNIQA-Gaborconv2d\CNNIQA\main.py”, line 212, in run<br>
trainer.run(train_loader, max_epochs=epochs)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 704, in run<br>
return self._internal_run()</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 783, in _internal_run<br>
self._handle_exception(e)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception<br>
raise e</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 753, in _internal_run<br>
time_taken = self._run_once_on_dataset()</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 854, in _run_once_on_dataset<br>
self._handle_exception(e)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 466, in _handle_exception<br>
raise e</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine\engine.py”, line 840, in _run_once_on_dataset<br>
self.state.output = self._process_function(self, self.state.batch)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\ignite\engine_<em>init</em>_.py”, line 102, in update<br>
y_pred = model(x)</p>
<p>File “C:\Users\VIJAY\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py”, line 1130, in _call_impl<br>
return forward_call(*input, **kwargs)</p>
<p>File “D:\GIT HUB Downloads\CNNIQA-Gaborconv2d\CNNIQA\main.py”, line 98, in forward<br>
spp = spatial_pyramid_pool(x,1,[int(x.size(2)),int(x.size(3))],self.output_num,5)</p>
<p>File “D:\GIT HUB Downloads\CNNIQA-Gaborconv2d\CNNIQA\spp_layer.py”, line 12, in spatial_pyramid_pool<br>
for i in range(len(out_pool_size)):</p>
<p>TypeError: object of type ‘int’ has no len()</p>

username: ptrblck
cooked: <p>The new error is raised in:</p>
<pre><code class="lang-python">for i in range(len(out_pool_size)):

TypeError: object of type ‘int’ has no len()
</code></pre>
<p>as <code>out_pool_size</code> is an <code>int</code> and doesn’t have a <code>len()</code> operation, so it seems you want to use <code>range(out_pool_size)</code> instead.</p>

username: jyothi_sri
cooked: <p>Thanq  for the reply <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>

username: jyothi_sri
cooked: <p>Can u give an example on how to use this spatial pyramid pooling in CNNs.</p>