|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import onnxruntime |
|
import argparse |
|
from PIL import Image |
|
import torchvision.transforms as transforms |
|
|
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('--onnx_path', type=str, default="EfficientNet_int.onnx", required=False) |
|
parser.add_argument('--image_path', type=str, required=True) |
|
parser.add_argument( |
|
"--ipu", |
|
action="store_true", |
|
help="Use IPU for inference.", |
|
) |
|
parser.add_argument( |
|
"--provider_config", |
|
type=str, |
|
default="vaip_config.json", |
|
help="Path of the config file for seting provider_options.", |
|
) |
|
parser.add_argument('--data_format', type=str, choices=["nchw", "nhwc"], default="nchw") |
|
args = parser.parse_args() |
|
|
|
|
|
def read_image(): |
|
|
|
image = Image.open(args.image_path) |
|
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) |
|
|
|
transform = transforms.Compose([ |
|
transforms.ToTensor(), |
|
transforms.Resize((224, 224)), |
|
normalize, |
|
]) |
|
img_tensor = transform(image).unsqueeze(0) |
|
if args.data_format == "nhwc": |
|
img_tensor = transform(image).unsqueeze(0).permute((0, 2, 3, 1)) |
|
return img_tensor.numpy() |
|
|
|
|
|
def main(): |
|
if args.ipu: |
|
providers = ["VitisAIExecutionProvider"] |
|
provider_options = [{"config_file": args.provider_config}] |
|
else: |
|
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] |
|
provider_options = None |
|
ort_session = onnxruntime.InferenceSession( |
|
args.onnx_path, providers=providers, provider_options=provider_options) |
|
ort_inputs = { |
|
ort_session.get_inputs()[0].name: read_image() |
|
} |
|
output = ort_session.run(None, ort_inputs)[0] |
|
print("class id =", output[0].argmax()) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|