-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitSegFormer.py
62 lines (49 loc) · 1.87 KB
/
splitSegFormer.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
import os
import random
from shutil import move
import glob
from os.path import join, isfile, splitext
BASE_PATH = "/home/ubuntu/work/pdfBLines/dataset"
train_path = f"{BASE_PATH}/train/"
val_path = f"{BASE_PATH}/valid/"
test_path = f"{BASE_PATH}/test/"
os.makedirs(os.path.dirname(val_path), exist_ok=True)
os.makedirs(os.path.dirname(test_path), exist_ok=True)
train_images = os.listdir(train_path)
train_images = list(filter(lambda x: "jpg" in x, train_images))
split_size = int(0.1 * len(train_images)) # % of the data for validation
val_test_images = random.sample(train_images, split_size)
val_images = val_test_images[:split_size//2]
test_images = val_test_images[split_size//2:]
################################
# Match masks with corresponding images
val_masks = []
for image in val_images:
# Extract mask base name
mask_base_name = image.split(".")[0]
# Loop through validation images to find match
val_masks.append(f"{mask_base_name}_mask.png")
val_masks.sort()
val_images.sort()
for image, mask in zip(val_images, val_masks):
mask_base_name = image.split(".")[0]
assert mask_base_name in mask
move(os.path.join(train_path, image), os.path.join(val_path, image))
move(os.path.join(train_path, mask), os.path.join(val_path, mask))
# break
################################
# Match masks with corresponding images
test_masks = []
for image in test_images:
# Extract mask base name
mask_base_name = image.split(".")[0]
# Loop through testidation images to find match
test_masks.append(f"{mask_base_name}_mask.png")
test_masks.sort()
test_images.sort()
for image, mask in zip(test_images, test_masks):
mask_base_name = image.split(".")[0]
assert mask_base_name in mask
move(os.path.join(train_path, image), os.path.join(test_path, image))
move(os.path.join(train_path, mask), os.path.join(test_path, mask))
# break