1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2025 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

// ncnn model exported from https://github.com/PeterL1n/RobustVideoMatting
//
// import torch
// from torch import nn
// from model import MattingNetwork
// from model.fast_guided_filter import FastGuidedFilterRefiner
// from model.deep_guided_filter import DeepGuidedFilterRefiner
//
// class Model(nn.Module):
//     def __init__(self):
//         super().__init__()
//
//         self.rvm = MattingNetwork('mobilenetv3').eval()
//         self.rvm.load_state_dict(torch.load('rvm_mobilenetv3.pth'))
//
//         self.refiner_deep = DeepGuidedFilterRefiner()
//         self.refiner_fast = FastGuidedFilterRefiner()
//
//     def forward_first_frame(self, src):
//         return self.rvm(src)
//
//     def forward(self, src, src_sm, r1, r2, r3, r4):
//
//         f1, f2, f3, f4 = self.rvm.backbone(src_sm)
//         f4 = self.rvm.aspp(f4)
//         hid, *rec = self.rvm.decoder(src_sm, f1, f2, f3, f4, r1, r2, r3, r4)
//
//         # downsample
//         fgr_residual, pha = self.rvm.project_mat(hid).split([3, 1], dim=-3)
//         fgr = fgr_residual + src_sm
//
//         # downsample + refiner_deep
//         fgr_residual_deep, pha_deep = self.refiner_deep(src, src_sm, fgr_residual, pha, hid)
//         fgr_deep = fgr_residual_deep + src
//
//         # downsample + refiner_fast
//         fgr_residual_fast, pha_fast = self.refiner_fast(src, src_sm, fgr_residual, pha, hid)
//         fgr_fast = fgr_residual_fast + src
//
//         # downsample + segmentation
//         seg = self.rvm.project_seg(hid)
//
//         return fgr, pha, fgr_deep, pha_deep, fgr_fast, pha_fast, seg, *rec
//
// import pnnx
//
// model = Model().eval()
//
// x = torch.rand(1, 3, 512, 512)
// x2 = torch.rand(1, 3, 256, 256)
// x2_hr = torch.rand(1, 3, 1024, 1024)
//
// # generate feats via forward_first_frame, with different shapes
// fgr, pha, r1, r2, r3, r4 = model.forward_first_frame(x)
// fgr2, pha2, r12, r22, r32, r42 = model.forward_first_frame(x2)
//
// # export with dynamic shape
// pnnx.export(model, "rvm_mobilenetv3.pt", (x, x, r1, r2, r3, r4), (x2_hr, x2, r12, r22, r32, r42))
//
// and then fix refiner_fast fp16 overflow issue in ncnn.param via appending 31=1 layer feat mask
//
// BinaryOp   div_58    2 1 401 399 402 0=3 31=1
//

#include "rvm.h"

#include "net.h"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <algorithm>

RVM::RVM()
{
    model_type = 0;
    target_size = 512;
    intra_inter = 0;
    segmentation = false;
    refine_deep = true;
    refine_fast = false;
}

RVM::~RVM()
{
}

int RVM::load(const char* parampath, const char* modelpath, bool use_gpu)
{
    rvm.clear();

    // rvm.opt = ncnn::Option();

    rvm.opt.use_fp16_packed = false;
    rvm.opt.use_fp16_storage = false;
    rvm.opt.use_fp16_arithmetic = false;

#if NCNN_VULKAN
    rvm.opt.use_vulkan_compute = use_gpu;
#endif

    rvm.load_param(parampath);
    rvm.load_model(modelpath);

    return 0;
}

void RVM::set_background_image(const cv::Mat& background)
{
    if (!background.empty())
    {
        // Convert to BGR format if needed and ensure it's 8-bit 3-channel
        cv::Mat bg_bgr;
        if (background.channels() == 4)
        {
            cv::cvtColor(background, bg_bgr, cv::COLOR_BGRA2BGR);
        }
        else if (background.channels() == 1)
        {
            cv::cvtColor(background, bg_bgr, cv::COLOR_GRAY2BGR);
        }
        else
        {
            bg_bgr = background.clone();
        }
        
        background_image = bg_bgr;
    }
}

void RVM::clear_background_image()
{
    background_image.release();
}

int RVM::load(AAssetManager* mgr, const char* parampath, const char* modelpath, bool use_gpu)
{
    rvm.clear();

    // rvm.opt = ncnn::Option();

    rvm.opt.use_fp16_packed = false;
    rvm.opt.use_fp16_storage = false;
    rvm.opt.use_fp16_arithmetic = false;

#if NCNN_VULKAN
    rvm.opt.use_vulkan_compute = use_gpu;
#endif

    rvm.load_param(mgr, parampath);
    rvm.load_model(mgr, modelpath);

    return 0;
}

void RVM::set_model_type(int _model_type)
{
    model_type = _model_type;
}

void RVM::set_target_size(int _target_size)
{
    target_size = _target_size;
}

void RVM::set_intra_inter(int _intra_inter)
{
    intra_inter = _intra_inter;
}

