File size: 4,066 Bytes
7f49ac7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import torch
from torch.utils.data import Dataset
import os
from natsort import natsorted
import cv2
import glob
import numpy as np
from PIL import Image
from skimage import io as img
class ImageAndMaskData(Dataset):
def __init__(self, img_dir, mask_dir, transform=None):
self.images = natsorted(glob.glob(img_dir + "/*"))
self.masks = natsorted(glob.glob(mask_dir + "/*"))
self.imgs_and_masks = list(zip(self.images, self.masks))
self.transform = transform
def __len__(self):
return len(self.imgs_and_masks)
def __getitem__(self, idx):
data = self.imgs_and_masks[idx]
img_path = data[0] # image
mask_path = data[1] # mask
#img = cv2.imread(img_path)
img = np.array(Image.open(img_path))
mask = np.array(Image.open(mask_path))[:,:,0:1] # take only one channel from mask
#print(mask.shape)
#print(mask.sum())
sample = np.concatenate((img, mask), axis=2)
#sample = torch.tensor(sample).to(torch.float)
#sample = img
sample = Image.fromarray(sample)
#sample = sample.permute((2, 0, 1))
# convert to 0,1 range
#sample = sample/255
#print(sample.shape)
#print(img.shape)
#print(mask.shape)
if self.transform:
sample = self.transform(sample)
return sample
# New functions to match with SinGAN-Seg process
def make_4_chs_img(image_path, mask_path):
im = img.imread(image_path)
mask = img.imread(mask_path)
# modifications - 22.02.2022
mask = (mask > 127)*255 # to get clean mask
# mask = 255 - (mask > 127)*255 # to get inverted mask
#print(np.unique(mask))
return np.concatenate((im, mask[:,:,0:1]), axis=2)
def norm(x):
out = (x -0.5) *2
return out.clamp(-1, 1)
def denorm(x):
out = (x + 1) / 2
return out.clamp(0, 1)
def np2torch(x):
#if opt.nc_im == 3 or opt.nc_im == 4: # added opt.nc_im == 4 by vajira to handle 4 channel image
x = x[:,:,:]
x = x.transpose((2, 0, 1))/255
x = torch.from_numpy(x)
#if not(opt.not_cuda):
# x = move_to_gpu(x, opt.device)
#x = x.type(torch.cuda.FloatTensor) if not(opt.not_cuda) else x.type(torch.FloatTensor)
x = x.type(torch.FloatTensor)
#x = x.type(torch.FloatTensor)
x = norm(x)
return x
class ImageAndMaskDataFromSinGAN(Dataset):
def __init__(self, img_dir, mask_dir, transform=None):
self.images = natsorted(glob.glob(img_dir + "/*"))
self.masks = natsorted(glob.glob(mask_dir + "/*"))
self.imgs_and_masks = list(zip(self.images, self.masks))
self.transform = transform
def __len__(self):
return len(self.imgs_and_masks)
def __getitem__(self, idx):
data = self.imgs_and_masks[idx]
image_path = data[0] # image
mask_path = data[1] # mask
#img = cv2.imread(img_path)
#img = np.array(Image.open(img_path))
# mask = np.array(Image.open(mask_path))[:,:,0:1] # take only one channel from mask
#print(mask.shape)
#print(mask.sum())
#sample = np.concatenate((img, mask), axis=2)
#sample = torch.tensor(sample).to(torch.float)
#sample = img
sample = make_4_chs_img(image_path, mask_path)#Image.fromarray(sample)
sample = np2torch(sample)
sample = sample[0:4,:,:]
#sample = sample.permute((2, 0, 1))
# convert to 0,1 range
#sample = sample/255
#print(sample.shape)
#print(img.shape)
#print(mask.shape)
if self.transform:
sample = self.transform(sample)
return sample
if __name__ == "__main__":
dataset = ImageAndMaskDataFromSinGAN("/work/vajira/DATA/kvasir_seg/real_images_root/real_images",
"/work/vajira/DATA/kvasir_seg/real_masks_root/real_masks")
print(dataset[1].shape)
#cv2.imwrite("test.png", dataset[1])
|