Upload dataset_biomass_s3olci.py
Browse files
biomass_s3olci/dataset_biomass_s3olci.py
CHANGED
|
@@ -4,6 +4,8 @@ import os
|
|
| 4 |
import rasterio
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
S3_OLCI_SCALE = [0.0139465,0.0133873,0.0121481,0.0115198,0.0100953,0.0123538,0.00879161,0.00876539,
|
| 9 |
0.0095103,0.00773378,0.00675523,0.0071996,0.00749684,0.0086512,0.00526779,0.00530267,
|
|
@@ -16,7 +18,7 @@ BIOMASS_STD = 110.5369
|
|
| 16 |
class S3OLCI_BiomassDataset(Dataset):
|
| 17 |
'''
|
| 18 |
4000/1000 train/test images 94x94x21 (full dataset is 25K)
|
| 19 |
-
CCI biomass 282x282
|
| 20 |
nodata: -inf
|
| 21 |
time series: 1-4 images / location
|
| 22 |
|
|
@@ -32,6 +34,18 @@ class S3OLCI_BiomassDataset(Dataset):
|
|
| 32 |
|
| 33 |
self.fnames = os.listdir(self.biomass_dir)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def __len__(self):
|
| 36 |
return len(self.fnames)
|
| 37 |
|
|
@@ -39,13 +53,19 @@ class S3OLCI_BiomassDataset(Dataset):
|
|
| 39 |
fname = self.fnames[idx]
|
| 40 |
biomass_path = os.path.join(self.biomass_dir, fname)
|
| 41 |
s3_path = os.path.join(self.img_dir, fname.replace('.tif',''))
|
| 42 |
-
img_fnames = os.listdir(s3_path)
|
| 43 |
-
s3_paths = []
|
| 44 |
-
for img_fname in img_fnames:
|
| 45 |
-
s3_paths.append(os.path.join(s3_path, img_fname))
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
imgs = []
|
| 48 |
img_paths = []
|
|
|
|
| 49 |
for img_path in s3_paths:
|
| 50 |
with rasterio.open(img_path) as src:
|
| 51 |
img = src.read()
|
|
@@ -59,12 +79,30 @@ class S3OLCI_BiomassDataset(Dataset):
|
|
| 59 |
for b in range(21):
|
| 60 |
img[b] = img[b]*S3_OLCI_SCALE[b]
|
| 61 |
img = torch.from_numpy(img).float()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
imgs.append(img)
|
| 63 |
img_paths.append(img_path)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with rasterio.open(biomass_path) as src:
|
| 70 |
biomass = src.read(1)
|
|
@@ -72,6 +110,6 @@ class S3OLCI_BiomassDataset(Dataset):
|
|
| 72 |
biomass = torch.from_numpy(biomass.astype('float32'))
|
| 73 |
biomass = (biomass - BIOMASS_MEAN) / BIOMASS_STD # 0-center normalized
|
| 74 |
if self.mode == 'static':
|
| 75 |
-
return imgs[0], biomass # 94x94x21, 282x282
|
| 76 |
elif self.mode == 'series':
|
| 77 |
-
return imgs[0], imgs[1], imgs[2], imgs[3], biomass # 94x94x21, 94x94x21, 94x94x21, 94x94x21, 282x282
|
|
|
|
| 4 |
import rasterio
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
+
from pyproj import Transformer
|
| 8 |
+
from datetime import date
|
| 9 |
|
| 10 |
S3_OLCI_SCALE = [0.0139465,0.0133873,0.0121481,0.0115198,0.0100953,0.0123538,0.00879161,0.00876539,
|
| 11 |
0.0095103,0.00773378,0.00675523,0.0071996,0.00749684,0.0086512,0.00526779,0.00530267,
|
|
|
|
| 18 |
class S3OLCI_BiomassDataset(Dataset):
|
| 19 |
'''
|
| 20 |
4000/1000 train/test images 94x94x21 (full dataset is 25K)
|
| 21 |
+
CCI biomass regression 282x282
|
| 22 |
nodata: -inf
|
| 23 |
time series: 1-4 images / location
|
| 24 |
|
|
|
|
| 34 |
|
| 35 |
self.fnames = os.listdir(self.biomass_dir)
|
| 36 |
|
| 37 |
+
if self.mode == 'static':
|
| 38 |
+
self.static_csv = os.path.join(root_dir, split, 'static_fnames.csv')
|
| 39 |
+
with open(self.static_csv, 'r') as f:
|
| 40 |
+
lines = f.readlines()
|
| 41 |
+
self.static_img = {}
|
| 42 |
+
for line in lines:
|
| 43 |
+
dirname = line.strip().split(',')[0]
|
| 44 |
+
img_fname = line.strip().split(',')[1]
|
| 45 |
+
self.static_img[dirname] = img_fname
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
def __len__(self):
|
| 50 |
return len(self.fnames)
|
| 51 |
|
|
|
|
| 53 |
fname = self.fnames[idx]
|
| 54 |
biomass_path = os.path.join(self.biomass_dir, fname)
|
| 55 |
s3_path = os.path.join(self.img_dir, fname.replace('.tif',''))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
if self.mode == 'static':
|
| 58 |
+
img_fname = self.static_img[fname.replace('.tif','')]
|
| 59 |
+
s3_paths = [os.path.join(s3_path, img_fname)]
|
| 60 |
+
else:
|
| 61 |
+
img_fnames = os.listdir(s3_path)
|
| 62 |
+
s3_paths = []
|
| 63 |
+
for img_fname in img_fnames:
|
| 64 |
+
s3_paths.append(os.path.join(s3_path, img_fname))
|
| 65 |
+
|
| 66 |
imgs = []
|
| 67 |
img_paths = []
|
| 68 |
+
meta_infos = []
|
| 69 |
for img_path in s3_paths:
|
| 70 |
with rasterio.open(img_path) as src:
|
| 71 |
img = src.read()
|
|
|
|
| 79 |
for b in range(21):
|
| 80 |
img[b] = img[b]*S3_OLCI_SCALE[b]
|
| 81 |
img = torch.from_numpy(img).float()
|
| 82 |
+
|
| 83 |
+
if self.meta:
|
| 84 |
+
cx,cy = src.xy(src.height // 2, src.width // 2)
|
| 85 |
+
#crs_transformer = Transformer.from_crs(src.crs, 'epsg:4326')
|
| 86 |
+
#lon, lat = crs_transformer.transform(cx,cy)
|
| 87 |
+
lon, lat = cx, cy
|
| 88 |
+
img_fname = os.path.basename(img_path)
|
| 89 |
+
date_str = img_fname.split('_')[1][:8]
|
| 90 |
+
date_obj = date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8]))
|
| 91 |
+
delta = (date_obj - self.reference_date).days
|
| 92 |
+
meta_info = np.array([lon, lat, delta, np.nan]).astype(np.float32)
|
| 93 |
+
else:
|
| 94 |
+
meta_info = np.array([np.nan,np.nan,np.nan,np.nan]).astype(np.float32)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
imgs.append(img)
|
| 98 |
img_paths.append(img_path)
|
| 99 |
+
|
| 100 |
+
if self.mode == 'series':
|
| 101 |
+
# pad to 4 images if less than 4
|
| 102 |
+
while len(imgs) < 4:
|
| 103 |
+
imgs.append(img)
|
| 104 |
+
img_paths.append(img_path)
|
| 105 |
+
meta_infos.append(meta_info)
|
| 106 |
|
| 107 |
with rasterio.open(biomass_path) as src:
|
| 108 |
biomass = src.read(1)
|
|
|
|
| 110 |
biomass = torch.from_numpy(biomass.astype('float32'))
|
| 111 |
biomass = (biomass - BIOMASS_MEAN) / BIOMASS_STD # 0-center normalized
|
| 112 |
if self.mode == 'static':
|
| 113 |
+
return imgs[0], meta_infos[0], biomass # 94x94x21, 282x282
|
| 114 |
elif self.mode == 'series':
|
| 115 |
+
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
|