@@ -59,7 +59,7 @@ def exif_size(img):
5959 s = (s [1 ], s [0 ])
6060 elif rotation == 8 : # rotation 90
6161 s = (s [1 ], s [0 ])
62- except :
62+ except Exception :
6363 pass
6464
6565 return s
@@ -420,7 +420,7 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
420420 cache , exists = np .load (cache_path , allow_pickle = True ).item (), True # load dict
421421 assert cache ['version' ] == self .cache_version # same version
422422 assert cache ['hash' ] == get_hash (self .label_files + self .img_files ) # same hash
423- except :
423+ except Exception :
424424 cache , exists = self .cache_labels (cache_path , prefix ), False # cache
425425
426426 # Display cache
@@ -514,13 +514,13 @@ def cache_labels(self, path=Path('./labels.cache'), prefix=''):
514514 with Pool (NUM_THREADS ) as pool :
515515 pbar = tqdm (pool .imap (verify_image_label , zip (self .img_files , self .label_files , repeat (prefix ))),
516516 desc = desc , total = len (self .img_files ))
517- for im_file , l , shape , segments , nm_f , nf_f , ne_f , nc_f , msg in pbar :
517+ for im_file , lb , shape , segments , nm_f , nf_f , ne_f , nc_f , msg in pbar :
518518 nm += nm_f
519519 nf += nf_f
520520 ne += ne_f
521521 nc += nc_f
522522 if im_file :
523- x [im_file ] = [l , shape , segments ]
523+ x [im_file ] = [lb , shape , segments ]
524524 if msg :
525525 msgs .append (msg )
526526 pbar .desc = f"{ desc } { nf } found, { nm } missing, { ne } empty, { nc } corrupt"
@@ -627,8 +627,8 @@ def __getitem__(self, index):
627627 @staticmethod
628628 def collate_fn (batch ):
629629 img , label , path , shapes = zip (* batch ) # transposed
630- for i , l in enumerate (label ):
631- l [:, 0 ] = i # add target image index for build_targets()
630+ for i , lb in enumerate (label ):
631+ lb [:, 0 ] = i # add target image index for build_targets()
632632 return torch .stack (img , 0 ), torch .cat (label , 0 ), path , shapes
633633
634634 @staticmethod
@@ -645,15 +645,15 @@ def collate_fn4(batch):
645645 if random .random () < 0.5 :
646646 im = F .interpolate (img [i ].unsqueeze (0 ).float (), scale_factor = 2.0 , mode = 'bilinear' , align_corners = False )[
647647 0 ].type (img [i ].type ())
648- l = label [i ]
648+ lb = label [i ]
649649 else :
650650 im = torch .cat ((torch .cat ((img [i ], img [i + 1 ]), 1 ), torch .cat ((img [i + 2 ], img [i + 3 ]), 1 )), 2 )
651- l = torch .cat ((label [i ], label [i + 1 ] + ho , label [i + 2 ] + wo , label [i + 3 ] + ho + wo ), 0 ) * s
651+ lb = torch .cat ((label [i ], label [i + 1 ] + ho , label [i + 2 ] + wo , label [i + 3 ] + ho + wo ), 0 ) * s
652652 img4 .append (im )
653- label4 .append (l )
653+ label4 .append (lb )
654654
655- for i , l in enumerate (label4 ):
656- l [:, 0 ] = i # add target image index for build_targets()
655+ for i , lb in enumerate (label4 ):
656+ lb [:, 0 ] = i # add target image index for build_targets()
657657
658658 return torch .stack (img4 , 0 ), torch .cat (label4 , 0 ), path4 , shapes4
659659
@@ -743,6 +743,7 @@ def load_mosaic9(self, index):
743743 s = self .img_size
744744 indices = [index ] + random .choices (self .indices , k = 8 ) # 8 additional image indices
745745 random .shuffle (indices )
746+ hp , wp = - 1 , - 1 # height, width previous
746747 for i , index in enumerate (indices ):
747748 # Load image
748749 img , _ , (h , w ) = load_image (self , index )
@@ -906,30 +907,30 @@ def verify_image_label(args):
906907 if os .path .isfile (lb_file ):
907908 nf = 1 # label found
908909 with open (lb_file ) as f :
909- l = [x .split () for x in f .read ().strip ().splitlines () if len (x )]
910- if any ([len (x ) > 8 for x in l ]): # is segment
911- classes = np .array ([x [0 ] for x in l ], dtype = np .float32 )
912- segments = [np .array (x [1 :], dtype = np .float32 ).reshape (- 1 , 2 ) for x in l ] # (cls, xy1...)
913- l = np .concatenate ((classes .reshape (- 1 , 1 ), segments2boxes (segments )), 1 ) # (cls, xywh)
914- l = np .array (l , dtype = np .float32 )
915- nl = len (l )
910+ lb = [x .split () for x in f .read ().strip ().splitlines () if len (x )]
911+ if any ([len (x ) > 8 for x in lb ]): # is segment
912+ classes = np .array ([x [0 ] for x in lb ], dtype = np .float32 )
913+ segments = [np .array (x [1 :], dtype = np .float32 ).reshape (- 1 , 2 ) for x in lb ] # (cls, xy1...)
914+ lb = np .concatenate ((classes .reshape (- 1 , 1 ), segments2boxes (segments )), 1 ) # (cls, xywh)
915+ lb = np .array (lb , dtype = np .float32 )
916+ nl = len (lb )
916917 if nl :
917- assert l .shape [1 ] == 5 , f'labels require 5 columns, { l .shape [1 ]} columns detected'
918- assert (l >= 0 ).all (), f'negative label values { l [ l < 0 ]} '
919- assert (l [:, 1 :] <= 1 ).all (), f'non-normalized or out of bounds coordinates { l [:, 1 :][l [:, 1 :] > 1 ]} '
920- _ , i = np .unique (l , axis = 0 , return_index = True )
918+ assert lb .shape [1 ] == 5 , f'labels require 5 columns, { lb .shape [1 ]} columns detected'
919+ assert (lb >= 0 ).all (), f'negative label values { lb [ lb < 0 ]} '
920+ assert (lb [:, 1 :] <= 1 ).all (), f'non-normalized or out of bounds coordinates { lb [:, 1 :][lb [:, 1 :] > 1 ]} '
921+ _ , i = np .unique (lb , axis = 0 , return_index = True )
921922 if len (i ) < nl : # duplicate row check
922- l = l [i ] # remove duplicates
923+ lb = lb [i ] # remove duplicates
923924 if segments :
924925 segments = segments [i ]
925926 msg = f'{ prefix } WARNING: { im_file } : { nl - len (i )} duplicate labels removed'
926927 else :
927928 ne = 1 # label empty
928- l = np .zeros ((0 , 5 ), dtype = np .float32 )
929+ lb = np .zeros ((0 , 5 ), dtype = np .float32 )
929930 else :
930931 nm = 1 # label missing
931- l = np .zeros ((0 , 5 ), dtype = np .float32 )
932- return im_file , l , shape , segments , nm , nf , ne , nc , msg
932+ lb = np .zeros ((0 , 5 ), dtype = np .float32 )
933+ return im_file , lb , shape , segments , nm , nf , ne , nc , msg
933934 except Exception as e :
934935 nc = 1
935936 msg = f'{ prefix } WARNING: { im_file } : ignoring corrupt image/label: { e } '
0 commit comments