Image datasets for Pytorch.
Project description
torch image dataset
Install:
pip install torch-image-dataset
Requirements:
pip install torch torchvision pillow opencv-python
Additional requirements (optional)
For saving/loading the dataset in a database:
conda install -c conda-forge python-lmdb
For fast image loaders:
conda install -c conda-forge pyturbojpeg
conda install -c conda-forge accimage
Image Loaders
ImageLoaders are objects that can load an image from a path and decode it (additionally they can resize the image).
The image loaders available are: LoaderPIL (from pillow), LoaderOpenCV (from opencv), LoaderAccImage (from accimage) and LoaderTurboJPEG (from turbojpeg).
To initialize a loader:
from imagedataset import LoaderOpenCV, Interpolation
size = (224, 224)
loader = LoaderOpenCV(size=size, interpolation=Interpolation.CV_AREA)
If the size is None there won't be any resize at loading time. The interpolation methods available are:
CV_BILINEAR (cv2.INTER_LINEAR)
CV_BILINEAR_EXACT (cv2.INTER_LINEAR_EXACT)
CV_NEAREST (cv2.INTER_NEAREST)
CV_BICUBIC (cv2.INTER_CUBIC)
CV_LANCZOS (cv2.INTER_LANCZOS4)
CV_AREA (cv2.INTER_AREA)
PIL_NEAREST (PIL Image.NEAREST)
PIL_BICUBIC (PIL Image.BICUBIC)
PIL_BILINEAR (PIL Image.BILINEAR
PIL_LANCZOS (PIL Image.LANCZOS)
ACCIMAGE_BUILDIN (interpolation of AccImage)
Not all loaders support all interpolations, but the opencv interpolations are accepted by all loaders (for example it is possible to use the CV_BILINEAR interpolation even in a LoaderPIL).
Image Decoders
ImageDecoders are very similar to ImageLoaders but their __call__ function accepts a bytes object and decodes to an image (and eventually resize it).
There is one decoder for each loader introduced before.
To create a decoder:
decoder = DecoderPIL(size=(224,224), interpolation=Interpolation.CV_BICUBIC)
Datasets
AdvanceImageFolder
Use case:
- keep track of pseudolabels (read/update).
- load full dataset into RAM to speed up training when disk memory access is slow.
- save and read the full dataset into a database to speed up training when the whole dataset cannot be stored in RAM.
- easy subsetting/splitting keeping the possibility to update and read pseudolabels.
Import:
import imagedataset as id
The folder structure required is similar to the one of torchvision.datasets.ImageFolder:
root\
class1\
img1
img2
...
class2\
img1
img2
...
...
The dataset is initialized by indicating the root directory.
Additionally, the name of the dataset can be indicated and it is possible to load just a subset of the dataset. load_percentage specifies the percentage of images to load (randomly picked without repetitions) or indices can be used to select specific indices of the dataset.
dataset = id.AdvanceImageFolder(root="path/to/mydataset/",
name="incredible_dataset",
load_percentage=1.,
indices=None)
The return type of the dataset is types.SimpleNamespace with the following fields:
- image
- id
- index
- label
- pseudolabel
For example you can:
item = dataset.__getitem__(0)
image = item.image
label = item.label
pseudolabel = item.pseudolabel
...
To use a dataloader it is possible to use:
loader = dataset.dataloader(batch_size=10)
Actually the dataloader function accepts the same arguments as torch.utils.data.DataLoader and returns a proper DataLoader with the appropriate collate_fn for batching.
Note: every tensor in the batch is of type int64 (classification labels) or uint8 (images or label arrays).
To speed up the training it is possible to use a CudaLoader that pre-fatches the samples:
loader = dataset.cudaloader(batch_size=10)
Note: the CudaLoader convers images to floats but do not scale them, so they are still in range [0,255]. Additionally the CudaLoader can perform normalization, for instance to bring images in range [0,1] it is possible:
loader = dataset.cudaloader(batch_size=10, mean=(0,0,0), std=(255, 255, 255))
To subset a dataset:
my_subset = dataset.subset(indices=[0,1,2], ids=None)
It is possible to select the indices or the ids of images. The indices are the positions of images in the sequence from 0 to len(dataset)-1. Ids are initialized as indices but they are kept constant while subsetting and splitting and, for this reason, it is possible to get the index of an image in the original dataset.
To split a dataset:
train_d, val_d, test_d = dataset.random_split(percentages=[0.5, 0.3, 0.2])
To drop a random part of the dataset, let say we want to keep just 50% of dataset:
dataset = datset.random_split(percentages=[0.5])
To load the dataset into RAM just use:
dataset.load_ram()
To write the dataset into a LMDB database:
dataset.write_database("mydb/path/")
Then, to get a dataset that reads the database:
dataset = id.from_database("mydb/path/")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch-image-dataset-0.0.3.tar.gz.
File metadata
- Download URL: torch-image-dataset-0.0.3.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bf8e1d456710226346102cecc02d6ea99d26511db6a86f1a5b6e7f9765eddc1
|
|
| MD5 |
98ce13a58de3bb1423eaf1f19dd2db35
|
|
| BLAKE2b-256 |
9a2655b0d96a4b4a333b44133802688ab1d4736294b00c02bc14abe963a921ed
|
File details
Details for the file torch_image_dataset-0.0.3-py3-none-any.whl.
File metadata
- Download URL: torch_image_dataset-0.0.3-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcbc3d65d2b6c9707a1986d3d3783cb8409d9343c9f1dd23889628c683faf6a3
|
|
| MD5 |
74dedca049da1719a3dc3ea1f05e1ab5
|
|
| BLAKE2b-256 |
ef2c2de8d6283efc93ce7a33dc11551a80e10f5d8da8e794c04526a4a0be83b2
|