from torch.utils.data import DataLoader, Dataset import cv2 import os import rasterio import torch import numpy as np from pyproj import Transformer from datetime import date S3_OLCI_SCALE = [0.0139465,0.0133873,0.0121481,0.0115198,0.0100953,0.0123538,0.00879161,0.00876539, 0.0095103,0.00773378,0.00675523,0.0071996,0.00749684,0.0086512,0.00526779,0.00530267, 0.00493004,0.00549962,0.00502847,0.00326378,0.00324118] BIOMASS_MEAN = 93.8317 BIOMASS_STD = 110.5369 class S3OLCI_BiomassDataset(Dataset): ''' 4000/1000 train/test images 94x94x21 (full dataset is 25K) CCI biomass regression 282x282 nodata: -inf time series: 1-4 images / location ''' def __init__(self, root_dir, split='train', mode='static'): self.root_dir = root_dir self.split = split self.mode = mode self.img_dir = os.path.join(root_dir, split, 's3_olci') self.biomass_dir = os.path.join(root_dir, split, 'biomass') self.fnames = os.listdir(self.biomass_dir) if self.mode == 'static': self.static_csv = os.path.join(root_dir, split, 'static_fnames.csv') with open(self.static_csv, 'r') as f: lines = f.readlines() self.static_img = {} for line in lines: dirname = line.strip().split(',')[0] img_fname = line.strip().split(',')[1] self.static_img[dirname] = img_fname def __len__(self): return len(self.fnames) def __getitem__(self, idx): fname = self.fnames[idx] biomass_path = os.path.join(self.biomass_dir, fname) s3_path = os.path.join(self.img_dir, fname.replace('.tif','')) if self.mode == 'static': img_fname = self.static_img[fname.replace('.tif','')] s3_paths = [os.path.join(s3_path, img_fname)] else: img_fnames = os.listdir(s3_path) s3_paths = [] for img_fname in img_fnames: s3_paths.append(os.path.join(s3_path, img_fname)) imgs = [] img_paths = [] meta_infos = [] for img_path in s3_paths: with rasterio.open(img_path) as src: img = src.read() chs = [] for b in range(21): #ch = cv2.resize(img[b], (94,94), interpolation=cv2.INTER_CUBIC) ch = cv2.resize(img[b], (282,282), interpolation=cv2.INTER_CUBIC) chs.append(ch) img = np.stack(chs) img[np.isnan(img)] = 0 for b in range(21): img[b] = img[b]*S3_OLCI_SCALE[b] img = torch.from_numpy(img).float() if self.meta: cx,cy = src.xy(src.height // 2, src.width // 2) #crs_transformer = Transformer.from_crs(src.crs, 'epsg:4326') #lon, lat = crs_transformer.transform(cx,cy) lon, lat = cx, cy img_fname = os.path.basename(img_path) date_str = img_fname.split('_')[1][:8] date_obj = date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8])) delta = (date_obj - self.reference_date).days meta_info = np.array([lon, lat, delta, np.nan]).astype(np.float32) else: meta_info = np.array([np.nan,np.nan,np.nan,np.nan]).astype(np.float32) imgs.append(img) img_paths.append(img_path) if self.mode == 'series': # pad to 4 images if less than 4 while len(imgs) < 4: imgs.append(img) img_paths.append(img_path) meta_infos.append(meta_info) with rasterio.open(biomass_path) as src: biomass = src.read(1) biomass = cv2.resize(biomass, (282,282), interpolation=cv2.INTER_CUBIC) # 0-650 biomass = torch.from_numpy(biomass.astype('float32')) biomass = (biomass - BIOMASS_MEAN) / BIOMASS_STD # 0-center normalized if self.mode == 'static': return imgs[0], meta_infos[0], biomass # 94x94x21, 282x282 elif self.mode == 'series': return imgs[0], imgs[1], imgs[2], imgs[3], meta_infos[0], meta_infos[1], meta_infos[2], meta_infos[3], biomass # 94x94x21, 94x94x21, 94x94x21, 94x94x21, 282x282