Package: torchlmdb
Another LMDB for PyTorchThis is a simple wrapper that lets you store a pytorch image dataset in an LMDB. This code wraps atorch.utils.Datasetthat 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 onpyarrow, 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.InstallationYou can install this module via pip,pip install torchlmdbbut it's really only a single file with a single class.UsageGiven an existing pytorch image dataset, for exampledataset = 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 touint8whendataset.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, callingLMDBDatasetwithdb_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.
Thenameargument 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:Adb_cfgcan be handed as argument with additional arguments.
The default arguments can be imported asLMDB_configand are set tomap_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"ReferencesThis implementation of an LMDB interface in pyTorch is based on some older forks I made ofhttps://github.com/pytorch/vision/blob/master/torchvision/datasets/lsun.pyandhttps://github.com/Lyken17/Efficient-PyTorch/blob/master/tools/folder2lmdb.py.MIT LicenseCopyright (c) 2022 Jonas GeipingPermission 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.