본문 바로가기

컴퓨터/머신러닝 (Machine Learning)

OpenVINO, intel 내장 그래픽, 사용 model inference 하기

Ubuntu 20.04

OpenVINO 2022.2

 

이전 글 (Pytorch model을 ONNX로, ONNX를 openVINO로 변환하기 (tistory.com) 에서 변환해서 저장한

이미지 분류모델을 OpenVINO를 사용해서 돌려(추론)보자

 

1. package import, normalize 함수 정의 및 추론할 데이터 경로 정

 

import cv2
import matplotlib.pyplot as plt
import numpy as np
from openvino.runtime import Core
import time
import os

def normalize(image: np.ndarray) -> np.ndarray:
    """
    Normalize the image to the given mean and standard deviation
    for CityScapes models.
    """
    image = image.astype(np.float32)
    mean = (0.485, 0.456, 0.406)
    std = (0.229, 0.224, 0.225)
    image /= 255.0
    image -= mean
    image /= std
    return image
    
pics = os.listdir('openvino_test/')

 

2. intel cpu 및 gpu 확인

 

ie = Core()
print(ie.available_devices)

devices = ie.available_devices
for device in devices:
    device_name = ie.get_property(device, "FULL_DEVICE_NAME")
    print(f"{device}: {device_name}")

 

output:

 

 

3. OpenVINO 모델 로드 및 shape 확인

프린트되는 각 input/output layer의 shape은
input으로 들어가야할 데이터의 구조와 output으로 나올 데이터의 구조를 나타낸다.

 

model = ie.read_model(model="cecum_openvino/cecum.xml", weights='cecum_openvino/cecum.bin')
compiled_model = ie.compile_model(model=model, device_name="GPU")

input_layer = compiled_model.input(0)
output_layer = compiled_model.output(0)
print(input_layer.shape, '\n', output_layer.shape)

 

output:

 

 

4. 추론

 

start = time.perf_counter()
for pic in pics:
    image_filename = "openvino_test/" + pic
    
    #순서대로 이미지 load 및 색 전환, resize, normalize
    image = cv2.cvtColor(cv2.imread(image_filename), cv2.COLOR_BGR2RGB)
    resized_image = cv2.resize(image, (256, 256))
    normalized_image = normalize(resized_image) #shape (256, 256, 3)

    #모델 input shape에 맞춰서 array reshape (1, 3, 256, 256)
    input_image = np.expand_dims(np.transpose(normalized_image, (2, 0, 1)), 0)

    #추론
    result_infer = compiled_model([input_image])[output_layer]
    result_index = np.argmax(result_infer)
    probability = np.exp(result_infer)/np.sum(np.exp(result_infer))
    
    #추론 확률 및 label 확인
    print(probability, result_index)
end = time.perf_counter()
time_openvino_gpu = end - start
print(
    f"runtime/pic: {time_openvino_gpu/len(pics)} \n"
    f"images per second: {len(pics)/time_openvino_gpu} "
)

 

output: