Size mismatch of predicted and Label

Farhan_Abbas: <p>I am trying to run a model but it the predicted output is different than the actual input. I think it has some padding or kernel size problem. The code is attached . Please take a look.<br>
I want to use 3d patch size with binary classification (num_classes=2, in_channels=1)</p>
<p>import numpy as np<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torch.nn import init<br>
from torchsummary import summary</p>
<p>def init_weights(net, init_type=‘normal’, gain=0.02):<br>
def init_func(m):<br>
classname = m.<strong>class</strong>.<strong>name</strong><br>
if hasattr(m, ‘weight’) and (classname.find(‘Conv’) != -1 or classname.find(‘Linear’) != -1):<br>
if init_type == ‘normal’:<br>
init.normal_(m.weight.data, 0.0, gain)<br>
elif init_type == ‘xavier’:<br>
init.xavier_normal_(m.weight.data, gain=gain)<br>
elif init_type == ‘kaiming’:<br>
init.kaiming_normal_(m.weight.data, a=0, mode=‘fan_in’)<br>
elif init_type == ‘orthogonal’:<br>
init.orthogonal_(m.weight.data, gain=gain)<br>
else:<br>
raise NotImplementedError(‘initialization method [%s] is not implemented’ % init_type)<br>
if hasattr(m, ‘bias’) and m.bias is not None:<br>
init.constant_(m.bias.data, 0.0)<br>
elif classname.find(‘BatchNorm3d’) != -1:<br>
init.normal_(m.weight.data, 1.0, gain)<br>
init.constant_(m.bias.data, 0.0)</p>
<pre><code>print('initialize network with %s' % init_type)
net.apply(init_func)
</code></pre>
<p>class Conv(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1):<br>
super().<strong>init</strong>()<br>
self.conv3d = nn.Conv3d(in_channels=in_channels, out_channels=out_channels,<br>
kernel_size=kernel_size, stride=stride, padding=padding)</p>
<pre><code>    self.batch_norm = nn.BatchNorm3d(num_features=out_channels)

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

def forward(self, x):
    x = self.conv3d(x)
    x = self.batch_norm(x)
    x = self.relu(x)

    return x
</code></pre>
<p>class AnalysisBlock(nn.Module):<br>
def <strong>init</strong>(self, in_channels, model_depth=4, pooling=2):<br>
super().<strong>init</strong>()<br>
init_out_channels = 32<br>
num_conv_layers = 2<br>
self.module_dict = nn.ModuleDict()</p>
<pre><code>    for depth in range(model_depth):
        out_channels = (2 ** depth) * init_out_channels

        for layer in range(num_conv_layers):
            conv_layer = Conv(in_channels, out_channels)
            self.module_dict["conv_{}_{}".format(depth, layer)] = conv_layer
            in_channels, out_channels = out_channels, out_channels * 2

        if depth != model_depth - 1:
            max_pool = nn.MaxPool3d(kernel_size=pooling, stride=2, padding=0)
            self.module_dict["max_pool_{}".format(depth)] = max_pool

def forward(self, x):
    # TODO: Maybe change to tensor instead of list
    synthesis_features = []
    for key, layer in self.module_dict.items():
        x = layer(x).contiguous()

        if key.startswith("conv") and key.endswith("1"):
            synthesis_features.append(x)

    return x, synthesis_features
</code></pre>
<p>class UpConv(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels, kernel_size=3, stride=2, padding=1, output_padding=1):<br>
super().<strong>init</strong>()<br>
# TODO Up Convolution or Conv Transpose?<br>
self.up_conv = nn.ConvTranspose3d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,<br>
stride=stride, padding=padding, output_padding=output_padding)</p>
<pre><code>def forward(self, x):
    return self.up_conv(x)
