Datasets:
upoad load_data.py to replace in Flair#2 Git to use HF dataset
Browse files- load_data.py +111 -0
load_data.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def load_data (config: dict):
|
2 |
+
|
3 |
+
def get_data_paths(config: dict, path_domains: str, paths_data: dict, matching_dict: dict) -> dict:
|
4 |
+
|
5 |
+
#### return data paths
|
6 |
+
def list_items(path, filter):
|
7 |
+
for path in Path(path).rglob(filter):
|
8 |
+
yield path.resolve().as_posix()
|
9 |
+
|
10 |
+
## data paths dict
|
11 |
+
data = {'PATH_IMG':[], 'PATH_SP_DATA':[], 'SP_COORDS':[], 'PATH_SP_DATES':[], 'PATH_SP_MASKS':[], 'PATH_LABELS':[], 'MTD_AERIAL':[]}
|
12 |
+
for domain in path_domains:
|
13 |
+
aerial = sorted(list(list_items(Path(path_data)/domain, 'IMG*.tif')), key=lambda x: int(x.split('_')[-1][:-4]))
|
14 |
+
sen2sp = sorted(list(list_items(Path(path_data)/domain, '*data.npy')))
|
15 |
+
sprods = sorted(list(list_items(Path(path_data)/domain, '*products.txt')))
|
16 |
+
smasks = sorted(list(list_items(Path(path_data)/domain, '*masks.npy')))
|
17 |
+
labels = sorted(list(list_items(Path(path_data)/domain, 'MSK*.tif')), key=lambda x: int(x.split('_')[-1][:-4]))
|
18 |
+
coords = []
|
19 |
+
for k in aerial:
|
20 |
+
coords.append(matching_dict[k.split('/')[-1]])
|
21 |
+
|
22 |
+
data['PATH_IMG'] += aerial
|
23 |
+
data['PATH_SP_DATA'] += sen2sp*len(aerial)
|
24 |
+
data['PATH_SP_DATES'] += sprods*len(aerial)
|
25 |
+
data['PATH_SP_MASKS'] += smasks*len(aerial)
|
26 |
+
data['SP_COORDS'] += coords
|
27 |
+
data['PATH_LABELS'] += labels
|
28 |
+
|
29 |
+
if config['aerial_metadata'] == True:
|
30 |
+
data = adding_encoded_metadata(config['data']['path_metadata_aerial'], data)
|
31 |
+
|
32 |
+
return data
|
33 |
+
|
34 |
+
###### READING CONFIG AND GETTING DATA PATHS
|
35 |
+
path_data = config['data']['HF_data_path']
|
36 |
+
|
37 |
+
train_domains, val_domains, test_domains = config['data']['domains_train'], config['data']['domains_val'], config['data']['domains_test']
|
38 |
+
|
39 |
+
with open(config['data']['path_sp_centroids'], 'r') as file:
|
40 |
+
matching_dict = json.load(file)
|
41 |
+
|
42 |
+
dict_train = get_data_paths(config, train_domains, path_data, matching_dict)
|
43 |
+
dict_val = get_data_paths(config, val_domains, path_data, matching_dict)
|
44 |
+
dict_test = get_data_paths(config, test_domains, path_data, matching_dict)
|
45 |
+
|
46 |
+
return dict_train, dict_val, dict_test
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
def adding_encoded_metadata(path_metadata_file: str, dict_paths: dict, loc_enc_size: int = 32):
|
56 |
+
"""
|
57 |
+
For every aerial image in the dataset, get metadata, encode and add to data dict.
|
58 |
+
"""
|
59 |
+
#### encode metadata
|
60 |
+
def coordenc_opt(coords, enc_size=32) -> np.array:
|
61 |
+
d = int(enc_size/2)
|
62 |
+
d_i = np.arange(0, d / 2)
|
63 |
+
freq = 1 / (10e7 ** (2 * d_i / d))
|
64 |
+
|
65 |
+
x,y = coords[0]/10e7, coords[1]/10e7
|
66 |
+
enc = np.zeros(d * 2)
|
67 |
+
enc[0:d:2] = np.sin(x * freq)
|
68 |
+
enc[1:d:2] = np.cos(x * freq)
|
69 |
+
enc[d::2] = np.sin(y * freq)
|
70 |
+
enc[d + 1::2] = np.cos(y * freq)
|
71 |
+
return list(enc)
|
72 |
+
|
73 |
+
def norm_alti(alti: int) -> float:
|
74 |
+
min_alti = 0
|
75 |
+
max_alti = 3164.9099121094 ### MAX DATASET
|
76 |
+
return [(alti-min_alti) / (max_alti-min_alti)]
|
77 |
+
|
78 |
+
def format_cam(cam: str) -> np.array:
|
79 |
+
return [[1,0] if 'UCE' in cam else [0,1]][0]
|
80 |
+
|
81 |
+
def cyclical_enc_datetime(date: str, time: str) -> list:
|
82 |
+
def norm(num: float) -> float:
|
83 |
+
return (num-(-1))/(1-(-1))
|
84 |
+
year, month, day = date.split('-')
|
85 |
+
if year == '2018': enc_y = [1,0,0,0]
|
86 |
+
elif year == '2019': enc_y = [0,1,0,0]
|
87 |
+
elif year == '2020': enc_y = [0,0,1,0]
|
88 |
+
elif year == '2021': enc_y = [0,0,0,1]
|
89 |
+
sin_month = np.sin(2*np.pi*(int(month)-1/12)) ## months of year
|
90 |
+
cos_month = np.cos(2*np.pi*(int(month)-1/12))
|
91 |
+
sin_day = np.sin(2*np.pi*(int(day)/31)) ## max days
|
92 |
+
cos_day = np.cos(2*np.pi*(int(day)/31))
|
93 |
+
h,m=time.split('h')
|
94 |
+
sec_day = int(h) * 3600 + int(m) * 60
|
95 |
+
sin_time = np.sin(2*np.pi*(sec_day/86400)) ## total sec in day
|
96 |
+
cos_time = np.cos(2*np.pi*(sec_day/86400))
|
97 |
+
return enc_y+[norm(sin_month),norm(cos_month),norm(sin_day),norm(cos_day),norm(sin_time),norm(cos_time)]
|
98 |
+
|
99 |
+
|
100 |
+
with open(path_metadata_file, 'r') as f:
|
101 |
+
metadata_dict = json.load(f)
|
102 |
+
for img in dict_paths['PATH_IMG']:
|
103 |
+
curr_img = img.split('/')[-1][:-4]
|
104 |
+
enc_coords = coordenc_opt([metadata_dict[curr_img]["patch_centroid_x"], metadata_dict[curr_img]["patch_centroid_y"]], enc_size=loc_enc_size)
|
105 |
+
enc_alti = norm_alti(metadata_dict[curr_img]["patch_centroid_z"])
|
106 |
+
enc_camera = format_cam(metadata_dict[curr_img]['camera'])
|
107 |
+
enc_temporal = cyclical_enc_datetime(metadata_dict[curr_img]['date'], metadata_dict[curr_img]['time'])
|
108 |
+
mtd_enc = enc_coords+enc_alti+enc_camera+enc_temporal
|
109 |
+
dict_paths['MTD_AERIAL'].append(mtd_enc)
|
110 |
+
|
111 |
+
return dict_paths
|