AnimeFace-Unified-PE-Core-S16
A lightweight 24.2M-parameter (15.17 MiB in MNN W4) unified anime face & avatar encoder built on Meta's Apache-2.0 PE-Core-S/16 backbone.
In a single forward pass (224×224, 37.9 ms/img on 4-thread CPU in MNN W4), it simultaneously outputs three decoupled representations:
e_id([B, 256], L2-normalized) — Expression-invariant character / skin identity embedding for open-set avatar & character retrieval (outperforms 37MCCIP-caformer-24on a 1,099-skin real-world screenshot benchmark while using 3× smaller vectors).expr_logits([B, 48], sigmoid) — 48-class anime facial micro-expression classifier (eyes, mouth, cheek/effects such as blush/sweatdrop/tears, and emotions), achieving 0.8250 Macro AUROC (MNN W8) / 0.8229 Macro AUROC (MNN W4) zero-shot on completely unseen characters — matching 99.3% of a 400M (2.1 GiB) Danbooru tagger at 1/140th the file size.e_expr([B, 128], L2-normalized) — Fine-grained intra-skin expression residual embedding for distinguishing subtle expression variants of the same character/outfit under screenshot compression, cropping, and rounded-avatar masking.
用途说明 / Intended Uses
本项目主要面向二次元对话截图解析、立绘/头像检索与移动端离线推理场景(例如 MomoTranScript 桌面端、Web 演示与 Android App):
- 一键替代传统角色 ID 编码器(如 CCIP):
15.17 MiB(MNN W4)即可在 1,099 皮肤生产库上取得比 37M CCIP(21.15 MiB)更高的角色与换皮召回率,且特征库体积缩小3×(256 维 vs 768 维)。 - 零额外耗时输出微表情语义:在识别角色头像的同一次前向里,直接给出
48类细粒度二次元微表情(如微笑、闭眼、脸红、流汗、惊讶、生气、大喊、面无表情等),即使对词库中未收录的新角色、同人自绘或 OC 头像也完全生效。 - 为
[image]表情包 / 角色差分补全语义说明:在纯文本剧本转录(如 MomoScript)或二创信息卡检索中,可为角色头像或[image]插图自动附加人类可读的表情状态说明。 - 端云一致、全平台即插即用:同时提供动态 batch FP32 ONNX(服务端/PC GPU & CPU)、PyTorch
safetensors、以及专为手机端优化的 MNN W4-HQQ b64 (15.16 MiB) 与 MNN W8 (23.92 MiB)。 - 原生 FP16 推理安全(v2.1-fp16safe 权重校准):通过零重训的数学等价权重缩放(压缩
blocks.0Head 2 饱和 attention sink 与 Pre-LN 残差流动态范围),整网在无 FlashAttention 的纯 FP16 算子(ONNX FP16 / MNNARMv8.2-FP16/OpenCL FP16/ TensorRT FP16)下所有中间张量均严格低于5,000(远低于 FP16 上限65,504),0 NaN且与原 FP32 权重输出余弦相似度≥ 0.999998。
Architecture / 分层解耦架构
Character identity requires expression invariance, while micro-expression recognition and intra-skin variant matching require high spatial sensitivity to eyes, mouth, blush, and sweatdrops. To prevent gradient conflict, heads are tapped at different depths of PE-Core-S/16 (12 ViT blocks, 224×224, 14×14 = 196 patch tokens):
Input Avatar (RGB 224×224, normalized mean=0.5, std=0.5)
│
▼
┌───────────────────────────────────────────────────────────┐
│ PE-Core-S/16 Backbone (12 blocks, Apache-2.0, 23.6M) │
│ ├─ Block 8 (layer -3, 196 spatial patch tokens, 384-d) │
│ │ ├──► Head B: Cross-Attn Tag Query Head (~0.4M) │──► expr_logits [B, 48]
│ │ └──► Head C: Center Patches + Tag Summary MLP │──► e_expr [B, 128]
│ └─ Block 11 (final layer, global pooled token, 384-d) │
│ └──► Head A: Identity Projection MLP │──► e_id [B, 256]
└───────────────────────────────────────────────────────────┘
- Training & Multi-Teacher Distillation:
- Head A (
e_id): Open-set cosine distillation fromCCIP-caformer-24+ deduplicated skin-cluster ArcFace (545clean clusters after merging150duplicate same-character skins at CCIP cosine ≥0.94). - Head B (
expr_logits): Dual-teacher soft-label BCE + per-tag variance-balanced standardized-logit distillation (0.65 × CL-Tagger-v2 outputs + 0.35 × DanbooruTagQuery-B/16 outputs) across 14.7k anime face sprites and 3.5k real screenshot avatar crops. - Head C (
e_expr): Intra-skin hard-negative InfoNCE matching clean gallery templates against realistically degraded views (crop/shift jitter, rounded masks, downscaling, JPEG artifacts).
- Head A (
Benchmarks / 评测结果
1. Full 1,099-Skin Real-World Stage-1 ID Benchmark
Evaluated on 200 held-out real Bilibili chat-screenshot avatar crops across 83 characters (including 20 queries of completely unseen characters never present in training):
| Model | Size / 4T CPU Latency | 1,099-Skin Char Top-1 / Top-5 | 1,099-Skin Exact Skin Top-1 / Top-5 | Unseen-Char Top-1 / Top-5 | Embedding Dim |
|---|---|---|---|---|---|
CCIP-caformer-24 (37M baseline) | 21.15 MiB (W4) | 96.5% / 100.0% | 56.0% / 99.0% | 100.0% / 100.0% | 768-d |
| AnimeFace-Unified v2 (FP32) | 95.06 MiB | 97.5% / 100.0% | 58.0% / 99.5% | 100.0% / 100.0% | 256-d |
| AnimeFace-Unified v2 (MNN W4) | 15.17 MiB / 37.9 ms | 97.5% / 100.0% | 58.0% / 99.5% | 100.0% / 100.0% | 256-d |
2. Strict Unseen-Character 12-Class Micro-Expression Recognition
Evaluated zero-shot on 287 full-resolution sprites from 22 completely unseen characters across 12 standard anime micro-expression attributes (open_mouth, closed_eyes, smile, blush, sweat_drop, happy, angry, sad, surprised, troubled, serious, shouting):
| Model | Params / Size | License | Macro AUROC | Macro F1 | closed_eyes | blush | surprised | shouting | open_mouth | smile | sweat_drop |
|---|---|---|---|---|---|---|---|---|---|---|---|
CL-Tagger-v2 (teacher) | 400M / 2.1 GiB | Custom (no redist.) | 0.8288 | 0.5313 | 0.97 | 0.91 | 0.88 | 0.88 | 0.86 | 0.81 | 0.80 |
| AnimeFace-Unified v2 (MNN W8) | 24.2M / 23.93 MiB | Apache-2.0 | 0.8250 | 0.5189 | 0.99 | 0.94 | 0.89 | 0.87 | 0.85 | 0.83 | 0.81 |
| AnimeFace-Unified v2 (MNN W4) | 24.2M / 15.17 MiB | Apache-2.0 | 0.8229 | 0.5190 | 0.99 | 0.94 | 0.88 | 0.87 | 0.86 | 0.83 | 0.81 |
DanbooruTagQuery-B/16 (teacher) | 96.8M / 369.5 MiB | DINOv3 License | 0.7860 | 0.4721 | 0.97 | 0.85 | 0.81 | 0.71 | 0.86 | 0.83 | 0.79 |
Camie-Tagger-v2 | 143M / 752.4 MiB | GPL-3.0 | 0.7818 | 0.4699 | 0.95 | 0.86 | 0.85 | 0.81 | 0.85 | 0.84 | 0.82 |
PE-Core-S/16 (frozen linear probe) | 23.6M / ~95 MiB | Apache-2.0 | 0.6560 | 0.3419 | 0.91 | 0.72 | 0.82 | 0.72 | 0.67 | 0.58 | 0.63 |
CCIP-caformer-24 (linear probe) | 37M / 21.15 MiB W4 | MIT/Apache | 0.5677 | 0.3040 | 0.76 | — | — | — | — | 0.42 | 0.69 |
3. Quantization Parity & CPU Speed (Desktop x86-64, 4 Threads)
| Artifact | File Size | 4T CPU Latency | e_id Cosine vs FP32 | e_expr Cosine vs FP32 | Logits MAE |
|---|---|---|---|---|---|
onnx/model.onnx (FP32, dynamic batch) | 95.06 MiB | 47.0 ms/img | 1.00000 | 1.00000 | 0.0000 |
mnn/model_w8.mnn (W8 asymmetric) | 23.93 MiB | 44.5 ms/img | 0.99979 | 0.99985 | 0.0034 |
mnn/model_w4.mnn (W4-HQQ block-64) | 15.17 MiB | 37.9 ms/img | 0.97217 | 0.98297 | 0.0412 |
Included Files
| File | Description |
|---|---|
onnx/model.onnx | Dynamic-batch ([batch_size, 3, 224, 224]) FP32 ONNX model for PC/Server GPU & CPU |
mnn/model_w4.mnn | 15.17 MiB MNN W4-HQQ b64 static [1, 3, 224, 224] model (recommended for Android / mobile) |
mnn/model_w8.mnn | 23.93 MiB MNN W8 asymmetric static [1, 3, 224, 224] model (near-FP32 fidelity) |
model.safetensors | Clean PyTorch deployment weights (no training-only layers) |
vocab_48.json | 48 micro-expression tags with categories (eyes/mouth/effects/emotions), Chinese gloss (zh), and calibrated thresholds |
config.json | Model & preprocessing specification |
inference.py | Minimal standalone ONNX Runtime inference script |
Quickstart (ONNX Runtime)
from PIL import Image
from inference import AnimeFaceUnifiedEncoder
enc = AnimeFaceUnifiedEncoder("onnx/model.onnx", "vocab_48.json")
e_id, probs, e_expr = enc.predict([Image.open("avatar.png")])
# 1. Character/skin identity matching: cosine similarity with gallery e_id [N, 256]
# 2. Human-readable micro-expression tags (works on unseen characters & OCs!)
tags = enc.decode_expressions(probs[0], top_k=5)
print([(t["tag"], t["zh"], t["score"]) for t in tags])
CLI one-liner:
python inference.py avatar.png --model onnx/model.onnx --vocab vocab_48.json --lang zh
Preprocessing Spec (for MNN / C++ / Android)
- Composite RGBA onto white
(255, 255, 255)background; convert to RGB. - Pad non-square crops to square (center-padded), then bicubic resize to
224×224. - Scale to
[0, 1], normalize(x - 0.5) / 0.5, transpose toNCHWfloat32. - Input tensor name:
pixel_values. Output tensor names:e_id([B, 256]),expr_logits([B, 48]),e_expr([B, 128]).
48 Micro-Expression Vocabulary
- Eyes (11):
closed_eyes,half-closed_eyes,one_eye_closed,wide-eyed,narrowed_eyes,glaring,empty_eyes,sparkling_eyes,rolling_eyes,^_^,>_< - Mouth (13):
open_mouth,closed_mouth,parted_lips,smile,light_smile,grin,smirk,frown,pout,wavy_mouth,:o,:d,clenched_teeth - Cheeks & Effects (9):
blush,full-face_blush,sweatdrop,sweat,tears,crying,tearing_up,gloom_(expression),shaded_face - Emotions & Brows (15):
happy,laughing,angry,annoyed,sad,surprised,worried,nervous,nervous_smile,serious,expressionless,shouting,embarrassed,smug,furrowed_brow
License & Acknowledgements
- License: Apache License 2.0.
- Base Model: Built on
facebook/PE-Core-S-16-384(Meta FAIR Perception Encoder, Apache-2.0) viatimm. - Distillation Teachers: Soft outputs distilled offline from
CCIP-caformer-24,realphongha/danbooru-tag-query, andcella110n/cl_tagger_v2output probabilities (no teacher weights or training images are included or redistributed).