Berkay Çamur
Berkay Çamur

Reputation: 5

Resizing inputs for torch model

I'm facing with this error properly and I could not see any exact solution or a solution formula for this error. My inputs are like (48x48) and that's not matching with the input shape of the resnet101. How can I edit my input to fit to the resnet101? You can see my code below, it probably helps you to understand my problem.

if __name__ == "__main__":
    vid = cv2.VideoCapture(0)
    emotions = []
    while vid.isOpened():
        image = cv2.imread("/home/berkay/Desktop/angry_man.jpg")
        _, frame = vid.read()

        # takes in a gray coloured filter of the frame
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # initializing the haarcascade face detector
        faces = face_cascade.detectMultiScale(frame)
        for (x,y,w,h) in faces:
            
            # takes the region of interest of the face only in gray
            roi_gray = gray[y:y+h, x:x+h]
            resized = cv2.resize(roi_gray, (48, 48))    # resizes to 48x48 sized image

            # predict the mood
            img = img2tensor(resized)
            prediction = predict(img)

In that point, I'm getting this error:

weight of size [64, 3, 7, 7], expected input[1, 1, 229, 229] to have 3 channels, but got 1 channels instead

How can I fix this? Thanks in advance

Upvotes: 0

Views: 147

Answers (1)

ndrwnaguib
ndrwnaguib

Reputation: 6115

You can modify the input layer of resnet so that it would accept a single-channel tensors inputs using

In [1]: model = resnet101()
In [2]: model.conv1 = nn.Conv2d(1, 64, kernel_size=(2, 2))
In [3]: model(torch.rand(10, 1, 48, 48))
Out[3]: 
tensor([[-0.5015,  0.6124,  0.1370,  ...,  1.2181, -0.4707,  0.3285],
        [-0.4776,  1.1027,  0.0161,  ...,  0.6363, -0.4733,  0.6218],
        [-0.3935,  0.8276, -0.0316,  ...,  0.6853, -0.4735,  0.6424],
        ...,
        [-0.2986,  1.1758,  0.0158,  ...,  0.7422, -0.4422,  0.4792],
        [-0.2668,  0.7884, -0.1205,  ...,  1.1445, -0.6249,  0.6697],
        [-0.2139,  1.0412,  0.2326,  ...,  0.8332, -0.8744,  0.4827]],
       grad_fn=<AddmmBackward0>)

(you will probably need to modify the kernel size accordingly too)

Upvotes: 1

Related Questions