-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.py
381 lines (320 loc) · 11.3 KB
/
API.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
A class to organize YOLO-structured data
Methods
---
get_images
list of absolute paths of images in root/<split>/images
clone
copy root/train and root/test to root/<folder_name>
shuffle_train_val
shuffle self.ls_train_images and assign to
save_yaml
save_txt
Folder structure
---
root/
train/ (required)
images/
img_1_13_jpg.rf.d69528304f2d10b633c6d94982185cb2.jpg
img_2_13_jpg.rf.d69528304f2d10b633c6d94982185cb2.jpg
...
labels/
img_1_13_jpg.rf.d69528304f2d10b633c6d94982185cb2.txt
img_2_13_jpg.rf.d69528304f2d10b633c6d94982185cb2.txt
...
test/ (required)
images/
img_3.jpg
img_4.jpg
...
labels/
img_3.txt
img_4.txt
test.txt (generated)
train.txt (generated)
val.txt (generated)
data.yaml (generated)
Example YAML
---
path: /home/niche/cowsformer/data/cow200/yolov5/run3
train: "train.txt"
val: "val.txt"
test: "test.txt"
names:
0: none
1: cow
Example train.txt
---
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_32_jpg
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_1_jpg
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_1_26_jpg
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_1_62_jpg
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_1_10_jpg
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_3_11_jpg
/home/niche/cowsformer/data/cow200/yolov5/run0/images/img_4_3_jpg
"""
import os
import shutil
import random
import PIL
import torch
import glob
import numpy as np
import supervision as sv
# local imports
from bbox import xywh2xyxy
class YOLO_API:
def __init__(
self,
root: str,
):
self.root = root
self.ls_train_images_all = self.get_images("train")
self.ls_train_images = None
self.ls_val_iamges = None
self.ls_test_images = self.get_images("test")
self.save_txt("test")
def get_PIL(self, split, idx):
"""
get PIL image from split and idx
params
------
split: str
"train" or "test"
idx: int
index of the image
return
------
PIL.Image
"""
if split == "train":
path = self.ls_train_images_all[idx]
else:
path = self.ls_test_images[idx]
return PIL.Image.open(path)
def get_images(self, split):
"""
search images names (.jpg) in root/<split>/images
params
------
split: str
"train" or "test"
return
------
a list of aboslute paths of images
"""
return self.get_filepaths(split, "images")
def get_labels(self, split):
"""
search label names (.txt) in root/<split>/labels
params
------
split: str
"train" or "test"
return
------
a list of aboslute paths of labels
"""
return self.get_filepaths(split, "labels")
def get_labels_pred(self, split):
"""
Dynamically find the directory name ending with 'labelsPred' and fetch label files from it.
Parameters
----------
split : str
"train" or "test"
Returns
-------
list
A list of absolute paths of labels
"""
base_path = os.path.join(self.root, split)
labels_pred_dir_pattern = os.path.join(base_path, "*labelsPred")
labels_pred_dirs = glob.glob(labels_pred_dir_pattern)
# Assuming there's only one such directory
if labels_pred_dirs:
# Extract the dynamic folder name part that matches the criteria
dynamic_folder_name = os.path.basename(labels_pred_dirs[0])
# Now pass this dynamic folder name to self.get_filepaths
return self.get_filepaths(split, dynamic_folder_name)
else:
return [] # Return an empty list if no matching directory is found
def get_filepaths(self, split, folder):
"""
get file paths of images or labels
params
---
split: str
"train" or "test"
folder: str
"images" or "labels"
"""
path_files = os.path.join(self.root, split, folder)
ext = ".jpg" if folder == "images" else ".txt"
ls_files = [f for f in os.listdir(path_files) if f.endswith(ext)]
ls_files = [os.path.join(path_files, f) for f in ls_files]
return sorted(ls_files)
def get_gt_pred_detections(self, gt_img_path,gt_label_path,pred_label_path):
gt_detections = []
gt_labels = [f for f in os.listdir(gt_label_path) if f.endswith(".txt")]
gt_labels = sorted([os.path.join(gt_label_path, f) for f in gt_labels])
gt_images = self.get_images(gt_img_path)
n_samples_gt = len(gt_images)
pred_detections = []
pred_labels = [f for f in os.listdir(pred_label_path) if f.endswith(".txt")]
pred_labels = sorted([os.path.join(pred_label_path, f) for f in pred_labels])
n_samples_pred = len(pred_labels)
if n_samples_gt != n_samples_pred:
print('gt_images and pred images are not same')
else:
pass
for i in range(n_samples_gt):
# get image info
image = PIL.Image.open(gt_images[i])
img_w, img_h = image.size
# get annotation
gt_label = gt_labels[i]
pred_label = pred_labels[i]
with open(gt_label, "r") as f:
gt_lines = f.readlines()
gt_lines = [i.strip() for i in gt_lines]
with open(pred_label, "r") as f:
pred_lines = f.readlines()
pred_lines = [i.strip() for i in pred_lines]
if not gt_lines:
#print('found empty GT')
continue
if gt_lines and not pred_lines:
#print('case found: empty pred')
pred_lines = ["0 0.000001 0.000002 0.000003 0.000004 0.0000001"]
### get gt detections
gt_ls_xyxy = []
gt_ls_cls = []
gt_ls_conf = []
for i in gt_lines:
gt_parts = i.split(" ")
gt_class_id = int(gt_parts[0])
gt_coords = tuple(
map(float, gt_parts[1:5])
) # x_center, y_center, width, height
#conf = float(parts[5]) if path_preds else None
gt_xyxy = xywh2xyxy(
gt_coords,
img_size=(img_w, img_h),
)
# append to lists
gt_ls_xyxy.append(gt_xyxy)
gt_ls_cls.append(gt_class_id)
# create sv.Detections
gt_ls_xyxy = torch.stack(gt_ls_xyxy).numpy()
gt_ls_cls = np.array(gt_ls_cls)
gt_ls_conf = np.array(gt_ls_conf)
gt_detection = sv.Detections(
gt_ls_xyxy,
class_id=gt_ls_cls,
confidence= None,
)
gt_detections.append(gt_detection)
### get pred detections
pred_ls_xyxy = []
pred_ls_cls = []
pred_ls_conf = []
for i in pred_lines:
pred_parts = i.split(" ")
#print(parts)
pred_class_id = int(pred_parts[0])
pred_coords = tuple(
map(float, pred_parts[1:5])
) # x_center, y_center, width, height
pred_conf = float(pred_parts[5])
pred_coords = tuple(value for value in pred_coords)
pred_xyxy = torch.tensor(pred_coords)
# append to lists
pred_ls_xyxy.append(pred_xyxy)
pred_ls_cls.append(pred_class_id)
pred_ls_conf.append(pred_conf)
# create sv.Detections
pred_ls_xyxy = torch.stack(pred_ls_xyxy).numpy()
pred_ls_cls = np.array(pred_ls_cls)
pred_ls_conf_array = np.array(pred_ls_conf)
pred_detection = sv.Detections(
pred_ls_xyxy,
class_id=pred_ls_cls,
confidence=pred_ls_conf_array,
)
pred_detections.append(pred_detection)
return gt_detections,pred_detections
def clone(self, folder_name):
"""
copy root/train and root/test to
root/<folder_name>/train and root/<folder_name>/test
"""
path_train = os.path.join(self.root, "train")
path_test = os.path.join(self.root, "test")
path_folder = os.path.join(self.root, folder_name)
if os.path.exists(path_folder):
shutil.rmtree(path_folder)
os.mkdir(path_folder)
shutil.copytree(path_train, os.path.join(path_folder, "train"))
shutil.copytree(path_test, os.path.join(path_folder, "test"))
# copy yaml and other txt
shutil.copy(os.path.join(self.root, "data.yaml"), path_folder)
shutil.copy(os.path.join(self.root, "train.txt"), path_folder)
shutil.copy(os.path.join(self.root, "test.txt"), path_folder)
def shuffle_train_val(self, n=None, k=5):
"""
shuffle self.ls_train_images and assign to
self.ls_train_images and self.ls_val_images
params
------
n: None or int or float
None: use all images
int: number of images to be included in the train/val set
float: ratio of images to be included in the train/val set
k: int
how many folds to split the train/val set
"""
# determine n
total_n = len(self.ls_train_images_all)
if n is None:
n = total_n
elif isinstance(n, float):
n = int(n * total_n)
n_val = int(n / k)
# shuffle training images
random.shuffle(self.ls_train_images_all)
train_images = self.ls_train_images_all[:n]
# split train/val
self.ls_train_images = train_images[:-n_val]
self.ls_val_images = train_images[-n_val:]
self.save_txt("train")
self.save_txt("val")
def save_yaml(self, classes, name="data.yaml"):
"""
make data.yaml in root
params
------
classes: list
e.g., ["cow", "none"]
name: str
name of the yaml file
"""
path_yaml = os.path.join(self.root, name)
with open(path_yaml, "w") as f:
f.write(f"path: {self.root}\n")
f.write(f'train: "train.txt"\n')
f.write(f'val: "val.txt"\n')
f.write(f'test: "test.txt"\n')
f.write("names:\n")
for i, c in enumerate(classes):
f.write(f" {i}: {c}\n")
def save_txt(self, split):
"""
save <split>.txt in root
"""
path_txt = os.path.join(self.root, f"{split}.txt")
with open(path_txt, "w") as f:
for img in getattr(self, f"ls_{split}_images"):
f.write(img + "\n")
def path_yaml(self):
return os.path.join(self.root, "data.yaml")