forked from microsoft/torchgeo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_stacked_bigearthnet.py
162 lines (144 loc) · 4.56 KB
/
create_stacked_bigearthnet.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
"""."""
# %%
import os
from typing import Any, Callable, Dict, Optional
import numpy as np
import rasterio
import torch
from rasterio.enums import Resampling
from torch import Tensor
from torchvision.transforms import Compose
from tqdm import tqdm
from torchgeo.datamodules import BigEarthNetDataModule
from torchgeo.datasets import BigEarthNet
# %%
path_to_stacked = "/scratch/users/mike/data/BigEarthNetStacked"
# %%
class BigEarthNetStacked(BigEarthNet):
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:
super().__init__(
root, split, bands, num_classes, transforms, load_target, download, checksum
)
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:
array = dataset.read(out_shape=self.image_size, out_dtype="int32")
return torch.from_numpy(array)
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,
)
if dataset.profile["height"] == 120:
profile = dataset.profile
images.append(array)
arrays = np.stack(images, axis=0)
profile["dtype"] = "int32"
profile["count"] = 14
dir, filename = path.split("/")[-2:]
dir_path = os.path.join(path_to_stacked, "BigEarthNet-v1.0", dir)
file_path = os.path.join(dir_path, filename)
try:
os.makedirs(dir_path)
except OSError:
pass
else:
with rasterio.open(file_path, "w", **profile) as dataset:
dataset.write(arrays)
tensor = torch.from_numpy(arrays)
return tensor
# %%
class BigEarthNetStackedDataModule(BigEarthNetDataModule):
def __init__(
self,
root_dir: str,
bands: str = "all",
num_classes: int = 19,
batch_size: int = 64,
num_workers: int = 0,
pin_memory: bool = False,
prefetch_factor: int = 10,
persistent_workers: bool = False,
load_target: bool = True,
**kwargs: Any,
) -> None:
super().__init__(
root_dir,
bands,
num_classes,
batch_size,
num_workers,
pin_memory,
prefetch_factor,
persistent_workers,
load_target,
**kwargs,
)
def setup(self, stage: Optional[str] = None) -> None:
"""Initialize the main ``Dataset`` objects.
This method is called once per GPU per run.
"""
transforms = Compose([self.preprocess])
self.train_dataset = BigEarthNetStacked(
self.root_dir,
split="train",
bands=self.bands,
num_classes=self.num_classes,
transforms=transforms,
load_target=self.load_target,
)
self.val_dataset = BigEarthNetStacked(
self.root_dir,
split="val",
bands=self.bands,
num_classes=self.num_classes,
transforms=transforms,
load_target=self.load_target,
)
self.test_dataset = BigEarthNetStacked(
self.root_dir,
split="test",
bands=self.bands,
num_classes=self.num_classes,
transforms=transforms,
load_target=self.load_target,
)
# %%
os.makedirs(path_to_stacked, exist_ok=True)
os.makedirs(os.path.join(path_to_stacked, "BigEarthNet-S1-v1.0"), exist_ok=True)
os.makedirs(os.path.join(path_to_stacked, "BigEarthNet-v1.0"), exist_ok=True)
# %%
dm = BigEarthNetStackedDataModule(
"/scratch/users/mike/data/BigEarthNet", num_workers=32
)
dm.setup()
# %%
for _ in tqdm(dm.train_dataloader()):
pass
for _ in tqdm(dm.val_dataloader()):
pass
for _ in tqdm(dm.test_dataloader()):
pass