yuxuanw8 commited on
Commit
36e605a
·
verified ·
1 Parent(s): a455fc2

Upload So2Sat.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. So2Sat.py +132 -0
So2Sat.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import shutil
4
+ import datasets
5
+ import tifffile
6
+
7
+ import numpy as np
8
+ import pandas as pd
9
+
10
+ S2_MEAN = [0.12375696117681, 0.10927746363683, 0.10108552032678, 0.11423986161140, 0.15926566920230, 0.18147236008771,
11
+ 0.17457403122913, 0.19501607349635, 0.15428468872076, 0.10905050699570]
12
+ S2_STD = [0.03958795, 0.04777826, 0.06636616, 0.06358874, 0.07744387, 0.09101635, 0.09218466, 0.10164581, 0.09991773, 0.08780632]
13
+
14
+ class EuroSATDataset(datasets.GeneratorBasedBuilder):
15
+ VERSION = datasets.Version("1.0.0")
16
+
17
+ DATA_URL = "https://huggingface.co/datasets/GFM-Bench/So2Sat/resolve/main/So2Sat.zip"
18
+
19
+ metadata = {
20
+ "s2c": {
21
+ "bands": ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12'],
22
+ "channel_wv": [492.4, 559.8, 664.6, 704.1, 740.5, 782.8, 832.8, 864.7, 1613.7, 2202.4],
23
+ "mean": S2_MEAN,
24
+ "std": S2_STD,
25
+ },
26
+ "s1": {
27
+ "bands": None,
28
+ "channel_wv": None,
29
+ "mean": None,
30
+ "std": None
31
+ }
32
+ }
33
+
34
+ SIZE = HEIGHT = WIDTH = 32
35
+
36
+ NUM_CLASSES = 17
37
+
38
+ spatial_resolution = 10
39
+
40
+ def __init__(self, *args, **kwargs):
41
+ super().__init__(*args, **kwargs)
42
+
43
+ def _info(self):
44
+ metadata = self.metadata
45
+ metadata['size'] = self.SIZE
46
+ metadata['num_classes'] = self.NUM_CLASSES
47
+ metadata['spatial_resolution'] = self.spatial_resolution
48
+ return datasets.DatasetInfo(
49
+ description=json.dumps(metadata),
50
+ features=datasets.Features({
51
+ "optical": datasets.Array3D(shape=(10, self.HEIGHT, self.WIDTH), dtype="float32"),
52
+ "label": datasets.Value("int32"),
53
+ "optical_channel_wv": datasets.Sequence(datasets.Value("float32")),
54
+ "spatial_resolution": datasets.Value("float32"),
55
+ }),
56
+ )
57
+
58
+ def _split_generators(self, dl_manager):
59
+ if isinstance(self.DATA_URL, list):
60
+ downloaded_files = dl_manager.download(self.DATA_URL)
61
+ combined_file = os.path.join(dl_manager.download_config.cache_dir, "combined.tar.gz")
62
+ with open(combined_file, 'wb') as outfile:
63
+ for part_file in downloaded_files:
64
+ with open(part_file, 'rb') as infile:
65
+ shutil.copyfileobj(infile, outfile)
66
+ data_dir = dl_manager.extract(combined_file)
67
+ os.remove(combined_file)
68
+ else:
69
+ data_dir = dl_manager.download_and_extract(self.DATA_URL)
70
+
71
+ return [
72
+ datasets.SplitGenerator(
73
+ name="train",
74
+ gen_kwargs={
75
+ "split": 'train',
76
+ "data_dir": data_dir,
77
+ },
78
+ ),
79
+ datasets.SplitGenerator(
80
+ name="val",
81
+ gen_kwargs={
82
+ "split": 'val',
83
+ "data_dir": data_dir,
84
+ },
85
+ ),
86
+ datasets.SplitGenerator(
87
+ name="test",
88
+ gen_kwargs={
89
+ "split": 'test',
90
+ "data_dir": data_dir,
91
+ },
92
+ )
93
+ ]
94
+
95
+ def _generate_examples(self, split, data_dir):
96
+ optical_channel_wv = np.array(self.metadata["s2c"]["channel_wv"])
97
+ spatial_resolution = self.spatial_resolution
98
+
99
+ data_dir = os.path.join(data_dir, "So2Sat")
100
+ metadata = pd.read_csv(os.path.join(data_dir, "metadata.csv"))
101
+ metadata = metadata[metadata["split"] == split].reset_index(drop=True)
102
+
103
+ for index, row in metadata.iterrows():
104
+ optical_path = os.path.join(data_dir, row.optical_path)
105
+ optical = self._read_image(optical_path).astype(np.float32) # CxHxW
106
+
107
+ label = int(row.label)
108
+
109
+ sample = {
110
+ "optical": optical,
111
+ "label": label,
112
+ "optical_channel_wv": optical_channel_wv,
113
+ "spatial_resolution": spatial_resolution,
114
+ }
115
+
116
+ yield f"{index}", sample
117
+
118
+ def _read_image(self, image_path):
119
+ """Read tiff image from image_path
120
+ Args:
121
+ image_path:
122
+ Image path to read from
123
+
124
+ Return:
125
+ image:
126
+ C, H, W numpy array image
127
+ """
128
+ image = tifffile.imread(image_path)
129
+ if len(image.shape) == 3:
130
+ image = np.transpose(image, (2, 0, 1))
131
+
132
+ return image