Package: vgg-pytorch
VGGNet-PyTorchUpdate (Feb 14, 2020)The update is for ease of use and deployment.Example: Export to ONNXExample: Extract featuresExample: VisualIt is also now incredibly simple to load a pretrained model with a new number of classes for transfer learning:fromvgg_pytorchimportVGGmodel=VGG.from_pretrained('vgg11',num_classes=10)Update (January 15, 2020)This update allows you to use NVIDIA's Apex tool for accelerated training. By default choicehybrid training precision+dynamic loss amplifiedversion, if you need to learn more and details aboutapextools, please visithttps://github.com/NVIDIA/apex.Update (January 9, 2020)This update adds a visual interface for testing, which is developed by pyqt5. At present, it has realized basic functions, and other functions will be gradually improved in the future.Update (January 6, 2020)This update adds a modular neural network, making it more flexible in use. It can be deployed to many common dataset classification tasks. Of course, it can also be used in your products.OverviewThis repository contains an op-for-op PyTorch reimplementation ofVGGNet.The goal of this implementation is to be simple, highly extensible, and easy to integrate into your own projects. This implementation is a work in progress -- new features are currently being implemented.At the moment, you can easily:Load pretrained VGGNet modelsUse VGGNet models for classification or feature extractionUpcoming features: In the next few days, you will be able to:Quickly finetune an VGGNet on your own datasetExport VGGNet models for productionTable of contentsAbout VGGInstallationUsageLoad pretrained modelsExample: ClassifyExample: Extract featuresExample: Export to ONNXExample: VisualContributingAbout VGGIf you're new to VGGNets, here is an explanation straight from the official PyTorch implementation:In this work we investigate the effect of the convolutional network depth on its
accuracy in the large-scale image recognition setting. Our main contribution is
a thorough evaluation of networks of increasing depth using an architecture with
very small (3 × 3) convolution filters, which shows that a significant improvement
on the prior-art configurations can be achieved by pushing the depth to 16–19
weight layers. These findings were the basis of our ImageNet Challenge 2014
submission, where our team secured the first and the second places in the localisation and classification tracks respectively. We also show that our representations
generalise well to other datasets, where they achieve state-of-the-art results. We
have made our two best-performing ConvNet models publicly available to facilitate further research on the use of deep visual representations in computer vision.InstallationInstall from pypi:$pip3installvgg_pytorchInstall from source:$gitclonehttps://github.com/Lornatang/VGGNet-PyTorch.git
$cdVGGNet-PyTorch
$pip3install-e.UsageLoading pretrained modelsLoad an vgg11 network:fromvgg_pytorchimportVGGmodel=VGG.from_name("vgg11")Load a pretrained vgg11:fromvgg_pytorchimportVGGmodel=VGG.from_pretrained("vgg11")Their 1-crop error rates on imagenet dataset with pretrained models are listed below.Model structureTop-1 errorTop-5 errorvgg1130.9811.37vgg11_bn29.7010.19vgg1330.0710.75vgg13_bn28.459.63vgg1628.419.62vgg16_bn26.638.50vgg1927.629.12vgg19_bn25.768.15Details about the models are below (for CIFAR10 dataset):Name# ParamsTop-1 Acc.Pretrained?vgg11132.9M91.1√vgg13133M92.8√vgg16138.4M92.6√vgg19143.7M92.3√------------------------------------------------------vgg11_bn132.9M92.2√vgg13_bn133M94.2√vgg16_bn138.4M93.9√vgg19_bn143.7M93.7√Example: ClassificationWe assume that in your current directory, there is aimg.jpgfile and alabels_map.txtfile (ImageNet class names). These are both included inexamples/simple.All pre-trained models expect input images normalized in the same way,
i.e. mini-batches of 3-channel RGB images of shape(3 x H x W), whereHandWare expected to be at least224.
The images have to be loaded in to a range of[0, 1]and then normalized usingmean = [0.485, 0.456, 0.406]andstd = [0.229, 0.224, 0.225].Here's a sample execution.importjsonimporttorchimporttorchvision.transformsastransformsfromPILimportImagefromvgg_pytorchimportVGG# Open imageinput_image=Image.open("img.jpg")# Preprocess imagepreprocess=transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225]),])input_tensor=preprocess(input_image)input_batch=input_tensor.unsqueeze(0)# create a mini-batch as expected by the model# Load class nameslabels_map=json.load(open("labels_map.txt"))labels_map=[labels_map[str(i)]foriinrange(1000)]# Classify with VGG11model=VGG.from_pretrained("vgg11")model.eval()# move the input and model to GPU for speed if availableiftorch.cuda.is_available():input_batch=input_batch.to("cuda")model.to("cuda")withtorch.no_grad():logits=model(input_batch)preds=torch.topk(logits,k=5).indices.squeeze(0).tolist()print("-----")foridxinpreds:label=labels_map[idx]prob=torch.softmax(logits,dim=1)[0,idx].item()print(f"{label:<75}({prob*100:.2f}%)")Example: Feature ExtractionYou can easily extract features withmodel.extract_features:importtorchfromvgg_pytorchimportVGGmodel=VGG.from_pretrained('vgg11')# ... image preprocessing as in the classification example ...inputs=torch.randn(1,3,224,224)print(inputs.shape)# torch.Size([1, 3, 224, 224])features=model.extract_features(inputs)print(features.shape)# torch.Size([1, 512, 7, 7])Example: Export to ONNXExporting to ONNX for deploying to production is now simple:importtorchfromvgg_pytorchimportVGGmodel=VGG.from_pretrained('vgg11')dummy_input=torch.randn(16,3,224,224)torch.onnx.export(model,dummy_input,"demo.onnx",verbose=True)Example: Visualcd $REPO$/framework
sh start.shThen open the browser and type in the browser addresshttp://127.0.0.1:8000/.Enjoy it.ImageNetSeeexamples/imagenetfor details about evaluating on ImageNet.For more datasets result. Please seeresearch/README.md.ContributingIf you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions, simply post them as GitHub issues.I look forward to seeing what the community does with these models!CreditVery Deep Convolutional Networks for Large-Scale Image RecognitionKaren Simonyan, Andrew ZissermanAbstractIn this work we investigate the effect of the convolutional network depth on its accuracy in the
large-scale image recognition setting. Our main contribution is a thorough evaluation of networks
of increasing depth using an architecture with very small (3x3) convolution filters, which shows
that a significant improvement on the prior-art configurations can be achieved by pushing the depth
to 16-19 weight layers. These findings were the basis of our ImageNet Challenge 2014 submission,
where our team secured the first and the second places in the localisation and classification tracks
respectively. We also show that our representations generalise well to other datasets, where they
achieve state-of-the-art results. We have made our two best-performing ConvNet models publicly
available to facilitate further research on the use of deep visual representations in computer vision.paper@article{VGG,
title:{Very Deep Convolutional Networks for Large-Scale Image Recognition},
author:{Karen Simonyan, Andrew Zisserman},
journal={iclr},
year={2015}
}