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
|
|
|
| 1 |
+
from torch.utils.data import DataLoader, Dataset
|
| 2 |
+
import cv2
|
| 3 |
+
import os
|
| 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,
|
| 12 |
+
0.00493004,0.00549962,0.00502847,0.00326378,0.00324118]
|
| 13 |
+
|
| 14 |
+
LC100_CLSID = {
|
| 15 |
+
0: 0, # unknown
|
| 16 |
+
20: 1,
|
| 17 |
+
30: 2,
|
| 18 |
+
40: 3,
|
| 19 |
+
50: 4,
|
| 20 |
+
60: 5,
|
| 21 |
+
70: 6,
|
| 22 |
+
80: 7,
|
| 23 |
+
90: 8,
|
| 24 |
+
100: 9,
|
| 25 |
+
111: 10,
|
| 26 |
+
112: 11,
|
| 27 |
+
113: 12,
|
| 28 |
+
114: 13,
|
| 29 |
+
115: 14,
|
| 30 |
+
116: 15,
|
| 31 |
+
121: 16,
|
| 32 |
+
122: 17,
|
| 33 |
+
123: 18,
|
| 34 |
+
124: 19,
|
| 35 |
+
125: 20,
|
| 36 |
+
126: 21,
|
| 37 |
+
200: 22, # ocean
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class S3OLCI_LC100ClsDataset(Dataset):
|
| 43 |
+
'''
|
| 44 |
+
6908/1727 train/test images 96x96x21
|
| 45 |
+
23 classes multilabel LULC
|
| 46 |
+
nodata: -inf
|
| 47 |
+
|
| 48 |
+
'''
|
| 49 |
+
def __init__(self, root_dir, mode='static', split='train', meta=False):
|
| 50 |
+
self.root_dir = root_dir
|
| 51 |
+
self.mode = mode
|
| 52 |
+
self.meta = meta
|
| 53 |
+
self.img_dir = os.path.join(root_dir, split, 's3_olci')
|
| 54 |
+
self.lc100_cls = os.path.join(root_dir, split, 'lc100_multilabel.csv')
|
| 55 |
+
self.fnames = []
|
| 56 |
+
self.labels = []
|
| 57 |
+
with open(self.lc100_cls, 'r') as f:
|
| 58 |
+
lines = f.readlines()
|
| 59 |
+
for line in lines:
|
| 60 |
+
self.fnames.append(line.strip().split(',')[0])
|
| 61 |
+
self.labels.append(list(map(int, line.strip().split(',')[1:])))
|
| 62 |
+
|
| 63 |
+
if self.mode == 'static':
|
| 64 |
+
self.static_csv = os.path.join(root_dir, split, 'static_fnames.csv')
|
| 65 |
+
with open(self.static_csv, 'r') as f:
|
| 66 |
+
lines = f.readlines()
|
| 67 |
+
self.static_img = {}
|
| 68 |
+
for line in lines:
|
| 69 |
+
dirname = line.strip().split(',')[0]
|
| 70 |
+
img_fname = line.strip().split(',')[1]
|
| 71 |
+
self.static_img[dirname] = img_fname
|
| 72 |
+
|
| 73 |
+
if self.meta:
|
| 74 |
+
self.reference_date = date(1970, 1, 1)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def __len__(self):
|
| 78 |
+
return len(self.fnames)
|
| 79 |
+
|
| 80 |
+
def __getitem__(self, idx):
|
| 81 |
+
fname = self.fnames[idx]
|
| 82 |
+
|
| 83 |
+
s3_path = os.path.join(self.img_dir, fname)
|
| 84 |
+
if self.mode == 'static':
|
| 85 |
+
img_fname = self.static_img[fname]
|
| 86 |
+
s3_paths = [os.path.join(s3_path, img_fname)]
|
| 87 |
+
else:
|
| 88 |
+
img_fnames = os.listdir(s3_path)
|
| 89 |
+
s3_paths = []
|
| 90 |
+
for img_fname in img_fnames:
|
| 91 |
+
s3_paths.append(os.path.join(s3_path, img_fname))
|
| 92 |
+
|
| 93 |
+
imgs = []
|
| 94 |
+
img_paths = []
|
| 95 |
+
meta_infos = []
|
| 96 |
+
for img_path in s3_paths:
|
| 97 |
+
with rasterio.open(img_path) as src:
|
| 98 |
+
img = src.read()
|
| 99 |
+
chs = []
|
| 100 |
+
for b in range(21):
|
| 101 |
+
ch = cv2.resize(img[b], (96,96), interpolation=cv2.INTER_CUBIC)
|
| 102 |
+
chs.append(ch)
|
| 103 |
+
img = np.stack(chs)
|
| 104 |
+
img[np.isnan(img)] = 0
|
| 105 |
+
for b in range(21):
|
| 106 |
+
img[b] = img[b]*S3_OLCI_SCALE[b]
|
| 107 |
+
img = torch.from_numpy(img).float()
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if self.meta:
|
| 111 |
+
# get lon, lat
|
| 112 |
+
cx,cy = src.xy(src.height // 2, src.width // 2)
|
| 113 |
+
# convert to lon, lat
|
| 114 |
+
#crs_transformer = Transformer.from_crs(src.crs, 'epsg:4326')
|
| 115 |
+
#lon, lat = crs_transformer.transform(cx,cy)
|
| 116 |
+
lon, lat = cx, cy
|
| 117 |
+
# get time
|
| 118 |
+
img_fname = os.path.basename(img_path)
|
| 119 |
+
date_str = img_fname.split('_')[1][:8]
|
| 120 |
+
date_obj = date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8]))
|
| 121 |
+
delta = (date_obj - self.reference_date).days
|
| 122 |
+
meta_info = np.array([lon, lat, delta, 0]).astype(np.float32)
|
| 123 |
+
else:
|
| 124 |
+
meta_info = np.array([np.nan,np.nan,np.nan,np.nan]).astype(np.float32)
|
| 125 |
+
|
| 126 |
+
imgs.append(img)
|
| 127 |
+
img_paths.append(img_path)
|
| 128 |
+
meta_infos.append(meta_info)
|
| 129 |
+
|
| 130 |
+
if self.mode == 'series':
|
| 131 |
+
# pad to 4 images if less than 4
|
| 132 |
+
while len(imgs) < 4:
|
| 133 |
+
imgs.append(img)
|
| 134 |
+
img_paths.append(img_path)
|
| 135 |
+
meta_infos.append(meta_info)
|
| 136 |
+
|
| 137 |
+
label = self.labels[idx]
|
| 138 |
+
labels = torch.zeros(23)
|
| 139 |
+
# turn into one-hot
|
| 140 |
+
for l in label:
|
| 141 |
+
cls_id = LC100_CLSID[l]
|
| 142 |
+
labels[cls_id] = 1
|
| 143 |
+
|
| 144 |
+
if self.mode == 'static':
|
| 145 |
+
return imgs[0], meta_infos[0], labels
|
| 146 |
+
elif self.mode == 'series':
|
| 147 |
+
return imgs[0], imgs[1], imgs[2], imgs[3], meta_infos[0], meta_infos[1], meta_infos[2], meta_infos[3], labels
|
| 148 |
+
|
| 149 |
+
if __name__ == '__main__':
|
| 150 |
+
dataset = S3OLCI_LC100ClsDataset(root_dir='../data/downstream/cgls_lc100', mode='static', split=None, meta=True)
|
| 151 |
+
dataloader = DataLoader(dataset, batch_size=64, shuffle=False, num_workers=4)
|
| 152 |
+
for i, data in enumerate(dataloader):
|
| 153 |
+
#print(data[0].shape)
|
| 154 |
+
#print(data[1].shape)
|
| 155 |
+
#print(data[1])
|
| 156 |
+
#print(data[2])
|
| 157 |
+
#print(data[0].max())
|
| 158 |
+
#break
|
| 159 |
+
pass
|
lc100_s3olci/dataset_lc100_s3olci_seg.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torch.utils.data import DataLoader, Dataset
|
| 2 |
+
import cv2
|
| 3 |
+
import os
|
| 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,
|
| 12 |
+
0.00493004,0.00549962,0.00502847,0.00326378,0.00324118]
|
| 13 |
+
|
| 14 |
+
LC100_CLSID = {
|
| 15 |
+
0: 0, # unknown
|
| 16 |
+
20: 1,
|
| 17 |
+
30: 2,
|
| 18 |
+
40: 3,
|
| 19 |
+
50: 4,
|
| 20 |
+
60: 5,
|
| 21 |
+
70: 6,
|
| 22 |
+
80: 7,
|
| 23 |
+
90: 8,
|
| 24 |
+
100: 9,
|
| 25 |
+
111: 10,
|
| 26 |
+
112: 11,
|
| 27 |
+
113: 12,
|
| 28 |
+
114: 13,
|
| 29 |
+
115: 14,
|
| 30 |
+
116: 15,
|
| 31 |
+
121: 16,
|
| 32 |
+
122: 17,
|
| 33 |
+
123: 18,
|
| 34 |
+
124: 19,
|
| 35 |
+
125: 20,
|
| 36 |
+
126: 21,
|
| 37 |
+
200: 22, # ocean
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class S3OLCI_LC100SegDataset(Dataset):
|
| 43 |
+
'''
|
| 44 |
+
6908/1727 train/test images 96x96x21
|
| 45 |
+
23 classes LULC segmentation
|
| 46 |
+
nodata: -inf
|
| 47 |
+
|
| 48 |
+
'''
|
| 49 |
+
def __init__(self, root_dir, mode='static', split='train', meta=False):
|
| 50 |
+
self.root_dir = root_dir
|
| 51 |
+
self.mode = mode
|
| 52 |
+
self.meta = meta
|
| 53 |
+
self.img_dir = os.path.join(root_dir, split, 's3_olci')
|
| 54 |
+
self.label_dir = os.path.join(root_dir, split, 'lc100')
|
| 55 |
+
self.fnames = os.listdir(self.label_dir)
|
| 56 |
+
|
| 57 |
+
if self.mode == 'static':
|
| 58 |
+
self.static_csv = os.path.join(root_dir, split, 'static_fnames.csv')
|
| 59 |
+
with open(self.static_csv, 'r') as f:
|
| 60 |
+
lines = f.readlines()
|
| 61 |
+
self.static_img = {}
|
| 62 |
+
for line in lines:
|
| 63 |
+
dirname = line.strip().split(',')[0]
|
| 64 |
+
img_fname = line.strip().split(',')[1]
|
| 65 |
+
self.static_img[dirname] = img_fname
|
| 66 |
+
|
| 67 |
+
if self.meta:
|
| 68 |
+
self.reference_date = date(1970, 1, 1)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def __len__(self):
|
| 72 |
+
return len(self.fnames)
|
| 73 |
+
|
| 74 |
+
def __getitem__(self, idx):
|
| 75 |
+
fname = self.fnames[idx]
|
| 76 |
+
label_path = os.path.join(self.label_dir, fname)
|
| 77 |
+
s3_path = os.path.join(self.img_dir, fname.replace('.tif', ''))
|
| 78 |
+
if self.mode == 'static':
|
| 79 |
+
img_fname = self.static_img[fname.replace('.tif', '')]
|
| 80 |
+
s3_paths = [os.path.join(s3_path, img_fname)]
|
| 81 |
+
else:
|
| 82 |
+
img_fnames = os.listdir(s3_path)
|
| 83 |
+
s3_paths = []
|
| 84 |
+
for img_fname in img_fnames:
|
| 85 |
+
s3_paths.append(os.path.join(s3_path, img_fname))
|
| 86 |
+
|
| 87 |
+
imgs = []
|
| 88 |
+
img_paths = []
|
| 89 |
+
meta_infos = []
|
| 90 |
+
for img_path in s3_paths:
|
| 91 |
+
with rasterio.open(img_path) as src:
|
| 92 |
+
img = src.read()
|
| 93 |
+
chs = []
|
| 94 |
+
for b in range(21):
|
| 95 |
+
#ch = cv2.resize(img[b], (96,96), interpolation=cv2.INTER_CUBIC)
|
| 96 |
+
ch = cv2.resize(img[b], (282,282), interpolation=cv2.INTER_CUBIC)
|
| 97 |
+
chs.append(ch)
|
| 98 |
+
img = np.stack(chs)
|
| 99 |
+
img[np.isnan(img)] = 0
|
| 100 |
+
for b in range(21):
|
| 101 |
+
img[b] = img[b]*S3_OLCI_SCALE[b]
|
| 102 |
+
img = torch.from_numpy(img).float()
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if self.meta:
|
| 106 |
+
# get lon, lat
|
| 107 |
+
cx,cy = src.xy(src.height // 2, src.width // 2)
|
| 108 |
+
# convert to lon, lat
|
| 109 |
+
#crs_transformer = Transformer.from_crs(src.crs, 'epsg:4326')
|
| 110 |
+
#lon, lat = crs_transformer.transform(cx,cy)
|
| 111 |
+
lon, lat = cx, cy
|
| 112 |
+
# get time
|
| 113 |
+
img_fname = os.path.basename(img_path)
|
| 114 |
+
date_str = img_fname.split('_')[1][:8]
|
| 115 |
+
date_obj = date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8]))
|
| 116 |
+
delta = (date_obj - self.reference_date).days
|
| 117 |
+
meta_info = np.array([lon, lat, delta, 0]).astype(np.float32)
|
| 118 |
+
else:
|
| 119 |
+
meta_info = np.array([np.nan,np.nan,np.nan,np.nan]).astype(np.float32)
|
| 120 |
+
|
| 121 |
+
imgs.append(img)
|
| 122 |
+
img_paths.append(img_path)
|
| 123 |
+
meta_infos.append(meta_info)
|
| 124 |
+
|
| 125 |
+
if self.mode == 'series':
|
| 126 |
+
# pad to 4 images if less than 4
|
| 127 |
+
while len(imgs) < 4:
|
| 128 |
+
imgs.append(img)
|
| 129 |
+
img_paths.append(img_path)
|
| 130 |
+
meta_infos.append(meta_info)
|
| 131 |
+
|
| 132 |
+
with rasterio.open(label_path) as src:
|
| 133 |
+
label = src.read(1)
|
| 134 |
+
label = cv2.resize(label, (282,282), interpolation=cv2.INTER_NEAREST) # 0-650
|
| 135 |
+
label_new = np.zeros_like(label)
|
| 136 |
+
for k,v in LC100_CLSID.items():
|
| 137 |
+
label_new[label==k] = v
|
| 138 |
+
label = torch.from_numpy(label_new.astype('int64'))
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
if self.mode == 'static':
|
| 145 |
+
return imgs[0], meta_infos[0], label
|
| 146 |
+
elif self.mode == 'series':
|
| 147 |
+
return imgs[0], imgs[1], imgs[2], imgs[3], meta_infos[0], meta_infos[1], meta_infos[2], meta_infos[3], label
|
| 148 |
+
|
| 149 |
+
if __name__ == '__main__':
|
| 150 |
+
dataset = S3OLCI_LC100SegDataset(root_dir='../data/downstream/cgls_lc100', mode='static', split='train', meta=True)
|
| 151 |
+
dataloader = DataLoader(dataset, batch_size=64, shuffle=False, num_workers=4)
|
| 152 |
+
for i, data in enumerate(dataloader):
|
| 153 |
+
#print(data[0].shape)
|
| 154 |
+
#print(data[1].shape)
|
| 155 |
+
#print(data[1])
|
| 156 |
+
#print(data[2])
|
| 157 |
+
#print(data[0].max())
|
| 158 |
+
#break
|
| 159 |
+
pass
|
lc100_s3olci/lc100_s3olci.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:57c016e8a10c43d347e15f58a7f6ae58ce0d4a8c83d2512886b2ec81144ab04b
|
| 3 |
+
size 1713726638
|