简体   繁体   English

我可以在训练深度学习 model 时对图像进行预转换吗?

[英]Can I pre-transform images when training a deep learning model?

If you ever trained a model, you sometimes can see DataLoader (eg I am using pytorch Dataloader) could be the bottleneck because everytime you get an training sample from the dataset, the data transformation will be performed on-the-fly.如果您曾经训练过 model,您有时会看到 DataLoader(例如,我使用的是 pytorch Dataloader)可能是瓶颈,因为每次从数据集中获取训练样本时,数据转换都会即时执行。 Here I take the getitem func of DatasetFolder from torchvision.datasets as an example.这里我以torchvision.datasetstorchvision.datasets的getitem func为例。

    path, target = self.samples[index]
    sample = self.loader(path)
    if self.transform is not None:
        sample = self.transform(sample)
    if self.target_transform is not None:
        target = self.target_transform(target)

    return sample, target

I wonder can we pre-processed the images (eg ImageNet) in advance to tensors and save to disk.我想知道我们是否可以提前将图像(例如 ImageNet)预处理为张量并保存到磁盘。 Then we modify the __getitem__ function to get these tensors directly from disk.然后我们修改__getitem__ function 以直接从磁盘获取这些张量。 How efficient is this approach?这种方法的效率如何? Anyone has tried this solution before?以前有人试过这个解决方案吗?

I think that maybe the loading from disk will burden and likely become a new bottleneck (instead of data transform we have before).我认为也许从磁盘加载会增加负担,并可能成为一个新的瓶颈(而不是我们之前的数据转换)。 Another thing is the size, for example, one ImageNet image takes 74 MB when being saved as tensors using standard transformation:另一件事是大小,例如,一个 ImageNet 图像在使用标准转换保存为张量时需要 74 MB:

transforms.Compose([
                transforms.Resize(256),
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ])

Two approaches:两种方法:

1)You can cache the Dataset if you have enough memory and use it later. 1)如果您有足够的memory,您可以缓存数据集并稍后使用。 The limitation is you are limited to 0 workers in the first epoch.限制是您在第一个时期被限制为0个工人。 Something like this.像这样的东西。

class ImageNetDataset(Dataset):
    def __init__(self, use_cache=False):
        self.cached_data = []
        self.use_cache = use_cache
        
    def __getitem__(self, index):
        if not self.use_cache:
            x = self.data[index] 
            self.cached_data.append(x)
        else:
            x = self.cached_data[index]
        return x
    
    def set_use_cache(self, use_cache):
        if use_cache:
            self.cached_data = torch.stack(self.cached_data)
        else:
            self.cached_data = []
        self.use_cache = use_cache

2)Transform the dataset beforehand and save it to disk. 2)预先转换数据集并将其保存到磁盘。 You need to write a small code for that separately.您需要为此单独编写一个小代码。 And use this folder during training.并在训练期间使用此文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM