导出模型与使用#

After training the model and configuring the parameters, you may want to deploy your model to other hosts. One way to do this is by directly copying the Project to another host and using it for deploying your Flow. However the Project contains all the data used for training and validating the model. Its size can be quite large making it inconvenient to copy it directly. VisionFlow supports exporting models to a standalone file for easy deployment.

Export model#

For example, you have a trained visionflow::Project my_first_project that you can easily export to the desired place. visionflow::Project::export_model() also conveniently supports encryption.

my_first_project->export_model("D:/path/to/save/my_first_project.vfmodel");


If everything is successful, you should now be able to find the my_first_project.vfmodel file in the D:/path/to/save/ directory. You can copy this file to the hosts where you need to deploy the Flow.

Note

The suffix of the exported model must be .vfmodel, and .vfmodel will be added automatically to path that do not have .vfmodel suffix.

Open the exported model#

When you deploy a Flow, you may need to make some adjustments to the model. At this point you first need to reload the model, but this time you do not need visionflow::Project. As with export, VisionFlow provides visionflow::Model to load the model and make the model adjustments. Make sure the model path and model extension are valid.

visionflow::Model model("D:/path/to/save/my_first_project.vfmodel");


Modify parameters directly in model#

Now you can use visionflow::Model like visionflow::Project to modify the parameters. Although in practical scenarios, the parameters that can be modified during the deployment phase are limited, VisionFlow still allow the modification of any parameter within the model through the interface. After opening the model, you can modify the parameters by reading them from the model, making the necessary changes, and then saving them back to the model as shown below:

// Read the parameters of the tool according to the name of the tool in your project.
std::string segmentation_id = "Segmentation";
auto filter_param = model.get_param({segmentation_id, "filter.args"});
if (!filter_param) {
    std::cerr << "Filter parameter for Segmentation not exist." << std::endl;
    exit(-1);
}

// We have changed the filter parameters for defects with the category name "MyClass".
filter_param->as<visionflow::param::PolygonsFilterParameters>()
    .get_class_thresholds("MyClass")
    .set_enable(true)
    .set_area_range({100, 50000});

// Then, you can re-save the modified parameters to the model.
model.set_param({segmentation_id, "filter.args"}, *filter_param);


Warning

The parameters set using the visionflow::Model::get_param() interface are only effective within the currently opened model. After you close this model and reopen it, these modifications will be lost. If you want to permanently save these modifications, you need to re-save as a new model.

Re-save model#

Important changes to the model can be made by re-saving it as a new model for later use. visionflow::Model::resave_to() also conveniently supports encryption.

model.resave_to("D:/other/path/resave.vfmodel");


获取模型中的工具列表#

通过 visionflow::Model::tool_list() 即可获取到此模型中的所有工具的 id 列表

// 获取到所有工具的 ids
auto tool_list = model.tool_list();

获取工具的连接关系#

通过以上步骤,我们即可建立起不同工具间的节点连接关系,具体示例如下:

auto tool_list = model.tool_list();

// 用于记录节点间的连接关系
// key: 一个节点的唯一标识
// value: 此节点后都连接了哪些节点
std::map<ToolNodeId, std::vector<ToolNodeId>> node_graph;

// 遍历 tool_list
for (const auto &tool_id : tool_list) {
   // 获取到 tool 的具体信息: tool_info
   auto tool_info = model.tool_info(tool_id);
   // 遍历所有的输入节点 input_edges
   for (const auto &node : tool_info.input_edges()) {
      // 当前节点的 tool_node_id (唯一标识)
      auto current_tool_node_id = ToolNodeId(tool_id, node.id());
      // 如果 real_id
      // 的返回值不是其自身,则说明此输入节点与其他工具的输出节点间存在连接关系
      if (model.real_id(current_tool_node_id) == current_tool_node_id)
          continue;

      // 遍历所有与此输入节点有连接关系的节点(也即其他工具的输出节点)
      for (const auto &tool_node_id : node.redirects()) {
      // tool_node_id 的后面连接了 current_tool_node_id
        node_graph[tool_node_id].emplace_back(current_tool_node_id);
      }
   }
}

Note

节点信息的获取#

节点信息的获取:

数据节点#

什么是数据节点?#

工具由节点和边组成。边是节点之间的数据流,节点是数据处理单元。我们的数据包括两类:用于配置数据处理单元的参数,以及数据处理单元生成的样本 visionflow::Sample 的属性。

数据节点详细信息的获取#

可通过 visionflow::DataEdge 获取得到数据节点的详细信息:

计算节点#

什么是计算节点?#

计算节点是工具中的核心数据处理单元。计算节点将接收其他计算节点生成的参数和属性,并处理数据以生成新的参数或属性。

计算节点详细信息的获取#

可通过 visionflow::ComputeNode 获取得到计算节点的详细信息: