Issue
In PyTorch data loader, how could I concatenate an image (let's say x.jpg) in-band wise to each and every input images. ie, in effect I will have 4 band input ( 3 band input jpg with 1 band x.jpg. How to implement it.
Please find below example of my current dataloader just to load the images. To this, I want to add x.jpg to "image"(ie input image, not to mask)
from PIL import Image
class lakeDataSet(Dataset):
def __init__(self, root, transform):
super().__init__()
self.root = root
self.img_dir = os.path.join(root,'image-c3/c3-crop') #9UAV
self.mask_dir = os.path.join(root,'label-c3/c3-crop')
# self.mask_dir = os.path.join(root,'test')
self.files = [fname for fname in os.listdir(self.img_dir) if fname.endswith('.jpg')]
self.transform = transform
def __len__(self):
return len(self.files)
def __getitem__(self,I):
fname = self.files[i]
img_path = os.path.join(self.img_dir, fname)
mask_path = os.path.join(self.mask_dir, fname)
img = self.transform(Image.open(img_path))
mask = self.transform(Image.open(mask_path))
return img, mask
Solution
I suppose the self.transform
already has ToTensor
. Otherwise you should specify it as well.
Then you can just concat the first dimension. Like
x_jpg = self.transform(Image.open('x.jpg'))
img = torch.cat((img, x_jpg), 0)
The x.jpg
has to have only 1 channel, if it's an RGB then obviously it'll become 6 channels instead of 4.
Answered By - Natthaphon Hongcharoen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.