创建自定义数据集功能时拆分图像

发布于 2025-02-10 02:48:50 字数 962 浏览 1 评论 0原文

我有5000x5000x3尺寸的图像,我想将图像分为多个较小的图像。我试图创建具有分裂图像的数据集。但是它占据了更多的空间,管理这些图像是非常繁琐的任务。然后,我尝试创建一条管道,在训练时拆分图像。但是,如何将其作为Pytorch自定义数据集类别进行混淆。

import os
import pandas as pd
from torchvision.io import read_image

class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
          self.img_labels = pd.read_csv(annotations_file)
          self.img_dir = img_dir
          self.transform = transform
          self.target_transform = target_transform

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

  def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label

I have 5000x5000X3 sized images, I want to split image into multiple smaller images. I have tried to create dataset with splitted images. But it occupies more space and is very tedious task to manage those images. Then I tried to create a pipeline where images are split while training. But confused with how to implement it as pytorch custom dataset class.

import os
import pandas as pd
from torchvision.io import read_image

class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
          self.img_labels = pd.read_csv(annotations_file)
          self.img_dir = img_dir
          self.transform = transform
          self.target_transform = target_transform

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

  def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2025-02-17 02:48:50

请首先查看: https://stackoverflow.com/a/a/72642001/9560771
并确保您是否喜欢使用在线增强离线增强

如果您喜欢离线,则必须将小图像保存到磁盘上。

否则,使用在线可以使用transforms.randomresizedcrop(xx)从输入图像中随机裁剪小图像

Please first look at this: https://stackoverflow.com/a/72642001/9560771
and make sure if you like to use online augmentations and offline augmentations

If you like offline you must save the small images onto the disk.

Else, using online you can use transforms.RandomResizedCrop(XX) to randomly crop small images from the input image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文