Hi everyone,
I am currently developing a meta learning for semantic segmentation using MAML approach and my dataset comprises of an image and its mask with tif format. My file path is ,/dataset> Train, Test and Validate and each has a sub-folder of image_folder and mask_folder. Am writing a custom dataset to random crop the image with size of 256x256 and number of crops for each image should be 5. I was able to do the random crop class but was trying to check what i did was correct but getting this error: TypeError: Unexpected type <class āstrā>
My code:
import os
import numpy as np
import torch
import torchvision.transforms as TF
from torch.utils import data
from skimage import io
from skimage import transform
from skimage import util
Constants.
num_classes = 4
Class that reads a sequence of image paths from a directory and creates a data.Dataset with them.
class ListDataset(data.Dataset):
def __init__(self, dataset, mode, normalization='minmax', hidden_classes=None):
# Initializing variables.
self.root = './' + dataset + '/'
self.mode = mode
self.normalization = normalization
self.hidden_classes = hidden_classes
if self.hidden_classes is not None:
self.n_classes = num_classes - len(hidden_classes)
else:
self.n_classes = num_classes
# Creating list of paths.
self.imgs = self.make_dataset()
# Check for consistency in list.
if len(self.imgs) == 0:
raise (RuntimeError('Found 0 images, please check the data set'))
def make_dataset(self):
# Making sure the mode is correct.
assert self.mode in ['Train', 'Test', 'Validate']
# Setting string for the mode.
img_dir = os.path.join(self.root, self.mode, 'image_folder')
msk_dir = os.path.join(self.root, self.mode, 'mask_folder')
if self.mode == 'Validate':
img_dir = os.path.join(self.root, 'Train', 'image_folder')
msk_dir = os.path.join(self.root, 'Train', 'mask_folder')
data_list = sorted([f for f in os.listdir(msk_dir) if os.path.isfile(os.path.join(msk_dir, f))])
#print(data_list)
# Creating list containing image and ground truth paths.
items = []
for it in data_list:
item = (os.path.join(img_dir, it.replace('Mask.tif', '.tif')), os.path.join(msk_dir, it))
items.append(item)
#print(item)
# print(img_dir, it, it.replace('_noBoundary.tif', '.tif'), os.path.join(msk_dir, it))
# Returning list.
return items
def transform(self, image, mask):
# Random crop
i, j, h, w = TF.RandomCrop.get_params(
image, output_size=(256, 256))
image = TF.crop(image, i, j, h, w)
mask = TF.crop(mask, i, j, h, w)
# Random horizontal flipping
if random.random() > 0.5:
image = TF.hflip(image)
mask = TF.hflip(mask)
# Random vertical flipping
if random.random() > 0.5:
image = TF.vflip(image)
mask = TF.vflip(mask)
# Transform to tensor
image = TF.to_tensor(image)
mask = TF.to_tensor(mask)
return image, mask
def __getitem__(self, index):
# Reading items from list.
img_path, msk_path = self.imgs[index]
# Reading images.
image = io.imread(img_path)
mask = io.imread(msk_path)
x, y = self.transform(image, mask)
return x, y
def __len__(self):
return len(self.imgs)
Please any Help?