一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

这篇具有很好参考价值的文章主要介绍了一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 integrationFC设计

LeNet-5网络结构全连接部分如图所示,该部分有2个全连接层,1个TanH激活层,1个SoftMax激活层:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计
图片来自附带的技术文档《Hardware Documentation》

integrationFC部分原理图,如图所示,图中W1和W2分别是存储全连接层FC1和全连接层FC2的权重:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计
全连接层FC1输入神经元个数为3840/32=120个,输出神经元个数为2688/32=84个,原理图如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

Tanh激活层的输入输出位宽均为32位,原理图如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

全连接层FC2输入神经元个数为2688/32=84个,输出神经元个数为320/32=10个,原理图如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

SMax激活层的输入输出位宽均为32位,原理图如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

2 integrationFC程序

创建UsingTheTanh文件:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

输入文件名:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

双击打开,输入代码:

module UsingTheTanh(x,clk,Output,resetExternal,FinishedTanh);
parameter DATA_WIDTH=32;
parameter nofinputs=784;// deterimining the no of inputs entering the function
input resetExternal;// controlling this layer
input  signed [nofinputs*DATA_WIDTH-1:0] x;
input clk;
output reg FinishedTanh;
reg reset;// for the inner tanh
output reg [nofinputs*DATA_WIDTH-1:0]Output;
wire [DATA_WIDTH-1:0]OutputTemp;
reg [7:0]counter=0;
wire Finished;
reg [7:0]i;
// the inner tanh taking inputs in 32 bits and then increment using the i operator
HyperBolicTangent TanhArray (x[DATA_WIDTH*i+:DATA_WIDTH],reset,clk,OutputTemp,Finished);
 
 
always@(posedge clk)
begin 
// if the external reset =1 then make everything to 0
if(resetExternal==1) begin reset=1;i=0;FinishedTanh=0; end
//checking if the tanh is not finished so continue your operation and low down the reset to continue
  else if(FinishedTanh==0) begin 
    if(reset==1)begin reset=0; end 
    // if it is finished then store the output of the tanh and increment the input forward
      else if (Finished==1)begin Output[DATA_WIDTH*i+:DATA_WIDTH]=OutputTemp;reset=1;i=i+1;end
// check if all the inputs are finished then the layer is OK
if(i==nofinputs)
  begin FinishedTanh=1;end
end 

end
endmodule 

如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

创建HyperBolicTangent文件:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

双击打开,输入代码:

module HyperBolicTangent (x,reset,clk,OutputFinal,Finished);
parameter DATA_WIDTH=32;
localparam taylor_iter=4;//I chose 5 Taylor Coefficients to undergo my tanh operation
input signed [DATA_WIDTH-1:0] x;

input clk;
input reset;
output reg Finished;
output reg[DATA_WIDTH-1:0]  OutputFinal;
reg [DATA_WIDTH*taylor_iter-1:0] Coefficients ; //-17/315 2/15 -1/3 1
wire [DATA_WIDTH-1:0] Xsquared; //To always generate a squared version of the input to increment the power by 2 always.
reg [DATA_WIDTH-1:0] ForXSqOrOne; //For Multiplying The power of X(1 or X^2)
reg [DATA_WIDTH-1:0] ForMultPrevious; //output of the first multiplication which is either with 1 or x(X or Output1)
wire [DATA_WIDTH-1:0] OutputOne; //the output of Mulitplying the X term with its corresponding power coeff.
wire [DATA_WIDTH-1:0] OutOfCoeffMult; //the output of Mulitplying the X term with its corresponding power coeff.
reg  [DATA_WIDTH-1:0] OutputAdditionInAlways;
wire [DATA_WIDTH-1:0] OutputAddition; //the output of the Addition each cycle 

