yolov3-tiny神经网络FPGA(ZYNQ7020)实现

本文详细介绍了如何在ZYNQ7020 FPGA上实现yolov3-tiny神经网络,通过HLS将网络各层封装为IP核,并在Vivado中调用。文章提供了设计的Block框图、仿真图,以及yolo_acc模块的代码。此外,还包括了完整的工程代码和测试代码。
摘要由CSDN通过智能技术生成

本文介绍使用ZYNQ实现yolov3-tiny,下图为ZYNQ上ARM与FPGA交互框图:

请添加图片描述
请添加图片描述

通过HLS将设计得每层网络进行封装IP核,在vivado进行调用:下图为the convolution, accumulation, max pooling, upsample and yolo blocks之间得连接。
请添加图片描述

下图为vivado上设计block框图:
在这里插入图片描述
下图对其中一个模块yolo_acc模块得仿真图:
在这里插入图片描述
在这里插入图片描述
本文有完整工程代码及仿真测试代码!

下面是yolo_acc模块的代码:

#include “yolo_acc.h”

void yolo_acc_top(yolo_quad_stream &inStream_a, yolo_quad_stream &inStream_b,
yolo_quad_stream &outStream,
ap_uint<9> input_h, ap_uint<9> input_w,
ap_uint<MAX_FOLD_CH_BIT> fold_input_ch,
ap_uint<1> leaky, ap_uint<1> bias_en)
{
#pragma HLS INTERFACE s_axilite port=bias_en bundle=CTRL_BUS
#pragma HLS INTERFACE s_axilite port=leaky bundle=CTRL_BUS
#pragma HLS INTERFACE s_axilite port=fold_input_ch bundle=CTRL_BUS
#pragma HLS INTERFACE s_axilite port=input_w bundle=CTRL_BUS
#pragma HLS INTERFACE s_axilite port=input_h bundle=CTRL_BUS
#pragma HLS INTERFACE s_axilite port=return bundle=CTRL_BUS
#pragma HLS INTERFACE axis register both port=outStream
#pragma HLS INTERFACE axis register both port=inStream_a
#pragma HLS INTERFACE axis register both port=inStream_b
fp_weight_type kernel_bias_fp[MAX_KERNEL_NUM];
// SCRIPT_START P_acc DO NOT EDIT OR DELETE THIS LINE
#pragma HLS ARRAY_PARTITION variable=kernel_bias_fp cyclic factor=1 dim=1
// SCRIPT_END P_acc DO NOT EDIT OR DELETE THIS LINE
for(ap_uint<MAX_FOLD_CH_BIT> i=0;i<fold_input_ch;i++)//division 2 is not safe here!!!
{
#pragma HLS LOOP_TRIPCOUNT min=4 max=4
#pragma HLS PIPELINE
if(bias_en==1)
{
quad_fp_side_channel curr_input;
curr_input = inStream_b.read();
kernel_bias_fp[4i] = curr_input.data.sub_data_0;
kernel_bias_fp[4
i+1] = curr_input.data.sub_data_1;
kernel_bias_fp[4i+2] = curr_input.data.sub_data_2;
kernel_bias_fp[4
i+3] = curr_input.data.sub_data_3;
}
}
for(int row_idx=0;row_idx<input_h;row_idx++)
{
#pragma HLS LOOP_TRIPCOUNT min=416 max=416
for(int col_idx=0;col_idx<input_w;col_idx++)
{
#pragma HLS LOOP_TRIPCOUNT min=416 max=416
for(int input_ch_idx=0;input_ch_idx<fold_input_ch;input_ch_idx++)
{
#pragma HLS LOOP_TRIPCOUNT min=4 max=4
#pragma HLS PIPELINE
quad_fp_side_channel curr_input_a,curr_input_b;
quad_fp_side_channel curr_output;
fp_data_type output_acc_0, output_acc_1, output_acc_2,output_acc_3;
curr_input_a = inStream_a.read();
curr_input_b = inStream_b.read();
output_acc_0 = curr_input_a.data.sub_data_0 + curr_input_b.data.sub_data_0;
output_acc_1 = curr_input_a.data.sub_data_1 + curr_input_b.data.sub_data_1;
output_acc_2 = curr_input_a.data.sub_data_2 + curr_input_b.data.sub_data_2;
output_acc_3 = curr_input_a.data.sub_data_3 + curr_input_b.data.sub_data_3;
curr_output.data.sub_data_0 = post_process_unit(output_acc_0,kernel_bias_fp[4input_ch_idx],bias_en,leaky);
curr_output.data.sub_data_1 = post_process_unit(output_acc_1,kernel_bias_fp[4
input_ch_idx+1],bias_en,leaky);
curr_output.data.sub_data_2 = post_process_unit(output_acc_2,kernel_bias_fp[4input_ch_idx+2],bias_en,leaky);
curr_output.data.sub_data_3 = post_process_unit(output_acc_3,kernel_bias_fp[4
input_ch_idx+3],bias_en,leaky);
curr_output.keep = curr_input_a.keep;
curr_output.strb = curr_input_a.strb;
curr_output.user = curr_input_a.user;
if((input_ch_idx == MAX_KERNEL_NUM/4-1)
&&(col_idx == input_w-1)
&&(row_idx == input_h-1))
curr_output.last = 1;
else
curr_output.last = 0;
curr_output.id = curr_input_a.id;
curr_output.dest = curr_input_a.dest;
outStream.write(curr_output);
}
}
}

}

