Problem in U-Net implementation

randinoo: <pre><code class="lang-auto">class UNet_down_block(torch.nn.Module):
    def __init__(self, input_channel, output_channel, down_size):
        super(UNet_down_block, self).__init__()
        self.conv1 = torch.nn.Conv2d(input_channel, output_channel, 3, padding=1)
        self.bn1 = torch.nn.BatchNorm2d(output_channel)
        self.conv2 = torch.nn.Conv2d(output_channel, output_channel, 3, padding=1)
        self.bn2 = torch.nn.BatchNorm2d(output_channel)
        self.conv3 = torch.nn.Conv2d(output_channel, output_channel, 3, padding=1)
        self.bn3 = torch.nn.BatchNorm2d(output_channel)
        self.max_pool = torch.nn.MaxPool2d(2, 2)
        self.relu = torch.nn.ReLU()
        self.down_size = down_size

    def forward(self, x):
        if self.down_size:
         x = self.max_pool(x)
         x = self.relu(self.bn1(self.conv1(x)))
         x = self.relu(self.bn2(self.conv2(x)))
         x = self.relu(self.bn3(self.conv3(x)))
        return x

class UNet_up_block(torch.nn.Module):
    def __init__(self, prev_channel, input_channel, output_channel):
        super(UNet_up_block, self).__init__()
        self.up_sampling = torch.nn.Upsample(scale_factor=2, mode='bilinear')
        self.conv1 = torch.nn.Conv2d(prev_channel + input_channel, output_channel, 3, padding=1)
        self.bn1 = torch.nn.BatchNorm2d(output_channel)
        self.conv2 = torch.nn.Conv2d(output_channel, output_channel, 3, padding=1)
        self.bn2 = torch.nn.BatchNorm2d(output_channel)
        self.conv3 = torch.nn.Conv2d(output_channel, output_channel, 3, padding=1)
        self.bn3 = torch.nn.BatchNorm2d(output_channel)
        self.relu = torch.nn.ReLU()

    def forward(self, prev_feature_map, x):
        x = self.up_sampling(x)
        x = torch.cat((x, prev_feature_map), dim=1)
        x = self.relu(self.bn1(self.conv1(x)))
        x = self.relu(self.bn2(self.conv2(x)))
        x = self.relu(self.bn3(self.conv3(x)))
        return x
class UNet(torch.nn.Module):
    def __init__(self):
        super(UNet, self).__init__()

        self.down_block1 = UNet_down_block(1, 16, False)
        self.down_block2 = UNet_down_block(16, 32, True)
        self.down_block3 = UNet_down_block(32, 64, True)
        self.down_block4 = UNet_down_block(64, 128, True)
        self.down_block5 = UNet_down_block(128, 256, True)
        self.down_block6 = UNet_down_block(256, 512, True)
        self.down_block7 = UNet_down_block(512, 1024, True)

        self.mid_conv1 = torch.nn.Conv2d(1024, 1024, 3, padding=1)
        self.bn1 = torch.nn.BatchNorm2d(1024)
        self.mid_conv2 = torch.nn.Conv2d(1024, 1024, 3, padding=1)
        self.bn2 = torch.nn.BatchNorm2d(1024)
        self.mid_conv3 = torch.nn.Conv2d(1024, 1024, 3, padding=1)
        self.bn3 = torch.nn.BatchNorm2d(1024)

        self.up_block1 = UNet_up_block(512, 1024,512)
        self.up_block2 = UNet_up_block(256, 512, 256)
        self.up_block3 = UNet_up_block(128, 256, 128)
        self.up_block4 = UNet_up_block(64, 128, 64)
        self.up_block5 = UNet_up_block(32, 64, 32)
        self.up_block6 = UNet_up_block(16, 32, 16)

        self.last_conv1 = torch.nn.Conv2d(16, 16, 3, padding=1)
        self.last_bn = torch.nn.BatchNorm2d(16)
        self.last_conv2 = torch.nn.Conv2d(16, 1, 1, padding=0)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        self.x1 = self.down_block1(x)
        self.x2 = self.down_block2(self.x1)
        self.x3 = self.down_block3(self.x2)
        self.x4 = self.down_block4(self.x3)
        self.x5 = self.down_block5(self.x4)
        self.x6 = self.down_block6(self.x5)
        self.x7 = self.down_block7(self.x6)
        self.x7 = self.relu(self.bn1(self.mid_conv1(self.x7)))
        self.x7 = self.relu(self.bn2(self.mid_conv2(self.x7)))
        self.x7 = self.relu(self.bn3(self.mid_conv3(self.x7)))
        x = self.up_block1(self.x6, self.x7)
        x = self.up_block2(self.x5, x)
        x = self.up_block3(self.x4, x)
        x = self.up_block4(self.x3, x)
        x = self.up_block5(self.x2, x)
        x = self.up_block6(self.x1, x)
        x = self.relu(self.last_bn(self.last_conv1(x)))
        x = self.last_conv2(x)
        return x

