imagenet-tflite
책 『밑바닥부터 만드는 추론 엔진: Rust로 배우는 AI 인퍼런스 엔지니어링』
(Building an Inference Engine from Scratch) 27장에서 쓰는 이미지 분류
모델 둘입니다. 남이 학습해 공개한 가중치를 .tflite 형식으로 바꾼
것이고, 가중치의 값은 바꾸지 않았습니다.
Two ImageNet classifiers converted to the .tflite format for chapter 27 of
the book. The weights were trained and published by others; this repository
only changes the file format so that readers can follow the chapter without
installing Python.
- 책 / Book: https://chosungmann.github.io/building-an-inference-engine-from-scratch/
- 코드 / Code: https://github.com/chosungmann/building-an-inference-engine-from-scratch
모델과 출처 / Models and sources
| 파일 | 원본 / Source | 판 / Revision | 라이선스 / License |
|---|---|---|---|
resnet50.tflite | timm/resnet50.tv_in1k (torchvision의 가중치 / original torchvision weights) | 78f3ecfdb38e | BSD-3-Clause, LICENSE_resnet50.txt |
mobilenetv2.tflite | timm/mobilenetv2_100.ra_in1k | 5afa12513b04 | Apache-2.0, LICENSE_mobilenetv2.txt |
두 라이선스 파일은 원본 프로젝트(pytorch/vision, huggingface/pytorch-image-models)의 것을 그대로 옮겼습니다.
바꾼 것 / Changes: litert-torch 0.10.0으로 PyTorch 모델을 .tflite로
변환했습니다. 변환기가 배치 정규화를 합성곱의 가중치에 접어 넣고, ReLU를
앞 연산의 설정으로 옮기고, 입력의 축 순서를 바꾸는 TRANSPOSE를 맨 앞에
넣었습니다. 그 밖에는 손대지 않았습니다.
The models were converted from PyTorch with litert-torch 0.10.0. The
converter folded batch normalization into the convolution weights, fused
ReLU into the preceding operators and inserted a TRANSPOSE at the input.
Nothing else was changed.
두 모델은 ImageNet-1k로 학습되었습니다. ImageNet의 사진은 이 저장소에 없습니다.
입력과 출력 / Input and output
입력은 정규화한 사진 (1, 3, 224, 224)이고 출력은 1,000가지 이름의 점수
(1, 1000)입니다. 전처리는 그래프에 들어 있지 않습니다. 짧은 변을 256으로
줄이고(Pillow의 BILINEAR), 가운데 224 × 224를 자르고, 0~1로 바꾼 뒤
평균 (0.485, 0.456, 0.406)을 빼고 표준편차 (0.229, 0.224, 0.225)로
나눕니다.
파일 / Files
| 파일 | 내용 |
|---|---|
resnet50.tflite | ResNet-50. 파라미터 2,556만 개 |
mobilenetv2.tflite | MobileNetV2. 파라미터 350만 개 |
labels.txt | 1,000가지 이름. 한 줄에 하나. timm 1.0.31의 ImageNetInfo가 주는 설명 |
reference.safetensors | 시험 사진 다섯 장(위키미디어 공용, CC0)에 대한 정답. 사진마다 푼 픽셀의 귀퉁이(*.decoded), 줄이고 자른 사진(*.shaped), 모델의 입력(*.input), 모델마다 PyTorch와 LiteRT의 점수(*.pytorch, *.litert). 크기를 바꾸는 코드를 시험하는 합성 사진(synthetic.*) |
mobilenetv2_int8.tflite | 29장. mobilenetv2.tflite를 LiteRT의 양자화 도구(ai-edge-quantizer 0.10.1)로 8비트 정수 모델로 바꾼 것. 가중치와 중간 결과가 모두 int8이고, 보정에는 시험 사진 다섯 장에서 잘라 낸 조각 45개를 썼습니다. 원본과 같은 Apache-2.0 |
reference_int8.safetensors | 29장의 정답. 사진마다 정수 입력(*.input), LiteRT 자신의 커널의 출력(*.builtin), XNNPACK에 넘긴 출력(*.xnnpack). 첫 사진은 LiteRT가 계산한 중간 텐서 전부(cup.tensor.<번호>). int8 값을 바이트 그대로(u8) 담았습니다 |
resnet50.onnx, mobilenetv2.onnx | 30장. 같은 가중치를 PyTorch 2.14.1의 torch.onnx.export로 ONNX(연산 18판)로 내보낸 것. 배치 정규화 접기와 상수 접기를 꺼서 배치 정규화와 활성화 함수가 따로 있는 마디로 남아 있습니다. 30장이 그것을 직접 접습니다. 라이선스는 각 원본과 같습니다(BSD-3-Clause, Apache-2.0) |
reference_onnx.safetensors | 30장의 정답. 모델마다, 사진마다 ONNX Runtime 1.32.0이 그래프 최적화를 끄고 낸 점수(<모델>.<사진>.onnxruntime). 입력은 reference.safetensors의 *.input입니다 |
reference_ch32.safetensors | 32장의 정답. 모델 파일은 들어 있지 않습니다(ViT-Tiny는 timm의 저장소에서, YOLOX-Tiny는 YOLOX 프로젝트의 릴리스에서 받습니다). ViT: 사진마다 입력(vit.<사진>.input)과 PyTorch의 점수(vit.<사진>.logits), 첫 사진은 중간 결과도. YOLOX: 사진마다 줄이고 채운 입력(yolox.<사진>.input), ONNX Runtime 1.32.0의 출력(yolox.<사진>.output), 줄인 비율(yolox.<사진>.ratio) |
reference_ch33.safetensors | 33장의 정답. 모델 파일과 소리 파일은 들어 있지 않습니다(Whisper-tiny는 OpenAI의 저장소에서, jfk.wav는 whisper.cpp의 저장소에서 받습니다). jfk.mel(멜 스펙트로그램), jfk.encoded(인코더의 출력), jfk.logits(지시 토큰 넷 뒤의 점수), jfk.greedy와 jfk.beam(transformers 5.12.1이 고른 토큰들) |
LICENSE_resnet50.txt, LICENSE_mobilenetv2.txt | 원본의 라이선스 |
만든 방법 / How it was made
저장소의 reference/ch27_make_tflite.py로 만들었습니다.
sh scripts/download_ch27.sh
pip install -r reference/requirements_ch27.txt
python reference/ch27_make_tflite.py
30장의 ONNX 파일은 reference/ch30_make_onnx.py로 만들었습니다.
The .onnx files (chapter 30) hold the same weights exported with
torch.onnx.export with batch-norm folding and constant folding turned off,
so that the chapter can perform those graph optimizations itself.
한계 / Limitations
mobilenetv2.tflite의 원래 설정은 사진을 줄일 때 bicubic을 씁니다. 이
저장소의 정답은 두 모델 모두 쌍선형으로 전처리한 입력으로 만들었습니다.
엔진끼리 견주는 데는 문제가 없지만, 그 모델이 낼 수 있는 가장 좋은
정확도는 아닙니다.