Delete example_raw/ssl4eo_s_dataset.py
Browse files- example_raw/ssl4eo_s_dataset.py +0 -234
example_raw/ssl4eo_s_dataset.py
DELETED
@@ -1,234 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import gzip
|
3 |
-
import json
|
4 |
-
import numpy as np
|
5 |
-
import rasterio
|
6 |
-
import re
|
7 |
-
from torch.utils.data import Dataset, DataLoader
|
8 |
-
import torch
|
9 |
-
#from cvtorchvision import cvtransforms
|
10 |
-
from kornia.augmentation import AugmentationSequential
|
11 |
-
import kornia
|
12 |
-
import argparse
|
13 |
-
|
14 |
-
|
15 |
-
class SSL4EO_S(Dataset):
|
16 |
-
def __init__(self, fnames_path, root_dir, modality=['s1_grd', 's2_toa', 's3_olci', 's5p_co', 's5p_no2', 's5p_so2', 's5p_o3', 'dem'], transform_s1=None, transform_s2=None, transform_s3=None, transform_s5p=None, transform_dem=None):
|
17 |
-
with gzip.open(fnames_path, 'rt', encoding='utf-8') as gz_file:
|
18 |
-
self.fnames_json = json.load(gz_file)
|
19 |
-
self.grid_ids = list(self.fnames_json.keys())
|
20 |
-
self.root_dir = root_dir
|
21 |
-
self.transform_s1 = transform_s1
|
22 |
-
self.transform_s2 = transform_s2
|
23 |
-
self.transform_s3 = transform_s3
|
24 |
-
self.transform_s5p = transform_s5p
|
25 |
-
self.transform_dem = transform_dem
|
26 |
-
self.modality = modality
|
27 |
-
|
28 |
-
def __len__(self):
|
29 |
-
return len(self.grid_ids)
|
30 |
-
|
31 |
-
def get_s1_s2(self,grid_id,modality):
|
32 |
-
arrays = []
|
33 |
-
meta_data = []
|
34 |
-
local_grids = list(self.fnames_json[grid_id][modality].keys())
|
35 |
-
grid_id_coord = self.fnames_json[grid_id]['grid_id_coord']
|
36 |
-
for local_grid in local_grids:
|
37 |
-
local_fpaths = self.fnames_json[grid_id][modality][local_grid]
|
38 |
-
imgs = []
|
39 |
-
meta = []
|
40 |
-
for local_fpath in local_fpaths:
|
41 |
-
with rasterio.open(os.path.join(self.root_dir, local_fpath)) as src:
|
42 |
-
img = src.read()
|
43 |
-
if modality=='s1_grd' and self.transform_s1:
|
44 |
-
#img = self.transform_s1(np.transpose(img, (1, 2, 0)))
|
45 |
-
img = torch.from_numpy(img).unsqueeze(0)
|
46 |
-
img = self.transform_s1(img).squeeze(0)
|
47 |
-
elif modality=='s2_toa' and self.transform_s2:
|
48 |
-
#img = self.transform_s2(np.transpose(img.astype(np.int16), (1, 2, 0)))
|
49 |
-
img = torch.from_numpy(img.astype(np.int16)).unsqueeze(0)
|
50 |
-
img = self.transform_s2(img.to(torch.float16)).squeeze(0)
|
51 |
-
imgs.append(img)
|
52 |
-
fname = local_fpath.split('/')[-1]
|
53 |
-
date = re.search(r'(\d{8})T', fname).group(1)
|
54 |
-
meta_info = f"{grid_id_coord}/{local_grid}/{date}"
|
55 |
-
meta.append(meta_info)
|
56 |
-
arrays.append(imgs)
|
57 |
-
meta_data.append(meta)
|
58 |
-
return arrays, meta_data
|
59 |
-
|
60 |
-
def get_s3(self,grid_id):
|
61 |
-
arrays = []
|
62 |
-
meta_data = []
|
63 |
-
fpaths = self.fnames_json[grid_id]['s3_olci']
|
64 |
-
grid_id_coord = self.fnames_json[grid_id]['grid_id_coord']
|
65 |
-
for fpath in fpaths:
|
66 |
-
with rasterio.open(os.path.join(self.root_dir, fpath)) as src:
|
67 |
-
img = src.read()
|
68 |
-
if self.transform_s3:
|
69 |
-
#img = self.transform_s3(np.transpose(img, (1, 2, 0)))
|
70 |
-
img = torch.from_numpy(img).unsqueeze(0)
|
71 |
-
img = self.transform_s3(img).squeeze(0)
|
72 |
-
arrays.append(img)
|
73 |
-
fname = fpath.split('/')[-1]
|
74 |
-
date = re.search(r'(\d{8})T', fname).group(1)
|
75 |
-
meta_info = f"{grid_id_coord}/{date}"
|
76 |
-
meta_data.append(meta_info)
|
77 |
-
return arrays, meta_data
|
78 |
-
|
79 |
-
def get_s5p(self,grid_id,modality):
|
80 |
-
arrays = []
|
81 |
-
meta_data = []
|
82 |
-
fpaths = self.fnames_json[grid_id][modality]
|
83 |
-
grid_id_coord = self.fnames_json[grid_id]['grid_id_coord']
|
84 |
-
for fpath in fpaths:
|
85 |
-
with rasterio.open(os.path.join(self.root_dir, fpath)) as src:
|
86 |
-
img = src.read()
|
87 |
-
if self.transform_s5p:
|
88 |
-
#img = self.transform_s5p(np.transpose(img, (1, 2, 0)))
|
89 |
-
img = torch.from_numpy(img).unsqueeze(0)
|
90 |
-
img = self.transform_s5p(img).squeeze(0)
|
91 |
-
arrays.append(img)
|
92 |
-
fname = fpath.split('/')[-1]
|
93 |
-
match = re.search(r'(\d{4})-(\d{2})-(\d{2})', fname)
|
94 |
-
date = f"{match.group(1)}{match.group(2)}{match.group(3)}"
|
95 |
-
meta_info = f"{grid_id_coord}/{date}"
|
96 |
-
meta_data.append(meta_info)
|
97 |
-
return arrays, meta_data
|
98 |
-
|
99 |
-
def get_dem(self,grid_id):
|
100 |
-
fpath = self.fnames_json[grid_id]['dem'][0]
|
101 |
-
with rasterio.open(os.path.join(self.root_dir, fpath)) as src:
|
102 |
-
img = src.read()
|
103 |
-
if self.transform_dem:
|
104 |
-
#img = self.transform_dem(np.transpose(img, (1, 2, 0)))
|
105 |
-
img = torch.from_numpy(img).unsqueeze(0)
|
106 |
-
img = self.transform_dem(img).squeeze(0)
|
107 |
-
return img
|
108 |
-
|
109 |
-
def __getitem__(self, idx):
|
110 |
-
grid_id = self.grid_ids[idx]
|
111 |
-
grid_id_coord = self.fnames_json[grid_id]['grid_id_coord']
|
112 |
-
sample = {}
|
113 |
-
meta_data = {}
|
114 |
-
# s1
|
115 |
-
if 's1_grd' in self.modality:
|
116 |
-
arr_s1, meta_s1 = self.get_s1_s2(grid_id,'s1_grd')
|
117 |
-
sample['s1_grd'] = arr_s1
|
118 |
-
meta_data['s1_grd'] = meta_s1
|
119 |
-
# s2
|
120 |
-
if 's2_toa' in self.modality:
|
121 |
-
arr_s2, meta_s2 = self.get_s1_s2(grid_id,'s2_toa')
|
122 |
-
sample['s2_toa'] = arr_s2
|
123 |
-
meta_data['s2_toa'] = meta_s2
|
124 |
-
# s3
|
125 |
-
if 's3_olci' in self.modality:
|
126 |
-
arr_s3, meta_s3 = self.get_s3(grid_id)
|
127 |
-
sample['s3_olci'] = arr_s3
|
128 |
-
meta_data['s3_olci'] = meta_s3
|
129 |
-
# s5p_co
|
130 |
-
if 's5p_co' in self.modality:
|
131 |
-
arr_s5p_co, meta_s5p_co = self.get_s5p(grid_id,'s5p_co')
|
132 |
-
sample['s5p_co'] = arr_s5p_co
|
133 |
-
meta_data['s5p_co'] = meta_s5p_co
|
134 |
-
# s5p_no2
|
135 |
-
if 's5p_no2' in self.modality:
|
136 |
-
arr_s5p_no2, meta_s5p_no2 = self.get_s5p(grid_id,'s5p_no2')
|
137 |
-
sample['s5p_no2'] = arr_s5p_no2
|
138 |
-
meta_data['s5p_no2'] = meta_s5p_no2
|
139 |
-
# s5p_o3
|
140 |
-
if 's5p_o3' in self.modality:
|
141 |
-
arr_s5p_o3, meta_s5p_o3 = self.get_s5p(grid_id,'s5p_o3')
|
142 |
-
sample['s5p_o3'] = arr_s5p_o3
|
143 |
-
meta_data['s5p_o3'] = meta_s5p_o3
|
144 |
-
# s5p_so2
|
145 |
-
if 's5p_so2' in self.modality:
|
146 |
-
arr_s5p_so2, meta_s5p_so2 = self.get_s5p(grid_id,'s5p_so2')
|
147 |
-
sample['s5p_so2'] = arr_s5p_so2
|
148 |
-
meta_data['s5p_so2'] = meta_s5p_so2
|
149 |
-
# dem
|
150 |
-
if 'dem' in self.modality:
|
151 |
-
arr_dem = self.get_dem(grid_id)
|
152 |
-
sample['dem'] = arr_dem
|
153 |
-
meta_data['dem'] = grid_id_coord
|
154 |
-
|
155 |
-
return sample, meta_data
|
156 |
-
|
157 |
-
|
158 |
-
if __name__ == '__main__':
|
159 |
-
parser = argparse.ArgumentParser()
|
160 |
-
parser.add_argument('--fnames_path', type=str, default='data_loading/fnames.json.gz')
|
161 |
-
parser.add_argument('--root_dir', type=str, default='data_loading/data')
|
162 |
-
args = parser.parse_args()
|
163 |
-
|
164 |
-
# transform_s1 = cvtransforms.Compose([
|
165 |
-
# cvtransforms.CenterCrop(224),
|
166 |
-
# cvtransforms.ToTensor()
|
167 |
-
# ])
|
168 |
-
# transform_s2 = cvtransforms.Compose([
|
169 |
-
# cvtransforms.CenterCrop(224),
|
170 |
-
# cvtransforms.ToTensor()
|
171 |
-
# ])
|
172 |
-
# transform_s3 = cvtransforms.Compose([
|
173 |
-
# cvtransforms.CenterCrop(96),
|
174 |
-
# cvtransforms.ToTensor()
|
175 |
-
# ])
|
176 |
-
# transform_s5p = cvtransforms.Compose([
|
177 |
-
# cvtransforms.CenterCrop(28),
|
178 |
-
# cvtransforms.ToTensor()
|
179 |
-
# ])
|
180 |
-
# transform_dem = cvtransforms.Compose([
|
181 |
-
# cvtransforms.CenterCrop(960),
|
182 |
-
# cvtransforms.ToTensor()
|
183 |
-
# ])
|
184 |
-
transform_s1 = AugmentationSequential(
|
185 |
-
#kornia.augmentation.SmallestMaxSize(264),
|
186 |
-
kornia.augmentation.CenterCrop(224),
|
187 |
-
)
|
188 |
-
transform_s2 = AugmentationSequential(
|
189 |
-
#kornia.augmentation.SmallestMaxSize(264),
|
190 |
-
kornia.augmentation.CenterCrop(224),
|
191 |
-
)
|
192 |
-
transform_s3 = AugmentationSequential(
|
193 |
-
kornia.augmentation.SmallestMaxSize(96),
|
194 |
-
kornia.augmentation.CenterCrop(96),
|
195 |
-
)
|
196 |
-
transform_s5p = AugmentationSequential(
|
197 |
-
kornia.augmentation.SmallestMaxSize(28),
|
198 |
-
kornia.augmentation.CenterCrop(28),
|
199 |
-
)
|
200 |
-
transform_dem = AugmentationSequential(
|
201 |
-
kornia.augmentation.SmallestMaxSize(960),
|
202 |
-
kornia.augmentation.CenterCrop(960),
|
203 |
-
)
|
204 |
-
|
205 |
-
|
206 |
-
ssl4eo_s = SSL4EO_S(args.fnames_path, args.root_dir, transform_s1=transform_s1, transform_s2=transform_s2, transform_s3=transform_s3, transform_s5p=transform_s5p, transform_dem=transform_dem)
|
207 |
-
dataloader = DataLoader(ssl4eo_s, batch_size=1, shuffle=True, num_workers=0) # batch size can only be 1 because of varying number of images per grid
|
208 |
-
|
209 |
-
for i, (sample, meta_data) in enumerate(dataloader):
|
210 |
-
#print(i)
|
211 |
-
print('Grid ID:', meta_data['dem'][0])
|
212 |
-
print(sample.keys())
|
213 |
-
print(meta_data.keys())
|
214 |
-
|
215 |
-
|
216 |
-
print('### S1 GRD ###')
|
217 |
-
print('Number of s1 local patches:', len(meta_data['s1_grd']), ' ', 'Number of time stamps for first local patch:', len(meta_data['s1_grd'][0]))
|
218 |
-
print('Example for one image:', sample['s1_grd'][0][0].shape, meta_data['s1_grd'][0][0])
|
219 |
-
print('### S2 TOA ###')
|
220 |
-
print('Number of s2 local patches:', len(meta_data['s2_toa']), ' ', 'Number of time stamps for first local patch:', len(meta_data['s2_toa'][0]))
|
221 |
-
print('Example for one image:', sample['s2_toa'][0][0].shape, meta_data['s2_toa'][0][0])
|
222 |
-
print('### S3 OLCI ###')
|
223 |
-
print('Number of s3 time stamps:', len(meta_data['s3_olci']))
|
224 |
-
print('Example for one image:', sample['s3_olci'][0].shape, meta_data['s3_olci'][0])
|
225 |
-
print('### S5P ###')
|
226 |
-
print('Number of s5p time stamps for CO/NO2/O3/SO2:', len(meta_data['s5p_co']), len(meta_data['s5p_no2']), len(meta_data['s5p_o3']), len(meta_data['s5p_so2']))
|
227 |
-
print('Example for one CO image:', sample['s5p_co'][0].shape, meta_data['s5p_co'][0])
|
228 |
-
print('Example for one NO2 image:', sample['s5p_no2'][0].shape, meta_data['s5p_no2'][0])
|
229 |
-
print('Example for one O3 image:', sample['s5p_o3'][0].shape, meta_data['s5p_o3'][0])
|
230 |
-
print('Example for one SO2 image:', sample['s5p_so2'][0].shape, meta_data['s5p_so2'][0])
|
231 |
-
print('### DEM ###')
|
232 |
-
print('One DEM image for the grid:', sample['dem'].shape, meta_data['dem'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