(ML) 評估轉換後 CoreML 模型的精度

YEN HUNG CHENG
3 min readJun 18, 2023

--

Photo by Florian Schmetz on Unsplash

目的:將 EfficientNetV2 模型轉換為 CoreML 模型,並使用 ImageNet(ILSVRC2012) validation images 評估其在各項精度上的表現

Compare the differences among different EfficientNetV2 models (S, M, L) in terms of ImageNet Top-1 accuracy and model size

Top-1 accuracy 是參考論文提出的數值,而 (MB of weight in model) 模型大小則是參考載入 Pytorch 中的 torchvision.models

source

Compare the inference time of different EfficientNetV2 models (S, M, L) on Small-ImageNet Top-1

推理的時間是在 Macbook Air M1 上,並使用 Small ImageNet Validation Dataset 進行計算的

Compare the Small-ImageNet Top-1 accuracy and inference time of different input image sizes in EfficientNetV2_S

推理的時間是在 Macbook Air M1 上,並使用 Small ImageNet Validation Dataset 進行計算的,並可以發現當輸入的照片尺寸越大,Top-1 也會越高

權衡模型大小以及推理時間後,我決定使用 EfficientNetV2_S 作為本次實驗的 backbone (骨幹),並使用 299 x 299 為輸入照片的大小

Convert PyTorch EfficientNetV2_S to CoreML

可以參考下方這一篇進行轉換,不過文中標籤的產生方式需要修改

# 原本產生標籤的方式
import urllib
import coremltools as ct

label_url = 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt'
class_labels = urllib.request.urlopen(label_url).read().splitlines()
class_labels = class_labels[1:] # remove the first class which is background assert len(class_labels) == 1000


for i, label in enumerate(class_labels):
if isinstance(label, bytes):
class_labels[i] = label.decode("utf8")


classifier_config = ct.ClassifierConfig(class_labels)
# 更改為這樣產生標籤
import json
import coremltools as ct

with open('/Users/jason/Downloads/Small-ImageNet-Validation-Dataset-1000-Classes-main/imagenet.json') as f:
imagenet_labels = json.load(f)

classifier_config = ct.ClassifierConfig(imagenet_labels)

檔案 imagenet.json 可以在 Small ImageNet Validation Dataset 找到

Compare the quantized accuracy and model size of EfficientNetV2_S (ImageNet Top-1 and Top-5 accuracy)

我進一步對轉換後的 EfficientNetV2_S 模型進行了量化,並使用 ImageNet validation images 評估了其精度。結果顯示,在將模型量化為 4bit 時,模型無法正常進行預測;而將模型量化為 16bit 時,Top-1 和 Top-5 的精度並未有任何改變;將模型量化為 8 bit 時,精度僅有些微下降 。

EfficientNetV2: Smaller Models and Faster Training

ImageNet Large Scale Visual Recognition Challenge 2012 (ILSVRC2012)

Small ImageNet Validation Dataset 1000 Classes

--

--