Transforms for greyscale to RGB

hi, i have grayscale images of shape (1,48,48) i want to convert them into RGB image. i want to use pretrained model but my images are in greyscale and my dataset is csv which contains pixel values . please suggest me some ideas
@ptrblck @tom

img = img.expand(3, -1, -1) might be a start. But I’m quite confident that there are many other helpful people around here could have helped you with it.

Best regards

Thomas

my images are in numpy array loaded from csv file

train _X shape: (28709, 1, 48, 48) train _Y shape: (28709,)
val _X shape: (3589, 1, 48, 48) val _Y shape: (3589,)
test _X shape: (3589, 1, 48, 48) test _Y shape: (3589,)

i tried train_X[0].expand(3,-1,-1) and it dint worked .

i created a custom dataset and trying to apply transforms but transforms.ToPILImage is not accepting my image . so i thought to convert into RGB but dont know how to do it

class MyDataset(Dataset):
def __init__(self, data, targets, transform=None):
    self.data = data
    self.targets = torch.LongTensor(targets)
    self.transform = transform

def __getitem__(self, index):
    x = self.data[index]
    y = self.targets[index]
    if self.transform:
        x = Image.fromarray(self.data[index].astype(np.uint8).transpose(1,2,0)).convert('RGB')
        x = self.transform(x)
    return x, y

def __len__(self):
    return len(self.data)

transform = transforms.Compose([transforms.ToPILImage(),
                            transforms.Resize(256),
                            transforms.RandomHorizontalFlip(),
                            transforms.RandomVerticalFlip(),
                            transforms.RandomRotation(15),
                            transforms.CenterCrop(224),
                            transforms.ToTensor(),
                            transforms.Normalize([0.5, 0.5, 0.5],
                                                [0.5, 0.5, 0.5])])
train_data= MyDataset(train_X, train_Y, transform=transform)
valid_data = MyDataset(val_X, val_Y, transform=transform)
test_data = MyDataset(test_X, test_Y, transform=transform)

This is thowring an error TypeError: Cannot handle this data type: (1, 1, 1), |u1

train_X_rgb = train_X.expand(-1, 3,-1,-1)
now you have three channels (RGB) with the value of the first.

it didnt worked, got
error :The expanded size of the tensor (-1) isn’t allowed in a leading, non-existing dimension 0

If your train_X had shape (28709, 1, 48, 48) as you said, you would be able to call .expand(-1, 3, -1, -1) on it to get (28709, 3, 48, 48). But you can also grab the general idea of expanding the size-1 channel dimension to three and apply it to any other shape.

Best regards

Thomas

thnaks for your help
hi i solved it using repeat(3,1,1) on each image

murali_perumalla, how did you use repeat(3,1,1) on each image? can you please explain with example?

hi, sorry for late reply . i have created a dataset class and i used .repeat function in that class to convert every image into RGB.

class MyDataset(Dataset):

    def __init__(self, data, targets, transform=None):

        self.data = data

        self.targets = torch.LongTensor(targets)

        self.transform = transform

    def __getitem__(self, index):

        x = self.data[index]

        x = torch.from_numpy(x)

        y = self.targets[index]

        if self.transform:

            x=x.repeat(3,1,1)

            x = self.transform(x)

        return x, y

    def __len__(self):

        return len(self.data)