Delete EuroSAT.py
Browse files- EuroSAT.py +0 -181
EuroSAT.py
DELETED
@@ -1,181 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import datasets
|
3 |
-
import numpy as np
|
4 |
-
import tifffile
|
5 |
-
import skimage.io as io
|
6 |
-
|
7 |
-
from typing import ClassVar
|
8 |
-
|
9 |
-
all_band_names = (
|
10 |
-
'B01',
|
11 |
-
'B02',
|
12 |
-
'B03',
|
13 |
-
'B04',
|
14 |
-
'B05',
|
15 |
-
'B06',
|
16 |
-
'B07',
|
17 |
-
'B08',
|
18 |
-
'B8A',
|
19 |
-
'B09',
|
20 |
-
'B10',
|
21 |
-
'B11',
|
22 |
-
'B12',
|
23 |
-
)
|
24 |
-
|
25 |
-
rgb_bands = ('B04', 'B03', 'B02')
|
26 |
-
|
27 |
-
class EuroSATConfig(datasets.BuilderConfig):
|
28 |
-
BAND_SETS: ClassVar[dict[str, tuple[str, ...]]] = {
|
29 |
-
'all': all_band_names,
|
30 |
-
'rgb': rgb_bands,
|
31 |
-
}
|
32 |
-
"""BuilderConfig for EuroSAT"""
|
33 |
-
def __init__(self, bands=BAND_SETS['all'], **kwargs):
|
34 |
-
super(EuroSATConfig, self).__init__(**kwargs)
|
35 |
-
self.bands = bands
|
36 |
-
self.band_indices = [int(all_band_names.index(b)) for b in bands if b in all_band_names]
|
37 |
-
|
38 |
-
# def update(self, **kwargs):
|
39 |
-
# """Update Config"""
|
40 |
-
# super().update(**kwargs)
|
41 |
-
# if 'bands' in kwargs:
|
42 |
-
# self.band_indices = [int(self.all_band_names.index(b)) for b in self.bands if b in all_band_names]
|
43 |
-
|
44 |
-
class EuroSAT(datasets.GeneratorBasedBuilder):
|
45 |
-
VERSION = datasets.Version("1.0.0")
|
46 |
-
|
47 |
-
BUILDER_CONFIGS = [
|
48 |
-
EuroSATConfig(
|
49 |
-
name="default",
|
50 |
-
description="Default configuration"
|
51 |
-
),
|
52 |
-
]
|
53 |
-
|
54 |
-
DEFAULT_CONFIG_NAME = "default"
|
55 |
-
|
56 |
-
HEIGHT = WIDTH = 64
|
57 |
-
|
58 |
-
def __init__(self, *args, **kwargs):
|
59 |
-
config = kwargs.pop('config', None)
|
60 |
-
config_keywords = ['bands']
|
61 |
-
|
62 |
-
if config:
|
63 |
-
if isinstance(config, dict):
|
64 |
-
for key, value in config.items():
|
65 |
-
if key in config_keywords:
|
66 |
-
kwargs[key] = value
|
67 |
-
if key in ['bands']:
|
68 |
-
kwargs['band_indices'] = [int(all_band_names.index(b)) for b in kwargs['bands'] if b in all_band_names]
|
69 |
-
|
70 |
-
|
71 |
-
self.height = self.HEIGHT
|
72 |
-
self.width = self.WIDTH
|
73 |
-
self.labels = [
|
74 |
-
"AnnualCrop",
|
75 |
-
"Forest",
|
76 |
-
"HerbaceousVegetation",
|
77 |
-
"Highway",
|
78 |
-
"Industrial",
|
79 |
-
"Pasture",
|
80 |
-
"PermanentCrop",
|
81 |
-
"Residential",
|
82 |
-
"River",
|
83 |
-
"SeaLake",
|
84 |
-
]
|
85 |
-
for k, _ in kwargs.items():
|
86 |
-
print(k)
|
87 |
-
super().__init__(*args, **kwargs)
|
88 |
-
self.num_channels = len(self.config.band_indices)
|
89 |
-
print(f"self.num_channels: {self.num_channels}")
|
90 |
-
# if 'config' in kwargs:
|
91 |
-
# user_config = kwargs['config']
|
92 |
-
# if isinstance(user_config, dict):
|
93 |
-
# for key, value in user_config.items():
|
94 |
-
# if hasattr(self.config, key):
|
95 |
-
# setattr(self.config, key, value)
|
96 |
-
# else:
|
97 |
-
# raise ValueError("check user_config, it should be a dict")
|
98 |
-
# else:
|
99 |
-
# raise ValueError("config is not specified in arguments")
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
def _info(self):
|
104 |
-
return datasets.DatasetInfo(
|
105 |
-
features=datasets.Features({
|
106 |
-
"image": datasets.Array3D(shape=(len(self.config.band_indices), self.height, self.width), dtype="float32"),
|
107 |
-
"label": datasets.ClassLabel(names=self.labels),
|
108 |
-
}),
|
109 |
-
supervised_keys=("image", "label")
|
110 |
-
)
|
111 |
-
|
112 |
-
def _split_generators(self, dl_manager):
|
113 |
-
data_dir = dl_manager.download_and_extract("https://huggingface.co/datasets/yuxuanw8/EuroSAT/resolve/main/EuroSAT.zip") # TODO: check out the correct address for downloading and extracting
|
114 |
-
|
115 |
-
return [
|
116 |
-
datasets.SplitGenerator(
|
117 |
-
name="train",
|
118 |
-
gen_kwargs={
|
119 |
-
"split_file": os.path.join(data_dir, "eurosat-train.txt"),
|
120 |
-
"data_dir": os.path.join(data_dir, "EuroSAT"),
|
121 |
-
},
|
122 |
-
),
|
123 |
-
datasets.SplitGenerator(
|
124 |
-
name="val",
|
125 |
-
gen_kwargs={
|
126 |
-
"split_file": os.path.join(data_dir, "eurosat-val.txt"),
|
127 |
-
"data_dir": os.path.join(data_dir, "EuroSAT"),
|
128 |
-
},
|
129 |
-
),
|
130 |
-
datasets.SplitGenerator(
|
131 |
-
name="test",
|
132 |
-
gen_kwargs={
|
133 |
-
"split_file": os.path.join(data_dir, "eurosat-test.txt"),
|
134 |
-
"data_dir": os.path.join(data_dir, "EuroSAT"),
|
135 |
-
},
|
136 |
-
)
|
137 |
-
]
|
138 |
-
|
139 |
-
def _generate_examples(self, split_file, data_dir):
|
140 |
-
|
141 |
-
"""
|
142 |
-
split_file: the filename that lists all data points
|
143 |
-
data_dir: directory where the actual data is stored
|
144 |
-
"""
|
145 |
-
|
146 |
-
"""
|
147 |
-
data_dir should be in following structure:
|
148 |
-
- EuroSAT
|
149 |
-
- AnnualCrop
|
150 |
-
- AnnualCrop_1.tif
|
151 |
-
- AnnualCrop_2.tif
|
152 |
-
- ...
|
153 |
-
- Forest
|
154 |
-
- Forest_1.tif
|
155 |
-
- FOrest_2.tif
|
156 |
-
- ...
|
157 |
-
- ...
|
158 |
-
"""
|
159 |
-
|
160 |
-
with open(split_file, 'r') as f:
|
161 |
-
for line in f:
|
162 |
-
line = line.strip()
|
163 |
-
|
164 |
-
file_name = line.replace(".jpg", ".tif")
|
165 |
-
label = file_name.split('_')[0]
|
166 |
-
data_path = os.path.join(data_dir, label, file_name)
|
167 |
-
|
168 |
-
img = tifffile.imread(data_path)
|
169 |
-
|
170 |
-
# permute img from HxWxC to CxHxW
|
171 |
-
img = np.transpose(img, (2, 0, 1))
|
172 |
-
|
173 |
-
# drop any channels if applicable
|
174 |
-
img = np.take(img, indices=self.config.band_indices, axis=0)
|
175 |
-
|
176 |
-
sample = {
|
177 |
-
"image": img.astype(np.float32),
|
178 |
-
"label": self.info.features['label'].str2int(label),
|
179 |
-
}
|
180 |
-
|
181 |
-
yield f"{label}_{file_name}", sample
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|