본문 바로가기

AI/OpenVINO

3-10. Named entity recognition(NER) with OpenVINO™

Rome’s Imperial Period was its last, beginning with the rise of Rome’s first emperor in 31 BC and lasting until the fall of Rome in AD 476. During this period, Rome saw several decades of peace, prosperity, and expansion. By AD 117, the Roman Empire had reached its maximum extant, spanning three continents including Asia Minor, northern Africa, and most of Europe.

In AD 286 the Roman Empire was split into eastern and western empires, each ruled by its own emperor. The western empire suffered several Gothic invasions and, in AD 455, was sacked by Vandals. Rome continued to decline after that until AD 476 when the western Roman Empire came to an end. The eastern Roman Empire, more commonly known as the Byzantine Empire, survived until the 15th century AD. It fell when Turks took control of its capital city, Constantinople (modern day Istanbul in Turkey) in AD 1453.

참고: https://docs.openvino.ai/2024/notebooks/named-entity-recognition-with-output.html

 

Named entity recognition with OpenVINO™ — OpenVINO™ documentation

Named entity recognition with OpenVINO™ This Jupyter notebook can be launched on-line, opening an interactive environment in a browser window. You can also make a local installation. Choose one of the following options: The Named Entity Recognition(NER)

docs.openvino.ai

 

이번 예제는 잘 알려져있는 NER에 대한 실습이다.

Named Entity Recognition... 이름으로 특정지을 수 있는 것들을 인식하는 것을 말한다.

자연어 처리를 하면서 중요 정보를 검출하거나 분류하는 것으로 많은 양의 글에 대한 요약 등을 할 때 사용한다.

이번에 사용할 모델은 distillbert based 모델로 자세한 내용은 여기를 참고하기 바란다.

이 모델은 사람과, 지역, 기관, 그 외로 인식할 수 있고 대소문자를 구별한다고 한다.

일단 실행하면서 알아가보자.

 

필요한 패키지들: diffusers, openvino, nncf, gradio, onnx, transformers, torch

그리고 optimum-intel.git이 필요한데, 기존의 예제를 충실히 진행했다면 다 설치되어 있을 것이다.

 

위에서 언급한 NER 모델을 다운받아 IR모델로 초기화하고 저장한다.

 

여기서는 앞의 예제에서 사용했던 NNCF Quantization을 하는데, 다음에 또 나오기 때문에 이런게있다 정도만 기억하고 넘어가자.

몇가지 함수에 대한 설명이 필요하다.

- get_calivration_dataset(): 후반 quatization 보정단계에서 사용하는 보정용 데이터를 만든다.

- quantize(): quantize한 모델을 OpenVINO IR 형식으로 저장한다.

- OVModelForTokenClassification(): quantization한 모델을 불러올 때 사용한다.

 

 

여기까지 진행하면 optimized_model에 받아온 NER 모델이 Quantization되고, 후보정도 되고, IR 모델이 로드된다.

pipeline()을 이용해서 원 모델과 이렇게 변형된 모델을 같이 비교하자.

 

어라... 원 모델이 1.24배 빠른데? ㅎㅎㅎ

 

아.. 모델크기는 차이가 4배 정도로 많이난다. 

 

그러면 실제 예문을 가지고 결과가 어떻게 나타나는지 보자.

원문은 아래와 같다. 원문 출처: https://www.mpm.edu/research-collections/anthropology/anthropology-collections-research/mediterranean-oil-lamps/roman-empire-brief-history

"

Rome’s Imperial Period was its last, beginning with the rise of Rome’s first emperor in 31 BC and lasting until the fall of Rome in AD 476. During this period, Rome saw several decades of peace, prosperity, and expansion. By AD 117, the Roman Empire had reached its maximum extant, spanning three continents including Asia Minor, northern Africa, and most of Europe.

In AD 286 the Roman Empire was split into eastern and western empires, each ruled by its own emperor. The western empire suffered several Gothic invasions and, in AD 455, was sacked by Vandals. Rome continued to decline after that until AD 476 when the western Roman Empire came to an end. The eastern Roman Empire, more commonly known as the Byzantine Empire, survived until the 15th century AD. It fell when Turks took control of its capital city, Constantinople (modern day Istanbul in Turkey) in AD 1453.

"

 

추론된 결과를 보자.

 

음... 이거 가지고 요약이 될라나 모르겠다.

 

이번 내용은 모델 자체를 배운다기 보단 OpenVINO에서 HuggingFace에 있는 모델을 불러와 변환하고 저장하는 방법과,

quatization해서 OpenVINO IR 형식으로 저장하는 방법을 익히는 예제라고 생각하자.