BGR to RGB for CUB_200 images by Image.split()
I am creating a PyTorch dataset and dataloader from CUB_200. When reading the images as pill, I need to change the BGR channels to RGB and I use the following code:
def _read_images_from_list(imagefile_list):

   imgs = []
   mean=[0.485, 0.456, 0.406]
   std= [0.229, 0.224, 0.225]

   Transformations = transforms.Compose([transforms.Resize([224, 224]), transforms.ToTensor(), transforms.Normalize(mean, std)])

   for imagefile in imagefile_list:
      # read images as PIL instead of NUMPY
      img = Image.open(imagefile)
      b, g, r = img.split()
      img = Image.merge(&quot;RGB&quot;, (r, g, b))
   
      img = Transformations(img) # ToTensor and between [0,1], then normalized using image net mean and std, then transposed into shape (C,H,W)

     imgs += [img]
return imgs

After going through a number of classes, I get the following error.
ValueError: not enough values to unpack (expected 3, got 1)

I wonder what should I do now? it means that one of the images has only one channel instead of one. Can this be the case or there is a problem with my code? I had a different implementation before but it worked. The reason I changed this implementation was that I could not normalize my images.
This is the old implementation:
def _read_images_from_list(imagefile_list):
  imgs = []
  for imagefile in imagefile_list:
    
    img = cv2.imread(imagefile).astype(np.float32)
    img = cv2.resize(img, (224, 224))
    # Convert RGB to BGR
    img_r, img_g, img_b = np.split(img, 3, axis=2)
    img = np.concatenate((img_b, img_g, img_r), axis=2)
    # Extract mean
    img -= np.array((103.94,116.78,123.68), dtype=np.float32)  # BGR mean
    
    # HWC -&gt; CHW, compatible with pytorch
    img = np.transpose(img, [2, 0, 1])
    imgs += [img]
return imgs


I would strongly recommend you use skimage.io to load your images, not opencv. It opens the images in RGB format by default, removing your shuffling overhead, but if you want to convert BGR to RGB you can use this:
import numpy as np

img = np.arange(27).reshape(3,3,3)
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]

rgb = np.dstack([r,g,b])

print(img)
print(&quot;#&quot;*20)
print(rgb)

