You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.6 KiB
75 lines
2.6 KiB
2 years ago
|
""" A dataset reader that reads single tarfile based datasets
|
||
4 years ago
|
|
||
2 years ago
|
This reader can read datasets consisting if a single tarfile containing images.
|
||
4 years ago
|
I am planning to deprecated it in favour of ParerImageInTar.
|
||
|
|
||
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
|
"""
|
||
4 years ago
|
import os
|
||
|
import tarfile
|
||
|
|
||
|
from timm.utils.misc import natural_key
|
||
|
|
||
3 years ago
|
from .class_map import load_class_map
|
||
|
from .img_extensions import get_img_extensions
|
||
2 years ago
|
from .reader import Reader
|
||
3 years ago
|
|
||
4 years ago
|
|
||
4 years ago
|
def extract_tarinfo(tarfile, class_to_idx=None, sort=True):
|
||
3 years ago
|
extensions = get_img_extensions(as_set=True)
|
||
4 years ago
|
files = []
|
||
|
labels = []
|
||
|
for ti in tarfile.getmembers():
|
||
|
if not ti.isfile():
|
||
|
continue
|
||
|
dirname, basename = os.path.split(ti.path)
|
||
|
label = os.path.basename(dirname)
|
||
|
ext = os.path.splitext(basename)[1]
|
||
3 years ago
|
if ext.lower() in extensions:
|
||
4 years ago
|
files.append(ti)
|
||
|
labels.append(label)
|
||
|
if class_to_idx is None:
|
||
|
unique_labels = set(labels)
|
||
|
sorted_labels = list(sorted(unique_labels, key=natural_key))
|
||
|
class_to_idx = {c: idx for idx, c in enumerate(sorted_labels)}
|
||
|
tarinfo_and_targets = [(f, class_to_idx[l]) for f, l in zip(files, labels) if l in class_to_idx]
|
||
|
if sort:
|
||
|
tarinfo_and_targets = sorted(tarinfo_and_targets, key=lambda k: natural_key(k[0].path))
|
||
|
return tarinfo_and_targets, class_to_idx
|
||
|
|
||
|
|
||
2 years ago
|
class ReaderImageTar(Reader):
|
||
4 years ago
|
""" Single tarfile dataset where classes are mapped to folders within tar
|
||
2 years ago
|
NOTE: This class is being deprecated in favour of the more capable ReaderImageInTar that can
|
||
4 years ago
|
operate on folders of tars or tars in tars.
|
||
4 years ago
|
"""
|
||
|
def __init__(self, root, class_map=''):
|
||
4 years ago
|
super().__init__()
|
||
|
|
||
|
class_to_idx = None
|
||
|
if class_map:
|
||
|
class_to_idx = load_class_map(class_map, root)
|
||
|
assert os.path.isfile(root)
|
||
|
self.root = root
|
||
4 years ago
|
|
||
4 years ago
|
with tarfile.open(root) as tf: # cannot keep this open across processes, reopen later
|
||
4 years ago
|
self.samples, self.class_to_idx = extract_tarinfo(tf, class_to_idx)
|
||
4 years ago
|
self.imgs = self.samples
|
||
|
self.tarfile = None # lazy init in __getitem__
|
||
|
|
||
|
def __getitem__(self, index):
|
||
|
if self.tarfile is None:
|
||
|
self.tarfile = tarfile.open(self.root)
|
||
|
tarinfo, target = self.samples[index]
|
||
4 years ago
|
fileobj = self.tarfile.extractfile(tarinfo)
|
||
|
return fileobj, target
|
||
4 years ago
|
|
||
|
def __len__(self):
|
||
|
return len(self.samples)
|
||
|
|
||
|
def _filename(self, index, basename=False, absolute=False):
|
||
|
filename = self.samples[index][0].name
|
||
|
if basename:
|
||
|
filename = os.path.basename(filename)
|
||
|
return filename
|