All Projects → rmccorm4 → PyTorch-LMDB

rmccorm4 / PyTorch-LMDB

Licence: other
Scripts to work with LMDB + PyTorch for Imagenet training

Projects that are alternatives of or similar to PyTorch-LMDB

Pytorch Classification
Classification with PyTorch.
Stars: ✭ 1,268 (+2487.76%)
Mutual labels:  imagenet, resnet
SAN
[ECCV 2020] Scale Adaptive Network: Learning to Learn Parameterized Classification Networks for Scalable Input Images
Stars: ✭ 41 (-16.33%)
Mutual labels:  imagenet, resnet
Resnet Imagenet Caffe
train resnet on imagenet from scratch with caffe
Stars: ✭ 105 (+114.29%)
Mutual labels:  imagenet, resnet
Segmentationcpp
A c++ trainable semantic segmentation library based on libtorch (pytorch c++). Backbone: ResNet, ResNext. Architecture: FPN, U-Net, PAN, LinkNet, PSPNet, DeepLab-V3, DeepLab-V3+ by now.
Stars: ✭ 49 (+0%)
Mutual labels:  imagenet, resnet
Senet Caffe
A Caffe Re-Implementation of SENet
Stars: ✭ 169 (+244.9%)
Mutual labels:  imagenet, resnet
Pretrained Models.pytorch
Pretrained ConvNets for pytorch: NASNet, ResNeXt, ResNet, InceptionV4, InceptionResnetV2, Xception, DPN, etc.
Stars: ✭ 8,318 (+16875.51%)
Mutual labels:  imagenet, resnet
Pyramidnet
Torch implementation of the paper "Deep Pyramidal Residual Networks" (https://arxiv.org/abs/1610.02915).
Stars: ✭ 121 (+146.94%)
Mutual labels:  imagenet, resnet
Mmclassification
OpenMMLab Image Classification Toolbox and Benchmark
Stars: ✭ 532 (+985.71%)
Mutual labels:  imagenet, resnet
Iresnet
Improved Residual Networks (https://arxiv.org/pdf/2004.04989.pdf)
Stars: ✭ 163 (+232.65%)
Mutual labels:  imagenet, resnet
Pytorch Imagenet Cifar Coco Voc Training
Training examples and results for ImageNet(ILSVRC2012)/CIFAR100/COCO2017/VOC2007+VOC2012 datasets.Image Classification/Object Detection.Include ResNet/EfficientNet/VovNet/DarkNet/RegNet/RetinaNet/FCOS/CenterNet/YOLOv3.
Stars: ✭ 130 (+165.31%)
Mutual labels:  imagenet, resnet
Imagenet resnet tensorflow2.0
Train ResNet on ImageNet in Tensorflow 2.0; ResNet 在ImageNet上完整训练代码
Stars: ✭ 42 (-14.29%)
Mutual labels:  imagenet, resnet
Fusenet
Deep fusion project of deeply-fused nets, and the study on the connection to ensembling
Stars: ✭ 230 (+369.39%)
Mutual labels:  imagenet, resnet
Classification models
Classification models trained on ImageNet. Keras.
Stars: ✭ 938 (+1814.29%)
Mutual labels:  imagenet, resnet
Caffe Model
Caffe models (including classification, detection and segmentation) and deploy files for famouse networks
Stars: ✭ 1,258 (+2467.35%)
Mutual labels:  imagenet, resnet
Pytorch2keras
PyTorch to Keras model convertor
Stars: ✭ 676 (+1279.59%)
Mutual labels:  imagenet, resnet
Ir Net
This project is the PyTorch implementation of our accepted CVPR 2020 paper : forward and backward information retention for accurate binary neural networks.
Stars: ✭ 119 (+142.86%)
Mutual labels:  imagenet, resnet
pytorch2keras
PyTorch to Keras model convertor
Stars: ✭ 788 (+1508.16%)
Mutual labels:  imagenet, resnet
Cnn Models
ImageNet pre-trained models with batch normalization for the Caffe framework
Stars: ✭ 355 (+624.49%)
Mutual labels:  imagenet, resnet
Imagenet
This implements training of popular model architectures, such as AlexNet, ResNet and VGG on the ImageNet dataset(Now we supported alexnet, vgg, resnet, squeezenet, densenet)
Stars: ✭ 126 (+157.14%)
Mutual labels:  imagenet, resnet
Octconv.pytorch
PyTorch implementation of Octave Convolution with pre-trained Oct-ResNet and Oct-MobileNet models
Stars: ✭ 229 (+367.35%)
Mutual labels:  imagenet, resnet

PyTorch-LMDB

Scripts to work with LMDB + PyTorch for Imagenet training

NOTE: This has only been tested in the NGC PyTorch 19.11-py3 container

Other environments have not been tested

Much of this code and LMDB documentation was adopted from https://github.com/Lyken17/Efficient-PyTorch, so credits to @Lyken17.

Quickstart

  1. Start interactive PyTorch container
nvidia-docker run -it -v ${PWD}:/mnt -v /imagenet:/imagenet --workdir=/mnt nvcr.io/nvidia/pytorch:19.11-py3
  1. Convert data to LMDB format
mkdir -p train-lmdb/
python folder2lmdb.py --dataset /imagenet/train -o train-lmdb/

mkdir -p val-lmdb/
python folder2lmdb.py --dataset /imagenet/val -o val-lmdb/
  1. Run training on LMDB data
time python main.py --arch resnet50 --train train-lmdb/ --val val-lmdb/ --lmdb --epochs 2
  1. (Optional) Compare to JPEG data
time python main.py --arch resnet50 --train /imagenet/train --val /imagenet/val --epochs 2

Multi-processing Distributed Data Parallel Training

You should always use the NCCL backend for multi-processing distributed training since it currently provides the best distributed training performance.

Single node, multiple GPUs:

JPEG

python main.py -a resnet50 --dist-url 'tcp://127.0.0.1:9999' --dist-backend 'nccl' --multiprocessing-distributed --world-size 1 --rank 0 --train /imagenet/train --val /imagenet/val

LMDB

  • NOTE: Since LMDB can't be pickled, you need to hack the folder2lmdb.ImageFolderLMDB to delay the loading of the environment, such as below:
class ImageFolderLMDB(data.Dataset):
    def __init__(self, db_path, transform=None, target_transform=None):
        # https://github.com/chainer/chainermn/issues/129
	# Delay loading LMDB data until after initialization to avoid "can't pickle Environment Object error"
        self.env = None
	
	# Workaround to have length from the start for ImageNet since we don't have LMDB at initialization time
	if 'train' in self.db_path:
            self.length = 1281167
        elif 'val' in self.db_path:
            self.length = 50000
        else:
            raise NotImplementedError
	...
	
    def _init_db(self):
        self.env = lmdb.open(self.db_path, subdir=os.path.isdir(self.db_path),
                             readonly=True, lock=False,
                             readahead=False, meminit=False)
        with self.env.begin(write=False) as txn:
            # self.length = txn.stat()['entries'] - 1
            self.length = pa.deserialize(txn.get(b'__len__'))
            self.keys = pa.deserialize(txn.get(b'__keys__'))

    def __getitem__(self, index):
        # Delay loading LMDB data until after initialization: https://github.com/chainer/chainermn/issues/129
        if self.env is None:
            self._init_db()
	...

Now we can launch LMDB version with torch.multiprocessing using above workaround:

python main.py -a resnet50 --dist-url 'tcp://127.0.0.1:9999' --dist-backend 'nccl' --multiprocessing-distributed --world-size 1 --rank 0 --train /imagenet/train-lmdb --val /imagenet/val-lmdb --lmdb

LMDB

LMDB is a json-like, but in binary stream key-value storage. In my design, the format of converted LMDB is defined as follow.

key value
img-id1 (jpeg_raw1, label1)
img-id2 (jpeg_raw2, label2)
img-id3 (jpeg_raw3, label3)
... ...
img-idn (jpeg_rawn, labeln)
__keys__ [img-id1, img-id2, ... img-idn]
__len__ n

As for details of reading/writing, please refer to code.

LMDB Dataset / DataLoader

folder2lmdb.py has an implementation of a PyTorch ImageFolder for LMDB data to be passed into the torch.utils.data.DataLoader.

In main.py, passing the --lmdb flag specifies to use folder2lmdb.ImageFolderLMDB instead of the default torchvision.datasets.ImageFolder when setting up the data.

# Data loading code
if args.lmdb:
    import folder2lmdb
    image_folder = folder2lmdb.ImageFolderLMDB
else:
    image_folder = datasets.ImageFolder

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
				 std=[0.229, 0.224, 0.225])

train_dataset = image_folder(
    args.train,
    transforms.Compose([
	transforms.RandomResizedCrop(224),
	transforms.RandomHorizontalFlip(),
	transforms.ToTensor(),
	normalize,
    ]))

val_dataset = image_folder(
    args.val, 
    transforms.Compose([
	transforms.Resize(256),
	transforms.CenterCrop(224),
	transforms.ToTensor(),
	normalize,
    ]))

ImageFolderLMDB can be simply used in place of ImageFolder like below:

from folder2lmdb import ImageFolderLMDB
from torch.utils.data import DataLoader
dataset = ImageFolderLMDB(path, transform, target_transform)
loader = DataLoader(dataset, batch_size=64)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].