Error for converting numpy images to tensor

torch_fresh: <p>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.”<br>
The code is</p>
<pre><code class="lang-auto">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')
</code></pre>
<p>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.</p>
<p>I don’t know how to deal with this error. Thank you for any help.</p>

tcapelle: <aside class="quote no-group" data-username="torch_fresh" data-post="1" data-topic="112883">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/torch_fresh/48/19272_2.png" class="avatar"> torch_fresh:</div>
<blockquote>
<pre><code class="lang-auto">transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])
</code></pre>
</blockquote>
</aside>
<p>The <code>ToTensor</code> tfm is expecting a channel last image, this works:</p>
<pre><code class="lang-auto">transform(np.random.random((224,244, 3)))
</code></pre>
<p>it is also not expecting a batch. From the dos:</p>
<blockquote>
<p>Convert a <code>PIL Image</code> or <code>numpy.ndarray</code> to tensor.</p>
</blockquote>
<p>It expects one PIL image.</p>

Eta_C: <p>Let me show some examples</p>
<p>example 1: use OpenCV and do transforms for each image</p>
<pre><code class="lang-python">transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
frames = []
for index in range(2):
    # read image
    frame = (np.random.rand(256, 256, 3) * 255).astype(np.uint8)

    # resize image
    frame = frame * (1/255)
    frame = cv2.resize(frame, (224, 224))
    frame = transform(frame)
    if index == 0:
        frames = [frame]
        continue
    frames.append(frame)

frames = torch.stack(frames)
</code></pre>
<p>example 2: use OpenCV and do transform after collecting all images</p>
<pre><code class="lang-python">frames = []
for index in range(2):
    # read image
    frame = (np.random.rand(256, 256, 3) * 255).astype(np.uint8)

    # resize image
    frame = frame * (1/255)
    frame = cv2.resize(frame, (224, 224))
    frame = np.transpose(frame, (2, 0, 1))
    if index == 0:
        frames = [frame]
        continue
    frames.append(frame)

frames = np.stack(frame, axis=0)
# transform
mean, std = torch.tensor([0.485, 0.456, 0.406]), torch.tensor([0.229, 0.224, 0.225])
frames = (torch.from_numpy(frames) - mean[None, :, None, None]) / std[None, :, None, None]
</code></pre>