{"record":{"id":"b2b46361fefbd056","repo":"tonhowtf/omniget","slug":"sa-da-do-modelo-com-formato-inesperado","errorCode":null,"errorMessage":"saída do modelo com formato inesperado: {:?}","messagePattern":"saída do modelo com formato inesperado: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/img_bg.rs","lineNumber":387,"sourceCode":"    session: &mut ort::session::Session,\n    img: &DynamicImage,\n    p: &BgParams,\n) -> anyhow::Result<GrayImage> {\n    let side = p.size as i64;\n    // O `ort` traz um `ndarray` próprio (0.17) e o crate usa o 0.16, então o\n    // tensor atravessa a fronteira como forma + dados contíguos, que é o que o\n    // `Tensor::from_array` aceita sem depender de versão de crate nenhuma.\n    let (data, _) = normalize_input(img, p).into_raw_vec_and_offset();\n    let tensor = ort::value::Tensor::from_array((vec![1, 3, side, side], data))\n        .map_err(|e| anyhow!(\"não montei o tensor de entrada: {e}\"))?;\n    let outputs = session\n        .run(ort::inputs![tensor])\n        .map_err(|e| anyhow!(\"a inferência falhou: {e}\"))?;\n    let (shape, raw) = outputs[0]\n        .try_extract_tensor::<f32>()\n        .map_err(|e| anyhow!(\"não li a saída do modelo: {e}\"))?;\n    if shape.len() < 2 {\n        return Err(anyhow!(\n            \"saída do modelo com formato inesperado: {:?}\",\n            &shape[..]\n        ));\n    }\n    let h = shape[shape.len() - 2].max(0) as u32;\n    let w = shape[shape.len() - 1].max(0) as u32;\n    let small = mask_from_raw(raw, w, h)?;\n    let (ow, oh) = (img.width(), img.height());\n    Ok(image::imageops::resize(\n        &small,\n        ow,\n        oh,\n        FilterType::Lanczos3,\n    ))\n}\n\nfn run_blocking(\n    opts: &BgOptions,","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/img_bg.rs#L369-L405","documentation":"A hand-written validation error: the model's output tensor has fewer than 2 dimensions, so the code cannot take the last two dims as mask height/width. This indicates the selected ONNX file is not the expected image-matting model (which should emit an NCHW or at least [.., H, W] mask), or its output was misidentified.","triggerScenarios":"`shape.len() < 2` after extracting outputs[0] — e.g. a model that returns a scalar, a 1-D class-probability vector, or when the wrong output index was picked.","commonSituations":"Using a classification model instead of a segmentation model; a model export that collapsed/omitted spatial dimensions; accidentally pointing `session_for` at a different ONNX file (wrong model_id to file mapping).","solutions":["Verify the model_id maps to the correct segmentation ONNX file and that outputs[0] is the mask output","Check the model's output shapes offline (netron or session.outputs) — expect rank 4 like [1,1,H,W]","If the model emits [N, C] logits, reshape/normalize before treating it as a spatial mask","Re-download the correct model if the file was replaced or partially overwritten"],"exampleFix":"// before\nif shape.len() < 2 {\n    return Err(anyhow!(\"saída do modelo com formato inesperado: {:?}\", &shape[..]));\n}\n// after\nif shape.len() < 4 || shape[1] != 1 {\n    return Err(anyhow!(\"saída do modelo com formato inesperado: {:?} — esperado [N,1,H,W]\", &shape[..]));\n}","handlingStrategy":"validation","validationCode":"let out_shape = session.outputs[0].output_type.tensor_shape();\nif out_shape.rank() < 2 {\n    return Err(anyhow!(\"modelo incompatível: saída tem rank {}, esperado >= 2\", out_shape.rank()));\n}","typeGuard":"fn is_spatial_mask(shape: &[i64]) -> bool {\n    shape.len() >= 2 && shape[shape.len() - 1] > 0 && shape[shape.len() - 2] > 0\n}","tryCatchPattern":"if !is_spatial_mask(&shape) {\n    return Err(anyhow!(\"saída do modelo com formato inesperado: {:?} — verifique se o model_id aponta para um modelo de segmentação\", &shape[..]));\n}","preventionTips":["Validate model output rank/shape when registering a new model_id in params_for","Inspect new ONNX files with Netron before wiring them in","Fail fast with a startup smoke test that checks output shape on a dummy input"],"tags":["onnx","shape","validation","model"],"backgroundTag":"unexpected-response-shape","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}