floatMult MSquaring (x,x,Xsquared);//Generating x^2
floatMult MGeneratingXterm (ForXSqOrOne,ForMultPrevious,OutputOne); //Generating the X term [x,x^3,x^5,...]
floatMult MTheCoefficientTerm (OutputOne,Coefficients[DATA_WIDTH-1:0],OutOfCoeffMult); //Multiplying the X term by its corresponding coeff.
floatAdd FADD1 (OutOfCoeffMult,OutputAdditionInAlways,OutputAddition); //Adding the new term to the previous one     ex: x-1/3*(x^3)
reg [DATA_WIDTH-1:0] AbsFloat; //To generate an absolute value of the input[For Checking the convergence]

always @ (posedge clk) begin
AbsFloat=x;//Here i hold the input then i make it positive whatever its sign to be able to compare to implement the rule |x|>pi/2   which is the convergence rule
AbsFloat[31]=0;
if(AbsFloat>32'sb00111111110010001111010111000011)begin 
  //The Finished bit is for letting the bigger module know that the tanh is finished
  if (x[31]==0)begin 
    OutputFinal= 32'b00111111100000000000000000000000;Finished =1'b 1;//here i assign it an immediate value of Positive Floating one
  end 
    if (x[31]==1)begin 
    OutputFinal= 32'b10111111100000000000000000000000;Finished =1'b 1;//here i assign it an immediate value of Negative Floating one
    end
end
//here i handle the case of it equals +- pi/2    so i got the exact value and handle it also immediately
else if (AbsFloat==32'sb00111111110010001111010111000011)
  begin 
    if (x[31]==0)begin 
  OutputFinal=32'b00111111110010001111010111000011;Finished=1'b 1;
  end
  else begin 
  OutputFinal=32'b10111111110010001111010111000011;Finished=1'b 1;
  end
  end
else begin 
  //First instance of the tanh
  if(reset==1'b1)begin  
	Coefficients=128'b10111101010111010000110111010001_00111110000010001000100010001001_10111110101010101010101010101011_00111111100000000000000000000000;//the 4 coefficients of taylor expansion
	ForXSqOrOne=32'b00111111100000000000000000000000; //initially 1
	OutputAdditionInAlways=32'b00000000000000000000000000000000; //initially 0
	ForMultPrevious=x;
Finished=0;
end
else begin
 	ForXSqOrOne=Xsquared;
	ForMultPrevious=OutputOne; //get the output of the second multiplication to multiply with x
	Coefficients=Coefficients>>32; //shift 32 bit to divide the out_m1 with the new number to compute the factorial
  OutputAdditionInAlways=OutputAddition;
  Finished=0;
end
// the end of the tanh
if(Coefficients==128'b00000000000000000000000000000000_00000000000000000000000000000000_00000000000000000000000000000000_00000000000000000000000000000000)begin 
	OutputFinal=OutputAddition;
	Finished =1'b 1;
end
end 
end
endmodule 

如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

双击打开integrationFC,修改代码如下:

module integrationFC (clk,reset,iFCinput,CNNoutput);

parameter DATA_WIDTH = 32;
parameter IntIn = 120;
parameter FC_1_out = 84;
parameter FC_2_out = 10;

input clk, reset;
input [IntIn*DATA_WIDTH-1:0] iFCinput;
output [FC_2_out*DATA_WIDTH-1:0] CNNoutput;

wire [FC_1_out*DATA_WIDTH-1:0] fc1Out;
wire [FC_1_out*DATA_WIDTH-1:0] fc1OutTanh;

wire [FC_2_out*DATA_WIDTH-1:0] fc2Out;
wire [FC_2_out*DATA_WIDTH-1:0] fc2OutSMax;

wire [DATA_WIDTH*FC_1_out-1:0] wFC1;
wire [DATA_WIDTH*FC_2_out-1:0] wFC2;

reg FC1reset;
reg FC2reset;
reg TanhReset;
wire TanhFlag;
reg SMaxEnable;
wire DoneFlag;

integer counter;
reg [7:0] address1;
reg [7:0] address2;

weightMemory 
#(.INPUT_NODES(IntIn),
  .OUTPUT_NODES(FC_1_out),
  .file("E:/FPGA_Learn/FPGA/Day1211/Weight/weightsdense_1_IEEE.txt"))
  W1(
    .clk(clk),
    .address(address1),
    .weights(wFC1)
    );
    
weightMemory 
#(.INPUT_NODES(FC_1_out),
  .OUTPUT_NODES(FC_2_out),
  .file("E:/FPGA_Learn/FPGA/Day1211/Weight/weightsdense_2_IEEE.txt"))
  W2(
    .clk(clk),
    .address(address2),
    .weights(wFC2)
    );  
    
layer
#(.INPUT_NODES(IntIn),
  .OUTPUT_NODES(FC_1_out))
 FC1(
    .clk(clk),
    .reset(FC1reset),
    .input_fc(iFCinput),
    .weights(wFC1),
    .output_fc(fc1Out)
    );

layer
#(.INPUT_NODES(FC_1_out),
  .OUTPUT_NODES(FC_2_out))
 FC2(
    .clk(clk),
    .reset(FC2reset),
    .input_fc(fc1OutTanh),
    .weights(wFC2),
    .output_fc(fc2Out)
    );
    
