in distributed_training/src_dir/main_trainer.py [0:0]
def _loader_file(self, image_path):
extensions = ('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif',
'.tiff', '.webp')
def is_valid_file(x: str) -> bool:
return x.lower().endswith(extensions)
is_valid_file = cast(Callable[[str], bool], is_valid_file)
self.classes = [d.name for d in os.scandir(image_path) if d.is_dir()]
self.classes.sort()
self.class_to_idx = {
cls_name: i
for i, cls_name in enumerate(self.classes)
}
instances = []
for target_class in sorted(self.class_to_idx.keys()):
class_index = self.class_to_idx[target_class]
target_dir = os.path.join(image_path, target_class)
if not os.path.isdir(target_dir):
continue
for root, _, fnames in sorted(os.walk(target_dir,
followlinks=True)):
for fname in sorted(fnames):
path = os.path.join(root, fname)
if is_valid_file(path):
item = path, class_index
instances.append(item)
return instances