{"record":{"id":"26be7d2b5258776a","repo":"tonhowtf/omniget","slug":"n-o-montei-o-tensor-de-entrada-e","errorCode":null,"errorMessage":"não montei o tensor de entrada: {e}","messagePattern":"não montei o tensor de entrada: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/img_bg.rs","lineNumber":379,"sourceCode":"    Ok(std::fs::metadata(dest).map(|m| m.len()).unwrap_or(0))\n}\n\n// ── Execução ───────────────────────────────────────────────────────────\n\n/// Roda a inferência num arquivo já aberto e devolve a máscara no tamanho\n/// original da imagem.\nfn mask_for_image(\n    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,","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/img_bg.rs#L361-L397","documentation":"This error wraps a failure from `ort::value::Tensor::from_array` when building the NCHW input tensor (shape [1,3,side,side]) for the background-removal ONNX model. The `ort` crate rejects the shape/data combination when the element count does not exactly match the declared shape, the data is not contiguous/aligned, or the type does not match the declared tensor type. It is thrown eagerly before any inference runs.","triggerScenarios":"Calling `Tensor::from_array((vec![1, 3, side, side], data))` where `normalize_input` produced a buffer whose length != 3*side*side, or `p.size` disagrees with the actual resized image dimensions, or the f32 Vec was mis-shaped.","commonSituations":"A model config whose `size` parameter was changed without updating `normalize_input`; switching to a model with a different input resolution; a refactor of `normalize_input` returning a different channel order or count; passing an RGBA (4-channel) buffer instead of RGB (3-channel).","solutions":["Verify `normalize_input` resizes to exactly p.size x p.size and produces 3 RGB channels as f32, so data.len() == 3*side*side","Log data.len() and the expected 3*side*side next to the error to confirm the mismatch","Check that p.size matches the ONNX model's declared input dimensions (inspect with `onnxruntime` tools or session.inputs)","Keep the shape Vec and data produced from the same `p` value, not from stale/other params"],"exampleFix":"// before\nlet (data, _) = normalize_input(img, p).into_raw_vec_and_offset();\nlet tensor = ort::value::Tensor::from_array((vec![1, 3, side, side], data))?;\n// after\nlet input = normalize_input(img, p);\nassert_eq!(input.len(), (3 * side * side) as usize, \"input buffer/shape mismatch\");\nlet (data, _) = input.into_raw_vec_and_offset();\nlet tensor = ort::value::Tensor::from_array((vec![1, 3, side, side], data))?;","handlingStrategy":"validation","validationCode":"let expected = 3 * p.size * p.size;\nlet input = normalize_input(img, p);\nif input.len() != expected {\n    return Err(anyhow!(\"input buffer {} != expected {} (1x3x{}x{})\", input.len(), expected, p.size, p.size));\n}","typeGuard":"fn is_valid_input(buf: &[f32], side: usize) -> bool {\n    buf.len() == 3 * side * side && buf.iter().all(|v| v.is_finite())\n}","tryCatchPattern":null,"preventionTips":["Keep shape and data derived from the same params value","Assert buffer length against 3*side*side in tests for normalize_input","Never pass 4-channel RGBA buffers as 3-channel NCHW input"],"tags":["onnx","tensor","rust","ort"],"backgroundTag":"tensor-shape-mismatch","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"}