</code></pre>
<p>RuntimeError:  Given groups=1, weight of size [32, 16, 3, 3], expected input[4592, 1, 10, 10] to have 16 channels, but got 1 channels instead</p>
<p>help me plzzzz</p>

tom: <p>The indentation of the down block’s forward is broken, you want to dedent the conv calls.</p>
<p>A bit of unsolicited advice: I would advise against assigning to self.x<i>something</i> in the forward but use local variables instead unless you have very good reasons to need them after the forward has completed.</p>

randinoo: <p>how please <a class="mention" href="/u/tom">@tom</a>  <img src="https://discuss.pytorch.org/images/emoji/apple/pensive.png?v=9" title=":pensive:" class="emoji" alt=":pensive:"> <img src="https://discuss.pytorch.org/images/emoji/apple/pensive.png?v=9" title=":pensive:" class="emoji" alt=":pensive:"></p>

randinoo: <p>RuntimeError: The size of tensor a (2) must match the size of tensor b (3332) at non-singleton dimension 3</p>

the-dharma-bum: <p>What are you giving as input to your model ?<br>
Beside the indentation error pointed out by tom above, your code is correct.<br>
The input must be a 4 dimensional tensor alike (batch_size, channels, height, width)<br>
For instance:</p>
<pre><code class="lang-python">dummy_input = torch.randn(1, 1, 256, 256)
model = UNet()
output = model(dummy_input)
print(output.size()) # torch.Size([1, 1, 256, 256])
</code></pre>
<p>With the indentation fixed, I tried this and it worked.</p>

randinoo: <p>using this code</p>
<pre><code class="lang-auto">class DiscriminatorNet(torch.nn.Module):
    """
    A three hidden-layer discriminative neural network
    """
    def __init__(self):
        super(DiscriminatorNet, self).__init__()
        n_features = 40
        n_out = 1
        
        self.hidden0 = nn.Sequential( 
            nn.Linear(n_features, 1024),
            nn.LeakyReLU(0.2),
            nn.Dropout(0.3)
        )
        self.hidden1 = nn.Sequential(
            nn.Linear(1024, 512),
            nn.LeakyReLU(0.2),
            nn.Dropout(0.3)
        )
        self.hidden2 = nn.Sequential(
            nn.Linear(512, 256),
            nn.LeakyReLU(0.2),
            nn.Dropout(0.3)
        )
        self.out = nn.Sequential(
            torch.nn.Linear(256, n_out),
            torch.nn.Sigmoid()
        )

    def forward(self, x):
        x = self.hidden0(x)
        x = self.hidden1(x)
        x = self.hidden2(x)
        x = self.out(x)
        return x

ValueError: Using a target size (torch.Size([2604, 1])) that is different to the input size (torch.Size([2604, 1, 40, 1])) is deprecated. Please ensure they have the same size.
</code></pre>

the-dharma-bum: <p>I’m not sure what you’re trying to do.<br>
I assume your error comes from a call to some criterion.<br>
If you want to classify 40 features and you have 2604 samples, your input tensor should be of size (2604, 40).</p>
<p>For instance, the following code works:</p>
<pre><code class="lang-python">criterion = nn.BCEWithLogitsLoss()

