-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-preprocess.py
131 lines (106 loc) · 3.9 KB
/
data-preprocess.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
import argparse
import json
import os
import random
import shutil
import cv2
import valohai
from helpers import get_run_identification, unpack_dataset
random.seed(10)
def save_dataset(out_path, subset="train"):
print(f"Saving metadata for {subset} subset")
out_path = out_path[:-1]
project_name, exec_id = get_run_identification()
metadata = {
"valohai.dataset-versions": [
{
"uri": f"dataset://drift-demo-ships-aerial/{project_name}_{subset}_{exec_id}",
"targeting_aliases": [f"dev_{subset}"],
"valohai.tags": ["dev", "ships-aerial"],
},
],
}
# The metadata must be saved for each file
for folder in os.listdir(out_path + "/" + subset):
for file in os.listdir(out_path + "/" + subset + "/" + folder):
metadata_path = os.path.join(
out_path,
subset,
folder,
f"{file}.metadata.json",
)
with open(metadata_path, "w") as outfile:
json.dump(metadata, outfile)
def prepare_data(data_path, save_path, train_size, valid_size, test_size, image_size):
sets = {"train": train_size, "valid": valid_size, "test": test_size}
data_path = os.path.join(data_path, os.listdir(data_path)[0])
for subset, size in sets.items():
print(f"Preparing {size} sample for {subset} subset")
img_files_path = os.path.join(data_path, f"{subset}/images")
lbl_files_path = os.path.join(data_path, f"{subset}/labels")
img_destination_path = valohai.outputs(f"prep_dataset/{subset}/images")
lbl_destination_path = valohai.outputs(f"prep_dataset/{subset}/labels")
image_files = os.listdir(img_files_path)
random_images = random.sample(image_files, size)
# Moving the images and labels to Valohai outputs, so they uploaded and versioned in Valohai
for i, image_file in enumerate(random_images):
image_path = os.path.join(img_files_path, image_file)
image = cv2.imread(image_path)
image = preprocess(image, image_size)
cv2.imwrite(img_destination_path.path(image_file), image)
shutil.move(
lbl_files_path + f"/{image_file[:-3]}txt",
lbl_destination_path.path("."),
)
# Save as Valohai dataset
save_dataset(save_path, subset)
def preprocess(img, img_size):
img = cv2.resize(img, (img_size, img_size))
# Possible preprocess - We proceed without since ultralytics manages that under the hood.
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# img = img / 255.
return img
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Dataset parameters for ship aerial images",
)
parser.add_argument(
"--train_size",
type=int,
default=20,
help="Number of samples in the training set",
)
parser.add_argument(
"--valid_size",
type=int,
default=5,
help="Number of samples in the validation set",
)
parser.add_argument(
"--test_size",
type=int,
default=5,
help="Number of samples in the test set",
)
parser.add_argument(
"--image_size",
type=int,
default=768,
help="Size of the images",
)
args = parser.parse_args()
dataset_packed = valohai.inputs("dataset").path(process_archives=False)
print("Got packed dataset ", dataset_packed)
output_path = valohai.outputs("prep_dataset").path(".")
print("Unpacking... ")
unpacked_dataset_path = unpack_dataset(dataset_packed, "unpacked_dataset")
print("Unpacked to ", unpacked_dataset_path)
prepare_data(
unpacked_dataset_path,
output_path,
args.train_size,
args.valid_size,
args.test_size,
args.image_size,
)
print("Dataset is prepared!")