Skip to content

Commit

Permalink
[Nano] Update Trainer -> InferenceOptimizer for Related Examples (#5781)
Browse files Browse the repository at this point in the history
* Update Trainer to InferenceOptimizer for related examples

* Diasble unrelated nano tests

* Enable unrelated nano tests again
  • Loading branch information
Oscilloscope98 authored Sep 15, 2022
1 parent 89423f1 commit ad4869e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
12 changes: 6 additions & 6 deletions python/nano/tutorial/inference/pytorch/pytorch_inference_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# pip install onnx onnxruntime
# ```

import torch
import torch
from torchvision.models import resnet18

Expand All @@ -35,17 +34,18 @@
predictions = y_hat.argmax(dim=1)
print(predictions)

# Accelerated Inference Using ONNX Runtime
from bigdl.nano.pytorch import Trainer
ort_model = Trainer.trace(model_ft,
accelerator="onnxruntime",
input_sample=torch.rand(1, 3, 224, 224))
# Accelerated Inference Using ONNXRuntime
from bigdl.nano.pytorch import InferenceOptimizer
ort_model = InferenceOptimizer.trace(model_ft,
accelerator="onnxruntime",
input_sample=torch.rand(1, 3, 224, 224))

y_hat = ort_model(x)
predictions = y_hat.argmax(dim=1)
print(predictions)

# Save Optimized Model
from bigdl.nano.pytorch import Trainer
Trainer.save(ort_model, "./optimized_model")

# Load the Optimized Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# pip install openvino-dev
# ```

import torch
import torch
from torchvision.models import resnet18

Expand All @@ -35,15 +34,16 @@
print(predictions)

# Accelerated Inference Using OpenVINO
from bigdl.nano.pytorch import Trainer
ov_model = Trainer.trace(model_ft,
accelerator="openvino",
input_sample=torch.rand(1, 3, 224, 224))
from bigdl.nano.pytorch import InferenceOptimizer
ov_model = InferenceOptimizer.trace(model_ft,
accelerator="openvino",
input_sample=torch.rand(1, 3, 224, 224))
y_hat = ov_model(x)
predictions = y_hat.argmax(dim=1)
print(predictions)

# Save Optimized Model
from bigdl.nano.pytorch import Trainer
Trainer.save(ov_model, "./optimized_model")

# Load the Optimized Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from torchvision import transforms
from torchvision.datasets import OxfordIIITPet
from torch.utils.data.dataloader import DataLoader
import torch
from torchvision.models import resnet18
from bigdl.nano.pytorch import Trainer
from torchmetrics import Accuracy
Expand Down Expand Up @@ -93,7 +92,9 @@ def finetune_pet_dataset(model_ft):
print(predictions)

# Static Quantization for PyTorch
q_model = Trainer.quantize(model, calib_dataloader=DataLoader(train_dataset, batch_size=32))
from bigdl.nano.pytorch import InferenceOptimizer
q_model = InferenceOptimizer.quantize(model,
calib_dataloader=DataLoader(train_dataset, batch_size=32))

# Inference with Quantized Model
y_hat = q_model(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from torchvision import transforms
from torchvision.datasets import OxfordIIITPet
from torch.utils.data.dataloader import DataLoader
import torch
from torchvision.models import resnet18
from bigdl.nano.pytorch import Trainer
from torchmetrics import Accuracy
Expand Down Expand Up @@ -94,9 +93,10 @@ def finetune_pet_dataset(model_ft):
print(predictions)

# Static Quantization for ONNX
q_model = Trainer.quantize(model,
accelerator='onnxruntime',
calib_dataloader=DataLoader(train_dataset, batch_size=32))
from bigdl.nano.pytorch import InferenceOptimizer
q_model = InferenceOptimizer.quantize(model,
accelerator='onnxruntime',
calib_dataloader=DataLoader(train_dataset, batch_size=32))

# Inference with Quantized Model
y_hat = q_model(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from torchvision import transforms
from torchvision.datasets import OxfordIIITPet
from torch.utils.data.dataloader import DataLoader
import torch
from torchvision.models import resnet18
from bigdl.nano.pytorch import Trainer
from torchmetrics import Accuracy
Expand Down Expand Up @@ -93,9 +92,10 @@ def finetune_pet_dataset(model_ft):
print(predictions)

# Static Quantization for OpenVINO
q_model = Trainer.quantize(model,
accelerator='openvino',
calib_dataloader=DataLoader(train_dataset, batch_size=32))
from bigdl.nano.pytorch import InferenceOptimizer
q_model = InferenceOptimizer.quantize(model,
accelerator='openvino',
calib_dataloader=DataLoader(train_dataset, batch_size=32))

# Inference with Quantized Model
y_hat = q_model(x)
Expand Down

0 comments on commit ad4869e

Please sign in to comment.