NVDLA Backend Examples¶
1. Compile a Pytorch Resnet Model¶
import torchvision
from torchvision import datasets, models
import torchvision.transforms as transforms
import torch
from onnc.bench import login, Project
# Acquire the pretrained Resnet18 Model from TorchVision model zoo
# To fit the cifar10 dataset, we need to modify the fullly connected layer
model = models.resnet18(pretrained=False)
numft = model.fc.in_features
model.fc = torch.nn.Sequential(torch.nn.Linear(numft, 1000),
torch.nn.ReLU(),
torch.nn.Linear(1000, 10))
# Define preprocess steps
transform = transforms.Compose([
transforms.Pad(4),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32),
transforms.ToTensor()])
# Acquire ImageNet dataset from TorchVision Datasets
test_dataset = torchvision.datasets.CIFAR10(root='./cifar10',
train=False,
download=True,
transform=transform)
# onnc-bench also accept a DataLoader object
data_loader = torch.utils.data.DataLoader(test_dataset)
# Code for training the model
# ...
# Set the model to evaluation mode
model.eval()
# Set up for compilation
login("your_account", "your_password")
project = Project(name='experiment')
project.add_model(model=model, # feed a Pytorch model object
samples=test_dataset, # feed a Pytorch Dataset object
#samples=data_loader # or you can feed a DataLoader instead
model_inputs=[["input", (1, 3, 32, 32), float]]
)
project.compile(target='NVDLA-NV-SMALL-DEFAULT')
deployment = project.save('./output')
print(deployment.compiled_files)
Note: Due to the dynamic graph nature of Pytorch, input signatures is required.
2. Compile a Keras Resnet model¶
import keras
import tensorflow as tf
import numpy as np
from onnc.bench import login, Project
# (x_train, y_train), (x_test, y_test) = tf.keras.datasets.imagenet.load_data()
# Imagenet is not public available now.
# Please download and preprocess Imagenet dataset from:
# https://image-net.org/challenges/LSVRC/2012/
# Here we use random data to demostrate the procedure
x_test = np.random.rand(100, 224, 224, 3)
model = tf.keras.applications.resnet50.ResNet50(weights="imagenet")
# Set up for compilation
login("your_account", "your_password")
project = Project(name='experiment')
project.add_model(model=model, # feed a Keras model object
samples=x_test, # feed a Keras/Numpy Dataset object
)
project.compile(target='NVDLA-NV-SMALL-DEFAULT')
deployment = project.save('./output')
print(deployment.compiled_files)
3. Compile a ONNX Model Zoo model¶
Please download onnx model and dataset from ONNX Model Zoo
Currently ONNC supports below models:
bvlc_alexnetbvlc_googlenet
bvlc_reference_caffenet
bvlc_reference_rcnn_ilsvrc13
densenet121
inception_v1
inception_v2
resnet50
shufflenet
squeezenet
vgg19
zfnet512
Note: Please make sure the Opset version of the ONNX file you download is 8
To run below example, please download and untar Resnet50 model and calibration samples from here.
from onnc.bench import login, Project
# Set up for compilation
login("your_account", "your_password")
project = Project(name='experiment')
project.add_model(model='resnet50/model.onnx', # feed a onnx file
samples='resnet50/test_data_set_0/input_0.pb', # calibration samples
)
project.compile(target='NVDLA-NV-SMALL-DEFAULT')
deployment = project.save('./output')
print(deployment.compiled_files)