UsingTheTanh
#(.nofinputs(FC_1_out))
Tanh1(
      .x(fc1Out),
      .clk(clk),
      .Output(fc1OutTanh),
      .resetExternal(TanhReset),
      .FinishedTanh(TanhFlag)
      );

softmax SMax(
      .inputs(fc2Out),
      .clk(clk),
      .enable(SMaxEnable),
      .outputs(CNNoutput),
      .ackSoft(DoneFlag)
      );

always @(posedge clk or posedge reset) begin
  if (reset == 1'b1) begin
    FC1reset = 1'b1;
    FC2reset = 1'b1;
    TanhReset = 1'b1;
    SMaxEnable = 1'b0;
    counter = 0;
    address1 = -1;
    address2 = -1;
  end
  else begin
      counter = counter + 1;
    if (counter > 0 && counter < IntIn + 10) begin
       FC1reset = 1'b0;
    end
    else if (counter > IntIn + 10 && counter < IntIn + 12 + FC_1_out*6) begin
       TanhReset = 1'b0;
       address2 = -3;
    end
    else if (counter > IntIn + 12 + FC_1_out*6 && counter < IntIn + 12 + FC_1_out*6 + FC_1_out + 10) begin
       FC2reset = 1'b0;
    end
    else if (counter > IntIn + 12 + FC_1_out*6 + FC_1_out + 10) begin
       SMaxEnable = 1'b1;
    end
    if (address1 != 8'hfe) begin
      address1 = address1 + 1;
    end
    else
      address1 = 8'hfe;
    address2 = address2 + 1;
  end
end

endmodule  

如图所示:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

对设计进行分析,操作如图:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

分析后的设计,Vivado自动生成原理图,如图:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

对设计进行综合,操作如图:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

综合完成,关闭即可:

一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计

希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距文章来源地址https://www.toymoban.com/news/detail-401042.html

到了这里,关于一起学习用Verilog在FPGA上实现CNN----(八)integrationFC设计的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • CNN的硚口实现: 由Verilog编写并在FPGA上合成

    目录 前言 一、环境设置 二、CNN的硬件设计思路 三、使用Verilog实现CNN

    2024年02月13日
    浏览(27)
  • 基于FPGA的电子时钟设计与实现 (在EDA开发板上实现电子时钟功能)

    开发板: 此款开发板使用的是 ALTERA 公司的 Cyclone IV 系列 FPGA,型号为 EP4CE6F17C8, 256 个引脚的 FBGA 封装。  题目:在EDA开发板上实现电子时钟功能 要求:实现电子时钟程序编写,实现在7段数码管显示时、分、秒,使用4x4矩阵按键模拟调节时钟指令输入按键,并实现整点报时

    2024年02月04日
    浏览(30)
  • FPGA Artix7-100T实现手写字硬件加速,纯Verilog编写的CNN神经网络加速器,有效减轻误识别问题

    fpga实现cnn神经网络加速 手写字硬件加速 FPGA artix7-100t 纯verilog编写 神经网络硬件加速 使用ov5640摄像头dvp接口 verilog实现手写字识别 包括卷积层、全连接层、池化层、softmax,有效减轻误识别问题。 注意: 该项目并未使用到arm核,是使用传统fpga的逻辑资源实现的。 ID:92299 7141

    2024年04月23日
    浏览(17)
  • FPGA设计——verilog实现乒乓操作并modelsim仿真

    乒乓操作是FPGA设计中常用的一种技巧,它通过数据流控制实现按节拍相互配合的切换,来提高数据处理效率,达到无缝缓冲和处理的效果。本文针对乒乓操作进行学习总结。 完整工程 一、原理图如下 : 1、二选一控制器来对缓冲模块1和2进行选择。 2、数据缓冲模块一般就是

    2023年04月08日
    浏览(27)
  • 《FPGA纯Verilog设计实现CameraLink视频编解码验证方案》

    FPGA纯verilog编解码CameraLink视频 本文详细描述了FPGA纯verilog实现CameraLink视频接收和发送的实现设计方案,目的在于验证CameraLink解码模块和编码模块的正确性,思路是这样的,由于项目之处没有CameraLink相机,但又必须验证关键的CameraLink解码模块和编码模块,所以做了这样一个巧

    2024年04月22日
    浏览(22)
  • 基于FPGA和Verilog实现的9层电梯控制器仿真设计

    资源下载地址:https://download.csdn.net/download/sheziqiong/85628810 资源下载地址:https://download.csdn.net/download/sheziqiong/85628810 电梯最少可以往返于0—9层楼。 乘客要去的楼层数A可手动输入并显示,按取消键可清除本次输入。 可自动显示电梯运行的楼层数B 当AB时,电梯上升; 当AB时,

    2024年02月02日
    浏览(57)
  • 【FPGA教程案例95】机器学习2——基于FPGA的SVM支持向量机二分类系统实现之Verilog编程设计

    FPGA教程目录 ​​​​​​MATLAB教程目录 本课程成果预览(o_check=0表示分类1,o_check=1表示分类2,识别率为98.7%) 目录 1.软件版本

    2023年04月08日
    浏览(41)
  • 【FPGA】Verilog设计入门——时序模块及其Verilog表述

    目录 1.边沿触发型触发器及其Verilog表述 2.电平触发型锁存器及其Verilog表述  3.含异步复位/时钟使能型触发器及其Verilog表述 4.同步复位型触发器及其Verilog表述  5.异步复位型锁存器及其Verilog表述 6.Verilog的时钟过程表述的特点和规律   7.异步时序模块的Verilog表述  8.4位二进制

    2024年02月07日
    浏览(27)
  • FPGA设计Verilog基础之Verilog的运算符

    注意:后续技术分享,第一时间更新,以及更多更及时的技术资讯和学习技术资料 ,将在公众号 CTO Plus 发布,请关注公众号: CTO Plus FPGA设计Verilog基础之Verilog的运算符 Verilog是一种硬件描述语言,支持多种运算符,包括算术运算符、比较(关系)运算符、逻辑运算符、条件

    2024年02月03日
    浏览(32)
  • FPGA设计Verilog基础之Verilog全局变量和局部变量定义

    注意:后续技术分享,第一时间更新,以及更多更及时的技术资讯和学习技术资料 ,将在公众号 CTO Plus 发布,请关注公众号: CTO Plus   在Verilog中,变量可以分为全局变量和局部变量两种类型。全局变量在整个模块中都可以使用,而局部变量只能在某个特定的代码块中使用。

    2024年02月15日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包