</code></pre>
<p>class SynthesisBlock(nn.Module):<br>
def <strong>init</strong>(self, out_channels, model_depth=4):<br>
super().<strong>init</strong>()<br>
init_out_channels = 128<br>
num_conv_layers = 2<br>
self.module_dict = nn.ModuleDict()</p>
<pre><code>    for depth in range(model_depth - 2, -1, -1):
        channels = (2 ** depth) * init_out_channels
        up_conv = UpConv(in_channels=channels, out_channels=channels)
        self.module_dict["deconv_{}".format(depth)] = up_conv

        for layer in range(num_conv_layers):
            in_channels, feat_channels = channels // 2, channels // 2
            if layer == 0:
                in_channels = in_channels + channels

            conv_layer = Conv(in_channels=in_channels, out_channels=feat_channels)
            self.module_dict["conv_{}_{}".format(depth, layer)] = conv_layer

        if depth == 0:
            # TODO Figure out kernel size + padding + stride for final
            # 1 x 1 x 1 conv
            final_conv = nn.Conv3d(in_channels=feat_channels, out_channels=out_channels, kernel_size=1, padding=2)
            self.module_dict["final_conv"] = final_conv

def forward(self, x, high_res_features):
    for key, layer in self.module_dict.items():
        if key.startswith("deconv"):
            x = layer(x)
            # Enforce same size
            features = high_res_features[int(key[-1])][:, :, 0:x.size()[2], 0:x.size()[3],
                       0:x.size()[4]].contiguous()
            x = torch.cat((features, x), dim=1).contiguous()
        else:
            x = layer(x)
    return x
</code></pre>
<p>class RecurrentBlock(nn.Module):<br>
def <strong>init</strong>(self, out_channels, t=2):<br>
super().<strong>init</strong>()<br>
self.t = t<br>
self.conv = nn.Sequential(<br>
nn.Conv3d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),<br>
nn.BatchNorm3d(out_channels),<br>
nn.ReLU(inplace=True)<br>
)</p>
<pre><code>def forward(self, x):
    for i in range(self.t):
        if i == 0:
            x1 = self.conv(x)

        x1 = self.conv(x + x1)
    return x1
</code></pre>
<p>class RRCNNBlock(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels, t=2):<br>
super().<strong>init</strong>()<br>
self.Conv_1x1 = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1)<br>
self.RCNN = nn.Sequential(<br>
RecurrentBlock(out_channels, t=t),<br>
RecurrentBlock(out_channels, t=t)<br>
)</p>
<pre><code>def forward(self, x):
    x = self.Conv_1x1(x)
    x1 = self.RCNN(x)
    return x + x1
