babalae/better-genshin-impact · error · InvalidOperationException
角色头像模型元素输出数量异常:logits={logits.Length}, elements={elementType
Error message
角色头像模型元素输出数量异常:logits={logits.Length}, elements={elementTypes.Length} What it means
Thrown as InvalidOperationException by AvatarGridIconRecognizer when the ONNX model's 'element_logits' output tensor length does not match the count of distinct element types derived from avatar.csv prototypes. The recognizer maps each logit index to a sorted distinct element type; a mismatch means the model and the CSV are out of sync (different training-time class count vs. prototype data).
Source
Thrown at BetterGenshinImpact/GameTask/Common/Job/AvatarGridIconRecognizer.cs:127
.Select(group => group.OrderByDescending(candidate => candidate.Score).First())
.OrderByDescending(candidate => candidate.Score)
.FirstOrDefault() ?? AvatarGridIconCandidate.Empty;
if (!recognizeElementType || candidate == AvatarGridIconCandidate.Empty)
{
return candidate;
}
var logits = results.First(result => result.Name == "element_logits").AsEnumerable<float>().ToArray();
var elementTypes = _prototypes
.Select(prototype => prototype.ElementType)
.Where(elementType => !string.IsNullOrWhiteSpace(elementType))
.Distinct(StringComparer.Ordinal)
.OrderBy(elementType => elementType, StringComparer.Ordinal)
.ToArray();
if (logits.Length != elementTypes.Length || logits.Length == 0)
{
throw new InvalidOperationException(
$"角色头像模型元素输出数量异常:logits={logits.Length}, elements={elementTypes.Length}");
}
var predictedElementType = elementTypes[Array.IndexOf(logits, logits.Max())];
return candidate with { ElementType = predictedElementType };
}
/// <summary>
/// 按模型训练协议生成头像和元素图标两个输入张量。
/// </summary>
/// <remarks>
/// 完整头像缩放为 115x115;元素输入必须从该图左上角裁剪 48x48 后再缩放为 64x64。
/// 两个输入均执行 BGR→RGB 及 [-1,1] 归一化,不能用完整头像代替元素输入。
/// </remarks>
internal static (DenseTensor<float> Image, DenseTensor<float> ElementImage) CreateInputTensors(Mat mat)
{
using Mat resized = mat.Resize(new Size(InputSize, InputSize));
using Mat elementRoi = resized.SubMat(0, ElementRoiSize, 0, ElementRoiSize);View on GitHub (pinned to a7cb36712d)
Solutions
- Ensure avatar.onnx and avatar.csv are from the same release/training run so element class counts align.
- Regenerate avatar.csv from the same training data used to export the ONNX model.
- If the model is intentionally updated, update the CSV and re-test element recognition.
Example fix
null
Defensive patterns
Strategy: validation
Validate before calling
// After loading prototypes, verify element count matches model output metadata var distinctElements = prototypes.Select(p => p.ElementType).Where(e => !string.IsNullOrWhiteSpace(e)).Distinct().Count(); // Compare against model metadata: _session.ModelMetadata or output node shape
Type guard
null
Try / catch
try { recognizer.Recognize(avatar, element, true); }
catch (InvalidOperationException ex) when (ex.Message.Contains("元素输出数量异常")) { /* model/CSV mismatch — update assets */ } Prevention
- Always ship avatar.onnx and avatar.csv as a matched pair from the same training run.
- Add a startup self-test that verifies the model's element output dimension matches the CSV.
When it happens
Trigger: Calling Recognize(avatarMat, elementMat, recognizeElementType: true) when the avatar.onnx model was updated/retrained with a different number of element classes than the distinct element_type values present in avatar.csv.
Common situations: The ONNX model file (avatar.onnx) was replaced with a newer version that has more/fewer element output nodes, but avatar.csv was not regenerated to match. The prototype CSV has blank element_type values that change the distinct count. A partial update of the AvatarGridIcon asset bundle.
Related errors
- Unable to GetLabelByIndex: index {i} out of range {labels.Co
- 无效的推理设备
- PaddleOCR config file {modelConfigFileName} not found: {conf
- Yap字典文件不存在
- 自动钓鱼模型文件不存在
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/aeae9f6b315253e9.
Report an issue: GitHub.