꾸준희
Enough is not enough
꾸준희

공지사항

  • 꾸준희 블로그
전체 방문자
2,415,845
오늘
1,788
어제
2,910
  • 분류 전체보기 (592)
    • Book Review (39)
    • Paper Review (23)
    • AI Research Topic (127)
      • Deep Learning (24)
      • Pose Estimation (29)
      • Object Detection (22)
      • Object Segmentation (3)
      • Object Tracking (11)
      • Video Surveillance (4)
      • Action Recognition (6)
      • Stereo Vision (6)
      • 3D Reconstruction (5)
      • Machine Vision (2)
      • Image Processing (11)
      • Dataset (4)
    • AI Development (77)
      • NVIDIA DeepStream (3)
      • NVIDIA TensorRT (30)
      • NVIDIA TAO Toolkit (2)
      • ONNX (9)
      • PyTorch (5)
      • TensorFlow (15)
      • TensorFlow Lite (1)
      • GPU | CUDA | PyCUDA (12)
    • Programming (147)
      • GStreamer | FFmpeg (6)
      • Python (27)
      • C | C++ (15)
      • OpenCV (34)
      • Linux (36)
      • Embedded linux (7)
      • Etc. (22)
    • Computer Science (64)
      • 학부 및 대학원 과목 (22)
      • 선형대수학 및 기타 수학 (9)
      • SQL-D (33)
    • 삽질 기록 (50)
    • 생각 (15)
    • 기타 (50)
      • 참고자료 (30)
      • 좋은 글 (5)
      • 티스토리 (2)
      • 논문 작성 관련 참고 (10)
      • 메모 (0)

블로그 메뉴

  • 👀 CV
  • 🌸 GitHub
  • 💌 LinkedIn
  • 📚 방명록

최근 댓글

  • 넵 저도 여기다 써놓고 두고두⋯
    꾸준희
  • 되게 헷갈리고 볼때마다 찾던⋯
    옐로우씨
  • 아뇨 ㅠ 저도 어디서 가져온거⋯
    꾸준희
  • 앗 감사합니다 🙇🏻‍♀️
    꾸준희
  • 항상 보면서 존경스럽습니다!⋯
    버터미소
08-18 16:12

티스토리

hELLO · Designed By 정상우.
꾸준희

AI Development/ONNX

[ONNX] ONNX 배치 사이즈 변경하는 방법 + 삽질

2021. 2. 2. 14:22
728x90
반응형

 

onnx batch size change 라는 키워드를 통해 나온 검색 결과에 의하면,

다음 코드를 사용하면 기존에 만들어 두었던 onnx 파일의 배치 사이즈를 변경할 수 있다고 하는데, 

아래 코드는 입력 사이즈만 변경하는 코드이다. 절대 아래와 같이 사용하면 안된다. 

import onnx

def change_input_dim(model):
    # Use some symbolic name not used for any other dimension
    sym_batch_dim = "N"
    # or an actal value
    actual_batch_dim = 4 

    # The following code changes the first dimension of every input to be batch-dim
    # Modify as appropriate ... note that this requires all inputs to
    # have the same batch_dim 
    inputs = model.graph.input
    for input in inputs:
        # Checks omitted.This assumes that all inputs are tensors and have a shape with first dim.
        # Add checks as needed.
        dim1 = input.type.tensor_type.shape.dim[0]
        # update dim to be a symbolic value
        dim1.dim_param = sym_batch_dim
        # or update it to be an actual value:
        # dim1.dim_value = actual_batch_dim


def apply(transform, infile, outfile):
    model = onnx.load(infile)
    transform(model)
    onnx.save(model, outfile)