</code></pre>
<p>class AttentionBlock(nn.Module):<br>
def <strong>init</strong>(self, f_g, f_l, f_int):<br>
super().<strong>init</strong>()<br>
self.w_g = nn.Sequential(<br>
nn.Conv3d(f_g, f_int, kernel_size=1, stride=1, padding=0),<br>
nn.BatchNorm3d(f_int)<br>
)</p>
<pre><code>    self.w_x = nn.Sequential(
        nn.Conv3d(f_l, f_int, kernel_size=1, stride=1, padding=0),
        nn.BatchNorm3d(f_int)
    )

    self.psi = nn.Sequential(
        nn.Conv3d(f_int, 1, kernel_size=1, stride=1, padding=0),
        nn.BatchNorm3d(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>class R2AnalysisBlock(nn.Module):<br>
def <strong>init</strong>(self, in_channels, model_depth=4, pooling=2):<br>
super().<strong>init</strong>()<br>
init_out_channels = 64<br>
num_conv_layers = 1<br>
self.module_dict = nn.ModuleDict()</p>
<pre><code>    for depth in range(model_depth):
        out_channels = (2 ** depth) * init_out_channels

        for layer in range(num_conv_layers):
            r2_conv_layer = RRCNNBlock(in_channels, out_channels)
            self.module_dict["r2_conv_{}".format(depth)] = r2_conv_layer
            in_channels, out_channels = out_channels, out_channels * 2

        if depth != model_depth - 1:
            max_pool = nn.MaxPool3d(kernel_size=pooling, stride=2, padding=0)
            self.module_dict["max_pool_{}".format(depth)] = max_pool

def forward(self, x):
    # TODO: Maybe change to tensor instead of list
    synthesis_features = []
    for key, layer in self.module_dict.items():
        x = layer(x).contiguous()

        if key.startswith("r2_conv"):
            synthesis_features.append(x)

    return x, synthesis_features
</code></pre>
<p>class R2SynthesisBlock(nn.Module):<br>
def <strong>init</strong>(self, out_channels, model_depth=4):<br>
super().<strong>init</strong>()<br>
init_out_channels = 128<br>
num_conv_layers = 1<br>
self.module_dict = nn.ModuleDict()</p>
<pre><code>    for depth in range(model_depth - 2, -1, -1):
        channels = (2 ** depth) * init_out_channels
        up_conv = UpConv(in_channels=channels, out_channels=channels)
        self.module_dict["deconv_{}".format(depth)] = up_conv

        att_gate = AttentionBlock(channels, channels // 2, channels // 2)
        self.module_dict["att_gate_{}".format(depth)] = att_gate

        for layer in range(num_conv_layers):
            in_channels, feat_channels = channels // 2, channels // 2
            if layer == 0:
                in_channels = in_channels + channels

            r2_conv_layer = RRCNNBlock(in_channels=in_channels, out_channels=feat_channels)
            self.module_dict["r2_conv_{}".format(depth)] = r2_conv_layer

        if depth == 0:
            # TODO Figure out kernel size + padding + stride for final
            # 1 x 1 x 1 conv
            final_conv = nn.Conv3d(in_channels=feat_channels, out_channels=out_channels, kernel_size=1, padding=2)
            self.module_dict["final_conv"] = final_conv

def forward(self, x, high_res_features):
    for key, layer in self.module_dict.items():
        if key.startswith("deconv"):
            g = layer(x)
            # Enforce same size

        elif key.startswith("att_gate"):
            features = high_res_features[int(key[-1])][:, :, 0:g.size()[2], 0:g.size()[3],
                       0:g.size()[4]].contiguous()
            features = layer(g=g, x=features)
            x = torch.cat((features, g), dim=1).contiguous()
        else:
            x = layer(x)
    return x
</code></pre>
<p>class UNet(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels, model_depth=5, pooling=2, final_activation=“softmax”):<br>
super().<strong>init</strong>()<br>
self.encoder = AnalysisBlock(in_channels=in_channels, model_depth=model_depth, pooling=pooling)<br>
self.encoder.cuda()<br>
self.decoder = SynthesisBlock(out_channels=out_channels, model_depth=model_depth)<br>
self.decoder.cuda()</p>
<pre><code>    if final_activation == "softmax":
        self.final = nn.Softmax(dim=1)
        self.final.cuda()
    elif final_activation == "sigmoid":
        self.final = nn.Sigmoid()
        self.final.cuda()
    else:
        self.final = None
    # TODO other final layers

def forward(self, x):
    x, features = self.encoder(x)
    x = self.decoder(x, features)
    if self.final:
        x = self.final(x)

    return x
</code></pre>
<p>class R2AttUNet(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels, model_depth=4, pooling=2, final_activation=“softmax”, t=2):<br>
super().<strong>init</strong>()</p>
<pre><code>    super().__init__()
    self.encoder = R2AnalysisBlock(in_channels=in_channels, model_depth=model_depth, pooling=pooling)
    self.encoder.cuda()
    self.decoder = R2SynthesisBlock(out_channels=out_channels, model_depth=model_depth)
    self.decoder.cuda()

    if final_activation == "softmax":
        self.final = nn.Softmax(dim=1)
        self.final.cuda()
    elif final_activation == "sigmoid":
        self.final = nn.Sigmoid()
        self.final.cuda()
    else:
        self.final = None
    # TODO other final layers

def forward(self, x):
    x, features = self.encoder(x)
    x = self.decoder(x, features)
    if self.final:
        x = self.final(x)

    return x
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
inputs = torch.randn(1, 1, 32, 32, 32)<br>
print("The shape of inputs: ", inputs.shape)<br>
model = R2AttUNet(in_channels=1, out_channels=2 )<br>
inputs = inputs.cuda()<br>
model.cuda()<br>
x = model(inputs)<br>
print(x.shape)<br>
summary(model, x)<br>
for →    inputs = torch.randn(1, 1, 32, 32, 32)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ecea7792607b670fea8edf1b6cf0a8f30196f5be.png" data-download-href="https://discuss.pytorch.org/uploads/default/ecea7792607b670fea8edf1b6cf0a8f30196f5be" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ecea7792607b670fea8edf1b6cf0a8f30196f5be.png" alt="error" data-base62-sha1="xNR1DzapbBtgBRsiepNbPELLUSi" width="690" height="489" 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">error</span><span class="informations">722×512 10.7 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>

Farhan_Abbas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Can you suggest any solution?</p>