igor-yusupov / rusty-yolo

Apache License 2.0
13 stars 3 forks source link

called `Result::unwrap()` on an `Err` value: Kind("incoherent elt kind, Ok(Float) != Double") #10

Closed lujing-jlu closed 7 months ago

lujing-jlu commented 7 months ago

Device: Cuda(0) thread 'main' panicked at /home/jlu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tch-0.15.0/src/wrappers/tensor.rs:422:38: called Result::unwrap() on an Err value: Kind("incoherent elt kind, Ok(Float) != Double") note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

RUST_BACKTRACE=full cargo run --example simple_inference Compiling libc v0.2.153 Compiling cfg-if v1.0.0 Compiling subtle v2.5.0 Compiling cpufeatures v0.2.12 Compiling base64ct v1.6.0 Compiling powerfmt v0.2.0 Compiling adler v1.0.2 Compiling time-core v0.1.2 Compiling num-conv v0.1.0 Compiling constant_time_eq v0.1.5 Compiling byteorder v1.5.0 Compiling rawpointer v0.2.1 Compiling itoa v1.0.11 Compiling ryu v1.0.17 Compiling ppv-lite86 v0.2.17 Compiling lazy_static v1.4.0 Compiling typenum v1.17.0 Compiling num-traits v0.2.18 Compiling serde v1.0.197 Compiling thiserror v1.0.58 Compiling crc32fast v1.4.0 Compiling half v2.4.1 Compiling matrixmultiply v0.3.8 Compiling miniz_oxide v0.7.2 Compiling deranged v0.3.11 Compiling password-hash v0.4.2 Compiling flate2 v1.0.28 Compiling jobserver v0.1.30 Compiling getrandom v0.2.14 Compiling generic-array v0.14.7 Compiling rand_core v0.6.4 Compiling cc v1.0.94 Compiling num-integer v0.1.46 Compiling num-complex v0.4.5 Compiling rand_chacha v0.3.1 Compiling rand v0.8.5 Compiling time v0.3.36 Compiling ndarray v0.15.6 Compiling crypto-common v0.1.6 Compiling block-buffer v0.10.4 Compiling inout v0.1.3 Compiling cipher v0.4.4 Compiling digest v0.10.7 Compiling aes v0.8.4 Compiling sha2 v0.10.8 Compiling hmac v0.12.1 Compiling sha1 v0.10.6 Compiling pbkdf2 v0.11.0 Compiling zstd-sys v2.0.10+zstd.1.5.6 Compiling bzip2-sys v0.1.11+1.0.8 Compiling bzip2 v0.4.4 Compiling serde_json v1.0.115 Compiling safetensors v0.3.3 Compiling zstd-safe v5.0.2+zstd.1.5.2 Compiling zstd v0.11.2+zstd.1.5.2 Compiling zip v0.6.6 Compiling torch-sys v0.15.0 Compiling tch v0.15.0 Compiling rusty-yolo v0.1.0 (/home/jlu/projects/rust/rusty-yolo) Finished dev [unoptimized + debuginfo] target(s) in 54.83s Running target/debug/examples/simple_inference Device: Cuda(0) thread 'main' panicked at /home/jlu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tch-0.15.0/src/wrappers/tensor.rs:422:38: called Result::unwrap() on an Err value: Kind("incoherent elt kind, Ok(Float) != Double") stack backtrace: 0: 0x557d74fcef76 - std::backtrace_rs::backtrace::libunwind::trace::hbee8a7973eeb6c93 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x557d74fcef76 - std::backtrace_rs::backtrace::trace_unsynchronized::hc8ac75eea3aa6899 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x557d74fcef76 - std::sys_common::backtrace::_print_fmt::hc7f3e3b5298b1083 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:68:5 3: 0x557d74fcef76 - ::fmt::hbb235daedd7c6190 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:44:22 4: 0x557d74fee4e0 - core::fmt::rt::Argument::fmt::h76c38a80d925a410 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/rt.rs:142:9 5: 0x557d74fee4e0 - core::fmt::write::h3ed6aeaa977c8e45 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/mod.rs:1120:17 6: 0x557d74fcd35f - std::io::Write::write_fmt::h78b18af5775fedb5 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/io/mod.rs:1810:15 7: 0x557d74fced54 - std::sys_common::backtrace::_print::h5d645a07e0fcfdbb at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:47:5 8: 0x557d74fced54 - std::sys_common::backtrace::print::h85035a511aafe7a8 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:34:9 9: 0x557d74fd02b7 - std::panicking::default_hook::{{closure}}::hcce8cea212785a25 10: 0x557d74fd0019 - std::panicking::default_hook::hf5fcb0f213fe709a at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:292:9 11: 0x557d74fd0748 - std::panicking::rust_panic_with_hook::h095fccf1dc9379ee at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:779:13 12: 0x557d74fd0622 - std::panicking::begin_panic_handler::{{closure}}::h032ba12139b353db at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:657:13 13: 0x557d74fcf476 - std::sys_common::backtrace::rust_end_short_backtrace::h9259bc2ff8fd0f76 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:171:18 14: 0x557d74fd0380 - rust_begin_unwind at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5 15: 0x557d74f4c935 - core::panicking::panic_fmt::h784f20a50eaab275 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14 16: 0x557d74f4cda3 - core::result::unwrap_failed::h03d8a5018196e1cd at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1649:5 17: 0x557d74f5f1cd - core::result::Result<T,E>::unwrap::h04c638ed48ca3c71 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1073:23 18: 0x557d74f53b21 - tch::wrappers::tensor::Tensor::copy_data::h16d4cc76846fc0f4 at /home/jlu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tch-0.15.0/src/wrappers/tensor.rs:422:9 19: 0x557d74f501ab - rusty_yolo::yolo::YOLO::non_max_suppression::hea056dedcf6eba60 at /home/jlu/projects/rust/rusty-yolo/src/yolo.rs:132:13 20: 0x557d74f4ec1a - rusty_yolo::yolo::YOLO::predict::h8331ed9ab4800003 at /home/jlu/projects/rust/rusty-yolo/src/yolo.rs:76:22 21: 0x557d74f4e33f - simple_inference::main::h4974ef197a91b833 at /home/jlu/projects/rust/rusty-yolo/examples/simple_inference/main.rs:16:23 22: 0x557d74f4d82b - core::ops::function::FnOnce::call_once::h6bb3ca89bba668a7 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5 23: 0x557d74f4d68e - std::sys_common::backtrace::__rust_begin_short_backtrace::h9595e56297644998 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:155:18 24: 0x557d74f4d191 - std::rt::lang_start::{{closure}}::h026d6d354bc1d5aa at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/rt.rs:166:18 25: 0x557d74fca6c1 - core::ops::function::impls::<impl core::ops::function::FnOnce for &F>::call_once::h37600b1e5eea4ecd at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:284:13 26: 0x557d74fca6c1 - std::panicking::try::do_call::hb4bda49fa13a0c2b at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:552:40 27: 0x557d74fca6c1 - std::panicking::try::h8bbf75149211aaaa at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:516:19 28: 0x557d74fca6c1 - std::panic::catch_unwind::h8c78ec68ebea34cb at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panic.rs:142:14 29: 0x557d74fca6c1 - std::rt::lang_start_internal::{{closure}}::hffdf44a19fd9e220 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/rt.rs:148:48 30: 0x557d74fca6c1 - std::panicking::try::do_call::hcb3194972c74716d at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:552:40 31: 0x557d74fca6c1 - std::panicking::try::hcdc6892c5f0dba4c at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:516:19 32: 0x557d74fca6c1 - std::panic::catch_unwind::h4910beb4573f4776 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panic.rs:142:14 33: 0x557d74fca6c1 - std::rt::lang_start_internal::h6939038e2873596b at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/rt.rs:148:20 34: 0x557d74f4d16a - std::rt::lang_start::ha3f0ef703536f73f at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/rt.rs:165:17 35: 0x557d74f4e51e - main 36: 0x7f970fa0d083 - libc_start_main at /build/glibc-wuryBv/glibc-2.31/csu/../csu/libc-start.c:308:16 37: 0x557d74f4d06e - _start 38: 0x0 -

