Unverified 提交 a6f197ae authored 作者: Glenn Jocher's avatar Glenn Jocher 提交者: GitHub

Update dataloaders.py (#8714)

* Update dataloaders.py * Update dataloaders.py
上级 7215a0fb
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
Dataloaders and dataset utils Dataloaders and dataset utils
""" """
import contextlib
import glob import glob
import hashlib import hashlib
import json import json
...@@ -55,13 +56,10 @@ def get_hash(paths): ...@@ -55,13 +56,10 @@ def get_hash(paths):
def exif_size(img): def exif_size(img):
# Returns exif-corrected PIL size # Returns exif-corrected PIL size
s = img.size # (width, height) s = img.size # (width, height)
try: with contextlib.suppress(Exception):
rotation = dict(img._getexif().items())[orientation] rotation = dict(img._getexif().items())[orientation]
if rotation in [6, 8]: # rotation 270 or 90 if rotation in [6, 8]: # rotation 270 or 90
s = (s[1], s[0]) s = (s[1], s[0])
except Exception:
pass
return s return s
...@@ -859,18 +857,13 @@ class LoadImagesAndLabels(Dataset): ...@@ -859,18 +857,13 @@ class LoadImagesAndLabels(Dataset):
# Ancillary functions -------------------------------------------------------------------------------------------------- # Ancillary functions --------------------------------------------------------------------------------------------------
def create_folder(path='./new'):
# Create folder
if os.path.exists(path):
shutil.rmtree(path) # delete output folder
os.makedirs(path) # make new output folder
def flatten_recursive(path=DATASETS_DIR / 'coco128'): def flatten_recursive(path=DATASETS_DIR / 'coco128'):
# Flatten a recursive directory by bringing all files to top level # Flatten a recursive directory by bringing all files to top level
new_path = Path(str(path) + '_flat') new_path = Path(f'{str(path)}_flat')
create_folder(new_path) if os.path.exists(new_path):
for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)): shutil.rmtree(new_path) # delete output folder
os.makedirs(new_path) # make new output folder
for file in tqdm(glob.glob(f'{str(Path(path))}/**/*.*', recursive=True)):
shutil.copyfile(file, new_path / Path(file).name) shutil.copyfile(file, new_path / Path(file).name)
...@@ -929,7 +922,7 @@ def autosplit(path=DATASETS_DIR / 'coco128/images', weights=(0.9, 0.1, 0.0), ann ...@@ -929,7 +922,7 @@ def autosplit(path=DATASETS_DIR / 'coco128/images', weights=(0.9, 0.1, 0.0), ann
for i, img in tqdm(zip(indices, files), total=n): for i, img in tqdm(zip(indices, files), total=n):
if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
with open(path.parent / txt[i], 'a') as f: with open(path.parent / txt[i], 'a') as f:
f.write('./' + img.relative_to(path.parent).as_posix() + '\n') # add image to txt file f.write(f'./{img.relative_to(path.parent).as_posix()}' + '\n') # add image to txt file
def verify_image_label(args): def verify_image_label(args):
...@@ -1011,14 +1004,13 @@ def dataset_stats(path='coco128.yaml', autodownload=False, verbose=False, profil ...@@ -1011,14 +1004,13 @@ def dataset_stats(path='coco128.yaml', autodownload=False, verbose=False, profil
def _unzip(path): def _unzip(path):
# Unzip data.zip # Unzip data.zip
if str(path).endswith('.zip'): # path is data.zip if not str(path).endswith('.zip'): # path is data.yaml
assert Path(path).is_file(), f'Error unzipping {path}, file not found'
ZipFile(path).extractall(path=path.parent) # unzip
dir = path.with_suffix('') # dataset directory == zip name
assert dir.is_dir(), f'Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/'
return True, str(dir), _find_yaml(dir) # zipped, data_dir, yaml_path
else: # path is data.yaml
return False, None, path return False, None, path
assert Path(path).is_file(), f'Error unzipping {path}, file not found'
ZipFile(path).extractall(path=path.parent) # unzip
dir = path.with_suffix('') # dataset directory == zip name
assert dir.is_dir(), f'Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/'
return True, str(dir), _find_yaml(dir) # zipped, data_dir, yaml_path
def _hub_ops(f, max_dim=1920): def _hub_ops(f, max_dim=1920):
# HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing # HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论