Skip to main content Scroll Top

BEN2 (default)

C++ (Windows, Linux, MacOS / CUDA and Metal accelerated) port of https://github.com/PramaLLC/BEN2.git.

Example Input & Outputs

Inputs Outputs
Input
Image Input
Input
Matte

Demo Code

 1#include "blace_ai.h"
 2#include <opencv2/opencv.hpp>
 3
 4// include the models you want to use
 5#include "ben2_v1_default_v1_ALL_export_version_v26.h"
 6
 7using namespace blace;
 8int main() {
 9  ::workload_management::BlaceWorld blace;
10
11  // load image into op
12  auto exe_path = util::getPathToExe();
13  std::filesystem::path photo_path = exe_path / "person.jpg";
14  auto world_tensor_orig =
15      CONSTRUCT_OP(ops::FromImageFileOp(photo_path.string()));
16
17  // interpolate to size consumable by model
18  auto interpolated = CONSTRUCT_OP(ops::Interpolate2DOp(
19      world_tensor_orig, 512, 896, ml_core::BICUBIC, false, true));
20
21  // construct model inference arguments
22  ml_core::InferenceArgsCollection infer_args;
23  infer_args.inference_args.backends = {
24      ml_core::TORCHSCRIPT_CUDA_FP16, ml_core::TORCHSCRIPT_MPS_FP16,
25      ml_core::TORCHSCRIPT_CUDA_FP32, ml_core::TORCHSCRIPT_MPS_FP32,
26      ml_core::ONNX_DML_FP32,         ml_core::TORCHSCRIPT_CPU_FP32};
27
28  // construct inference operation
29  auto infer_op = ben2_v1_default_v1_ALL_export_version_v26_run(
30      interpolated, 0, infer_args, util::getPathToExe().string());
31
32  // normalize matte to zero-one range
33  auto normalized_matte =
34      CONSTRUCT_OP(ops::MapToRangeOp(infer_op, ml_core::ZERO_TO_ONE));
35
36  // construct evaluator and evaluate to cv::Mat
37  computation_graph::GraphEvaluator evaluator(normalized_matte);
38  auto [return_code, cv_result] = evaluator.evaluateToCVMat();
39
40  // multiply for plotting
41  cv_result *= 255.;
42
43  // save to disk and return
44  auto out_file = exe_path / "matte.png";
45  cv::imwrite(out_file.string(), cv_result);
46
47  return 0;
48}

Tested on version v1.0.5 of blace.ai sdk. Might also work on newer or older releases (check if release notes of blace.ai state breaking changes).

Quickstart

  1. Download blace.ai SDK and unzip. In the bootstrap script build_run_demos.ps1 (Windows) or build_run_demos.sh (Linux/MacOS) set the BLACE_AI_CMAKE_DIR environment variable to the cmake folder inside the unzipped SDK, e.g. export BLACE_AI_CMAKE_DIR="<unzip_folder>/package/cmake".
  2. Download the model payload(s) (.bin files) from below and place in the same folder as the bootstrapper scripts.
  3. Then run the bootstrap script with
    powershell build_run_demo.ps1 (Windows)
    sh build_run_demo.sh (Linux and MacOS).
    This will build and execute the demo.

Supported Backends

Torchscript CPU Torchscript CUDA FP16 * Torchscript CUDA FP32 * Torchscript MPS FP16 * Torchscript MPS FP32 * ONNX CPU FP32 ONNX DirectML FP32 *

(*: Hardware Accelerated)

Artifacts

Torchscript Payload Demo Project Header

License