Problem in U-Net implementation

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

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

help me plzzzz

The indentation of the down block’s forward is broken, you want to dedent the conv calls.

A bit of unsolicited advice: I would advise against assigning to self.xsomething in the forward but use local variables instead unless you have very good reasons to need them after the forward has completed.

how please @tom :pensive: :pensive:

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

What are you giving as input to your model ?
Beside the indentation error pointed out by tom above, your code is correct.
The input must be a 4 dimensional tensor alike (batch_size, channels, height, width)
For instance:

dummy_input = torch.randn(1, 1, 256, 256)
model = UNet()
output = model(dummy_input)
print(output.size()) # torch.Size([1, 1, 256, 256])

With the indentation fixed, I tried this and it worked.

using this code

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.

I’m not sure what you’re trying to do.
I assume your error comes from a call to some criterion.
If you want to classify 40 features and you have 2604 samples, your input tensor should be of size (2604, 40).

For instance, the following code works:

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

I can only suggest you to identify which torch function actually causes the error you face and to check the relevent pytorch documentation.

help me plz @the-dharma-bum

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)))

Hard to say what is the error here without knowing what are train_y and output1.

The following sample code may help you:

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
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)
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)))

@the-dharma-bum error
image