File size: 10,534 Bytes
ab687e7 |
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
import os
import logging
from glob import glob
from pathlib import Path
from typing import Optional, Union
import torch
import numpy as np
from torch.utils.data import Dataset
from torch.utils.dlpack import from_dlpack
import xarray as xr
from terragpu.engine import array_module, df_module
import terragpu.ai.preprocessing as preprocessing
xp = array_module()
xf = df_module()
class PLSegmentationDataset(Dataset):
def __init__(
self,
images_regex: Optional[str] = None,
labels_regex: Optional[str] = None,
dataset_dir: Optional[str] = None,
generate_dataset: bool = False,
tile_size: int = 256,
max_patches: Union[float, int] = 100,
augment: bool = True,
chunks: dict = {'band': 1, 'x': 2048, 'y': 2048},
input_bands: list = ['CB', 'B', 'G', 'Y', 'R', 'RE', 'N1', 'N2'],
output_bands: list = ['B', 'G', 'R'],
seed: int = 24,
normalize: bool = True,
pytorch: bool = True):
super().__init__()
# Dataset metadata
self.input_bands = input_bands
self.output_bands = output_bands
self.chunks = chunks
self.tile_size = tile_size
self.seed = seed
self.max_patches = max_patches
# Preprocessing metadata
self.generate_dataset = generate_dataset
self.normalize = normalize
# Validate several input sources
assert dataset_dir is not None, \
f'dataset_dir: {dataset_dir} does not exist.'
# Setup directories structure
self.dataset_dir = dataset_dir # where to store dataset
self.images_dir = os.path.join(self.dataset_dir, 'images')
self.labels_dir = os.path.join(self.dataset_dir, 'labels')
if self.generate_dataset:
logging.info(f"Starting to prepare dataset: {self.dataset_dir}")
# Assert images_dir and labels_dir to be not None
self.images_regex = images_regex # images location
self.labels_regex = labels_regex # labels location
# Create directories to store dataset
os.makedirs(self.images_dir, exist_ok=True)
os.makedirs(self.labels_dir, exist_ok=True)
self.prepare_data()
assert os.path.exists(self.images_dir), \
f'{self.images_dir} does not exist. Make sure prepare_data: true.'
assert os.path.exists(self.labels_dir), \
f'{self.labels_dir} does not exist. Make sure prepare_data: true.'
self.files = self.get_filenames()
self.augment = augment
self.pytorch = pytorch
# -------------------------------------------------------------------------
# Dataset methods
# -------------------------------------------------------------------------
def __len__(self):
return len(self.files)
def __repr__(self):
s = 'Dataset class with {} files'.format(self.__len__())
return s
def __getitem__(self, idx):
idx = idx % len(self.files)
x, y = self.open_image(idx), self.open_mask(idx)
if self.augment:
x, y = self.transform(x, y)
return x, y
def transform(self, x, y):
if xp.random.random_sample() > 0.5: # flip left and right
x = torch.fliplr(x)
y = torch.fliplr(y)
if xp.random.random_sample() > 0.5: # reverse second dimension
x = torch.flipud(x)
y = torch.flipud(y)
if xp.random.random_sample() > 0.5: # rotate 90 degrees
x = torch.rot90(x, k=1, dims=[1, 2])
y = torch.rot90(y, k=1, dims=[0, 1])
if xp.random.random_sample() > 0.5: # rotate 180 degrees
x = torch.rot90(x, k=2, dims=[1, 2])
y = torch.rot90(y, k=2, dims=[0, 1])
if xp.random.random_sample() > 0.5: # rotate 270 degrees
x = torch.rot90(x, k=3, dims=[1, 2])
y = torch.rot90(y, k=3, dims=[0, 1])
# standardize 0.70, 0.30
# if np.random.random_sample() > 0.70:
# image = preprocess.standardizeLocalCalcTensor(image, means, stds)
# else:
# image = preprocess.standardizeGlobalCalcTensor(image)
return x, y
# -------------------------------------------------------------------------
# preprocess methods
# -------------------------------------------------------------------------
def prepare_data(self):
logging.info("Preparing dataset...")
images_list = sorted(glob(self.images_regex))
labels_list = sorted(glob(self.labels_regex))
for image, label in zip(images_list, labels_list):
# Read imagery from disk and process both image and mask
filename = Path(image).stem
image = xr.open_rasterio(image, chunks=self.chunks).load()
label = xr.open_rasterio(label, chunks=self.chunks).values
# Modify bands if necessary - in a future version, add indices
image = preprocessing.modify_bands(
img=image, input_bands=self.input_bands,
output_bands=self.output_bands)
# Asarray option to force array type
image = xp.asarray(image.values)
label = xp.asarray(label)
# Move from chw to hwc, squeze mask if required
image = xp.moveaxis(image, 0, -1).astype(np.int16)
label = xp.squeeze(label) if len(label.shape) != 2 else label
logging.info(f'Label classes from image: {xp.unique(label)}')
# Generate dataset tiles
image_tiles, label_tiles = preprocessing.gen_random_tiles(
image=image, label=label, tile_size=self.tile_size,
max_patches=self.max_patches, seed=self.seed)
logging.info(f"Tiles: {image_tiles.shape}, {label_tiles.shape}")
# Save to disk
for id in range(image_tiles.shape[0]):
xp.save(
os.path.join(self.images_dir, f'{filename}_{id}.npy'),
image_tiles[id, :, :, :])
xp.save(
os.path.join(self.labels_dir, f'{filename}_{id}.npy'),
label_tiles[id, :, :])
return
# -------------------------------------------------------------------------
# dataset methods
# -------------------------------------------------------------------------
def list_files(self, files_list: list = []):
for i in os.listdir(self.images_dir):
files_list.append(
{
'image': os.path.join(self.images_dir, i),
'label': os.path.join(self.labels_dir, i)
}
)
return files_list
def open_image(self, idx: int, invert: bool = True):
# image = imread(self.files[idx]['image'])
image = xp.load(self.files[idx]['image'], allow_pickle=False)
image = image.transpose((2, 0, 1)) if invert else image
image = (
image / xp.iinfo(image.dtype).max) if self.normalize else image
return from_dlpack(image.toDlpack()) # .to(torch.float32)
def open_mask(self, idx: int, add_dims: bool = False):
# mask = imread(self.files[idx]['label'])
mask = xp.load(self.files[idx]['label'], allow_pickle=False)
mask = xp.expand_dims(mask, 0) if add_dims else mask
return from_dlpack(mask.toDlpack()) # .to(torch.torch.int64)
class SegmentationDataset(Dataset):
def __init__(
self, dataset_dir, pytorch=True, augment=True):
super().__init__()
self.files: list = self.list_files(dataset_dir)
self.augment: bool = augment
self.pytorch: bool = pytorch
self.invert: bool = True
self.normalize: bool = True
self.standardize: bool = True
# -------------------------------------------------------------------------
# Common methods
# -------------------------------------------------------------------------
def __len__(self):
return len(self.files)
def __repr__(self):
s = 'Dataset class with {} files'.format(self.__len__())
return s
def __getitem__(self, idx):
# get data
x = self.open_image(idx)
y = self.open_mask(idx)
# augment the data
if self.augment:
if xp.random.random_sample() > 0.5: # flip left and right
x = torch.fliplr(x)
y = torch.fliplr(y)
if xp.random.random_sample() > 0.5: # reverse second dimension
x = torch.flipud(x)
y = torch.flipud(y)
if xp.random.random_sample() > 0.5: # rotate 90 degrees
x = torch.rot90(x, k=1, dims=[1, 2])
y = torch.rot90(y, k=1, dims=[0, 1])
if xp.random.random_sample() > 0.5: # rotate 180 degrees
x = torch.rot90(x, k=2, dims=[1, 2])
y = torch.rot90(y, k=2, dims=[0, 1])
if xp.random.random_sample() > 0.5: # rotate 270 degrees
x = torch.rot90(x, k=3, dims=[1, 2])
y = torch.rot90(y, k=3, dims=[0, 1])
return x, y
# -------------------------------------------------------------------------
# IO methods
# -------------------------------------------------------------------------
def get_filenames(self, dataset_dir: str, files_list: list = []):
images_dir = os.path.join(dataset_dir, 'images')
labels_dir = os.path.join(dataset_dir, 'labels')
for i in os.listdir(images_dir):
files_list.append(
{
'image': os.path.join(images_dir, i),
'label': os.path.join(labels_dir, i)
}
)
return files_list
def open_image(self, idx: int):
image = xp.load(self.files[idx]['image'], allow_pickle=False)
if self.invert:
image = image.transpose((2, 0, 1))
if self.normalize:
image = (image / xp.iinfo(image.dtype).max)
if self.standardize:
image = preprocessing.standardize_local(image)
return from_dlpack(image.toDlpack()).float()
def open_mask(self, idx: int, add_dims: bool = False):
mask = xp.load(self.files[idx]['label'], allow_pickle=False)
mask = xp.expand_dims(mask, 0) if add_dims else mask
return from_dlpack(mask.toDlpack()).long()
|