Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Nano] Update Trainer -> InferenceOptimizer for Related Examples #5781

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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