yolov_acc测试代码:

#include “…/src/yolo_acc.h”
#include “weight_file.h”
#define ACC_INPUT_H 416
#define ACC_INPUT_W 416
#define ACC_INPUT_FOLD_CH 4
#define BIAS_EN 1
#define LEAKY 1

int main()
{
yolo_quad_stream inputStream_a(“in_stream_a”), inputStream_b(“in_stream_b”),
outputStream(“out_stream”);
bool flag = false;
FILE layer_input;
FILE layer_output_hls;
FILE layer_output_sdk;
FILE error_log;
static fp_data_type output_data[ACC_INPUT_H
ACC_INPUT_H
ACC_INPUT_FOLD_CH
4];
layer_input = fopen(“layer_input.dat”,“r”);
layer_output_hls = fopen(“layer_output_hls.dat”,“w”);
layer_output_sdk = fopen(“layer_output_sdk.dat”,“r”);
error_log = fopen(“error.log”,“w”);
for(int i=0;i<ACC_INPUT_FOLD_CH;i++)
{
quad_fp_side_channel curr_input;
fp_data_type bias_p = (fp_data_type )&kernel_bias_fp_bits[0];
curr_input.data.sub_data_0 = bias_p[4
i];
curr_input.data.sub_data_1 = bias_p[4
i+1];
curr_input.data.sub_data_2 = bias_p[4
i+2];
curr_input.data.sub_data_3 = bias_p[4i+3];
curr_input.keep = 1;
curr_input.strb = 1;
curr_input.user = 1;
curr_input.id = 0;
curr_input.dest = 0;
inputStream_b << curr_input;
}
for(int i=0;i<ACC_INPUT_H
ACC_INPUT_W;i++)
{
for(int j=0;j<ACC_INPUT_FOLD_CH;j++)
{
quad_fp_side_channel curr_input_a,curr_input_b;
short input_data_sub0;
short input_data_sub1;
short input_data_sub2;
short input_data_sub3;
fscanf(layer_input,“%hd”,&input_data_sub0);
fscanf(layer_input,“%hd”,&input_data_sub1);
fscanf(layer_input,“%hd”,&input_data_sub2);
fscanf(layer_input,“%hd”,&input_data_sub3);
fp_data_type *sub0_p = (fp_data_type *)&input_data_sub0;
fp_data_type *sub1_p = (fp_data_type *)&input_data_sub1;
fp_data_type *sub2_p = (fp_data_type *)&input_data_sub2;
fp_data_type *sub3_p = (fp_data_type *)&input_data_sub3;
curr_input_a.data.sub_data_0 = *sub0_p;
curr_input_a.data.sub_data_1 = *sub1_p;
curr_input_a.data.sub_data_2 = *sub2_p;
curr_input_a.data.sub_data_3 = *sub3_p;
curr_input_a.keep = 1;
curr_input_a.strb = 1;
curr_input_a.user = 1;
curr_input_a.id = 0;
curr_input_a.dest = 0;
curr_input_b.data.sub_data_0 = 0;
curr_input_b.data.sub_data_1 = 0;
curr_input_b.data.sub_data_2 = 0;
curr_input_b.data.sub_data_3 = 0;
curr_input_b.keep = 1;
curr_input_b.strb = 1;
curr_input_b.user = 1;
curr_input_b.id = 0;
curr_input_b.dest = 0;
inputStream_a << curr_input_a;
inputStream_b << curr_input_b;
}
}

全套工程付费下载链接:
https://download.csdn.net/download/QQ_778132974/85111365

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芯作者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值