Datasets:
IGNF
/

ArXiv:
License:
File size: 4,919 Bytes
b880d60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
def load_data (config: dict): 
    
    def get_data_paths(config: dict, path_domains: str, paths_data: dict, matching_dict: dict) -> dict: 
        
        #### return data paths 
        def list_items(path, filter): 
            for path in Path(path).rglob(filter): 
                yield path.resolve().as_posix() 

        ## data paths dict
        data = {'PATH_IMG':[], 'PATH_SP_DATA':[], 'SP_COORDS':[], 'PATH_SP_DATES':[],  'PATH_SP_MASKS':[], 'PATH_LABELS':[], 'MTD_AERIAL':[]} 
        for domain in path_domains: 
            aerial = sorted(list(list_items(Path(path_data)/domain, 'IMG*.tif')), key=lambda x: int(x.split('_')[-1][:-4])) 
            sen2sp = sorted(list(list_items(Path(path_data)/domain, '*data.npy'))) 
            sprods = sorted(list(list_items(Path(path_data)/domain, '*products.txt')))
            smasks = sorted(list(list_items(Path(path_data)/domain, '*masks.npy')))
            labels = sorted(list(list_items(Path(path_data)/domain, 'MSK*.tif')), key=lambda x: int(x.split('_')[-1][:-4])) 
            coords = [] 
            for k in aerial: 
                coords.append(matching_dict[k.split('/')[-1]]) 
            
            data['PATH_IMG'] += aerial 
            data['PATH_SP_DATA'] += sen2sp*len(aerial) 
            data['PATH_SP_DATES'] += sprods*len(aerial)
            data['PATH_SP_MASKS'] += smasks*len(aerial) 
            data['SP_COORDS'] += coords 
            data['PATH_LABELS'] += labels
 
        if config['aerial_metadata'] == True:
            data = adding_encoded_metadata(config['data']['path_metadata_aerial'], data) 

        return data  

    ###### READING CONFIG AND GETTING DATA PATHS
    path_data = config['data']['HF_data_path'] 

    train_domains, val_domains, test_domains = config['data']['domains_train'], config['data']['domains_val'], config['data']['domains_test']

    with open(config['data']['path_sp_centroids'], 'r') as file: 
        matching_dict = json.load(file) 

    dict_train = get_data_paths(config, train_domains, path_data, matching_dict) 
    dict_val   = get_data_paths(config, val_domains,   path_data, matching_dict) 
    dict_test  = get_data_paths(config, test_domains,  path_data, matching_dict) 
    
    return dict_train, dict_val, dict_test    
    
    
    
    
    
    
    
    
 def adding_encoded_metadata(path_metadata_file: str, dict_paths: dict, loc_enc_size: int = 32):
    """
    For every aerial image in the dataset, get metadata, encode and add to data dict.
    """
    #### encode metadata
    def coordenc_opt(coords, enc_size=32) -> np.array:
        d = int(enc_size/2)
        d_i = np.arange(0, d / 2)
        freq = 1 / (10e7 ** (2 * d_i / d))

        x,y = coords[0]/10e7, coords[1]/10e7
        enc = np.zeros(d * 2)
        enc[0:d:2]    = np.sin(x * freq)
        enc[1:d:2]    = np.cos(x * freq)
        enc[d::2]     = np.sin(y * freq)
        enc[d + 1::2] = np.cos(y * freq)
        return list(enc)           

    def norm_alti(alti: int) -> float:
        min_alti = 0
        max_alti = 3164.9099121094  ### MAX DATASET
        return [(alti-min_alti) / (max_alti-min_alti)]        

    def format_cam(cam: str) -> np.array:
        return [[1,0] if 'UCE' in cam else [0,1]][0]

    def cyclical_enc_datetime(date: str, time: str) -> list:
        def norm(num: float) -> float:
            return (num-(-1))/(1-(-1))
        year, month, day = date.split('-')
        if year == '2018':   enc_y = [1,0,0,0]
        elif year == '2019': enc_y = [0,1,0,0]
        elif year == '2020': enc_y = [0,0,1,0]
        elif year == '2021': enc_y = [0,0,0,1]    
        sin_month = np.sin(2*np.pi*(int(month)-1/12)) ## months of year
        cos_month = np.cos(2*np.pi*(int(month)-1/12))    
        sin_day = np.sin(2*np.pi*(int(day)/31)) ## max days
        cos_day = np.cos(2*np.pi*(int(day)/31))     
        h,m=time.split('h')
        sec_day = int(h) * 3600 + int(m) * 60
        sin_time = np.sin(2*np.pi*(sec_day/86400)) ## total sec in day
        cos_time = np.cos(2*np.pi*(sec_day/86400))
        return enc_y+[norm(sin_month),norm(cos_month),norm(sin_day),norm(cos_day),norm(sin_time),norm(cos_time)] 
    
    
    with open(path_metadata_file, 'r') as f:
        metadata_dict = json.load(f)              
    for img in dict_paths['PATH_IMG']:
        curr_img     = img.split('/')[-1][:-4]
        enc_coords   = coordenc_opt([metadata_dict[curr_img]["patch_centroid_x"], metadata_dict[curr_img]["patch_centroid_y"]], enc_size=loc_enc_size)
        enc_alti     = norm_alti(metadata_dict[curr_img]["patch_centroid_z"])
        enc_camera   = format_cam(metadata_dict[curr_img]['camera'])
        enc_temporal = cyclical_enc_datetime(metadata_dict[curr_img]['date'], metadata_dict[curr_img]['time'])
        mtd_enc      = enc_coords+enc_alti+enc_camera+enc_temporal 
        dict_paths['MTD_AERIAL'].append(mtd_enc)    
        
    return dict_paths