lujing-jlu commented 7 months ago

` fn non_max_suppression( &self, pred: &tch::Tensor, conf_thresh: f64, iou_thresh: f64, ) -> Vec { let (npreds, pred_size) = pred.size2().unwrap(); let nclasses = (pred_size - 5) as usize;

    let mut bboxes: Vec<Vec<BBox>> = (0..nclasses).map(|_| vec![]).collect();

    for index in 0..npreds {
        let pred_index: tch::Tensor = pred.get(index);
        // let pred: Vec<f64> = Vec::<f64>::from(pred_index);
        let len = pred_index.size1().unwrap() as usize;
        let mut pred_vec = vec![0f32; len]; // Create a vector of the appropriate size.
        pred_index.copy_data(&mut pred_vec, len); // Copy the tensor data into the vector.
        let pred: Vec<f64> = pred_vec.iter().map(|x| *x as f64).collect();

        let confidence = pred[4];

        if confidence > conf_thresh {
            let mut class_index = 0;

            for i in 0..nclasses {
                if pred[5 + i] > pred[5 + class_index] {
                    class_index = i;
                }
            }

            if pred[5 + class_index] > 0. {
                let bbox = BBox {
                    xmin: pred[0] - pred[2] / 2.,
                    ymin: pred[1] - pred[3] / 2.,
                    xmax: pred[0] + pred[2] / 2.,
                    ymax: pred[1] + pred[3] / 2.,
                    conf: confidence,
                    cls: class_index,
                };
                bboxes[class_index].push(bbox)
            }
        }
    }

    for bboxes_for_class in bboxes.iter_mut() {
        bboxes_for_class.sort_by(|b1, b2| b2.conf.partial_cmp(&b1.conf).unwrap());

        let mut current_index = 0;
        for index in 0..bboxes_for_class.len() {
            let mut drop = false;
            for prev_index in 0..current_index {
                let iou = self.iou(&bboxes_for_class[prev_index], &bboxes_for_class[index]);

                if iou > iou_thresh {
                    drop = true;
                    break;
                }
            }
            if !drop {
                bboxes_for_class.swap(current_index, index);
                current_index += 1;
            }
        }
        bboxes_for_class.truncate(current_index);
    }

    let mut result = vec![];

    for bboxes_for_class in bboxes.iter() {
        for bbox in bboxes_for_class.iter() {
            result.push(*bbox);
        }
    }

    return result;
}`