|
import numpy as np |
|
import torch |
|
from torch.utils.data import Dataset, DataLoader, Subset |
|
from pathlib import Path |
|
import os |
|
import rasterio |
|
import cv2 |
|
import pdb |
|
from pyproj import Transformer |
|
|
|
|
|
EXTENSIONS = (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp") |
|
ALL_BANDS = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B10', 'B11', 'B12', 'B8A'] |
|
S2A_BANDS = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B11', 'B12', 'B8A'] |
|
RGB_BANDS = ['B04', 'B03', 'B02'] |
|
S1_BANDS = ['VV', 'VH'] |
|
|
|
|
|
BAND_STATS = { |
|
'mean': { |
|
'B01': 1353.72696296, |
|
'B02': 1117.20222222, |
|
'B03': 1041.8842963, |
|
'B04': 946.554, |
|
'B05': 1199.18896296, |
|
'B06': 2003.00696296, |
|
'B07': 2374.00874074, |
|
'B08': 2301.22014815, |
|
'B8A': 2599.78311111, |
|
'B09': 732.18207407, |
|
'B10': 12.09952894, |
|
'B11': 1820.69659259, |
|
'B12': 1118.20259259, |
|
'VV': -12.54847273, |
|
'VH': -20.19237134 |
|
}, |
|
'std': { |
|
'B01': 897.27143653, |
|
'B02': 736.01759721, |
|
'B03': 684.77615743, |
|
'B04': 620.02902871, |
|
'B05': 791.86263829, |
|
'B06': 1341.28018273, |
|
'B07': 1595.39989386, |
|
'B08': 1545.52915718, |
|
'B8A': 1750.12066835, |
|
'B09': 475.11595216, |
|
'B10': 98.26600935, |
|
'B11': 1216.48651476, |
|
'B12': 736.6981037, |
|
'VV': 5.25697717, |
|
'VH': 5.91150917 |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_file(filename): |
|
return filename.lower().endswith(EXTENSIONS) |
|
|
|
def normalize(img, mean, std): |
|
min_value = mean - 2 * std |
|
max_value = mean + 2 * std |
|
img = (img - min_value) / (max_value - min_value) * 255.0 |
|
img = np.clip(img, 0, 255).astype(np.uint8) |
|
|
|
|
|
return img |
|
|
|
class EurosatDataset(Dataset): |
|
|
|
def __init__(self, root, bands='B2', split='train', transform=None, normalize=False, meta=False): |
|
self.root = Path(root,split) |
|
self.transform = transform |
|
if bands=='B13': |
|
self.bands = ALL_BANDS |
|
elif bands=='B12': |
|
self.bands = S2A_BANDS |
|
elif bands=='RGB': |
|
self.bands = RGB_BANDS |
|
elif bands=='B2': |
|
self.bands = S1_BANDS |
|
|
|
self.normalize = normalize |
|
|
|
self.classes = sorted([d.name for d in self.root.iterdir() if d.is_dir()]) |
|
self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.classes)} |
|
|
|
self.samples = [] |
|
self.targets = [] |
|
|
|
|
|
for froot, _, fnames in sorted(os.walk(self.root, followlinks=True)): |
|
for fname in sorted(fnames): |
|
if is_valid_file(fname): |
|
path = os.path.join(froot, fname) |
|
self.samples.append(path) |
|
target = self.class_to_idx[Path(path).parts[-2]] |
|
self.targets.append(target) |
|
|
|
|
|
self.meta = meta |
|
|
|
def __getitem__(self, index): |
|
path = self.samples[index] |
|
target = self.targets[index] |
|
|
|
with rasterio.open(path) as f: |
|
if self.bands == ALL_BANDS: |
|
array = f.read().astype(np.int16) |
|
elif self.bands == S2A_BANDS: |
|
array = f.read((1,2,3,4,5,6,7,8,9,11,12,13)).astype(np.int16) |
|
elif self.bands == RGB_BANDS: |
|
array = f.read((4,3,2)).astype(np.int16) |
|
elif self.bands == S1_BANDS: |
|
array = f.read().astype(np.float32) |
|
|
|
img = array.transpose(1, 2, 0) |
|
|
|
if self.meta: |
|
|
|
cx,cy = f.xy(f.height // 2, f.width // 2) |
|
|
|
crs_transformer = Transformer.from_crs(f.crs, 'epsg:4326') |
|
lon, lat = crs_transformer.transform(cx,cy) |
|
|
|
meta_info = np.array([lon, lat, 0, 0]).astype(np.float32) |
|
|
|
|
|
|
|
channels = [] |
|
|
|
for i,b in enumerate(self.bands): |
|
ch = img[:,:,i] |
|
if self.normalize: |
|
ch = normalize(ch, mean=BAND_STATS['mean'][b], std=BAND_STATS['std'][b]) |
|
elif self.bands == S2A_BANDS: |
|
ch = (ch / 10000.0 * 255.0).astype('uint8') |
|
|
|
if b=='B8A': |
|
channels.insert(8,ch) |
|
else: |
|
channels.append(ch) |
|
|
|
img = np.stack(channels, axis=0).astype('float32') / 255.0 |
|
|
|
if self.transform is not None: |
|
img = self.transform(img) |
|
|
|
if self.meta: |
|
return img, target, meta_info |
|
else: |
|
return img, target |
|
|
|
def __len__(self): |
|
return len(self.samples) |
|
|
|
|
|
class Subset(Dataset): |
|
r""" |
|
Subset of a dataset at specified indices. |
|
|
|
Arguments: |
|
dataset (Dataset): The whole Dataset |
|
indices (sequence): Indices in the whole set selected for subset |
|
""" |
|
def __init__(self, dataset, indices, transform=None): |
|
self.dataset = dataset |
|
self.indices = indices |
|
self.transform = transform |
|
|
|
def __getitem__(self, idx): |
|
im, target = self.dataset[self.indices[idx]] |
|
if self.transform: |
|
im = self.transform(im) |
|
return im, target |
|
|
|
def __len__(self): |
|
return len(self.indices) |
|
|