Attention Unet Tuple Issue

bigmb: <p>Hello,<br>
So I am wanted to implement Attention Unet. But there is some issue coming up while using it.<br>
I am using my own medical dataset and also doing a lot of preprocessing with data.<br>
When I am using your model I get this error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/4e0a6c399ed38ce1c3a075346a5c8a16fc5f8258.png" data-download-href="https://discuss.pytorch.org/uploads/default/4e0a6c399ed38ce1c3a075346a5c8a16fc5f8258" title="error1"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e0a6c399ed38ce1c3a075346a5c8a16fc5f8258_2_690x205.png" alt="error1" data-base62-sha1="b8nzxVG4lwUqd8nCh1nX9m5dzbW" width="690" height="205" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e0a6c399ed38ce1c3a075346a5c8a16fc5f8258_2_690x205.png, https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e0a6c399ed38ce1c3a075346a5c8a16fc5f8258_2_1035x307.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/4/4e0a6c399ed38ce1c3a075346a5c8a16fc5f8258.png 2x" data-dominant-color="313135"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error1</span><span class="informations">1039×310 51.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><span class="hashtag-raw">#Not</span> able to post more pics due to new user.<br>
<span class="hashtag-raw">#My</span> attention Model is as follows:</p>
<p><span class="hashtag-raw">#And</span> the Forward loop for the AttUnet is :</p>
<p><span class="hashtag-raw">#Any</span> ideas why this is happening?</p>

ptrblck: <p>Could you post the code snippets using three backticks ```?<br>
This would avoid the limit of images you can post as a new user and also gives us the ability to easily copy post the code to debug it.</p>

bigmb: <p>My attention Model is as follows:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/9/9cb96d036e140a67c9e1af866677e7baa582760a.png" alt="1" data-base62-sha1="mmrL1jssErNecAIJLQjUo4CKf5E" width="624" height="444"></p>
<p>And the Forward loop for the AttUnet is :</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/35efea62f10191ada511a016168befca87ce69af.png" data-download-href="https://discuss.pytorch.org/uploads/default/35efea62f10191ada511a016168befca87ce69af" title="2"><img src="https://discuss.pytorch.org/uploads/default/original/2X/3/35efea62f10191ada511a016168befca87ce69af.png" alt="2" data-base62-sha1="7H9k0mSYbVMo6R8rbd0f0COvNkH" width="272" height="500" data-dominant-color="313132"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">2</span><span class="informations">341×626 23.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Any ideas why this is happening?</p>

ptrblck: <p>The error seems to point to an empty tuple.<br>
Based on the line of code, if looks like <code>input</code> is empty, as you’ll get the same error message here:</p>
<pre><code class="lang-python">x = ()
x[0]
&gt; IndexError: tuple index out of range
</code></pre>
<p>As I said, it’s hard to debug your issue, if you post images instead of a (small) executable code snippet.</p>

bigmb: <p>Sorry for the trouble.<br>
Took me sometime to get used to this.</p>
<p>This is my attention Block:</p>
<pre><code class="lang-auto">
class Attention_block(nn.Module):
    def __init__(self, F_g, F_l, F_int):
        super(Attention_block, self).__init__()
        self.W_g = nn.Sequential(
            nn.Conv2d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True),
            nn.BatchNorm2d(F_int)
        )

        self.W_x = nn.Sequential(
            nn.Conv2d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True),
            nn.BatchNorm2d(F_int)
        )

        self.psi = nn.Sequential(
            nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
            nn.BatchNorm2d(1),
            nn.Sigmoid()
        )

        self.relu = nn.ReLU(inplace=True)

    def forward(self, g, x):
        g1 = self.W_g(g)
        x1 = self.W_x(x)
        psi = self.relu(g1 + x1)
        psi = self.psi(psi)

        return x * psi
</code></pre>
<p>This is my model:</p>
<pre><code class="lang-auto">
class AttU_Net(nn.Module):
    def __init__(self, img_ch=3, output_ch=1):
        super(AttU_Net, self).__init__()

        self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)

        self.Conv1 = conv_block(ch_in=img_ch, ch_out=64)
        self.Conv2 = conv_block(ch_in=64, ch_out=128)
        self.Conv3 = conv_block(ch_in=128, ch_out=256)
        self.Conv4 = conv_block(ch_in=256, ch_out=512)
        self.Conv5 = conv_block(ch_in=512, ch_out=1024)

        self.Up5 = up_conv(ch_in=1024, ch_out=512)
        self.Att5 = Attention_block(F_g=512, F_l=512, F_int=256)
        self.Up_conv5 = conv_block(ch_in=1024, ch_out=512)

        self.Up4 = up_conv(ch_in=512, ch_out=256)
        self.Att4 = Attention_block(F_g=256, F_l=256, F_int=128)
        self.Up_conv4 = conv_block(ch_in=512, ch_out=256)

        self.Up3 = up_conv(ch_in=256, ch_out=128)
        self.Att3 = Attention_block(F_g=128, F_l=128, F_int=64)
        self.Up_conv3 = conv_block(ch_in=256, ch_out=128)

        self.Up2 = up_conv(ch_in=128, ch_out=64)
        self.Att2 = Attention_block(F_g=64, F_l=64, F_int=32)
        self.Up_conv2 = conv_block(ch_in=128, ch_out=64)

        self.Conv_1x1 = nn.Conv2d(64, output_ch, kernel_size=1, stride=1, padding=0)
        self.active = torch.nn.Sigmoid()


    def forward(self, x):
        # encoding path
        x1 = self.Conv1(x)

        x2 = self.Maxpool(x1)
        x2 = self.Conv2(x2)

        x3 = self.Maxpool(x2)
        x3 = self.Conv3(x3)

        x4 = self.Maxpool(x3)
        x4 = self.Conv4(x4)

        x5 = self.Maxpool(x4)
        x5 = self.Conv5(x5)

        # decoding + concat path
        d5 = self.Up5(x5)
        x4 = self.Att5(g=d5, x=x4)
        d5 = torch.cat((x4, d5), dim=1)
        d5 = self.Up_conv5(d5)

        d4 = self.Up4(d5)
        x3 = self.Att4(g=d4, x=x3)
        d4 = torch.cat((x3, d4), dim=1)
        d4 = self.Up_conv4(d4)

        d3 = self.Up3(d4)
        x2 = self.Att3(g=d3, x=x2)
        d3 = torch.cat((x2, d3), dim=1)
        d3 = self.Up_conv3(d3)

        d2 = self.Up2(d3)
        x1 = self.Att2(g=d2, x=x1)
        d2 = torch.cat((x1, d2), dim=1)
        d2 = self.Up_conv2(d2)

        d1 = self.Conv_1x1(d2)
        d1 = self.active(d1)

        return d1
</code></pre>
<p>This is what I am passing:</p>
<pre><code class="lang-auto">model_test1 = AttU_Net(3, 1).to(device)
torchsummary.summary(model_test1, input_size=(3, 128, 128))
</code></pre>