Simple Implementation of a drop-in database for image datasets in PyTorch.
Project description
Another LMDB for PyTorch
This is a simple wrapper that lets you store a pytorch image dataset in an LMDB. This code wraps a torch.utils.Dataset
that outputs image data and saves all images into a single database structure under the hood. In this case this an LMDB. There are a ton of variations of exactly this problem on github, but none were really what I wanted so I wrote another one.
What is different in this version?
- The wrapped dataset works like normal, it can be pickled for a dataloader, can have data augmentations and mirrors attributes of the wrapped dataset.
- No external dependencies on
pyarrow
, image data is directly written asuint8
byte streams and directly read into tensor data. - Data is saved uncompressed, but not in floats like in some other projects. This format is minimal and fast to read.
- Arbitrary image transformations such as resizing can be easily baked into the database.
- The LMDB can be written to a unique temporary file and cleaned up after deallocation.
Installation
You can install this module via pip,
pip install torchlmdb
but it's really only a single file with a single class.
Usage
Given an existing pytorch image dataset, for example
dataset = torchvision.datasets.CIFAR10(root="~/data", train=False)
simply wrap the dataset:
from torchlmdb import LMDBDataset
wrapped_dataset = LMDBDataset(dataset, name="val")
The wrapped dataset behaves like the original dataset, but stores the data in an LMDB database under the hood.
Requirements:
The wrapped dataset needs to return pillow images that can be cast to uint8
when dataset.transform=None
. This is the case
for all of the torchvision image datasets, for example. Other transformations (for example random flips and normalization)
are ok and will be seamlessly applied to the LMDB output, so that random transforms are still random and not encoded into the db.
However, you can also encode transformations directly into the stored data. For example, calling LMDBDataset
with db_tranform=torchvision.transforms.Resize(16)
will store all data as 16x16 images in the database. Transforms like this are significantly more efficient than reading in a large image and downsampling it on the fly.
The name
argument is user controlled and, when the database is not temporary, should uniquely identify this dataset when multiple databases exist in the root directory.
Arguments:
LMDBDataset:
Args:
dataset: The original image dataset (or any dataset that returns PIL data)
root: Where to create the database
name: A name for the newly created database
can_create: Set to false to forbid database creation (For example in distrbuted training)
temporary_db: Create the database only temporary and clean it up after deletion of this object
db_transform: A torchvision.transform (or composition) to be applied during database creation.
force_db_rebuild: Force a rebuilding of the database
db_cfg: A struct of additional configuration options as described in the readme.
Advanced Arguments:
A db_cfg
can be handed as argument with additional arguments.
The default arguments can be imported as LMDB_config
and are set to
map_size = 1099511627776 * 2 # Linux can grow memory as needed.
write_frequency = 65536 # how often to flush during database creation
shuffle_while_writing = False # Shuffle during DB creation
db_channels_first = True # Write in CHW format if possible.
rounds = 1 # Can write multiple rounds of the same dataset (for example with different augmentations).
num_db_attempts = 10 # how many attempts to open the database
max_readers = 128 # How many processes can read the database simultaneously.
readahead = True # beneficial for long sequential reads, disable when randomly accessing large DBs
meminit = True
max_spare_txns = 128
access = "get" # can be "cursor" or "get"
References
This implementation of an LMDB interface in pyTorch is based on some older forks I made of https://github.com/pytorch/vision/blob/master/torchvision/datasets/lsun.py and https://github.com/Lyken17/Efficient-PyTorch/blob/master/tools/folder2lmdb.py .
MIT License
Copyright (c) 2022 Jonas Geiping
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
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
Hashes for torchlmdb-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 509a49a534527b909e0c1f0554a7c4eaa6662a6de34dcd6a1442f777cdb855f4 |
|
MD5 | c64ca3348ef44e779174568da8362247 |
|
BLAKE2b-256 | 2ff64d698c9954cfa295813d631e07d498783a7d8deb0819ca34a3957a490ad7 |