File size: 7,272 Bytes
1908732
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a71d19
1908732
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a71d19
1908732
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import torch
import rasterio
from PIL import Image, ImageDraw, ImageFont
import os
import numpy as np

class InMemoryDataset(torch.utils.data.Dataset):
    def __init__(self, data_list, preprocess_func):
        self.data_list = data_list
        self.preprocess_func = preprocess_func

    def __getitem__(self, i):
        return self.preprocess_func(self.data_list[i])

    def __len__(self):
        return len(self.data_list)

INPUT_SIZE = 224
PATCH_SIZE = 224
STRIDE = 224

root = 'datasets/WorldFloodsv2'
test_path_s2 = f'{root}/train/S2/'
test_path_labels = f'{root}/train/gt/'  
 

extension = '.tif'

timor_leste_events = {
    "EMSR507_AOI01_DEL_PRODUCT": "Pleiades-1A-1B",
    "EMSR507_AOI02_DEL_PRODUCT": "PlanetScope",
    "EMSR507_AOI03_DEL_PRODUCT": "PlanetScope",
    "EMSR507_AOI05_DEL_PRODUCT": "Sentinel-2",
    "EMSR507_AOI07_GRA_PRODUCT": "PlanetScope"
}

files_s2 = [(f"{test_path_s2}{event_id}{extension}", satellite) 
            for event_id, satellite in timor_leste_events.items()]

files_gt = [(f"{test_path_labels}{event_id}{extension}", satellite) 
            for event_id, satellite in timor_leste_events.items()]

 
output_root_s2 = "./datasets/Timor_Processed/S2"
os.makedirs(output_root_s2, exist_ok=True)

output_root_gt = "./datasets/Timor_Processed/GT"
os.makedirs(output_root_gt, exist_ok=True)

def sliding_window_crop(image, window_size=PATCH_SIZE, stride=STRIDE):
    C, H, W = image.shape
    patches = []
    for y in range(0, H, stride):
        for x in range(0, W, stride):
            y_end = min(y + window_size, H)
            x_end = min(x + window_size, W)
            y_start = max(y_end - window_size, 0)
            x_start = max(x_end - window_size, 0)
            patch = image[:, y_start:y_end, x_start:x_end]
            patches.append(patch)
    return patches


def read_tif_as_tensor(tif_path):
    with rasterio.open(tif_path) as src:
        img = src.read()  # shape: (bands, H, W)
        img = torch.from_numpy(img).float()
    return img


def save_patch_as_tif(patch_tensor, output_path):
    patch_np = patch_tensor.numpy()
    with rasterio.open(
        output_path,
        'w',
        driver='GTiff',
        height=patch_np.shape[1],
        width=patch_np.shape[2],
        count=patch_np.shape[0],
        dtype=patch_np.dtype
    ) as dst:
        dst.write(patch_np)


def plot_patches(patches, cols=5, save_path=None, is_label=False):
    rows = (len(patches) + cols - 1) // cols
    patch_images = []
    font = ImageFont.load_default()

    for idx, patch in enumerate(patches):
        if is_label:
            # Labels assumed to be single-channel
            patch_np = patch[0].numpy()
            patch_np = ((patch_np - patch_np.min()) / (patch_np.max() - patch_np.min() + 1e-8) * 255).astype(np.uint8)
            img = Image.fromarray(patch_np).convert("L")
        else:
            # RGB visualization for images
            patch_np = patch[:3].numpy()
            patch_np = (patch_np - patch_np.min()) / (patch_np.max() - patch_np.min() + 1e-8) * 255
            patch_np = patch_np.transpose(1,2,0).astype(np.uint8)
            img = Image.fromarray(patch_np)
        draw = ImageDraw.Draw(img)
        draw.text((5,5), str(idx), fill=(255,0,0), font=font)
        patch_images.append(img)

    width, height = patch_images[0].size
    grid_img = Image.new('RGB' if not is_label else 'L', (cols * width, rows * height), color=(255,255,255) if not is_label else 255)
    for i, img in enumerate(patch_images):
        row = i // cols
        col = i % cols
        grid_img.paste(img, (col*width, row*height))

    if save_path:
        grid_img.save(save_path)

