From 0cbedb24e4cc22b542f3e4e06dce1f43a8090e35 Mon Sep 17 00:00:00 2001 From: michaelbguo Date: Thu, 25 Dec 2025 21:10:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96RMBG2.0=E5=9B=BE?= =?UTF-8?q?=E5=83=8F=E9=A2=84=E5=A4=84=E7=90=86=E6=B5=81=E7=A8=8B=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E5=89=8D=E8=BD=AC=E6=8D=A2RGB=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BB=A5=E5=87=8F=E5=B0=91=E9=87=8D=E5=A4=8D=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/rmbg2_0/generate.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/models/rmbg2_0/generate.rs b/src/models/rmbg2_0/generate.rs index 6306db4..49337d6 100644 --- a/src/models/rmbg2_0/generate.rs +++ b/src/models/rmbg2_0/generate.rs @@ -85,12 +85,13 @@ impl RMBG2_0 { return Ok(vec![]); } - // 并行预处理:提取原始尺寸和转换为 tensor + // 并行预处理:提取原始尺寸、RGB 数据和转换为 tensor let preprocessed: Vec<_> = imgs .par_iter() .map(|img| { let height = img.height(); let width = img.width(); + let rgb_img = img.to_rgb8(); let tensor = img_transform_with_resize( img, self.h, @@ -100,17 +101,17 @@ impl RMBG2_0 { &self.device, self.dtype, ); - (img.clone(), height, width, tensor) + (rgb_img, height, width, tensor) }) .collect(); // 检查预处理是否有错误 let mut tensors = Vec::with_capacity(preprocessed.len()); let mut meta: Vec<_> = Vec::with_capacity(preprocessed.len()); - for (img, height, width, tensor_result) in preprocessed { + for (rgb_img, height, width, tensor_result) in preprocessed { let tensor = tensor_result?; tensors.push(tensor); - meta.push((img, height, width)); + meta.push((rgb_img, height, width)); } // 批量推理:将所有图片合并为一个 batch @@ -121,14 +122,13 @@ impl RMBG2_0 { let results: Vec> = meta .into_par_iter() .enumerate() - .map(|(i, (img, height, width))| { + .map(|(i, (rgb_img, height, width))| { let rmbg_tensor = batch_output.i(i)?; let alpha_img = float_tensor_to_dynamic_image(&rmbg_tensor)?; let alpha_img = alpha_img.resize_exact(width, height, image::imageops::FilterType::CatmullRom); let alpha_gray = alpha_img.to_luma8(); - let rgb_img = img.to_rgb8(); let rgb_raw = rgb_img.as_raw(); let alpha_raw = alpha_gray.as_raw(); let pixel_count = (width * height) as usize;