未验证 提交 29e7d3f4 编写于 作者: Z zjd1988 提交者: GitHub

add mish activate op to support yolov4 (#390)

* modify src and test codes for yolov4

* recover crop source code

* add yolov4 example code

* add mish ops and yolov4 example code

* modify example/CMakeList.txt and tm_yolov4.cpp

* delete yolov4 demo codes

* rm yolov4 cmakelist.txt

* Update mish.c
上级 63fcc116
......@@ -121,6 +121,7 @@ enum
OP_UNSQUEEZE,
OP_UPSAMPLE,
OP_ZEROSLIKE,
OP_MISH,
OP_BUILTIN_LAST
};
......
......@@ -114,5 +114,6 @@
#define OP_UNSQUEEZE_NAME "Unsqueeze"
#define OP_UPSAMPLE_NAME "Upsample"
#define OP_ZEROSLIKE_NAME "ZerosLike"
#define OP_MISH_NAME "Mish"
#endif
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, OPEN AI LAB
* Author: 942002795@qq.com
*/
#include <math.h>
#include "sys_port.h"
#include "module.h"
#include "tengine_errno.h"
#include "tengine_log.h"
#include "tengine_ir.h"
#include "../../cpu_node_ops.h"
#include "tengine_op.h"
int ref_mish_fp32(struct ir_tensor* input_tensor, struct ir_tensor* output_tensor, int num_thread)
{
int w = input_tensor->dims[3];
int h = output_tensor->dims[2];
int channels = input_tensor->dims[1];
int size = h * w;
int c_step = h * w;
float* input_data = input_tensor->data;
float* out_data = output_tensor->data;
#pragma omp parallel for num_threads(num_thread)
for (int q = 0; q < channels; q++)
{
float* src = input_data + c_step * q;
float* dst = out_data + c_step * q;
for (int i = 0; i < size; i++)
{
dst[i] = src[i] * tanhf(log(1 + exp(src[i])));
}
}
return 0;
}
static int init_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
return 0;
}
static int release_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
return 0;
}
static int run(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
struct ir_node* ir_node = exec_node->ir_node;
struct ir_graph* ir_graph = ir_node->graph;
struct ir_tensor* input_tensor;
struct ir_tensor* output_tensor;
input_tensor = get_ir_graph_tensor(ir_graph, ir_node->input_tensors[0]);
output_tensor = get_ir_graph_tensor(ir_graph, ir_node->output_tensors[0]);
ref_mish_fp32(input_tensor, output_tensor, exec_graph->num_thread);
return 0;
}
static int reshape(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
struct ir_node* node = exec_node->ir_node;
struct ir_graph* ir_graph = node->graph;
struct ir_tensor* input = get_ir_graph_tensor(ir_graph, node->input_tensors[0]);
struct ir_tensor* output = get_ir_graph_tensor(ir_graph, node->output_tensors[0]);
int ret = set_ir_tensor_shape(output, input->dims, input->dim_num);
return ret;
}
static int score(struct node_ops* node_ops, struct exec_graph* exec_graph, struct ir_node* exec_node)
{
return OPS_SCORE_CANDO;
}
static struct node_ops hcl_node_ops = {.prerun = NULL,
.run = run,
.reshape = reshape,
.postrun = NULL,
.init_node = init_node,
.release_node = release_node,
.score = score};
static int reg_mish_hcl_ops(void* arg)
{
return register_builtin_node_ops(OP_MISH, &hcl_node_ops);
}
static int unreg_mish_hcl_ops(void* arg)
{
return unregister_builtin_node_ops(OP_MISH, &hcl_node_ops);
}
AUTO_REGISTER_OPS(reg_mish_hcl_ops);
AUTO_UNREGISTER_OPS(unreg_mish_hcl_ops);
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, OPEN AI LAB
* Author: 942002795@qq.com
*/
#include <stdio.h>
#include <assert.h>
#include "sys_port.h"
#include "tengine_ir.h"
#include "tengine_errno.h"
#include "tengine_log.h"
#include "tengine_op.h"
#include "parameter.h"
static int infer_shape(struct ir_node* node)
{
struct ir_graph* ir_graph = node->graph;
struct ir_tensor* input = get_ir_graph_tensor(ir_graph, node->input_tensors[0]);
struct ir_tensor* output = get_ir_graph_tensor(ir_graph, node->output_tensors[0]);
set_ir_tensor_shape(output, input->dims, input->dim_num);
return 0;
}
static int init_op(struct ir_op* op)
{
op->same_shape = 0;
op->infer_shape = infer_shape;
return 0;
}
static void release_op(struct ir_op* op) {}
static int register_mish_op(void* arg)
{
struct op_method m;
m.op_version = 1;
m.init_op = init_op;
m.release_op = release_op;
return register_op(OP_MISH, OP_MISH_NAME, &m);
}
static int unregister_mish_op(void* arg)
{
return unregister_op(OP_MISH, 1);
}
AUTO_REGISTER_OP(register_mish_op);
AUTO_UNREGISTER_OP(unregister_mish_op);
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, OPEN AI LAB
* Author: 942002795@qq.com
*/
#include <stdio.h>
#include <stdlib.h>
#include "sys_port.h"
#include "module.h"
#include "tengine_ir.h"
#include "tengine_errno.h"
#include "tengine_log.h"
#include "tengine_serializer.h"
#include "tm2_serializer.h"
#include "tengine_op.h"
static int mish_op_map(int op)
{
return OP_MISH;
}
static int tm2_load_mish(struct ir_graph* ir_graph, struct ir_node* ir_node, const TM2_Node* tm_node,
const TM2_Operator* tm_op)
{
return 0;
}
static int reg_tm2_ops(void* arg)
{
struct serializer* tm2_s = find_serializer("tengine");
if (tm2_s == NULL)
{
TLOG_ERR("tengine serializer has not been registered yet\n");
return -1;
}
tm2_s->register_op_loader(tm2_s, TM2_OPTYPE_MISH, 1, tm2_load_mish, mish_op_map, NULL);
return 0;
}
static int unreg_tm2_ops(void* arg)
{
struct serializer* tm2_s = find_serializer("tengine");
tm2_s->unregister_op_loader(tm2_s, TM2_OPTYPE_MISH, 1, tm2_load_mish);
return 0;
}
REGISTER_MODULE_INIT(MOD_OP_LEVEL, "reg_mish_ops", reg_tm2_ops);
REGISTER_MODULE_EXIT(MOD_OP_LEVEL, "unreg_mish_ops", unreg_tm2_ops);
......@@ -137,6 +137,7 @@ typedef uint8_t tm_bool_t; /* bool is 1-byte unsigned integer */
#define TM2_OPSTR_REDUCEL2 "ReduceL2"
#define TM2_OPSTR_MEAN "Mean"
#define TM2_OPSTR_MATMUL "MatMul"
#define TM2_OPSTR_MISH "Mish"
/* Operator types */
#define TM2_OPTYPE_ACCURACY 0 /* No Param */
#define TM2_OPTYPE_BATCHNORMALIZATION 1 /* TM2_BatchNormParam */
......@@ -230,7 +231,10 @@ typedef uint8_t tm_bool_t; /* bool is 1-byte unsigned integer */
#define TM2_OPTYPE_REDUCEL2 89
#define TM2_OPTYPE_MEAN 90
#define TM2_OPTYPE_MATMUL 91
#define TM2_OPTYPE_NUM 92
#define TM2_OPTYPE_MISH 97
#define TM2_OPTYPE_NUM 98
/* --------------------- -------- TM objects -------------------------------- */
......
test.jpg

120.2 KB

Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册