# 2604 1D vectors of length 40
dummy_input  = torch.randn(2604, 40)
# 2604 integers in [0, 1], casted to float.
dummy_target = torch.randint(2, (2604, 1)).float()  

model  = DiscriminatorNet()
output = model(dummy_input)

print(output.size())        # torch.Size([2604, 1])
print(dummy_target.size())  # torch.Size([2604, 1])

loss = criterion(dummy_target, output)
print(isinstance(loss.item(), float)) # True
</code></pre>
<p>I can only suggest you to identify which torch function actually causes the error you face and to check the relevent pytorch documentation.</p>

randinoo: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/4/04969c2781019268c205f510ef7f48967909c7ed.png" data-download-href="https://discuss.pytorch.org/uploads/default/04969c2781019268c205f510ef7f48967909c7ed" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/4/04969c2781019268c205f510ef7f48967909c7ed.png" alt="image" data-base62-sha1="EAAB06KCT8uS1UYe1fgpowVdCd" width="690" height="340" data-dominant-color="F6F5F8"><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">image</span><span class="informations">848×418 22.1 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>help me plz <a class="mention" href="/u/the-dharma-bum">@the-dharma-bum</a></p>
<pre><code class="lang-auto">with torch.no_grad():
        output1 = model((train_x.float()))     

        softmax = torch.exp(output1).cpu()    
        prob1 = list(softmax.numpy())
        predictions = np.argmax(prob1, axis=1)
        print('Validation accuracy train: {:.4f}%'.format(float(accuracy_score(train_y, predictions, normalize =False) * 100)))
</code></pre>

the-dharma-bum: <p>Hard to say what is the error here without knowing what are train_y and output1.</p>
<p>The following sample code may help you:</p>
<pre><code class="lang-python">batch_size  = 64
num_classes = 2

dummy_outputs = torch.randn(batch_size, num_classes)
dummy_targets = torch.randint(num_classes, size=(batch_size, ))

predictions = dummy_outputs.softmax(dim=1).argmax(dim=1).numpy()
targets     = dummy_targets.numpy()

accuracy = accuracy_score(targets, predictions)

print(accuracy) # around 0.5 since random predictions and random targets
</code></pre>

randinoo: <pre><code class="lang-auto">def train(epoch):
    model.train()
    tr_loss = 0
    x_train, y_train = Variable(train_x), Variable(train_y)
    x_val, y_val = Variable(val_x), Variable(val_y)
    if torch.cuda.is_available():
        x_train = x_train.cuda()
        y_train = y_train.cuda()
        x_val = x_val.cuda()
        y_val = y_val.cuda()

    
    optimizer.zero_grad()
    output_train = model(x_train.float())
    output_val = model(x_val.float())
    output_train  = torch.randn(2604, 1)
    y_train = torch.randint(2, (2604, 1)).float() 
    loss_train = criterion(output_train, y_train)
    output_val  = torch.randn(2604, 1)
    y_val = torch.randint(2, (2604, 1)).float() 
    loss_val = criterion(output_val, y_val)
    train_losses.append(loss_train)
    val_losses.append(loss_val)
</code></pre>
<pre><code class="lang-auto">with torch.no_grad():
        output1 = model((train_x.float()))     
        softmax1 = torch.exp(output1).cpu()    
        prob1 = list(softmax1.numpy())
        predictions1 = np.argmax(prob1, axis=1)
        predictions1 = predictions1.argmax()
        train_y = train_y.numpy()
        print('Validation accuracy train: {:.4f}%'.format(float(accuracy_score(train_y, predictions1) * 100)))
</code></pre>
<p><a class="mention" href="/u/the-dharma-bum">@the-dharma-bum</a>  error<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/5/255c137093a7e60855d64ee04ade3fe9a3328f51.png" alt="image" data-base62-sha1="5kuWap8ZVrEpKSifDtgcG8wCKOJ" width="682" height="135"></p>