SNPE TensorFlow Converter API 笔记

DlcConverter

_resolve_graph_operations_from_model

遍历 Graph 中所有 operation,从 output_nodes 开始遍历,访问的 operation 加入到 visited 列表,并将遍历结果作为列表返回,具体遍历过程如下:

...
for output_op_name in model.out_nodes_names:
    queue = [operations_map[output_op_name]]
    visited = set()
    while len(queue) > 0:
        head = queue.pop(0)
        if head in visited:
            continue
        visited.add(head)

        if head in input_ops:
            continue

        for t in head.inputs:
            queue.append(t.op)

    all_ops_in_paths.update(visited)

从上述代码中可以知道,如果当前队列的 head operation 在 input_ops 中,则不会将该 head operation 加入到 visited 列表中。 head operation 访问完成以后,再将该 head operation 对应的 inputs 加入到待访问列表 queue 中:

for t in head.inputs:
    queue.append(t.op)

_resolve_descriptors_from_nodes

首先找到所有的 layer_resolvers,然后根据 tf.Operation 列表构建 TFGraphBuilder:

resolvers = self._create_layer_resolvers()

constructor = TFGraphBuilder(ops)
constructor.link_nodes()

constructor.link_nodes() 将所有 TFOperationNode 根据 tf.Operation 的 inputs 的关系重新建立起和 TensorFlow Graph 一样的节点联系, link_nodes 主要功能代码如下:

for tf_tensor in node.original_node.inputs:
    input_node = self.nodes_map.get(tf_tensor.op.name, None)
    if input_node is None:
        input_node = NonConsumableTFOperationNode(tf_tensor.op)
        self.nodes_map[tf_tensor.op.name] = input_node
    node.inputs.append(input_node)

上述代码中 original_node 即为 tf.Operation 类型,nodes_maptf.Operation nameTFOperationNode 的组成一个字典。node.inputs.append(input_node) 构建 node 的 input 列表,完成 link 的建立。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值