Browse Source

fuse onnx unsqueeze-prelu

tags/20200106
nihuini 6 years ago
parent
commit
66b8a7ee36
1 changed files with 54 additions and 6 deletions
  1. +54
    -6
      tools/onnx/onnx2ncnn.cpp

+ 54
- 6
tools/onnx/onnx2ncnn.cpp View File

@@ -495,9 +495,6 @@ static void fuse_hardswish(onnx::GraphProto* mutable_graph, std::map<std::string
if (node2->op_type() != "Mul")
continue;

if (node_reference.find(node2->output(0)) == node_reference.end() || node_reference[node2->output(0)] != 1)
continue;

if (node2->input(0) != node->input(0) || node2->input(1) != node->output(0))
continue;

@@ -635,9 +632,6 @@ static void fuse_batchnorm1d_squeeze_unsqueeze(onnx::GraphProto* mutable_graph,
if (node_reference.find(node2->output(0)) == node_reference.end() || node_reference[node2->output(0)] != 1)
continue;

if (node_reference.find(node3->output(0)) == node_reference.end() || node_reference[node3->output(0)] != 1)
continue;

if (node2->input(0) != node->output(0) || node3->input(0) != node2->output(0))
continue;

@@ -659,6 +653,59 @@ static void fuse_batchnorm1d_squeeze_unsqueeze(onnx::GraphProto* mutable_graph,
}
}

static void fuse_unsqueeze_prelu(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, onnx::TensorProto>& binaryop_weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count, std::vector<std::string>& reduced_binaryop_weights)
{
int node_count = mutable_graph->node_size();
for (int i=0; i<node_count; i++)
{
onnx::NodeProto* node = mutable_graph->mutable_node(i);

// PReLU <= Unsqueeze - PReLU
if (node->op_type() == "Unsqueeze")
{
// check weight
if (weights.find(node->input(0)) == weights.end())
continue;

onnx::TensorProto& B = weights[node->input(0)];
if (B.dims_size() != 1)
continue;

if (node_reference.find(node->output(0)) == node_reference.end() || node_reference[node->output(0)] != 1)
continue;

// axes = (1, 2)
std::vector<int> axes = get_node_attr_ai(*node, "axes");
if (axes.size() != 2)
continue;
if (axes[0] != 1 || axes[1] != 2)
continue;

if (i+1 >= node_count)
continue;

onnx::NodeProto* node2 = mutable_graph->mutable_node(i+1);

if (node2->op_type() != "PRelu")
continue;

if (node2->input(1) != node->output(0))
continue;

// reduce
node->set_op_type("noop_reducedncnn");

node_reference.erase(node_reference.find(node->output(0)));
blob_names.erase(node->output(0));

node2->set_input(1, node->input(0));

reduced_node_count += 1;
i += 1;
}
}
}

int main(int argc, char** argv)
{
const char* onnxpb = argv[1];
@@ -852,6 +899,7 @@ int main(int argc, char** argv)
fuse_hardsigmoid (mutable_graph, weights, binaryop_weights, node_reference, blob_names, reduced_node_count, reduced_binaryop_weights);
fuse_hardswish (mutable_graph, weights, binaryop_weights, node_reference, blob_names, reduced_node_count, reduced_binaryop_weights);
fuse_batchnorm1d_squeeze_unsqueeze(mutable_graph, weights, binaryop_weights, node_reference, blob_names, reduced_node_count, reduced_binaryop_weights);
fuse_unsqueeze_prelu(mutable_graph, weights, binaryop_weights, node_reference, blob_names, reduced_node_count, reduced_binaryop_weights);

// remove node_reference entry with reference equals to one
int splitncnn_blob_count = 0;


Loading…
Cancel
Save