Package: resnet-pytorch
ResNet-PyTorchUpdate (Feb 20, 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:fromresnet_pytorchimportResNetmodel=ResNet.from_pretrained('resnet18',num_classes=10)Update (February 2, 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.OverviewThis repository contains an op-for-op PyTorch reimplementation ofDeep Residual Learning for Image Recognition.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 ResNet modelsUse ResNet models for classification or feature extractionUpcoming features: In the next few days, you will be able to:Quickly finetune an ResNet on your own datasetExport ResNet models for productionTable of contentsAbout ResNetInstallationUsageLoad pretrained modelsExample: ClassifyExample: Extract featuresExample: Export to ONNXExample: VisualContributingAbout ResNetIf you're new to ResNets, here is an explanation straight from the official PyTorch implementation:Resnet models were proposed in "Deep Residual Learning for Image Recognition". Here we have the 5 versions of resnet models,
which contains 5, 34, 50, 101, 152 layers respectively. Detailed model architectures can be found in Table 1.InstallationInstall from pypi:$pip3installresnet_pytorchInstall from source:$gitclonehttps://github.com/Lornatang/ResNet-PyTorch.git
$cdResNet-PyTorch
$pip3install-e.UsageLoading pretrained modelsLoad an resnet18 network:fromresnet_pytorchimportResNetmodel=ResNet.from_name("resnet18")Load a pretrained resnet18:fromresnet_pytorchimportResNetmodel=ResNet.from_pretrained("resnet18")Their 1-crop error rates on imagenet dataset with pretrained models are listed below.Model structureTop-1 errorTop-5 errorresnet1830.2410.92resnet3426.708.58resnet5023.857.13resnet10122.636.44resnet15221.695.94Option B of resnet-18/34/50/101/152 only uses projections to increase dimensions.For results extending to the cifar10 dataset, seeexamples/cifarExample: 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.transformsastransformsfromPILimportImagefromresnet_pytorchimportResNet# 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 ResNet18model=ResNet.from_pretrained("resnet18")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:importtorchfromresnet_pytorchimportResNetmodel=ResNet.from_pretrained('resnet18')# ... 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, 1, 1])Example: Export to ONNXExporting to ONNX for deploying to production is now simple:importtorchfromresnet_pytorchimportResNetmodel=ResNet.from_pretrained('resnet18')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:10004/.Enjoy it.ImageNetSeeexamples/imagenetfor details about evaluating on ImageNet.CreditDeep Residual Learning for Image RecognitionKaiming He, Xiangyu Zhang, Shaoqing Ren, Jian SunAbstractDeeper neural networks are more difficult to train. We
present a residual learning framework to ease the training
of networks that are substantially deeper than those used
previously. We explicitly reformulate the layers as learning residual functions with reference to the layer inputs, instead of learning unreferenced functions. We provide comprehensive empirical evidence showing that these residual
networks are easier to optimize, and can gain accuracy from
considerably increased depth. On the ImageNet dataset we
evaluate residual nets with a depth of up to 152 layers—8×
deeper than VGG nets [41] but still having lower complexity. An ensemble of these residual nets achieves 3.57% error
on the ImageNet test set. This result won the 1st place on the
ILSVRC 2015 classification task. We also present analysis
on CIFAR-10 with 100 and 1000 layers.
The depth of representations is of central importance
for many visual recognition tasks. Solely due to our extremely deep representations, we obtain a 28% relative improvement on the COCO object detection dataset. Deep
residual nets are foundations of our submissions to ILSVRC
& COCO 2015 competitions1
, where we also won the 1st
places on the tasks of ImageNet detection, ImageNet localization, COCO detection, and COCO segmentation.papercode@article{He2015,
	author = {Kaiming He and Xiangyu Zhang and Shaoqing Ren and Jian Sun},
	title = {Deep Residual Learning for Image Recognition},
	journal = {arXiv preprint arXiv:1512.03385},
	year = {2015}
}