创建自定义数据集功能时拆分图像
我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请首先查看: 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
andoffline 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