Transforms for greyscale to RGB

murali_perumalla: <p>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<br>
<a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/tom">@tom</a></p>

tom: <p><code>img = img.expand(3, -1, -1)</code> might be a start. But I’m quite confident that there are many other helpful people around here could have helped you with it.</p>
<p>Best regards</p>
<p>Thomas</p>

murali_perumalla: <p>my images are in numpy array loaded from csv file</p>
<p>train _X shape: (28709, 1, 48, 48) train _Y shape: (28709,)<br>
val _X shape: (3589, 1, 48, 48) val _Y shape: (3589,)<br>
test _X shape: (3589, 1, 48, 48) test _Y shape: (3589,)</p>
<p>i tried train_X[0].expand(3,-1,-1) and it dint worked .</p>
<p>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</p>
<pre><code>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)
</code></pre>
<p>This is thowring an error  TypeError: Cannot handle this data type: (1, 1, 1), |u1</p>

tom: <aside class="quote no-group" data-username="murali_perumalla" data-post="3" data-topic="70997">
<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/murali_perumalla/48/20381_2.png" class="avatar"> murali_perumalla:</div>
<blockquote>
<p>train_X[0].expand(3,-1,-1)</p>
</blockquote>
</aside>
<p><code>train_X_rgb = train_X.expand(-1, 3,-1,-1)</code><br>
now you have three channels (RGB) with the value of the first.</p>

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

tom: <p>If your train_X had shape (28709, 1, 48, 48) as you said, you would be able to call <code>.expand(-1, 3, -1, -1)</code> 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.</p>
<p>Best regards</p>
<p>Thomas</p>

murali_perumalla: <p>thnaks for your help<br>
hi i solved it using repeat(3,1,1) on each image</p>

Md_Golam_Morshed: <p>murali_perumalla, how did you use repeat(3,1,1) on each image? can you please explain with example?</p>

murali_perumalla: <p>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.</p>
<pre><code class="lang-auto">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)
</code></pre>