카테고리 없음
라즈베리파이5에서 EfficientNet Performance 측정
파란김치
2024. 2. 23. 01:37
1. Pytorch 측정
라즈베리파이5의 CPU에서 EfficientNet-b0에 대해 이미지 사이즈 별로 100번 추론했을 때 평균시간을 측정 해 보았습니다.
측정 시간기준은 추론 뿐 아니라 이미지 transform 및 class index를 계산하는 시간도 함께 포함 했습니다.
측정 코드는 아래와 같습니다.
from PIL import Image
from efficientnet_pytorch import EfficientNet
from torchvision import transforms
import time
IMG_SIZES = [ 256, 512, 640 ]
model = EfficientNet.from_pretrained('efficientnet-b0')
model.eval()
img = Image.open("./asd.png").convert("RGB")
def test(img_size):
transform = transforms.Compose(
[
transforms.Resize((img_size, img_size)),
transforms.ToTensor(),
transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])
])
times = []
for i in range(100):
start = time.time()
input = transform(img).unsqueeze(0)
preds = model(input)
class_index = preds[0].argmax(dim=-1).numpy()
end = time.time()
total_time = end - start
times.append(total_time)
print(f"IMG SIZE ({img_size}, {img_size}) avg {sum(times) / len(times) } sec")
for size in IMG_SIZES:
test(size)
측정 결과는?????
이미지 사이즈(픽셀) | 256x256 | 512x512 | 640x640 |
평균 시간(초) | 0.45 | 1.01 | 1.62 |
현장에서는 512정도는 되어야 제품 구별이나 불량검사가 가능할텐데... 속도를 더 줄여 보자
2. Onnx 측정
onnx로 export 해서 테스트 해봤다
코드는 아래와 같다
from PIL import Image
import torch
from efficientnet_pytorch import EfficientNet
from torchvision import transforms
import onnxruntime as ort
import os
import time
IMG_SIZES = [ 256, 512, 640 ]
model = EfficientNet.from_pretrained('efficientnet-b0')
model.eval()
model.set_swish(memory_efficient=False)
img = Image.open("./123.png").convert("RGB")
def test(img_size):
onnx_file = f"EfficientNet-B0-{img_size}.onnx"
if not os.path.isfile(onnx_file):
torch.onnx.export(model, torch.rand(1, 3, img_size, img_size), onnx_file)
session = ort.InferenceSession(onnx_file)
transform = transforms.Compose(
[
transforms.Resize((img_size, img_size)),
transforms.ToTensor(),
transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])
])
times = []
for i in range(100):
start = time.time()
input = transform(img).unsqueeze(0)
preds = session.run(None, {"input.1" : input.detach().numpy()})
class_index = preds[0].argmax(axis=-1)
print(class_index)
end = time.time()
total_time = end - start
times.append(total_time)
print(f"IMG SIZE ({img_size}, {img_size}) avg {sum(times) / len(times) } sec")
print(f"IMG SIZE ({img_size}, {img_size}) avg {sum(times) / len(times) } sec")
for size in IMG_SIZES:
test(size)
측정 결과는?????
측정 결과는?????
이미지 사이즈 (픽셀) | 256x256 | 512x512 | 640x640 |
평균 시간(초) | 0.12 | 0.42 | 0.62 |
띠용 엄청난 결과다
3. pytorch - onnx 비교
비교해보자
이미지 사이즈 (픽셀) | 256x256 | 512x512 | 640x640 |
pytorch 평균 시간(초) | 0.45 | 1.01 | 1.62 |
onnx 평균 시간(초) | 0.12 | 0.42 | 0.62 |
비율(onnx/pytorch) | 0.27 | 0.41 | 0.38 |
굳 onnx 좋았어