-
ONNX 무작정 따라하기Linux Basic 2023. 7. 16. 02:33
https://pytorch.org/docs/master/onnx.html#example-alexnet-from-pytorch-to-onnx
torch.onnx — PyTorch master documentation
torch.onnx ONNX exporter. Open Neural Network eXchange (ONNX) is an open standard format for representing machine learning models. The torch.onnx module can export PyTorch models to ONNX. The model can then be consumed by any of the many runtimes that supp
pytorch.org
pytorch에 onnx 예제가 있다 무조건 따라하면서 이해를 해보자!
onnx 에대한 자세한 설명은 https://adjh54.tistory.com/203 사이트에서 자세하게 잘 설명하고 있다.
onnx는 공용으로 중간다리 역할을 하는 프레임워크다. 즉 보드에서 실행할때 사용하게되거나
속도를 빠르게하기 위해서 Quantization을 하거할때 필요한 지식이다.
여기서는 간단하게 로드하고 실행하는 것만 해보면서 감을 잡는 용도로 사용함.
아래 코드는 onnx를 export 하는 코드이다.
input 이름과 output 이름을 지정해주고
onnx파일 이름도 지정해 주는것을 볼 수 있다.
에러시 conda install torchvision를 설치 해주자!import torch import torchvision dummy_input = torch.randn(10, 3, 224, 224, device="cuda") model = torchvision.models.alexnet(pretrained=True).cuda() # Providing input and output names sets the display names for values # within the model's graph. Setting these does not change the semantics # of the graph; it is only for readability. # # The inputs to the network consist of the flat list of inputs (i.e. # the values you would pass to the forward() method) followed by the # flat list of parameters. You can partially specify names, i.e. provide # a list here shorter than the number of inputs to the model, and we will # only set that subset of names, starting from the beginning. input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ] output_names = [ "output1" ] torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)
만들었으면 netron이라는 라이브러리가 있음.
해당 라이브러리로 보면됨.pip install netron netron alexnet.onnx
아래 처럼 확인 가능함.
onnx 를 사용할때는 버전 확인이 중요함.
https://onnxruntime.ai/docs/reference/compatibility.htmlCompatibility
ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
onnxruntime.ai
import onnx import numpy as np # Load the ONNX model onnx_model = onnx.load("alexnet.onnx") version = onnx_model.ir_version print("ONNX 모델 ir 버전:", version) # ONNX 모델의 버전 정보 version_info = onnx_model.opset_import[0] # 메이저 버전과 마이너 버전 추출 major_version = version_info.version minor_version = version_info.domain # ONNX 버전 출력 print(f"ONNX opset 버전: {major_version}.{minor_version}") import onnxruntime as ort # ONNX Runtime 버전 확인 version = ort.__version__ print("ONNX Runtime 버전:", version)
나의 경우는 아래처럼 나왔고
하위 호환이므로 실행이됨.
ONNX 모델 ir 버전: 7
ONNX opset 버전: 14.
ONNX Runtime 버전: 1.15.1
마지막으로 inference 하는 것을 진행함.
버전이 업데이트 되어서 아래처럼 해야 실행됨.import onnxruntime as ort providers = [ "CUDAExecutionProvider"] # ort_sess = ort.InferenceSession(model_path) ort_session = ort.InferenceSession("alexnet.onnx", providers=providers) outputs = ort_session.run( None, {"actual_input_1": np.random.randn(10, 3, 224, 224).astype(np.float32)}, ) print(outputs[0])
반응형'Linux Basic' 카테고리의 다른 글
gcc & LD_PRELOAD & 동적 라이브러리 & makefile -2 (0) 2024.04.29 gcc & 정적 라이브러리 & 동적 라이브러리 & makefile - 1 (0) 2024.04.27 cmake on linux (0) 2023.09.17 onnx docker 사용하기 (0) 2023.07.16 LD_PRELOAD (1) 2023.02.26