yuxuanw8 commited on
Commit
a901fa0
·
verified ·
1 Parent(s): c488943

Upload DFC2020.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. DFC2020.py +163 -0
DFC2020.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import shutil
4
+ import tifffile
5
+ import datasets
6
+
7
+ import pandas as pd
8
+ import numpy as np
9
+
10
+ S2_MEAN = [1370.19151926, 1184.3824625, 1120.77120066, 1136.26026392, 1263.73947144, 1645.40315151, 1846.87040806, 1762.59530783, 1972.62420416, 582.72633433, 14.77112979, 1732.16362238, 1247.91870117]
11
+
12
+ S2_STD = [633.15169573, 650.2842772, 712.12507725, 965.23119807, 948.9819932, 1108.06650639, 1258.36394548, 1233.1492281, 1364.38688993, 472.37967789, 14.3114637, 1310.36996126, 1087.6020813]
13
+
14
+ S1_MEAN = [-12.54847273, -20.19237134]
15
+
16
+ S1_STD = [5.25697717, 5.91150917]
17
+
18
+ class DFC2020Dataset(datasets.GeneratorBasedBuilder):
19
+ VERSION = datasets.Version("1.0.0")
20
+
21
+ DATA_URL = "https://huggingface.co/datasets/GFM-Bench/DFC2020/resolve/main/data/DFC2020.zip"
22
+
23
+ metadata = {
24
+ "s2c": {
25
+ "bands": ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B9", "B10", "B11", "B12"],
26
+ "channel_wv": [442.7, 492.4, 559.8, 664.6, 704.1, 740.5, 782.8, 832.8, 864.7, 945.1, 1373.5, 1613.7, 2202.4],
27
+ "mean": S2_MEAN,
28
+ "std": S2_STD,
29
+ },
30
+ "s1": {
31
+ "bands": ["VV", "VH"],
32
+ "channel_wv": [5500, 5700],
33
+ "mean": S1_MEAN,
34
+ "std": S1_STD
35
+ }
36
+ }
37
+
38
+ SIZE = HEIGHT = WIDTH = 96
39
+
40
+ spatial_resolution = 10
41
+
42
+ DFC2020_CLASSES = [
43
+ 255, # class 0 unused in both schemes
44
+ 0, 0, 0, 0, 0,
45
+ 1, 1,
46
+ 255, # --> will be masked if no_savanna == True
47
+ 255, # --> will be masked if no_savanna == True
48
+ 2,
49
+ 3,
50
+ 4, # 12 --> 6
51
+ 5, # 13 --> 7
52
+ 4, # 14 --> 6
53
+ 255,
54
+ 6,
55
+ 7
56
+ ]
57
+
58
+ NUM_CLASSES = 8
59
+
60
+ def __init__(self, *args, **kwargs):
61
+ super().__init__(*args, **kwargs)
62
+
63
+
64
+ def _info(self):
65
+ metadata = self.metadata
66
+ metadata['size'] = self.SIZE
67
+ metadata['num_classes'] = self.NUM_CLASSES
68
+ metadata['spatial_resolution'] = self.spatial_resolution
69
+ return datasets.DatasetInfo(
70
+ description=json.dumps(metadata),
71
+ features=datasets.Features({
72
+ "optical": datasets.Array3D(shape=(13, self.HEIGHT, self.WIDTH), dtype="float32"),
73
+ "radar": datasets.Array3D(shape=(2, self.HEIGHT, self.WIDTH), dtype="float32"),
74
+ "label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"),
75
+ "optical_channel_wv": datasets.Sequence(datasets.Value("float32")),
76
+ "radar_channel_wv": datasets.Sequence(datasets.Value("float32")),
77
+ "spatial_resolution": datasets.Value("int32"),
78
+ }),
79
+ )
80
+
81
+ def _split_generators(self, dl_manager):
82
+ if isinstance(self.DATA_URL, list):
83
+ downloaded_files = dl_manager.download(self.DATA_URL)
84
+ combined_file = os.path.join(dl_manager.download_config.cache_dir, "combined.tar.gz")
85
+ with open(combined_file, 'wb') as outfile:
86
+ for part_file in downloaded_files:
87
+ with open(part_file, 'rb') as infile:
88
+ shutil.copyfileobj(infile, outfile)
89
+ data_dir = dl_manager.extract(combined_file)
90
+ os.remove(combined_file)
91
+ else:
92
+ data_dir = dl_manager.download_and_extract(self.DATA_URL)
93
+
94
+ return [
95
+ datasets.SplitGenerator(
96
+ name="train",
97
+ gen_kwargs={
98
+ "split": 'train',
99
+ "data_dir": data_dir,
100
+ },
101
+ ),
102
+ datasets.SplitGenerator(
103
+ name="val",
104
+ gen_kwargs={
105
+ "split": 'val',
106
+ "data_dir": data_dir,
107
+ },
108
+ ),
109
+ datasets.SplitGenerator(
110
+ name="test",
111
+ gen_kwargs={
112
+ "split": 'test',
113
+ "data_dir": data_dir,
114
+ },
115
+ )
116
+ ]
117
+
118
+ def _generate_examples(self, split, data_dir):
119
+ optical_channel_wv = self.metadata["s2c"]["channel_wv"]
120
+ radar_channel_wv = self.metadata["s1"]["channel_wv"]
121
+ spatial_resolution = self.spatial_resolution
122
+
123
+ data_dir = os.path.join(data_dir, "DFC2020")
124
+ metadata = pd.read_csv(os.path.join(data_dir, "metadata.csv"))
125
+ metadata = metadata[metadata["split"] == split].reset_index(drop=True)
126
+
127
+ for index, row in metadata.iterrows():
128
+ optical_path = os.path.join(data_dir, row.optical_path)
129
+ optical = self._read_image(optical_path).astype(np.float32) # CxHxW
130
+
131
+ radar_path = os.path.join(data_dir, row.radar_path)
132
+ radar = self._read_image(radar_path).astype(np.float32)
133
+
134
+ label_path = os.path.join(data_dir, row.label_path)
135
+ label = self._read_image(label_path)[0, :, :]
136
+ label = np.take(self.DFC2020_CLASSES, label.astype(np.int64))
137
+ label = label.astype(np.int32)
138
+
139
+ sample = {
140
+ "optical": optical,
141
+ "radar": radar,
142
+ "optical_channel_wv": optical_channel_wv,
143
+ "radar_channel_wv": radar_channel_wv,
144
+ "label": label,
145
+ "spatial_resolution": spatial_resolution,
146
+ }
147
+
148
+ yield f"{index}", sample
149
+
150
+ def _read_image(self, image_path):
151
+ """Read tiff image from image_path
152
+ Args:
153
+ image_path:
154
+ Image path to read from
155
+
156
+ Return:
157
+ image:
158
+ C, H, W numpy array image
159
+ """
160
+ image = tifffile.imread(image_path)
161
+ image = np.transpose(image, (2, 0, 1))
162
+
163
+ return image