Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

python - Loading original images besides the transformed ones using ImageFolder

I'm trying to train a GAN to colorize images. For that, I'm using ImageFolder of torchvision to load grayscale images but I also need the original data alongwith the transformed ones.

I want it in the fastest way as the data is large. I want to make ImageFolder load both at the same time to reduce the time complexity.

def load_data_bw(opt):
    datapath = '/content/gdrive/My Drive/faces/2003'

    dataset = torchvision.datasets.ImageFolder(datapath,
                       transform=transforms.Compose([
                                                    transforms.Grayscale(num_output_channels=3), #load images as grayscale with three channels
                                                    transforms.RandomChoice(
                                                       [transforms.Resize(opt['loadSize'], interpolation=1),
                                                        transforms.Resize(opt['loadSize'], interpolation=2),
                                                        transforms.Resize(opt['loadSize'], interpolation=3),
                                                        transforms.Resize((opt['loadSize'], opt['loadSize']),
                                                                          interpolation=1),
                                                        transforms.Resize((opt['loadSize'], opt['loadSize']),
                                                                          interpolation=2),
                                                        transforms.Resize((opt['loadSize'], opt['loadSize']),
                                                                          interpolation=3)]
                                                    ),
                                                    transforms.RandomChoice(
                                                       [transforms.RandomResizedCrop(opt['fineSize'], interpolation=1),
                                                        transforms.RandomResizedCrop(opt['fineSize'], interpolation=2),
                                                        transforms.RandomResizedCrop(opt['fineSize'], interpolation=3)]
                                                    ),
                                                    transforms.ColorJitter(brightness=0.1, contrast=0.1),
                                                    transforms.RandomHorizontalFlip(),
                                                    transforms.ToTensor()
                                               ]))
    return dataset

I'm expecting to get:

for iteration, orig_data, gray_data in enumerate(training_data_loader, 1):
    # code..
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I assume you have 2 dataset variables i.e. dataset_bw and dataset_color that you can load as you mention using ImageFolder. Then you could do the following :

class GAN_dataset(Dataset):
    def __init__(self, dataset_bw, dataset_color):
        self.dataset1 = dataset_bw
        self.dataset2 = dataset_color

    def __getitem__(self, index):
        x1 = self.dataset1[index]
        x2 = self.dataset2[index]

        return x1, x2

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

dataset = GAN_dataset(dataset_bw, dataset_color)
loader = DataLoader(dataset, batch_size = ...)

This way you when you iterate through loader, you will get two images as you require.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...