apply(change_input_dim, r"input-file-name, r"output-file-name")

 

이는 입력 배치 사이즈는 변경해주지만, 출력 배치 사이즈는 변경하지 않는다. 실제로 netron 을 사용하여 그래프를 확인해보면 출력 단에서는 배치 사이즈가 변경 되지 않았음을 알 수 있다. 

 

즉, 입출력에 예민한 TensorRT에서 배치 사이즈를 1이 아닌 값으로 정적(no Dynamic)으로 고정하여 엔진을 생성할 때는

위와 같은 방법으로 생성된 onnx 모델을 사용하면 절대 안되므로, 

onnx.export 를 사용하여 원하는 배치 사이즈를 가지는 텐서 값을 통해 정상적으로 onnx 모델을 생성하여 TensorRT을 생성 해야한다. 

저 코드를 알려준 저자에게 -1 눌러주고 왔다....

 

 

관련 이슈 1 : github.com/onnx/keras-onnx/issues/605

 

Explicitly set batch size of generate ONNX model · Issue #605 · onnx/keras-onnx

Hello, Is there a way to explicitly set batch size of onnx model starting from a tf.keras model? I have a tf.keras model with (None, 256, 256, 1) input shape, when converted to onnx input shape bec...

github.com

관련 이슈 2: github.com/onnx/onnx/issues/2182

 

Changing Batch SIze · Issue #2182 · onnx/onnx

I have an onnx model. I just want to change the batch size of the model. I have attached an image of a single node of the graph. The first one shows batch size = 1 and the second one shows batch si...

github.com

 

728x90
반응형
저작자표시비영리
  • 카카오스토리
  • 트위터
  • 페이스북

'AI Development > ONNX' 카테고리의 다른 글

[ONNX] ONNX Simplifier 사용하여 모델 간소화 하기  (8) 2021.07.26
[ONNX] Pytorch 모델을 ONNX 모델로 변환 할 때 dynamic_axes 지정하는 방법  (0) 2021.06.29
[ONNX] ONNX 배치 사이즈 변경하는 방법 + 삽질  (3) 2021.02.02
[ONNX] Pytorch 모델을 ONNX 모델로 변환하기  (4) 2020.08.23
[ONNX] onnx-graphsurgeon 이용하여 plugin 사용하기 - Group Normalization  (3) 2020.07.21
[ONNX] Netron : ONNX model Visualization  (0) 2020.07.16
    'AI Development/ONNX' 카테고리의 다른 글
    • [ONNX] ONNX Simplifier 사용하여 모델 간소화 하기
    • [ONNX] Pytorch 모델을 ONNX 모델로 변환 할 때 dynamic_axes 지정하는 방법
    • [ONNX] Pytorch 모델을 ONNX 모델로 변환하기
    • [ONNX] onnx-graphsurgeon 이용하여 plugin 사용하기 - Group Normalization
    꾸준희
    꾸준희
    생각과 기록 그리고 발전
    댓글쓰기
    1. 그놈 목소리
      2021.02.15 13:31 신고
      안녕하세요.

      좋은 글 잘 보고있습니다.

      저는 현재 Jetson Multimedia API를 이용해서 애플리케이션을 개발중에 있습니다.

      https://docs.nvidia.com/jetson/l4t-multimedia/l4t_mm_vid_decode_trt.html

      onnx모델을 로드해서 trt를 콜하는 예제인데요.
      도움말을 보면 only support dynamic batch(N=-1) onnx mode이라고 되어있는데요.
      샘플로 주어지는 onnx모델을 netron으로 시각화해보면
      input이름 : data
      -1, 3, 368, 640
      output 이름: layer7_cov,
      -1, 4, 23, 4
      output 이름: layer7_bbox
      -1, 16, 23, 40
      이렇게 나옵니다.

      이 dynamic batch라는 걸 implicit batch라고도하나요?
      pytorch공식문서의 onnx 변환 튜토리얼을 읽어봐도 dynamic batch로 만드는 건 안나와있더라고요.
      혹시 이미 만들어진 onnx모델을 dynamic batch로 만드는 방법이 있나요?


      감사합니다.
      수정/삭제댓글쓰기댓글보기
      1. 꾸준희
        2021.02.15 15:16 신고
        메일 답변 드렸어요 ^^
        수정/삭제
    2. 그놈 목소리
      2021.02.15 16:16 신고
      감사합니다~
      수정/삭제댓글쓰기댓글보기
    다음 글
    [ONNX] Pytorch 모델을 ONNX 모델로 변환 할 때 dynamic_axes 지정하는 방법
    이전 글
    [ONNX] Pytorch 모델을 ONNX 모델로 변환하기
    • 이전
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 다음

    티스토리툴바