void RVM::set_postproc_mode(bool _segmentation, bool _refine_deep, bool _refine_fast)
{
    segmentation = _segmentation;
    refine_deep = _refine_deep;
    refine_fast = _refine_fast;
}

int RVM::detect(const cv::Mat& rgb, InterFeatures& feats, cv::Mat& fgr, cv::Mat& pha, cv::Mat& seg)
{
    const int w = rgb.cols;
    const int h = rgb.rows;

    const int max_stride = 16;

    bool refine_deep = true;
    // bool refine_fast = true;

    const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};

    ncnn::Mat in_pad;
    ncnn::Mat in_small_pad;

    int wpad = 0;
    int hpad = 0;

    bool downsample = std::max(w, h) > target_size;
    if (downsample)
    {
        // letterbox pad to multiple of max_stride
        int w2 = w;
        int h2 = h;
        float scale = 1.f;
        if (w > h)
        {
            scale = (float)target_size / w;
            w2 = target_size;
            h2 = h2 * scale;
        }
        else
        {
            scale = (float)target_size / h;
            h2 = target_size;
            w2 = w2 * scale;
        }

        ncnn::Mat in_small = ncnn::Mat::from_pixels_resize(rgb.data, ncnn::Mat::PIXEL_RGB, w, h, w2, h2);

        // letterbox pad to target_size rectangle
        int w2pad = (w2 + max_stride - 1) / max_stride * max_stride - w2;
        int h2pad = (h2 + max_stride - 1) / max_stride * max_stride - h2;
        ncnn::copy_make_border(in_small, in_small_pad, h2pad / 2, h2pad - h2pad / 2, w2pad / 2, w2pad - w2pad / 2, ncnn::BORDER_CONSTANT, 114.f);

        in_small_pad.substract_mean_normalize(0, norm_vals);

        int w3 = w;
        int h3 = h;
        if (w > h)
        {
            w3 = w;
            h3 = in_small_pad.h / scale;
            wpad = 0;
            hpad = h3 - h;
        }
        else
        {
            h3 = h;
            w3 = in_small_pad.w / scale;
            wpad = w3 - w;
            hpad = 0;
        }

        ncnn::Mat in = ncnn::Mat::from_pixels(rgb.data, ncnn::Mat::PIXEL_RGB, w, h);

        ncnn::copy_make_border(in, in_pad, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, ncnn::BORDER_CONSTANT, 114.f);

        in_pad.substract_mean_normalize(0, norm_vals);
    }
    else
    {
        ncnn::Mat in = ncnn::Mat::from_pixels(rgb.data, ncnn::Mat::PIXEL_RGB, w, h);

        // letterbox pad to target_size rectangle
        wpad = (w + max_stride - 1) / max_stride * max_stride - w;
        hpad = (h + max_stride - 1) / max_stride * max_stride - h;
        ncnn::copy_make_border(in, in_pad, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, ncnn::BORDER_CONSTANT, 114.f);

        in_pad.substract_mean_normalize(0, norm_vals);

        in_small_pad = in_pad;
    }

    bool is_first_frame = false;
    if (feats.r1.empty() && feats.r2.empty() && feats.r3.empty() && feats.r4.empty())
    {
        is_first_frame = true;
    }

    if (model_type == 0)
    {
        // rvm_mobilenetv3
        feats.r1.create(in_small_pad.w / 2, in_small_pad.h / 2, 16);
        feats.r2.create(in_small_pad.w / 4, in_small_pad.h / 4, 20);
        feats.r3.create(in_small_pad.w / 8, in_small_pad.h / 8, 40);
        feats.r4.create(in_small_pad.w / 16, in_small_pad.h / 16, 64);
    }
    else // if (model_type == 1)
    {
        // rvm_resnet50
        feats.r1.create(in_small_pad.w / 2, in_small_pad.h / 2, 16);
        feats.r2.create(in_small_pad.w / 4, in_small_pad.h / 4, 32);
        feats.r3.create(in_small_pad.w / 8, in_small_pad.h / 8, 64);
        feats.r4.create(in_small_pad.w / 16, in_small_pad.h / 16, 128);
    }

    if (intra_inter == 0 || is_first_frame)
    {
        feats.r1.fill(0.f);
        feats.r2.fill(0.f);
        feats.r3.fill(0.f);
        feats.r4.fill(0.f);
    }

    ncnn::Extractor ex = rvm.create_extractor();

    ex.input("in0", in_pad);
    ex.input("in1", in_small_pad);

    ex.input("in2", feats.r1);
    ex.input("in3", feats.r2);
    ex.input("in4", feats.r3);
    ex.input("in5", feats.r4);

    ncnn::Mat out_fgr;
    ncnn::Mat out_pha;
    ncnn::Mat out_seg;

    if (segmentation)
    {
        // segmentation
        ex.extract("out6", out_seg);
    }
    else if (downsample)
    {
        if (refine_deep)
        {
            // downsample + refine deep
            ex.extract("out2", out_fgr);
            ex.extract("out3", out_pha);
        }
        else // if (refine_fast)
        {
            // downsample + refine fast
            ex.extract("out4", out_fgr);
            ex.extract("out5", out_pha);
        }
    }
    else
    {
        // no downsample
        ex.extract("out0", out_fgr);
        ex.extract("out1", out_pha);
    }

    if (intra_inter == 1)
    {
        // feats
        ex.extract("out7", feats.r1, 1);
        ex.extract("out8", feats.r2, 1);
        ex.extract("out9", feats.r3, 1);
        ex.extract("out10", feats.r4, 1);
    }

    const float denorm_vals[3] = {255.f, 255.f, 255.f};

    if (segmentation)
    {
        out_seg.substract_mean_normalize(0, denorm_vals);
        seg.create(in_pad.h, in_pad.w, CV_8UC1);
        out_seg.to_pixels_resize(seg.data, ncnn::Mat::PIXEL_GRAY, in_pad.w, in_pad.h);

        // cut letterbox pad
        seg = seg(cv::Rect(wpad / 2, hpad / 2, w, h));
    }
    else
    {
        out_fgr.substract_mean_normalize(0, denorm_vals);
        fgr.create(out_fgr.h, out_fgr.w, CV_8UC3);
        out_fgr.to_pixels(fgr.data, ncnn::Mat::PIXEL_RGB);

        out_pha.substract_mean_normalize(0, denorm_vals);
        pha.create(out_pha.h, out_pha.w, CV_8UC1);
        out_pha.to_pixels(pha.data, ncnn::Mat::PIXEL_GRAY);

        // cut letterbox pad
        fgr = fgr(cv::Rect(wpad / 2, hpad / 2, w, h));
        pha = pha(cv::Rect(wpad / 2, hpad / 2, w, h));
    }

    return 0;
}

