File size: 8,279 Bytes
0e5ae7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a0bcb8
 
0e5ae7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a0bcb8
 
0e5ae7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import click
import json
from PIL import Image
from tqdm import tqdm


def resize_image(image: Image, size: int):
    # resize the image so that the shorter side is `size` pixels
    w, h = image.size

    if w < h:
        new_w = size
        new_h = int(size * h / w)
    else:
        new_w = int(size * w / h)
        new_h = size

    return image.resize((new_w, new_h))


SUPPORTED_WEATHERS = ["clear", "rainy", "foggy", "snowy"]
SUPPORTED_TIMESOFDAY = ["daytime", "night"]
WEATHER_MAPPING = {
    "clear": "clear",
    "rainy": "rain",
    "foggy": "fog",
    "snowy": "snow",
}


@click.command()
@click.option("--bdd100k_dir", type=str, default="datasets/bdd100k")
@click.option("--acdc_dir", type=str, default="datasets/acdc")
@click.option("--output_dir", type=str, default="datasets/swim_data")
def build_swim_dataset(bdd100k_dir: str, acdc_dir: str, output_dir: str):
    # build the dataset with format
    # swim_data
    # β”œβ”€β”€ train
    # β”‚   β”œβ”€β”€ images
    # β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”œβ”€β”€ ...
    # β”‚   β”œβ”€β”€ labels.json
    # β”œβ”€β”€ val
    # β”‚   β”œβ”€β”€ images
    # β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”œβ”€β”€ ...
    # β”‚   β”œβ”€β”€ labels.json
    #
    # labels.json
    # [
    #     {
    #         "name": "000000.jpg",
    #         "weather": "clear"|"rain"|"fog"|"snow",
    #         "timeofday": "daytime"|"night",
    #         "source": "bdd100k"|"acdc",
    #     }
    # ]
    #
    # note:
    # - all images are resized so that the shorter side is 512 pixels
    # - train: bdd100k train + acdc train + acdc val
    # - val: bdd100k val + acdc test
    #
    # bdd100k format:
    # bdd100k
    # β”œβ”€β”€ images
    # β”‚   β”œβ”€β”€ 100k
    # β”‚   β”‚   β”œβ”€β”€ train
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # β”‚   β”‚   β”œβ”€β”€ val
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # β”œβ”€β”€ labels
    # β”‚   β”œβ”€β”€ det_20
    # β”‚   β”‚   β”œβ”€β”€ det_train.json
    # β”‚   β”‚   β”œβ”€β”€ det_val.json
    #
    # acdc format:
    # acdc
    # β”œβ”€β”€ fog
    # β”‚   β”œβ”€β”€ test
    # β”‚   β”‚   β”œβ”€β”€ <subfolder>
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # β”‚   β”œβ”€β”€ test_ref (clear images)
    # β”‚   β”‚   β”œβ”€β”€ <subfolder>
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # |   β”œβ”€β”€ train
    # β”‚   β”‚   β”œβ”€β”€ <subfolder>
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # |   β”œβ”€β”€ train_ref (clear images)
    # β”‚   β”‚   β”œβ”€β”€ <subfolder>
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # |   β”œβ”€β”€ val
    # β”‚   β”‚   β”œβ”€β”€ <subfolder>
    # β”‚   β”‚   β”‚   β”œβ”€β”€ 000000.jpg
    # β”‚   β”‚   β”‚   β”œβ”€β”€ ...
    # |   β”œβ”€β”€ val_ref (clear images)
    # β”‚   β”‚   β”œβ”€β”€ <subfolder>
    # β”œβ”€β”€ rain
    # β”œβ”€β”€ snow
    # β”œβ”€β”€ night
    os.makedirs(output_dir, exist_ok=True)
    os.makedirs(os.path.join(output_dir, "train", "images"), exist_ok=True)
    os.makedirs(os.path.join(output_dir, "val", "images"), exist_ok=True)

    count = 0

    # build train dataset
    labels = []

    # bdd100k train
    with open(os.path.join(bdd100k_dir, "labels", "det_20", "det_train.json")) as f:
        bdd100k_train_labels = json.load(f)

    for label in tqdm(bdd100k_train_labels, desc="bdd100k train"):
        if label["attributes"]["weather"] not in SUPPORTED_WEATHERS:
            continue
        if label["attributes"]["timeofday"] not in SUPPORTED_TIMESOFDAY:
            continue

        new_name = f"{count:06d}.jpg"
        count += 1

        image = Image.open(
            os.path.join(bdd100k_dir, "images", "100k", "train", label["name"])
        )
        image = resize_image(image, 512)
        image.save(os.path.join(output_dir, "train", "images", new_name))

        labels.append(
            {
                "name": new_name,
                "weather": WEATHER_MAPPING[label["attributes"]["weather"]],
                "timeofday": label["attributes"]["timeofday"],
                "source": "bdd100k",
            }
        )

    # acdc train + val
    for weather in ["fog", "rain", "snow", "night"]:
        for split in ["train", "val", "train_ref", "val_ref"]:
            for subfolder in os.listdir(os.path.join(acdc_dir, weather, split)):
                for filename in os.listdir(
                    os.path.join(acdc_dir, weather, split, subfolder)
                ):
                    new_name = f"{count:06d}.jpg"
                    count += 1

                    image = Image.open(
                        os.path.join(acdc_dir, weather, split, subfolder, filename)
                    )
                    image = resize_image(image, 512)
                    image.save(os.path.join(output_dir, "train", "images", new_name))

                    if split.endswith("ref"):
                        timeofday_ = "daytime"
                        weather_ = "clear"
                    else:
                        timeofday_ = "night" if weather == "night" else "daytime"
                        weather_ = "clear" if weather == "night" else weather

                    labels.append(
                        {
                            "name": new_name,
                            "weather": weather_,
                            "timeofday": timeofday_,
                            "source": "acdc",
                        }
                    )

    with open(os.path.join(output_dir, "train", "labels.json"), "w") as f:
        json.dump(labels, f, indent=4)

    # build val dataset
    labels = []

    # bdd100k val
    with open(os.path.join(bdd100k_dir, "labels", "det_20", "det_val.json")) as f:
        bdd100k_val_labels = json.load(f)

    for label in tqdm(bdd100k_val_labels, desc="bdd100k val"):
        if label["attributes"]["weather"] not in SUPPORTED_WEATHERS:
            continue
        if label["attributes"]["timeofday"] not in SUPPORTED_TIMESOFDAY:
            continue

        new_name = f"{count:06d}.jpg"
        count += 1

        image = Image.open(
            os.path.join(bdd100k_dir, "images", "100k", "val", label["name"])
        )
        image = resize_image(image, 512)
        image.save(os.path.join(output_dir, "val", "images", new_name))

        labels.append(
            {
                "name": new_name,
                "weather": WEATHER_MAPPING[label["attributes"]["weather"]],
                "timeofday": label["attributes"]["timeofday"],
                "source": "bdd100k",
            }
        )

    # acdc test
    for weather in ["fog", "rain", "snow", "night"]:
        for split in ["test", "test_ref"]:
            for subfolder in os.listdir(os.path.join(acdc_dir, weather, split)):
                for filename in os.listdir(
                    os.path.join(acdc_dir, weather, split, subfolder)
                ):
                    new_name = f"{count:06d}.jpg"
                    count += 1

                    image = Image.open(
                        os.path.join(acdc_dir, weather, split, subfolder, filename)
                    )
                    image = resize_image(image, 512)
                    image.save(os.path.join(output_dir, "val", "images", new_name))

                    if split.endswith("ref"):
                        timeofday_ = "daytime"
                        weather_ = "clear"
                    else:
                        timeofday_ = "night" if weather == "night" else "daytime"
                        weather_ = "clear" if weather == "night" else weather

                    labels.append(
                        {
                            "name": new_name,
                            "weather": weather_,
                            "timeofday": timeofday_,
                            "source": "acdc",
                        }
                    )

    with open(os.path.join(output_dir, "val", "labels.json"), "w") as f:
        json.dump(labels, f, indent=4)


if __name__ == "__main__":
    build_swim_dataset()