forked from hukaixuan19970627/yolov5_obb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
1199 lines (1023 loc) · 54.4 KB
/
datasets.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import glob
import math
import os
import random
import shutil
import time
from pathlib import Path
from threading import Thread
import cv2
import numpy as np
import torch
from PIL import Image, ExifTags
from torch.utils.data import Dataset
from tqdm import tqdm
from utils.general import xyxy2xywh, xywh2xyxy, torch_distributed_zero_first, cvminAreaRect2longsideformat, longsideformat2cvminAreaRect
help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.dng']
vid_formats = ['.mov', '.avi', '.mp4', '.mpg', '.mpeg', '.m4v', '.wmv', '.mkv']
# Get orientation exif tag
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
def get_hash(files):
# Returns a single hash value of a list of files
return sum(os.path.getsize(f) for f in files if os.path.isfile(f))
def exif_size(img):
# Returns exif-corrected PIL size
s = img.size # (width, height)
try:
rotation = dict(img._getexif().items())[orientation]
if rotation == 6: # rotation 270
s = (s[1], s[0])
elif rotation == 8: # rotation 90
s = (s[1], s[0])
except:
pass
return s
def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False,
rank=-1, world_size=1, workers=8):
'''
确保只有DDP中的第一个进程首先处理数据集,然后其他进程可以使用缓存。
Make sure only the first process in DDP process the dataset first, and the following others can use the cache.
return:
dataloader : 数据加载器,结合了数据集和取样器
i: batch_index, 第i个batch (索引方式) 以下为具体数据加载器中的内容
imgs : torch.Size([batch_size, 3, resized_noheight, resized_width])
targets : torch.Size = (该batch中的目标数量, [该image属于该batch的第几个图片, class, 经归一化后的xywh])
paths : List['img1_path','img2_path',......,'img-1_path'] len(paths)=batch_size
shapes : size= batch_size, 不进行mosaic时进行矩形训练时才有值
Class dataset 其中有:
self.img_files 路径文件夹下所有图片路径 self.img_files=['??\\images\\train2017\\1.jpg',...,]
self.label_files 路径文件夹下所有label_txt路径 self.label_files=['??\\labels\\train2017\\1.txt',...,]
self.n 路径文件夹下所有图片的总数量
self.batch , self.img_size , self.augment , self.hyp , self.image_weights , self.rect , self.mosaic , self.mosaic_border , self.stride ,
self.shapes [[1.jpg的形状]...[n.jpg的形状]] eg:[[480 80][360 640]...[480 640]]
self.labels [array( [对应1.txt的labels信息] ,dtype=float32), ..., array( [对应n.txt的labels信息] ,dtype=float32)]
'''
with torch_distributed_zero_first(rank):
dataset = LoadImagesAndLabels(path, imgsz, batch_size,
augment=augment, # augment images
hyp=hyp, # augmentation hyperparameters
rect=rect, # rectangular training
cache_images=cache,
single_cls=opt.single_cls,
stride=int(stride),
pad=pad,
rank=rank)
batch_size = min(batch_size, len(dataset))
nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers
sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None
dataloader = InfiniteDataLoader(dataset,
# 从数据库中每次抽出batch size个样本
batch_size=batch_size,
num_workers=nw,
sampler=sampler,
pin_memory=True,
collate_fn=LoadImagesAndLabels.collate_fn) # torch.utils.data.DataLoader()
return dataloader, dataset
class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
"""
Dataloader that reuses workers.
Uses same syntax as vanilla DataLoader.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler))
self.iterator = super().__iter__()
def __len__(self):
return len(self.batch_sampler.sampler)
def __iter__(self):
for i in range(len(self)):
yield next(self.iterator)
class _RepeatSampler(object):
""" Sampler that repeats forever.
Args:
sampler (Sampler)
"""
def __init__(self, sampler):
self.sampler = sampler
def __iter__(self):
while True:
yield from iter(self.sampler)
class LoadImages: # for inference
'''
for inference. LoadImages(path, img_size=640)
'''
def __init__(self, path, img_size=640):
p = str(Path(path)) # os-agnostic
p = os.path.abspath(p) # absolute path
if '*' in p:
files = sorted(glob.glob(p, recursive=True)) # glob
elif os.path.isdir(p):
files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
elif os.path.isfile(p):
files = [p] # files
else:
raise Exception('ERROR: %s does not exist' % p)
images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats]
videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats]
ni, nv = len(images), len(videos)
self.img_size = img_size
self.files = images + videos
self.nf = ni + nv # number of files
self.video_flag = [False] * ni + [True] * nv
self.mode = 'images'
if any(videos):
self.new_video(videos[0]) # new video
else:
self.cap = None
assert self.nf > 0, 'No images or videos found in %s. Supported formats are:\nimages: %s\nvideos: %s' % \
(p, img_formats, vid_formats)
def __iter__(self):
self.count = 0
return self
def __next__(self):
'''
return path, img, img0, self.cap
返回路径,resize+pad的图片,原始图片,视频对象
'''
if self.count == self.nf:
raise StopIteration
path = self.files[self.count]
if self.video_flag[self.count]:
# Read video
self.mode = 'video'
ret_val, img0 = self.cap.read()
if not ret_val:
self.count += 1
self.cap.release()
if self.count == self.nf: # last video
raise StopIteration
else:
path = self.files[self.count]
self.new_video(path)
ret_val, img0 = self.cap.read()
self.frame += 1
print('video %g/%g (%g/%g) %s: ' % (self.count + 1, self.nf, self.frame, self.nframes, path), end='')
else:
# Read image
self.count += 1
img0 = cv2.imread(path) # BGR
assert img0 is not None, 'Image Not Found ' + path
print('image %g/%g %s: ' % (self.count, self.nf, path), end='')
# Padded resize
img = letterbox(img0, new_shape=self.img_size)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
# cv2.imwrite(path + '.letterbox.jpg', 255 * img.transpose((1, 2, 0))[:, :, ::-1]) # save letterbox image
# 返回路径,resize+pad的图片,原始图片,视频对象
return path, img, img0, self.cap
def new_video(self, path):
self.frame = 0
self.cap = cv2.VideoCapture(path)
self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
def __len__(self):
return self.nf # number of files
class LoadWebcam: # for inference
def __init__(self, pipe=0, img_size=640):
self.img_size = img_size
if pipe == '0':
pipe = 0 # local camera
# pipe = 'rtsp://192.168.1.64/1' # IP camera
# pipe = 'rtsp://username:[email protected]/1' # IP camera with login
# pipe = 'rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa' # IP traffic camera
# pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera
# https://answers.opencv.org/question/215996/changing-gstreamer-pipeline-to-opencv-in-pythonsolved/
# pipe = '"rtspsrc location="rtsp://username:[email protected]/1" latency=10 ! appsink' # GStreamer
# https://answers.opencv.org/question/200787/video-acceleration-gstremer-pipeline-in-videocapture/
# https://stackoverflow.com/questions/54095699/install-gstreamer-support-for-opencv-python-package # install help
# pipe = "rtspsrc location=rtsp://root:[email protected]:554/axis-media/media.amp?videocodec=h264&resolution=3840x2160 protocols=GST_RTSP_LOWER_TRANS_TCP ! rtph264depay ! queue ! vaapih264dec ! videoconvert ! appsink" # GStreamer
self.pipe = pipe
self.cap = cv2.VideoCapture(pipe) # video capture object
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
if cv2.waitKey(1) == ord('q'): # q to quit
self.cap.release()
cv2.destroyAllWindows()
raise StopIteration
# Read frame
if self.pipe == 0: # local camera
ret_val, img0 = self.cap.read()
img0 = cv2.flip(img0, 1) # flip left-right
else: # IP camera
n = 0
while True:
n += 1
self.cap.grab()
if n % 30 == 0: # skip frames
ret_val, img0 = self.cap.retrieve()
if ret_val:
break
# Print
assert ret_val, 'Camera Error %s' % self.pipe
img_path = 'webcam.jpg'
print('webcam %g: ' % self.count, end='')
# Padded resize
img = letterbox(img0, new_shape=self.img_size)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
return img_path, img, img0, None
def __len__(self):
return 0
class LoadStreams: # multiple IP or RTSP cameras
def __init__(self, sources='streams.txt', img_size=640):
self.mode = 'images'
self.img_size = img_size
if os.path.isfile(sources):
with open(sources, 'r') as f:
sources = [x.strip() for x in f.read().splitlines() if len(x.strip())]
else:
sources = [sources]
n = len(sources)
self.imgs = [None] * n
self.sources = sources
for i, s in enumerate(sources):
# Start the thread to read frames from the video stream
print('%g/%g: %s... ' % (i + 1, n, s), end='')
cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
assert cap.isOpened(), 'Failed to open %s' % s
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
_, self.imgs[i] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
print(' success (%gx%g at %.2f FPS).' % (w, h, fps))
thread.start()
print('') # newline
# check for common shapes
s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
if not self.rect:
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
def update(self, index, cap):
# Read next stream frame in a daemon thread
n = 0
while cap.isOpened():
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n == 4: # read every 4th frame
_, self.imgs[index] = cap.retrieve()
n = 0
time.sleep(0.01) # wait time
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
img0 = self.imgs.copy()
if cv2.waitKey(1) == ord('q'): # q to quit
cv2.destroyAllWindows()
raise StopIteration
# Letterbox
img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0]
# Stack
img = np.stack(img, 0)
# Convert
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
img = np.ascontiguousarray(img)
return self.sources, img, img0, None
def __len__(self):
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
def rotate_augment(angle, scale, image, labels):
"""
旋转目标增强 随机旋转
@param angle: 旋转增强角度 int 单位为度
@param scale: 设为1,尺度由train.py中定义
@param image: img信息 shape(heght, width, 3)
@param labels: (num, [classid x_c y_c longside shortside Θ]) Θ ∈ int[0,180)
@return:
array rotated_img: augmented_img信息 shape(heght, width, 3)
array rotated_labels: augmented_label: (num, [classid x_c y_c longside shortside Θ])
"""
Pi_angle = -angle * math.pi / 180.0 # 弧度制,后面旋转坐标需要用到,注意负号!!!
rows, cols = image.shape[:2]
a, b = cols / 2, rows / 2
M = cv2.getRotationMatrix2D(center=(a, b), angle=angle, scale=scale)
rotated_img = cv2.warpAffine(image, M, (cols, rows)) # 旋转后的图像保持大小不变
rotated_labels = []
for label in labels:
# rect=[(x_c,y_c),(w,h),Θ] Θ:flaot[0-179] -> (-180,0)
rect = longsideformat2cvminAreaRect(label[1], label[2], label[3], label[4], (label[5] - 179.9))
# poly = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
poly = cv2.boxPoints(rect) # 返回rect对应的四个点的值 normalized
# 四点坐标反归一化
poly[:, 0] = poly[:, 0] * cols
poly[:, 1] = poly[:, 1] * rows
# 下面是计算旋转后目标相对旋转过后的图像的位置
X0 = (poly[0][0] - a) * math.cos(Pi_angle) - (poly[0][1] - b) * math.sin(Pi_angle) + a
Y0 = (poly[0][0] - a) * math.sin(Pi_angle) + (poly[0][1] - b) * math.cos(Pi_angle) + b
X1 = (poly[1][0] - a) * math.cos(Pi_angle) - (poly[1][1] - b) * math.sin(Pi_angle) + a
Y1 = (poly[1][0] - a) * math.sin(Pi_angle) + (poly[1][1] - b) * math.cos(Pi_angle) + b
X2 = (poly[2][0] - a) * math.cos(Pi_angle) - (poly[2][1] - b) * math.sin(Pi_angle) + a
Y2 = (poly[2][0] - a) * math.sin(Pi_angle) + (poly[2][1] - b) * math.cos(Pi_angle) + b
X3 = (poly[3][0] - a) * math.cos(Pi_angle) - (poly[3][1] - b) * math.sin(Pi_angle) + a
Y3 = (poly[3][0] - a) * math.sin(Pi_angle) + (poly[3][1] - b) * math.cos(Pi_angle) + b
poly_rotated = np.array([(X0, Y0), (X1, Y1), (X2, Y2), (X3, Y3)])
# 四点坐标归一化
poly_rotated[:, 0] = poly_rotated[:, 0] / cols
poly_rotated[:, 1] = poly_rotated[:, 1] / rows
rect_rotated = cv2.minAreaRect(np.float32(poly_rotated)) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
c_x = rect_rotated[0][0]
c_y = rect_rotated[0][1]
w = rect_rotated[1][0]
h = rect_rotated[1][1]
theta = rect_rotated[-1] # Range for angle is [-90,0)
# (num, [classid x_c y_c longside shortside Θ])
label[1:] = cvminAreaRect2longsideformat(c_x, c_y, w, h, theta)
if (sum(label[1:-1] <= 0) + sum(label[1:3] >= 1)) >= 1: # 0<xy<1, 0<side<=1
# print('bbox[:2]中有>= 1的元素,bbox中有<= 0的元素,已将某个box排除,')
np.clip(label[1:-1], 0, 1, out=label[1:-1])
label[-1] = int(label[-1] + 180.5) # range int[0,180] 四舍五入
if label[-1] == 180: # range int[0,179]
label[-1] = 179
rotated_labels.append(label)
return rotated_img, np.array(rotated_labels)
class LoadImagesAndLabels(Dataset):
"""
for training/testing
Args:
path: train_path or test_path eg:../coco128/images/train2017/
img_size,batch_size,augment,hyp,rect,image_weights,cache_images,single_cls,stride,pad,rank
return:
class Dataset:
self.img_files 路径文件夹下所有图片路径 self.img_files=['??\\images\\train2017\\1.jpg',...,]
self.label_files 路径文件夹下所有label_txt路径 self.label_files=['??\\labels\\train2017\\1.txt',...,]
self.n 路径文件夹下所有图片的总数量
self.batch , self.img_size , self.augment , self.hyp , self.image_weights , self.rect , self.mosaic , self.mosaic_border , self.stride ,
self.shapes [[1.jpg的形状]...[n.jpg的形状]] eg:[[480 80][360 640]...[480 640]]
self.labels [array( [对应1.txt的labels信息] ,dtype=float32), ..., array( [对应n.txt的labels信息] ,dtype=float32)]
"""
def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
cache_images=False, single_cls=False, stride=32, pad=0.0, rank=-1):
try:
f = [] # image files
for p in path if isinstance(path, list) else [path]:
p = str(Path(p)) # os-agnostic
# 举例:parent = ‘..\coco128\images’ + '\'
parent = str(Path(p).parent) + os.sep
if os.path.isfile(p): # file
with open(p, 'r') as t:
t = t.read().splitlines()
f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
elif os.path.isdir(p): # folder
f += glob.iglob(p + os.sep + '*.*')
else:
raise Exception('%s does not exist' % p)
# p路径文件夹下所有图片路径都会存在self.img_files中 self.img_files=['??\\images\\train2017\\1.jpg',...,]
self.img_files = sorted(
[x.replace('/', os.sep) for x in f if os.path.splitext(x)[-1].lower() in img_formats])
except Exception as e:
raise Exception('Error loading data from %s: %s\nSee %s' % (path, e, help_url))
n = len(self.img_files)
assert n > 0, 'No images found in %s. See %s' % (path, help_url)
bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index
nb = bi[-1] + 1 # number of batches
self.n = n # number of images
self.batch = bi # batch index of image
self.img_size = img_size
self.augment = augment
self.hyp = hyp
self.image_weights = image_weights
self.rect = False if image_weights else rect
self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training)
self.mosaic_border = [-img_size // 2, -img_size // 2]
self.stride = stride
# Define labels
sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # sa=/images/, sb=/labels/ as substrings
# p路径文件夹下所有label_txt都会存在self.label_files self.label_files=['??\\labels\\train2017\\1.txt',...,]
self.label_files = [x.replace(sa, sb, 1).replace(os.path.splitext(x)[-1], '.txt') for x in self.img_files]
# Check cache
# 初始化图片与标签,为缓存图片、标签做准备
'''
创建缓存文件cache
List cache: {
'??\\images\\train2017\\1.jpg':[array( [对应1.txt的labels信息] ,dtype=float32), (weights, heights))] ,
...
'??\\images\\train2017\\n.jpg':[array( [对应n.txt的labels信息] ,dtype=float32), (weights, heights))]
}
'''
cache_path = str(Path(self.label_files[0]).parent) + '.cache' # cached labels
if os.path.isfile(cache_path):
cache = torch.load(cache_path) # load
if cache['hash'] != get_hash(self.label_files + self.img_files): # dataset changed
cache = self.cache_labels(cache_path) # re-cache
else:
cache = self.cache_labels(cache_path) # cache
# Get labels
'''
self.shapes = [[1.jpg的形状]...[n.jpg的形状]]
self.labels = [array( [对应1.txt的labels信息] ,dtype=float32), ..., array( [对应n.txt的labels信息] ,dtype=float32)]
'''
labels, shapes = zip(*[cache[x] for x in self.img_files])
self.shapes = np.array(shapes, dtype=np.float64)
self.labels = list(labels)
# Rectangular Training https://github.com/ultralytics/yolov3/issues/232
if self.rect:
# Sort by aspect ratio 按纵横比的数值从小到大重新进行排序,矩形训练通常以成批处理
s = self.shapes # wh
ar = s[:, 1] / s[:, 0] # aspect ratio
irect = ar.argsort()
self.img_files = [self.img_files[i] for i in irect]
self.label_files = [self.label_files[i] for i in irect]
self.labels = [self.labels[i] for i in irect]
self.shapes = s[irect] # wh
ar = ar[irect]
# Set training image shapes
shapes = [[1, 1]] * nb
for i in range(nb):
ari = ar[bi == i]
mini, maxi = ari.min(), ari.max()
if maxi < 1:
shapes[i] = [maxi, 1]
elif mini > 1:
shapes[i] = [1, 1 / mini]
self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride
# Cache labels
create_datasubset, extract_bounding_boxes, labels_loaded = False, False, False
nm, nf, ne, ns, nd = 0, 0, 0, 0, 0 # number missing, found, empty, datasubset, duplicate
'''
self.label_files 路径文件夹下所有label_txt路径 self.label_files=['??\\labels\\train2017\\1.txt',...,]
self.labels = [array( [对应1.txt的labels信息] ,dtype=float32), ..., array( [对应n.txt的labels信息] ,dtype=float32)]
'''
pbar = enumerate(self.label_files)
if rank in [-1, 0]:
pbar = tqdm(pbar)
for i, file in pbar:
l = self.labels[i] # label 第i张image的labels信息 size = (目标数量, [class, xywh_center(归一化),Θ])
if l is not None and l.shape[0]:
# 判断标签是否有6列 [class ,xywh, Θ]
assert l.shape[1] == 6, '> 6 label columns: %s' % file
# 判断标签是否全部>=0
assert (l >= 0).all(), 'negative labels: %s' % file
# 判断标签坐标x y 是否归一化
assert (l[:, 1:3] <= 1).all(), 'non-normalized or out of bounds coordinate labels: %s' % file
# 找出标签中重复的坐标
if np.unique(l, axis=0).shape[0] < l.shape[0]: # duplicate rows 若有重复目标则nd自增
nd += 1 # print('WARNING: duplicate rows in %s' % self.label_files[i]) # duplicate rows
# 如果数据集只有一个类,设置类别标签为0
if single_cls:
l[:, 0] = 0 # force dataset into single-class mode
self.labels[i] = l
nf += 1 # file found
# Create subdataset (a smaller dataset)
if create_datasubset and ns < 1E4:
if ns == 0:
create_folder(path='./datasubset')
os.makedirs('./datasubset/images')
exclude_classes = 43
if exclude_classes not in l[:, 0]:
ns += 1
# shutil.copy(src=self.img_files[i], dst='./datasubset/images/') # copy image
with open('./datasubset/images.txt', 'a') as f:
f.write(self.img_files[i] + '\n')
# Extract object detection boxes for a second stage classifier
# 获取目标框与图片,并将框从图片截取下来保存到本地(默认不使用)
if extract_bounding_boxes:
p = Path(self.img_files[i]) # 第i张image的path
img = cv2.imread(str(p))
h, w = img.shape[:2]
for j, x in enumerate(l): # l : label 第i张image的labels信息 size = (目标数量, [class, xywh])
f = '%s%sclassifier%s%g_%g_%s' % (p.parent.parent, os.sep, os.sep, x[0], j, p.name)
if not os.path.exists(Path(f).parent):
os.makedirs(Path(f).parent) # make new output folder
# 对归一化的坐标乘以w,h
# x.size = [class ,xywh]
b = x[1:] * [w, h, w, h] # box
b[2:] = b[2:].max() # rectangle to square
b[2:] = b[2:] * 1.3 + 30 # pad
# xywh格式转xyxy
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int)
b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
assert cv2.imwrite(f, img[b[1]:b[3], b[0]:b[2]]), 'Failure extracting classifier boxes'
else:
ne += 1 # print('empty labels for image %s' % self.img_files[i]) # file empty
# os.system("rm '%s' '%s'" % (self.img_files[i], self.label_files[i])) # remove
if rank in [-1, 0]:
pbar.desc = 'Scanning labels %s (%g found, %g missing, %g empty, %g duplicate, for %g images)' % (
cache_path, nf, nm, ne, nd, n)
if nf == 0:
s = 'WARNING: No labels found in %s. See %s' % (os.path.dirname(file) + os.sep, help_url)
print(s)
assert not augment, '%s. Can not train without labels.' % s
# Cache images into memory for faster training (WARNING: large datasets may exceed system RAM)
# 提前缓存图片到内存中,可以提升训练速度
self.imgs = [None] * n
if cache_images:
gb = 0 # Gigabytes of cached images
pbar = tqdm(range(len(self.img_files)), desc='Caching images')
self.img_hw0, self.img_hw = [None] * n, [None] * n
for i in pbar: # max 10k images
self.imgs[i], self.img_hw0[i], self.img_hw[i] = load_image(self, i) # img, hw_original, hw_resized
gb += self.imgs[i].nbytes
pbar.desc = 'Caching images (%.1fGB)' % (gb / 1E9)
def cache_labels(self, path='labels.cache'):
'''
Cache dataset labels, check images and read shapes
'''
x = {} # dict
pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files))
for (img, label) in pbar:
try:
l = []
image = Image.open(img)
image.verify() # PIL verify
# _ = io.imread(img) # skimage verify (from skimage import io)
shape = exif_size(image) # image size
assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels'
if os.path.isfile(label):
with open(label, 'r') as f:
l = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32) # labels
if len(l) == 0: # 当labels文件中内容为空时也要确保shape一致
l = np.zeros((0, 6), dtype=np.float32)
x[img] = [l, shape]
except Exception as e:
x[img] = [None, None]
print('WARNING: %s: %s' % (img, e))
x['hash'] = get_hash(self.label_files + self.img_files)
torch.save(x, path) # save for next time
return x
def __len__(self):
return len(self.img_files)
# def __iter__(self):
# self.count = -1
# print('ran dataset iter')
# #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF)
# return self
def __getitem__(self, index): # 只要实例对象(假定为p)做p[i]运算时,就会调用类中的__getitem__方法
'''
return torch.from_numpy(img), labels_out, self.img_files[index], shapes
@param index: dataset类的索引,只要调用实例对象(假定为p)做p[i]运算时,就会调用__getitem__方法
@return:
img: 经预处理后的img;size = [3, resized_height, resized_width]
labels_out : (目标数量, [0, classid,归一化后的xywh,Θ])
self.img_files[index] : 图片索引index的文件路径
shapes:
'''
if self.image_weights:
index = self.indices[index]
hyp = self.hyp
mosaic = self.mosaic and random.random() < hyp['mosaic']
if mosaic:
# Load mosaic
# img4 : size = (3 , size1, size2);
# labels : size = (单张img4中的目标GT数量, [classid ,LT_x,LT_y,RB_x,RB_y,Θ]);
img, labels = load_mosaic(self, index)
shapes = None
# MixUp https://arxiv.org/pdf/1710.09412.pdf 对mosaic处理后的图片再一次进行随机mixup处理
if random.random() < hyp['mixup']:
img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1))
r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0
img = (img * r + img2 * (1 - r)).astype(np.uint8)
labels = np.concatenate((labels, labels2), 0)
else:
# Load image
# 加载图片并根据设定的输入大小与图片原大小的比例ratio进行resize(未做填充pad到正方形)
img, (h0, w0), (h, w) = load_image(self, index)
# Letterbox
# 如果进行矩形训练,则获取每个batch的输入图片的shape
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
# Load labels
labels = []
# self.labels = [array( [对应1.txt的labels信息] ,dtype=float32), ..., array( [对应n.txt的labels信息] ,dtype=float32)]
x = self.labels[index] # x.size = (目标数量, [class, xywh, Θ])
if x.size > 0:
# Normalized xywh to pixel xyxy format
# 根据pad调整框的标签坐标,并从归一化的xywh->未归一化的xyxy
labels = x.copy() # labels.size = (单张图片中的目标数量, [class, xyxy])
labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width
labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height
labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0]
labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1]
if self.augment:
# Augment imagespace
if not mosaic:
# 随机对图片进行旋转,平移,缩放,裁剪
img, labels = random_perspective(img, labels,
degrees=hyp['degrees'],
translate=hyp['translate'],
scale=hyp['scale'],
shear=hyp['shear'],
perspective=hyp['perspective'])
# Augment colorspace
# 随机改变图片的色调(H),饱和度(S),亮度(V)
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
# Apply cutouts
# if random.random() < 0.9:
# labels = cutout(img, labels)
# labels.size = (目标数量, [class, xyxy, Θ])
nL = len(labels) # number of labels
if nL:
# 调整框的标签,xyxy to xywh
labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh
# 重新归一化标签0 - 1
labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1
labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1
# labels.size = (目标数量, [class, xywh, Θ])
if self.augment:
# flip up-down 上下翻转 沿x轴翻转 (y变 x不变)
if random.random() < hyp['flipud']:
img = np.flipud(img)
if nL:
labels[:, 2] = 1 - labels[:, 2] # y变x不变
labels[:, -1] = 180 - labels[:, -1] # θ根据左右偏转也进行改变
labels[labels[:, -1] == 180, -1] = 0 # 原θ=0时,情况特殊不做改变
# flip left-right 左右翻转 沿y轴翻转(y不变 x变)
if random.random() < hyp['fliplr']:
img = np.fliplr(img)
if nL:
labels[:, 1] = 1 - labels[:, 1] # x变y不变
labels[:, -1] = 180 - labels[:, -1] # θ根据左右偏转也进行改变
labels[labels[:, -1] == 180, -1] = 0 # 原θ=0时,情况特殊不做改变
# # 旋转augment
# if nL:
# degrees = 10.0
# rotate_angle = random.uniform(-degrees, degrees)
# img, labels = rotate_augment(rotate_angle, 1, img, labels)
# 初始化标签框对应的图片序号,配合下面的collate_fn使用
labels_out = torch.zeros((nL, 7))
if nL:
# labels.size=(目标数量, [class,xywh,Θ]) -> labels_out.size=(目标数量, [?, class,xywh,Θ])
labels_out[:, 1:] = torch.from_numpy(labels)
# Convert
# img.size=[resized_height,resized_width,3] -> [3, resized_height, resized_width]
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
'''
img: 经预处理后的img size= [3, resized_height, resized_width]
labels_out : (目标数量, [0, classid,归一化后的xywh,Θ])
self.img_files[index] : 图片索引index的文件路径
shapes:不进行mosaic时进行矩形训练时才有值
'''
return torch.from_numpy(img), labels_out, self.img_files[index], shapes
@staticmethod
def collate_fn(batch): # 取样器取样本的函数 即可通过该函数重写并自定义聚合为batch的方式
"""
return img, labels, path, shapes
@param batch: 一个batch里面包含img,label,path,shapes 重写batch取样函数
@return:
img : size = (batch_size, 3 , resized_height, resized_width) 没有归一化
labels : size = (batch中的目标数量, [图片在当前batch中的索引,classid,归一化后的xywh, Θ])
eg:[[0, 6, 0.5, 0.5, 0.26, 0.35, 179],
[0, 6, 0.5, 0.5, 0.26, 0.35, 179],
[1, 6, 0.5, 0.5, 0.26, 0.35, 179],
[2, 6, 0.5, 0.5, 0.26, 0.35, 179],]
path: 该batch中所有image的路径 size=batch_size
shapes: 该batch中所有image的shapes size=batch_size 不进行mosaic时进行矩形训练时才有值
"""
# 一个batch中的img,标签信息,路径信息,形状信息 batch中的每个索引都由__getitem__函数提供
# eg: label:[[1.txt的labels信息], ... ,[2.txt的labels信息]]
img, label, path, shapes = zip(*batch) # transposed
for i, l in enumerate(label): # i对应一个batch中的图片索引
l[:, 0] = i # add target image index for build_targets()
# stack 和cat都是对tensor沿指定维度拼接,stack会增加一个维度,cat不会增加维度
# img增加一个batch_size维度
# label打破一个维度由label:[[1.txt的labels信息], ... ,[2.txt的labels信息]] -> [batch中的目标数量,[图片在当前batch中的索引,classid,归一化后的xywh]]
return torch.stack(img, 0), torch.cat(label, 0), path, shapes
# Ancillary functions --------------------------------------------------------------------------------------------------
def load_image(self, index):
'''
loads 1 image from dataset 加载训练列表中的一张图片
@param self: dataset类
@param index: 用于索引当前训练集中的图片
@return:
----------------------------------------
若图片无缓存:
img: 图像像素矩阵 size=(height, width, 3)
(h0, w0): 图像原始的(height,width)
img.shape[:2]: 图像resize之后的(height,width)
否则:
self.imgs[index]: 图像像素矩阵 size=(height, width, 3)
self.img_hw0[index]: 图像原始的(height,width)
self.img_hw[index]: 图像resize之后的(height,width)
----------------------------------------
'''
img = self.imgs[index]
if img is None: # not cached
path = self.img_files[index]
img = cv2.imread(path) # BGR
assert img is not None, 'Image Not Found ' + path
h0, w0 = img.shape[:2] # orig hw
r = self.img_size / max(h0, w0) # resize image to img_size
if r != 1: # always resize down, only resize up if training with augmentation
interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR
img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp)
return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized
else:
return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
dtype = img.dtype # uint8
x = np.arange(0, 256, dtype=np.int16)
lut_hue = ((x * r[0]) % 180).astype(dtype)
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
# Histogram equalization
# if random.random() < 0.2:
# for i in range(3):
# img[:, :, i] = cv2.equalizeHist(img[:, :, i])
def load_mosaic(self, index):
'''
loads 4 images in a mosaic
@param self: 一个dataset类
@param index: 索引号,用于索引整个训练集合中的图片
@return:
——img4 : size = (resized_height,resized_ width, 3);经
——labels4 : size = (单张img4中的目标GT数量, [classid ,LT_x,LT_y,RB_x,RB_y,Θ]未归一化);
'''
labels4 = []
s = self.img_size
# 随机取mosaic中心点
yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
# 随机取其他三张图片的索引
indices = [index] + [random.randint(0, len(self.labels) - 1) for _ in range(3)] # 3 additional image indices
for i, index in enumerate(indices):
# Load image
# img.size = [resized_height,resized_ width, 3]
img, _, (h, w) = load_image(self, index)
# place img in img4
if i == 0: # top left
img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) 用于确定原图片在img4左上角的坐标(左上右下)
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) 用于确定原图片剪裁进img4中的图像内容范围
elif i == 1: # top right
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
elif i == 2: # bottom left
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
elif i == 3: # bottom right
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
# img4.size = [resized_height,resized_ width, 3]
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
padw = x1a - x1b # 原图片未剪裁进img4中的宽度
padh = y1a - y1b # 原图片未剪裁进img4中的高度
# Labels
# self.labels[array([对应1.txt的labels信息] ,dtype = float32), ..., array([对应n.txt的labels信息] ,dtype = float32)]
x = self.labels[index]
labels = x.copy()
if x.size > 0: # Normalized xywh to pixel xyxy format 归一化的xywh转为非归一化的xyxy(左上右下)坐标形式
labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw # Left_top_x
labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh # Left_top_y
labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw # right_bottom_x
labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh # right_bottom_y
labels4.append(labels) # labels4:[array([对应1.txt的labels信息 size=[n1,6]], ... ,array([对应4.txt的labels信息] size=[n4,6]]
# Concat/clip labels
if len(labels4):
# labels4:[array([对应1.txt的labels信息 size=[n1,6]], ... ,array([对应4.txt的labels信息] size=[n4,6]] -> [4张图片的gt总数n1+n2+n3+n4,6]
# 即labels4.shape=(一张mosaic图片中的GT数量, [classid ,LT_x,LT_y,RB_x,RB_y,Θ])
labels4 = np.concatenate(labels4, 0) # 将第一个维度取消
np.clip(labels4[:, 1:5], 0, 2 * s, out=labels4[:, 1:5]) # 限定labels4[:, 1:5]中最小值只能为0,最大值只能为2*self.size
# img4, labels4 = replicate(img4, labels4) # replicate
# Augment
img4, labels4 = random_perspective(img4, labels4,
degrees=self.hyp['degrees'],
translate=self.hyp['translate'],
scale=self.hyp['scale'],
shear=self.hyp['shear'],
perspective=self.hyp['perspective'],
border=self.mosaic_border) # border to remove
'''
img4 : (size1, size2, 3)
labels4 : (单张img4中的目标GT数量, [classid ,LT_x,LT_y,RB_x,RB_y,Θ])
'''
return img4, labels4
def replicate(img, labels):
# Replicate labels
h, w = img.shape[:2]
boxes = labels[:, 1:].astype(int)
x1, y1, x2, y2 = boxes.T
s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
x1b, y1b, x2b, y2b = boxes[i]
bh, bw = y2b - y1b, x2b - x1b
yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
img[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
return img, labels
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
'''
Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
@param new_shape: 矩形训练后的输出size
@param color: 用于填充图片未覆盖区域的背景色
@return:
@param img: 待矩形训练后的输入图像
@return:
img : 矩形训练后的输出图像
ratio : [width_ratio , height_ratio] 最终size/原始size
(dw, dh) :最小的左右/上下填充大小
'''
shape = img.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
# 计算缩放因子
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup: # only scale down, do not scale up (for better test mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
# 获取最小的矩形填充
if auto: # minimum rectangle
dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
# 计算上下左右填充大小
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
# 进行填充
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return img, ratio, (dw, dh)
def random_perspective(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)):
'''
遍性数据增强:
进行随机旋转,缩放,错切,平移,center,perspective数据增强
Args:
img: shape=(height, width, 3)
targets :size = (单张图片中的目标数量, [class, xyxy, Θ])