1. Mediapipe 란??
Mediapipe는 임의의 sensory data (e.g. images, video streams)에 대해 추론(inference)를 수행할 수 있도록하는 pipeline 구축을 위한 framework입니다. Mediapipe을 통해 Deep Learning(DL) model의 추론 pipeline을 module단위의 여러 요소들로 구성된 graph로 built하게 됩니다.
그래서 mediapipe는 mobile device나 IoT에 최적화된 deployment flow를 제공하게 되므로 on-device에 DL modeld을 deploy하고 싶으면 쓰면 좋을것 같네요.
1.1 Mediapipe 왜 써야하는가??
- End-to-End acceleration
- ML model inference 및 processing이 hardware에 최적화되어 있음
- Cross-platform support를 지원
- android, ios, desktop, web 와 같은 다양한 platform에 통합된 solution을 제공
- Computational acceleration을 위한 GPU 지원
- Image frame의 병렬적(parallel) 연산 지원
- FlowLimitCalculator 지원
- Input node와 inference node사이에서 queue에 들어오는 image frame수를 제한시킴
- 일반적으로 input node의 incoming image들이 상대적으로 queue에 많이 들어오기 때문에 이를 제한시켜 queue에 들어오는 inference node의 outcoming image수와 비슷하게 만듬
- 한 모델의 result를 다른 model의 입력으로 사용될 수 있도록 함
- input image를 받는 A라는 모델이 ouput을 내는데 해당 output이 B라는 모델의 input으로 들어가도록 함.
1.2 Mediapipe solution 종류
다음과 같이 mediapipe에서는 다양한 task의 solution을 제공하는데요. 저는 오늘 C++을 통해 object detection model을 Macbook pro에 deploy해볼겁니다. (뒤에서...ㅎ)
1.3 Mediapipe Components
mediapipe에는 다음과 같은 component들에 대한 개념을 정의하고 정의된 개념들을 통해 pipeline을 구성하게됩니다.
- Packet: 기초적인 data flow 단위
- Streams: Timestamped sequence(시간순의) of packets (e.g. video stream, audio)
- Side packets: Single packet without timestamp
- static/one-time Input (e.g. ML model, config file, ...)
- Node: Input stream 또는 input packet을 입력으로 processing하여 output stream 또는 output packet으로 결과를 냄
- processing은 data transformations, media processing, model inference 중 하나를 뜻함
- 각 node는 하나의 calculator file을 가지는데 해당 노드의 functional code임
- Graph: node들로 이루어진 network로 각 node는 computation 및 operation을 수행하고 node간에는 input과 ouput이 연결되어 있음
1.4 Mediapipe Repository Structure
mediapipe를 예제를 직접 사용해보고 하려면 repo structure이해가 좀 필요하니 봐두시면 좋습니다. 먼저 mediapipe framework는 Bazel build framework입니다. Bazel build를 통해 cross-platform 를 제공하게 된다는 사실을 알아두고 structure에서 중요한 것만 살펴보죠!
- mediapipe/calculators: calculators 집합이며 data transformations, processing, inference에 대한 code script가 저장됨
- 모든 files들은 기본적으로 C++로 코딩되어있으며 .proto file을 통해 node option을 전달함
- mediapipe/examples: android, IOS, coral, desktop build를 위한 project example을 담고 있음
- mediapipe/framework: input streams의 workflow, node creation, graph의 creation 및 verification을 담당
- mediapipe/gpu: GPU acceleration을 위한 calculator들을 포함
- mediapipe/grpahs: mediapipe의 graph와 BUILD파일를 담고 있음
- graph 파일은 .pbtxt 확장자를 가지며 pipeline workflow를 작성함
- BUILD 파일은 bazel operation과 dependencies (calculators, model)를 작성함
- mediapipe/models: examples에서 사용되는 tflite모델을 담고있음
- third_party: mediapipe build에 필요한 Tensorflow, OpenCV 등의 third party 의 BUILD 파일을 담고있음
- BUILD: Bazel project를 build하기 위해 필요한 파일
2. Mediapipe Object detection 실습
실습하기전에 mediapipe를 설치해야겠죠?? 저는 Macbook pro(v12.2.1) 으로 실험환경을 셋팅 했습니다. 일단 mac os환경에 맞춰진 mediapipe를 설치해보죠.
2.1 Mediapipe 설치
- homebrew, xcode 및 xcode command line tools 설치가 되있음을 가정합니다. (homebrew 3.4.1, xcode 13.2.1 썼습니다)
- xcode command line tools은
$ xcode-select --install
명령어로 설치 가능합니다.
- xcode command line tools은
- bazelisk 설치:
$ brew install bazelisk
- git clone Mediapipe repo
$ git clone https://github.com/google/mediapipe.git
$ cd mediapipe
- OpenCV, FFmpeg 설치
$ brew install opencv@3
# There is a known issue caused by the glog dependency. Uninstall glog.
$ brew uninstall --ignore-dependencies glog
- Python3, Python six libraray 설치
$ brew install python
$ sudo ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
$ python --version
Python 3.7.4
$ pip3 install --user six
- mediapie Hello world C++ exmaple 실행
$ export GLOG_logtostderr=1
# Need bazel flag 'MEDIAPIPE_DISABLE_GPU=1' as desktop GPU is currently not supported
$ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
mediapipe/examples/desktop/hello_world:hello_world
2.2 Mediapipe Object detection in C++
제 맥북의 카메라를 통해 live로 detection을 하도록 할것입니다. 예제 실습이기 때문에 mediapipe에서 제공하는 graph와 model(ssdlite)로 진행하겠습니다.
- application에 대해 bazel build
- 해당 BUILD파일의 name(object_detection_cpu)과 일치하는 cc_binary를 bazel build하게 됨
$ bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/object_detection:object_detection_cpu
- applicatio에 대해 bazel run
$ GLOG_logtostderr=1 bazel-bin/mediapipe/examples/desktop/object_detection/object_detection_cpu \
--calculator_graph_config_file=mediapipe/graphs/object_detection/object_detection_desktop_live.pbtxt
위명령어를 하게되면 다음과 같이 live demo object detection이 가능해집니다!
2.3 Mediapipe Object detection graph visualizer
다음은 run할 때 사용된 graph_config_file 옵션인 mediapipe/graphs/object_detection/object_detection_desktop_live.pbtxt 를 visualization한 것입니다. visualizer을 통해 input stream이 들어와 model inference 결과까지 어떻게 process되는지 한눈에 확인가능합니다.
다음에는 mediapipe에서 제공되는 모델이 아닌 yolov5 model을 가지고 custom하게 graph, BUILD 파일을 수정해가며 desktop에 deploy하는 방법을 올리도록 하겠습니다.
'AI Engineering > TensorFlow' 카테고리의 다른 글
TensorFlow.js (1) - TensorFlow.js 이해 및 detection 예제 (1) | 2022.03.17 |
---|---|
Mediapipe (2) - custom segmentation model with mediapipe (0) | 2022.03.14 |
TFLite 뽀개기 (3) - Quantization (2) | 2022.03.09 |
TFLite 뽀개기 (2) - TFLite Inference (0) | 2022.03.09 |
TFLite 뽀개기 (1) - TFLite 기본 이해 및 사용하기 (0) | 2022.03.09 |