I use the Opencv to read images, and then convert them to tensor to feed DNN for inference. The reason is that I need to use Opencv to handle the images and then DNN inference. However, when I use transforms to preprocess the images. It gives the error: “ValueError: pic should be 2/3 dimensional. Got 4 dimensions.”
The code is
for index in range(len(datasets)):
    img = datasets[index]
    frame = cv2.imread(img)
    frame = frame * (1/255)
    frame = cv2.resize(frame , (224, 224))
    frame = np.transpose(frame , (2, 0, 1))
    if index == 0:
        frames = frame 
        continue
    frames = np.stack((frames , frame))

 transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])

frames = transform(frames ).to('cuda')

Then it has the error “ValueError: pic should be 2/3 dimensional. Got 4 dimensions.” The shape of frames is (2, 3, 224, 224). 2 is the number of batches.
I don’t know how to deal with this error. Thank you for any help.