简体   繁体   English

如何在Pytorch中为图像及​​其遮罩创建自定义数据集?

[英]How make customised dataset in Pytorch for images and their masks?

I have two dataset folder of tif images, one is a folder called BMMCdata, and the other one is the mask of BMMCdata images called BMMCmasks(the name of images are corresponds). 我有两个tif图像的数据集文件夹,一个是名为BMMCdata的文件夹,另一个是称为BMMCmasks的BMMCdata图像的遮罩(图像名称是对应的)。 I am trying to make a customised dataset and also split the data randomly to train and test. 我正在尝试制作一个自定义的数据集,还随机拆分数据以进行训练和测试。 at the moment I am getting an error 目前我遇到了错误

self.filenames.append(fn)
AttributeError: 'CustomDataset' object has no attribute 'filenames'

Any comment will be appreciated a lot. 任何评论将不胜感激。

import torch
from torch.utils.data.dataset import Dataset  # For custom data-sets
from torchvision import transforms
from PIL import Image
import os.path as osp
import glob

folder_data = "/Users/parto/PycharmProjects/U-net/BMMCdata/data"

class CustomDataset(Dataset):
def __init__(self, root):

    self.filename = folder_data
    self.root = root
    self.to_tensor = transforms.ToTensor()
    filenames = glob.glob(osp.join(folder_data, '*.tif'))
    for fn in filenames:
        self.filenames.append(fn)
    self.len = len(self.filenames)
    print(fn)

def __getitem__(self, index):
    image = Image.open(self.filenames[index])
    return self.transform(image)

def __len__(self):

    return self.len
custom_img = CustomDataset(folder_data)
# total images in set
print(custom_img.len)

train_len = int(0.6*custom_img.len)
test_len = custom_img.len - train_len
train_set, test_set = CustomDataset.random_split(custom_img, lengths=[train_len, test_len])
# check lens of subset
len(train_set), len(test_set)

train_set = CustomDataset(folder_data)
train_set = torch.utils.data.TensorDataset(train_set, train=True, batch_size=4)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=4, shuffle=True, num_workers=1)
print(train_set)
print(train_loader)

test_set = torch.utils.data.DataLoader(Dataset, batch_size=4, sampler= train_sampler)
test_loader = torch.utils.data.DataLoader(Dataset, batch_size=4)

answer given by @ptrblck in pytorch community. pytorch社区中@ptrblck给出的答案。 thank you 谢谢

 # get all the image and mask path and number of images
 folder_data = glob.glob("D:\\Neda\\Pytorch\\U-net\\BMMCdata\\data\\*.tif")
 folder_mask = glob.glob("D:\\Neda\\Pytorch\\U-net\\BMMCmasks\\masks\\*.tif")

 # split these path using a certain percentage
 len_data = len(folder_data)
 print(len_data)
 train_size = 0.6

 train_image_paths = folder_data[:int(len_data*train_size)]
 test_image_paths = folder_data[int(len_data*train_size):]

 train_mask_paths = folder_mask[:int(len_data*train_size)]
 test_mask_paths = folder_mask[int(len_data*train_size):]


 class CustomDataset(Dataset):
    def __init__(self, image_paths, target_paths, train=True):   # initial logic 
      happens like transform

         self.image_paths = image_paths
         self.target_paths = target_paths
         self.transforms = transforms.ToTensor()

    def __getitem__(self, index):

        image = Image.open(self.image_paths[index])
        mask = Image.open(self.target_paths[index])
        t_image = self.transforms(image)
     return t_image, mask

def __len__(self):  # return count of sample we have

    return len(self.image_paths)

train_dataset = CustomDataset(train_image_paths, train_mask_paths, train=True)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=1)

test_dataset = CustomDataset(test_image_paths, test_mask_paths, train=False)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4, shuffle=False, num_workers=1)

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

相关问题 如何创建具有多个标签和掩码的自定义 Pytorch 数据集? - How to Create a Custom Pytorch Dataset with Multiple Labels and Masks? Keras - 用于大型图像和掩码数据集的生成器 - Keras - Generator for large dataset of Images and Masks 如何使用pytorch为CNN制作自定义数据集? - How to make a custom dataset for CNN using pytorch? 如何在语义分割任务中使用 tf.dataset 处理图像和掩码? - How to deal with images and masks using tf.dataset in a semantic segmentation task? 如何使用 Pytorch 将增强图像添加到原始数据集? - How to add augmented images to original dataset using Pytorch? Python,class 数据集,如何将图像与 pytorch 中的相应标签连接起来 - Python, class dataset, how to concatenate images with their respective labels in pytorch 如何从图像列表开始加载数据集 Pytorch - How to load a dataset starting from list of images Pytorch 如何在pytorch中对数据集进行排序 - How to sort a dataset in pytorch 在 Pytorch 中,如何使用 BoolTensor 掩码跨多个暗淡切片张量? - In Pytorch how to slice tensor across multiple dims with BoolTensor masks? 如何从图像中删除小的独立蒙版? - How to delete small independent masks from images?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM