Unity/Barracuda & Sentis

[Unity Sentis 튜토리얼] Unity Sentis 소개 및 설치 + AdaIN 샘플 소개

pnltoen 2023. 11. 3.
반응형

Barrcuda → Sentis

Unity Technologies

 


서론

 

기존에 2가지 포스팅을 진행하면서, Barracuda 패키지의 설치 방법을 알아보았습니다. 

 

[Unity Barracuda] 유니티 바라쿠다 튜토리얼 (StyleTransfer-AdaIN)

Barracuda Unity Technologies 서론 많이 늦어졌지만 Unity Barracuda를 실습해보고자 합니다. 기존 사내에 하늘 같은 그 분께서 AdaIN을 사용하시는 것을 보여주셨는데 해당 내용을 토대로 내용을 진행해보겠

pnltoen.tistory.com

 

그 이 후 튜토리얼을 만드는 것 보다 하나의 샘플이 있으면 좋다는 생각을 하게 되었고 Unity Technologies Korea의 Senior Software Engineer이신 Sky Kim님의 도움을 받아 AdaIN 예제를 블로깅 하였습니다. 자세한 내용은 Github Repo를 확인해주시길 바랍니다.

 

[Unity Barracuda] 유니티 바라쿠다 튜토리얼 (StyleTransfer-AdaIN)

Barracuda Unity Technologies 서론 많이 늦어졌지만 Unity Barracuda를 실습해보고자 합니다. 기존 사내에 하늘 같은 그 분께서 AdaIN을 사용하시는 것을 보여주셨는데 해당 내용을 토대로 내용을 진행해보겠

pnltoen.tistory.com

 

GitHub - pnltoen/Barracuda_AdaIN

Contribute to pnltoen/Barracuda_AdaIN development by creating an account on GitHub.

github.com

 

오늘은 Unity의 Sentis와 관련해서 오해를 잡아보고, 사용방법에 대해 알아보고자 합니다.

 

Unity Industry Summit

 

지난 10월 진행한 유니티 APAC Industry Summit 행사에서 Unity Sentis 데모가 있었습니다.

 

Unity Senior Machine Learning Engineer the Sky Kim (한얼 킴) 너무 멋지십니다... 개인적으로 데모 너무 좋았지만... 걱정한 부분이 Sentis는 Onnx를 Inference 하는 엔진인데... 대화하고 답변을 받는 데모로 오해하실까봐... 걱정이 되었습니당...

 

본론

 

Sentis 소개

유튜브에 Unity Sentis를 검색해보면 "Unity AI와 함께 열리는 새로운 가능성"이라는 영상이 있습니다.

 

해당 영상을 보면, "Sentis를 통해서 NPC가 이야기를 할 수 있고 내가 한 말을 이해할 수 있는건가?" 라고 생각할 수 있지만... 기본적으로 Microsoft의 onnx를 Inference 할 수 있다는 점에서 동일합니다.

 

라고... 이야기 하면 이해가 어려워하셔서... 간단한 예시를 들어보도록 하겠습니다.

 

유명한 파이토치의 모델 저장하기 & 불러오기를 참고해봅시다.

 

모델 저장하기 & 불러오기

Author: Matthew Inkawhich, 번역: 박정환, 김제필,. 이 문서에서는 PyTorch 모델을 저장하고 불러오는 다양한 방법을 제공합니다. 이 문서 전체를 다 읽는 것도 좋은 방법이지만, 필요한 사용 예의 코드만

tutorials.pytorch.kr

 

학습한 모델을 저장하고 추론하는 방법에 대해서 알려주는 문서입니다. 우리가 문서를 작성할 때 한글 (.hwp) 워드 (.word)와 같이 많은 편집툴을 사용하지만 공용 포맷이라는 개념을 들어보신 적 있으실겁니다. 바로 .pdf 와 같은 포맷입니다. 대부분의 편집기는 pdf로 내보내는 기능을 지원하고 이를 통해 pdf 뷰어 기능만 있으면 해당 문서를 쉽게 열어볼 수 있습니다.

 

마찬가지로 모델에도 공통된 포맷이 있습니다 (정확하게 말하면 general formats) 이 중 하나라 Windows Machine Learning에서 사용할 수 있는 ONNX (Open Neural Network Exchange)입니다.

 

ONNX 모델

Windows ML은 ONNX 형식의 모델을 평가하기 때문에 다양한 ML 프레임워크와 도구 간에 모델을 교환할 수 있습니다.

learn.microsoft.com

 

여기까지 이해가 어려우시다면 Machine Learning에도 문서 작성의 pdf와 같이 Onnx라는 공용된 포맷이 있고 유니티는 이 포맷을 추론 (Inference)할 수 있도록 Sentis(= 구 Barracuda) 패키지를 제공한다는 점입니다.

 

Sentis로 업그레이드 해야하는 이유

본 내용은 Sentis 문서의 What's new in Sentis 1.0을 참고하였습니다.

 

What's new in Sentis 1.0 | Sentis | 1.0.0-exp.6

What's new in Sentis 1.0 Sentis is the new name for the Barracuda package. This is a summary of the changes from Barracuda 3.0 to Sentis 1.0. For information on how to upgrade, refer to the Upgrade Guide. Added You can now override model layers in your cod

docs.unity3d.com

1.0 개선 사항

  • Sentis는 이제 Custom Layer를 지원합니다.
  • GPU와 CPU에서 성능 개선
  • Onnx 지원 범위 개선
  • 2gb 이상의 모델을 Inference 할 수 있도록 개선

1.1 개선 사항

  • GPUPixel 백엔드가 TensorInts와 다른 Operators를 지원합니다.
  • StreamingAssets으로 모델을 내보낼 수 있습니다.
  • 비동기 GPU 텐서와 콜백을 위한 인터페이스
  • 소수점 및 텐서 슬라이스를 위한 Ops 유틸리티 개선

1.2 개선 사항

  • LayerNoramlization, Gelu, Resize1d, Upsample, Onehot Opersators 지원
  • serizlied assets 개선
  • ScalarMad 레이어 및 Operator에 대한 최적화

그 외 문서 확인 사항 

  • You can import most ONNX model files with an opset version between 7 and 15

 

Sentis 설치 및 업그레이드 튜토리얼

현재 Unity Registry를 통한 설치 방법은 확인하지 못하였다. 따라서 Package Manager - Add package by name - com.unity.sentis을 통해 설치를 진행한다.

Barracuda를 삭제하고 Sentis를 설치하니 네임스페이스 관련 오류가 나타납니다. 차례차례 진행해보도록 하겠습니다.

 

변경사항과 관련해서 하나하나 정리를 하고자 하였으나, 생각보다 양이 조금 더 있어서 수정사항을 아래와 같이 기재하였습니다.

 

 

  • NameSpace 변경
    • using Unity.Barracuda; => using Unity.Sentis; 
  • Onnx 변수명 변경
    • NNModel => ModelAssets 
  • Worker Type 변경 (Backend Type 참고)
    • CPU 사용 시 =>  WorkerFactory.Type.ComputePrecompiled => WorkerFactory.CreateWorker(BackendType.CPU, m_RuntimeModel)
 

Create an engine to run a model | Sentis | 1.2.0-exp.2

Create an engine to run a model To run a model, create a worker. A worker is the engine that breaks the model down into executable tasks and schedules the tasks to run on the GPU or CPU. A worker is an instance of an IWorker object. Create a Worker Use Wor

docs.unity3d.com

 

  • Texture -> Tensor 변환
    • (Barracuda) Tensor & Channel 예 : Tensor _t = new Tensor(Texture, 3);
    • (Sentis) TextureConverter 사용 예: TensorFloat or Tensor _t = TextureConverter.ToTensor(texture, width, height, channel) 
  • Tensor -> Texture 변환
    • (Barracuda) output.ToRenderTexture(result);
    • (Sentis) TextureConverter.RenderToTexture(t_output, result)

 

Github Sample

개인 Github에 해당 내용을 업데이트 하였습니다.

 

GitHub - pnltoen/Unity_Sentis_AdaIN

Contribute to pnltoen/Unity_Sentis_AdaIN development by creating an account on GitHub.

github.com

관련해서 궁금하신 사항이 있으실 경우 편하게 댓글 남겨주시면 감사하겠습니다.

반응형

댓글