Expected 4 dimensional weight, expected 3

import math
from copy import copy
from pathlib import Path

import numpy as np
import pandas as pd
import requests
import torch
import torch.nn as nn
from PIL import Image
from torch.cuda import amp

def autopad(k, p=None):  # kernel, padding
    # Pad to 'same'
    if p is None:
        p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-pad
    return p

class Conv(nn.Module):
    # Standard convolution
    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groups
        super(Conv, self).__init__()
        self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
        self.bn = nn.BatchNorm2d(c2)
        self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())

    def forward(self, x):
        print("Conv forward")
        return self.act(self.bn(self.conv(x)))

    def fuseforward(self, x):
        return self.act(self.conv(x))

class Focus(nn.Module):
    # Focus wh information into c-space
    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groups
        super(Focus, self).__init__()
        self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
        # self.contract = Contract(gain=2)

    def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
        return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
        # return self.conv(self.contract(x))

x = torch.ones(3,416,416)
print("input dimension : ",x.shape)
print(x[..., ::2, ::2].shape)
print(x[..., 1::2, ::2].shape)
print(x[..., ::2, 1::2].shape)
print(x[..., 1::2, 1::2].shape)
x = torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]])
focuslayer = Focus(3,6)
x = focuslayer.conv(x)

error message :

Expected 4-dimensional input for 4-dimensional weight [6, 12, 1, 1], but got 3-dimensional input of size [12, 208, 208] instead

Can anyone please help me debug this ? I am a beginner trying to understand how layers work.

Usually conv layers expect a 4 dimensional input where the first dimension is the batch dimension. If you are trying to pass a single image or example can you try something like x.unsqueeze(0) to add a batch dimension?

1 Like

Thank you so much sir.