int RVM::draw(cv::Mat& rgb, const cv::Mat& fgr, const cv::Mat& pha, const cv::Mat& seg)
{
    const int w = rgb.cols;
    const int h = rgb.rows;

    // Default background color (RGB: 120, 255, 155)
    const cv::Vec3b default_bg_color(120, 255, 155);

    if (!fgr.empty() && !pha.empty())
    {
        // composite
        for (int y = 0; y < h; y++)
        {
            const uchar* pf = fgr.ptr<const uchar>(y);
            const uchar* pa = pha.ptr<const uchar>(y);
            uchar* p = rgb.ptr<uchar>(y);
            
            for (int x = 0; x < w; x++)
            {
                const float alpha = pa[0] / 255.f;
                const float beta = 1 - alpha;
                
                cv::Vec3b bg_pixel;
                if (!background_image.empty())
                {
                    // Use background image pixel
                    int bg_x = x * background_image.cols / w;
                    int bg_y = y * background_image.rows / h;
                    bg_x = std::min(bg_x, background_image.cols - 1);
                    bg_y = std::min(bg_y, background_image.rows - 1);
                    bg_pixel = background_image.at<cv::Vec3b>(bg_y, bg_x);
                    std::swap(bg_pixel[0], bg_pixel[2]);
                }
                else
                {
                    // Use default background color
                    bg_pixel = default_bg_color;
                }
                
                p[0] = cv::saturate_cast<uchar>(pf[0] * alpha + bg_pixel[0] * beta);
                p[1] = cv::saturate_cast<uchar>(pf[1] * alpha + bg_pixel[1] * beta);
                p[2] = cv::saturate_cast<uchar>(pf[2] * alpha + bg_pixel[2] * beta);
                
                pf += 3;
                pa += 1;
                p += 3;
            }
        }
    }
    else if (!seg.empty())
    {
        // composite seg
        for (int y = 0; y < h; y++)
        {
            uchar* p = rgb.ptr<uchar>(y);
            const uchar* ps = seg.ptr<const uchar>(y);
            
            for (int x = 0; x < w; x++)
            {
                const float alpha = ps[0] / 255.f;
                const float beta = 1 - alpha;
                
                cv::Vec3b bg_pixel;
                if (!background_image.empty())
                {
                    // Use background image pixel
                    int bg_x = x * background_image.cols / w;
                    int bg_y = y * background_image.rows / h;
                    bg_x = std::min(bg_x, background_image.cols - 1);
                    bg_y = std::min(bg_y, background_image.rows - 1);
                    bg_pixel = background_image.at<cv::Vec3b>(bg_y, bg_x);
                    std::swap(bg_pixel[0], bg_pixel[2]);
                }
                else
                {
                    // Use default background color
                    bg_pixel = default_bg_color;
                }
                
                p[0] = cv::saturate_cast<uchar>(p[0] * alpha + bg_pixel[0] * beta);
                p[1] = cv::saturate_cast<uchar>(p[1] * alpha + bg_pixel[1] * beta);
                p[2] = cv::saturate_cast<uchar>(p[2] * alpha + bg_pixel[2] * beta);
                
                ps += 1;
                p += 3;
            }
        }
    }

    return 0;
}