# Class color map: 0=invalid, 1=land, 2=flood, 3=permanent water
CLASS_COLORS = {
    0: (0, 0, 0),       # black for invalid/no data
    1: (34, 139, 34),   # green for flood (gt)
    2: (0, 0, 255),     # blue for cloud (gt)
}

def plot_label_patches(label_patches, cols=5, save_path=None):
    rows = (len(label_patches) + cols - 1) // cols
    patch_images = []
    font = ImageFont.load_default()

    for idx, patch in enumerate(label_patches):
        patch_np = patch[0].numpy().astype(int)  # assume single channel
        H, W = patch_np.shape
        color_img = np.zeros((H, W, 3), dtype=np.uint8)
        for cls, color in CLASS_COLORS.items():
            color_img[patch_np == cls] = color
        img = Image.fromarray(color_img)
        draw = ImageDraw.Draw(img)
        draw.text((5,5), str(idx), fill=(255,0,0), font=font)
        patch_images.append(img)

    width, height = patch_images[0].size
    grid_img = Image.new('RGB', (cols * width, rows * height), color=(255,255,255))
    for i, img in enumerate(patch_images):
        row = i // cols
        col = i % cols
        grid_img.paste(img, (col*width, row*height))

    if save_path:
        grid_img.save(save_path)

 
for tif_path, satellite in files_s2:
    print(f"Processing {tif_path} ({satellite})...")
    img_tensor = read_tif_as_tensor(tif_path)
    patches = sliding_window_crop(img_tensor, PATCH_SIZE, STRIDE)

    base_name = os.path.splitext(os.path.basename(tif_path))[0]
    patch_output_dir = os.path.join(output_root_s2, base_name)
    os.makedirs(patch_output_dir, exist_ok=True)

    # Save image patches
    for idx, patch in enumerate(patches):
        patch_name = f"{base_name}_{idx}.tif"
        save_patch_as_tif(patch, os.path.join(patch_output_dir, patch_name))

    # # Plot image patches
    # plot_save_path = os.path.join(patch_output_dir, f"{base_name}_grid.png")
    # plot_patches(patches, save_path=plot_save_path)

    # # If labels exist in a corresponding folder
    # label_path = tif_path.replace('/S2/', '/gt/')  # assuming label folder structure
    # if os.path.exists(label_path):
    #     label_tensor = read_tif_as_tensor(label_path)
    #     label_patches = sliding_window_crop(label_tensor, PATCH_SIZE, STRIDE)
    #     plot_label_path = os.path.join(patch_output_dir, f"{base_name}_labels_grid.png")
    #     plot_label_patches(label_patches, save_path=plot_label_path)

 
for tif_path, satellite in files_gt:
    print(f"Processing {tif_path} ({satellite})...")
    img_tensor = read_tif_as_tensor(tif_path)
    patches = sliding_window_crop(img_tensor, PATCH_SIZE, STRIDE)

    base_name = os.path.splitext(os.path.basename(tif_path))[0]
    patch_output_dir = os.path.join(output_root_gt, base_name)
    os.makedirs(patch_output_dir, exist_ok=True)

    # Save image patches
    for idx, patch in enumerate(patches):
        patch_name = f"{base_name}_{idx}.tif"
        save_patch_as_tif(patch, os.path.join(patch_output_dir, patch_name))

    # # Plot image patches
    # plot_save_path = os.path.join(patch_output_dir, f"{base_name}_grid.png")
    # plot_patches(patches, save_path=plot_save_path)

    # # If labels exist in a corresponding folder
    # label_path = tif_path.replace('/S2/', '/gt/')  # assuming label folder structure
    # if os.path.exists(label_path):
    #     label_tensor = read_tif_as_tensor(label_path)
    #     label_patches = sliding_window_crop(label_tensor, PATCH_SIZE, STRIDE)
    #     plot_label_path = os.path.join(patch_output_dir, f"{base_name}_labels_grid.png")
    #     plot_label_patches(label_patches, save_path=plot_label_path)