forked from microsoft/torchgeo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_bigearthnet_ffcv.py
169 lines (141 loc) · 4.84 KB
/
create_bigearthnet_ffcv.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""."""
# %%
import glob
import json
import os
from typing import Any, Callable, Dict, Optional
import numpy as np
import rasterio
import torch
from ffcv.fields import NDArrayField
from ffcv.writer import DatasetWriter
from rasterio.enums import Resampling
from torch import Tensor
from tqdm import tqdm
from torchgeo.datasets import BigEarthNet
# %%
DATA_DIR = "/scratch/users/mike/data"
BIGEARTHNET_DIR = "BigEarthNet"
# %%
class BigEarthNetNumpy(BigEarthNet):
"""BigEarthNet but returns numpy arrays instead of torch tensors."""
def __init__(
self,
root: str = "data",
split: str = "train",
bands: str = "all",
num_classes: int = 19,
transforms: Optional[Callable[[Dict[str, Tensor]], Dict[str, Tensor]]] = None,
load_target: bool = True,
download: bool = False,
checksum: bool = False,
) -> None:
"""Initialize a new BigEarthNet dataset instance."""
super().__init__(
root, split, bands, num_classes, transforms, load_target, download, checksum
)
def __getitem__(self, index: int) -> Dict[str, Tensor]:
"""Return an index within the dataset.
Args:
index: index to return
Returns:
data and label at that index
"""
image = self._load_image(index)
if self.load_target:
label = self._load_target(index)
if self.transforms is not None:
image = self.transforms(image)
return (image, label)
def _load_image(self, index: int) -> Tensor:
"""Load a single image.
Args:
index: index to return
Returns:
the raster image or target
"""
paths = self._load_paths(index)
images = []
if len(paths) == 1:
with rasterio.open(paths[0]) as dataset:
arrays = dataset.read(
out_shape=self.image_size, out_dtype="int32"
).permute(1, 2, 0)
else:
for path in paths:
# Bands are of different spatial resolutions
# Resample to (120, 120)
with rasterio.open(path) as dataset:
array = dataset.read(
indexes=1,
out_shape=self.image_size,
out_dtype="int32",
resampling=Resampling.bilinear,
)
images.append(array)
arrays = np.stack(images, axis=-1)
return arrays
def _load_target(self, index: int) -> Tensor:
"""Load the target mask for a single image.
Args:
index: index to return
Returns:
the target label
"""
if self.bands == "s2":
folder = self.folders[index]["s2"]
else:
folder = self.folders[index]["s1"]
path = glob.glob(os.path.join(folder, "*.json"))[0]
with open(path) as f:
labels = json.load(f)["labels"]
# labels -> indices
indices = [self.class2idx[label] for label in labels]
# Map 43 to 19 class labels
if self.num_classes == 19:
indices_optional = [self.label_converter.get(idx) for idx in indices]
indices = [idx for idx in indices_optional if idx is not None]
target = np.zeros(self.num_classes, dtype=np.dtype("int8"))
target[indices] = 1
return target
def _load_folders(self) -> list[Dict[str, str]]:
"""Load folder paths.
Returns:
list of dicts of s1 and s2 folder paths
"""
filename = self.splits_metadata[self.split]["filename"]
if self.split == "train":
filename += self.splits_metadata["val"]["filename"]
dir_s1 = self.metadata["s1"]["directory"]
dir_s2 = self.metadata["s2"]["directory"]
with open(os.path.join(self.root, filename)) as f:
lines = f.read().strip().splitlines()
pairs = [line.split(",") for line in lines]
folders = [
{
"s1": os.path.join(self.root, dir_s1, pair[1]),
"s2": os.path.join(self.root, dir_s2, pair[0]),
}
for pair in pairs
]
return folders
# %%
ds = BigEarthNetNumpy(
os.path.join(DATA_DIR, BIGEARTHNET_DIR), bands="all", split="train"
)
# %%
for split in tqdm(["train"]):
ds = BigEarthNetNumpy(
os.path.join(DATA_DIR, BIGEARTHNET_DIR), bands="all", split=split
)
write_path = os.path.join(DATA_DIR, f"FFCV_new/BigEarthNet_{split}.beton")
writer = DatasetWriter(
write_path,
{
"image": NDArrayField(shape=(120, 120, 14), dtype=np.dtype("int32")),
"label": NDArrayField(shape=(19,), dtype=np.dtype("int8")),
},
num_workers=8,
)
writer.from_indexed_dataset(ds)
# %%