导出模型与使用#
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);
Note
For visionflow::DataEdge::kParam
use visionflow::Model::get_param()
,
for visionflow::DataEdge::kProp
use visionflow::Model::get_context()
.
The same for visionflow::DataEdge::kParam
use visionflow::Model::set_param()
,
for visionflow::DataEdge::kProp
use visionflow::Model::set_context()
.
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();
获取工具的连接关系#
首先获取到 tool list
visionflow::Model::tool_list()
- 然后我们可以通过
visionflow::Model::tool_info()
获取到一个工具的具体信息visionflow::ToolInfo
通过
visionflow::ToolInfo::input_edges()
获取得到一个工具中都有哪些输入数据节点
- 然后我们可以通过
- 最后我们可以通过
visionflow::DataEdge
获取到一个数据节点的具体信息: 通过
visionflow::DataEdge::redirects()
我们便可以获取得到有哪些数据节点与之存在着连接关系
- 最后我们可以通过
通过以上步骤,我们即可建立起不同工具间的节点连接关系,具体示例如下:
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::Model::real_id()
需要注意的是,对于综合判断工具中的输入节点其可能对应多个不同工具的输出节点,因此对于visionflow::ToolNodeId
其有一个后缀的概念,在执行visionflow::Model::real_id()
时可以通过visionflow::ToolNodeId::operator[]()
接口添加一个下标后缀,应精确标识用于获取哪一个连接节点关于一个
visionflow::ToolNodeId
对象是否有后缀?具体下标后缀是什么?可以分别通过visionflow::ToolNodeId::has_index()
和visionflow::ToolNodeId::index()
获取得到
节点信息的获取#
- 节点信息的获取:
输入节点:可通过
visionflow::ToolInfo::input_edges()
获取得到输出节点:可通过
visionflow::ToolInfo::output_edges()
获取得到数据节点:可通过
visionflow::ToolInfo::data_edges()
获取得到计算节点:可通过
visionflow::ToolInfo::compute_nodes()
获取得到
数据节点#
什么是数据节点?#
工具由节点和边组成。边是节点之间的数据流,节点是数据处理单元。我们的数据包括两类:用于配置数据处理单元的参数,以及数据处理单元生成的样本 visionflow::Sample
的属性。
数据节点详细信息的获取#
- 可通过
visionflow::DataEdge
获取得到数据节点的详细信息: 获取数据节点的 id:
visionflow::DataEdge::id()
获取数据节点的数据类型:
visionflow::DataEdge::type()
获取数据节点的 concept type
visionflow::DataEdge::DataCptTag
:visionflow::DataEdge::cpt_tag()
获取数据节点的最大连接数:
visionflow::DataEdge::allow_max_connect()
获取数据节点的最后一次更新时间:
visionflow::DataEdge::last_update_time()
判断数据节点是否是输出节点:
visionflow::DataEdge::is_tool_output()
- 判断数据节点是否是虚拟节点(虚拟节点:其是一个工具的输入数据节点但又只能由其他工具生成):
visionflow::DataEdge::is_virtual_port()
- 当节点为虚拟节点时才有效的方法(对于非虚拟节点返回数据为空):
获取节点所需要的 feature names,只有提供了所需特征类型的节点才能够与之相连接
visionflow::DataEdge::required_features()
获取哪些工具中的哪些输出节点连接到了此数据节点
visionflow::DataEdge::redirects()
- 当节点为非虚拟节点时才有效的方法(对于虚拟节点返回数据为空):
获取哪些计算节点可以生成此数据节点:
visionflow::DataEdge::used_by()
获取哪些计算节点用到了此数据节点:
visionflow::DataEdge::generated_by()
获取数据节点是否处于可用状态:
visionflow::DataEdge::is_active()
- 判断数据节点是否是虚拟节点(虚拟节点:其是一个工具的输入数据节点但又只能由其他工具生成):
计算节点#
什么是计算节点?#
计算节点是工具中的核心数据处理单元。计算节点将接收其他计算节点生成的参数和属性,并处理数据以生成新的参数或属性。
计算节点详细信息的获取#
- 可通过
visionflow::ComputeNode
获取得到计算节点的详细信息: 获取计算节点的 id:
visionflow::ComputeNode::id()
获取计算节点的数据类型:
visionflow::ComputeNode::type()
获取计算节点的最后一次更新时间:
visionflow::ComputeNode::last_update_time()
获取计算节点所需要的参数数据节点:
visionflow::ComputeNode::param_nodes()
获取计算节点所需要的属性数据节点:
visionflow::ComputeNode::input_nodes()
获取计算节点所生成的数据节点:
visionflow::ComputeNode::output_nodes()
获取计算节点的 concept type
visionflow::ComputeNode::ComputeCptTag
:visionflow::ComputeNode::cpt_tag()