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 asuint8byte 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.
Metadata
Release files for torchlmdb 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchlmdb-0.1.1.tar.gz | 8.8 kB | Without provenance | Details for torchlmdb-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchlmdb-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for torchlmdb-0.1.1-py3-none-any.whl |
Total release size: 17.8 kB
Release files / torchlmdb-0.1.1.tar.gz
| Download URL | torchlmdb-0.1.1.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2e567c23008ac79ba5ecc7cb5dc2b5cf781cad81f7a1beada71b1bc183273ef
|
|
BLAKE2b-256 checksum How to use checksums |
719975f49732855893c708013820a9c174ede6d4d5135ea7ffe3c5516e5397c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.8.2 keyring/23.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.5
|
Release files / torchlmdb-0.1.1-py3-none-any.whl
| Download URL | torchlmdb-0.1.1-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
509a49a534527b909e0c1f0554a7c4eaa6662a6de34dcd6a1442f777cdb855f4
|
|
BLAKE2b-256 checksum How to use checksums |
2ff64d698c9954cfa295813d631e07d498783a7d8deb0819ca34a3957a490ad7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.8.2 keyring/23.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.5
|
Security for torchlmdb 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.