当前位置: 首页 > news >正文

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

1 integrationFC设计

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

在这里插入图片描述
图片来自附带的技术文档《Hardware Documentation》

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

在这里插入图片描述
全连接层FC1输入神经元个数为3840/32=120个,输出神经元个数为2688/32=84个,原理图如图所示:

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

2 integrationFC程序

创建UsingTheTanh文件:

在这里插入图片描述

输入文件名:

在这里插入图片描述

双击打开,输入代码:

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 continueelse 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 forwardelse 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 

如图所示:

在这里插入图片描述

创建HyperBolicTangent文件:

在这里插入图片描述

双击打开,输入代码:

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 finishedif (x[31]==0)begin OutputFinal= 32'b00111111100000000000000000000000;Finished =1'b 1;//here i assign it an immediate value of Positive Floating oneend if (x[31]==1)begin OutputFinal= 32'b10111111100000000000000000000000;Finished =1'b 1;//here i assign it an immediate value of Negative Floating oneend
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;endelse begin OutputFinal=32'b10111111110010001111010111000011;Finished=1'b 1;endend
else begin //First instance of the tanhif(reset==1'b1)begin  Coefficients=128'b10111101010111010000110111010001_00111110000010001000100010001001_10111110101010101010101010101011_00111111100000000000000000000000;//the 4 coefficients of taylor expansionForXSqOrOne=32'b00111111100000000000000000000000; //initially 1OutputAdditionInAlways=32'b00000000000000000000000000000000; //initially 0ForMultPrevious=x;
Finished=0;
end
else beginForXSqOrOne=Xsquared;ForMultPrevious=OutputOne; //get the output of the second multiplication to multiply with xCoefficients=Coefficients>>32; //shift 32 bit to divide the out_m1 with the new number to compute the factorialOutputAdditionInAlways=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 

如图所示:

在这里插入图片描述

双击打开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) beginif (reset == 1'b1) beginFC1reset = 1'b1;FC2reset = 1'b1;TanhReset = 1'b1;SMaxEnable = 1'b0;counter = 0;address1 = -1;address2 = -1;endelse begincounter = counter + 1;if (counter > 0 && counter < IntIn + 10) beginFC1reset = 1'b0;endelse if (counter > IntIn + 10 && counter < IntIn + 12 + FC_1_out*6) beginTanhReset = 1'b0;address2 = -3;endelse if (counter > IntIn + 12 + FC_1_out*6 && counter < IntIn + 12 + FC_1_out*6 + FC_1_out + 10) beginFC2reset = 1'b0;endelse if (counter > IntIn + 12 + FC_1_out*6 + FC_1_out + 10) beginSMaxEnable = 1'b1;endif (address1 != 8'hfe) beginaddress1 = address1 + 1;endelseaddress1 = 8'hfe;address2 = address2 + 1;end
endendmodule  

如图所示:

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

综合完成,关闭即可:

在这里插入图片描述

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

分享决定高度,学习拉开差距

相关文章:

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

1 integrationFC设计 LeNet-5网络结构全连接部分如图所示&#xff0c;该部分有2个全连接层&#xff0c;1个TanH激活层&#xff0c;1个SoftMax激活层&#xff1a; 图片来自附带的技术文档《Hardware Documentation》 integrationFC部分原理图&#xff0c;如图所示&#xff0c;…...

面试题总结

1.js的数据类型 分为基本数据类型和引用数据类型。 基本数据类型 ES5的5种&#xff1a;Null&#xff0c;undefined&#xff0c;Boolean&#xff0c;Number&#xff0c;String&#xff0c; ES6新增&#xff1a;Symbol表示独一无二的值 ES10新增&#xff1a;BigInt 表示任意大的…...

go进阶(1) -深入理解goroutine并发运行机制

并发指的是同时进行多个任务的程序&#xff0c;Web处理请求&#xff0c;读写处理操作&#xff0c;I/O操作都可以充分利用并发增长处理速度&#xff0c;随着网络的普及&#xff0c;并发操作逐渐不可或缺 一、goroutine简述 在Golang中一个goroutines就是一个执行单元&#xff…...

mongodb 操作记录

#启动服务 net start MongoDB #停止服务 net stop MongoDB #进入mongo shell 方式 mongo db #查看当前数据库是那个 #插入一条数据 db.runoob.insert({x:10}) #查找数据 db.runoob.find() 查询所有的数据库 show dbs #连接mongodb mongodb://[username:password]host1[:po…...

JDBC简单的示例

JDBC 编程步骤 加载驱动程序&#xff1a; Class.forName(driverClass) //加载MySql驱动 Class.forName("com.mysql.jdbc.Driver") //加载Oracle驱动 Class.forName("oracle.jdbc.driver.OracleDriver")获得数据库连接&#xff1a; DriverManager.getCon…...

Spring架构篇--2.3 远程通信基础--IO多路复用select,poll,epoll模型

前言&#xff1a;对于传统的BIO&#xff08;同步阻塞&#xff09;模型&#xff0c;当有客户端连接达到服务端&#xff0c;服务端在对改连接进行连接建立&#xff0c;和数据传输过程中&#xff0c;是无法响应其他客户端的&#xff0c;只有当服务端完成对一个客户端处理后&#x…...

python--matplotlib(4)

前言 Matplotlib画图工具的官网地址是 http://matplotlib.org/ Python环境下实现Matlab制图功能的第三方库&#xff0c;需要numpy库的支持&#xff0c;支持用户方便设计出二维、三维数据的图形显示&#xff0c;制作的图形达到出版级的标准。 其他matplotlib文章 python--matpl…...

【项目精选】城市公交查询系统(论文+视频+源码)

点击下载源码 1.1 选题背景 随着低碳生活的普及&#xff0c;人们更倾向于低碳环保的出行方式&#xff0c;完善公交系统无疑具有重要意义。公交是居民日常生活中最常使用的交通工具之一&#xff0c;伴随着我国经济繁荣和城市人口增长&#xff0c;出行工具的选择也变得越来越重要…...

less、sass、webpack(前端工程化)

目录 一、Less 1.配置less环境 1.先要安装node&#xff1a;在cmd中&#xff1a;node -v检查是否安装node 2.安装less :cnpm install -g less 3.检查less是否安装成功&#xff1a;lessc -v 4.安装成功后&#xff0c;在工作区创建xx.less文件 5.在控制台编译less,命令&…...

解析Java中的class文件

解析class文件需要把class文件当成文件流来处理&#xff0c;定义ClassReader结构体 type ClassReader struct {data []byte }go语言中的reslice语法可以跳过已经读过的数据。 同时定义了ClassFile数据结构来描述class文件的各个部分&#xff0c;该数据结构如下所示&#xff1…...

直播预告 | 企业如何轻松完成数据治理?火山引擎 DataLeap 给你一份实战攻略!

更多技术交流、求职机会&#xff0c;欢迎关注字节跳动数据平台微信公众号&#xff0c;回复【1】进入官方交流群 企业数字化转型正席卷全球&#xff0c;这不仅是趋势所在&#xff0c;也是企业发展必然面对的考题&#xff0c;也是企业最关心、最难决策的难题&#xff0c;数字化不…...

华为OD机试真题Python实现【 磁盘容量】真题+解题思路+代码(20222023)

磁盘容量 题目 磁盘的容量单位常用的有M、G、T 他们之间的换算关系为1T =1024G,1G=1024M 现在给定n块磁盘的容量,请对他们按从小到大的顺序进行稳定排序 例如给定5块盘的容量 5 1T 20M 3G 10G6T 3M12G9M 排序后的结果为 20M 3G 3M12G9M 1T 10G6T 注意单位可以重复出现 上述…...

php调试配置

错误信息输出 错误日志 nginx把对php的请求发给php-fpm fastcgi进程来处理&#xff0c;默认的php-fpm只会输出php-fpm的错误信息&#xff0c;在php-fpm的errors log里也看不到php的errorlog。原因是php-fpm的配置文件php-fpm.conf中默认是关闭worker进程的错误输出&#xff0…...

Spring架构篇--1 项目演化过程

前言&#xff1a;如今spring微服务以其灵活开发易于维护已基本占领开发占地&#xff0c;项目从一开始并不是这种服务的拆分&#xff0c;是一步步演变成现在的架构&#xff1b; 项目演化之路&#xff1a; 1 单体架构&#xff1a;开发和运维都较简单&#xff1a; 单体架构&am…...

华为OD机试真题Python实现【斗地主 2】真题+解题思路+代码(20222023)

斗地主 2 题目描述 在斗地主扑克牌游戏中,扑克牌由小到大的顺序为3 4 5 6 7 8 9 10 J Q K A 2 玩家可以出的扑克牌阵型有,单张,对子,顺子,飞机,炸弹等 其中顺子的出牌规则为,由至少 5 张由小到大连续递增的扑克牌组成 且不能包含2 例如:{3,4,5,6,7}、{3,4,5,6,7,8,9,1…...

Intel SIMD: AVX2

AVX2 资料&#xff1a; Intel 内部指令 — AVX和AVX2学习笔记Intel Intrinsics — AVX & AVX2 Learning NotesModule x86 AVX 向量寄存器有三种&#xff1a; 128-bit (XMM forms)&#xff0c;AVX2 支持&#xff0c;符号 __m128, __m128d, __m128i256-bit (YMM forms)&a…...

Spring Cloud Nacos源码讲解(二)- Nacos客户端服务注册源码分析

Nacos客户端服务注册源码分析 服务注册信息 ​ 我们从Nacos-Client开始说起&#xff0c;那么说到客户端就涉及到服务注册&#xff0c;我们先了解一下Nacos客户端都会将什么信息传递给服务器&#xff0c;我们直接从Nacos Client项目的NamingTest说起 public class NamingTest…...

华为OD机试 - 停车场最大距离(Python) | 机试题+算法思路+考点+代码解析 【2023】

停车场最大距离 题目 停车场有一横排车位0代表没有停车,1代表有车. 至少停了一辆车在车位上,也至少有一个空位没有停车. 为防止刮蹭,需为停车人找到一个车位 使得停车人的车最近的车辆的距离是最大的 返回此时的最大距离 输入 一个用半角逗号分割的停车标识字符串,停车标识为…...

RPC(2)------ Netty(NIO) + 多种序列化协议 + JDK动态代理实现

依赖包解释 Guava 包含了若干被Google的 Java项目广泛依赖 的核心库&#xff0c;例如&#xff1a;集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] 、字符串处理 [string process…...

CAN现场总线基础知识总结,看这一篇就理清了(CAN是什么,电气属性,CAN通协议等)

【系列专栏】&#xff1a;博主结合工作实践输出的&#xff0c;解决实际问题的专栏&#xff0c;朋友们看过来&#xff01; 《QT开发实战》 《嵌入式通用开发实战》 《从0到1学习嵌入式Linux开发》 《Android开发实战》 《实用硬件方案设计》 长期持续带来更多案例与技术文章分享…...

QMC5883L的驱动

简介 本篇文章的代码已经上传到了github上面&#xff0c;开源代码 作为一个电子罗盘模块&#xff0c;我们可以通过I2C从中获取偏航角yaw&#xff0c;相对于六轴陀螺仪的yaw&#xff0c;qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

oracle与MySQL数据库之间数据同步的技术要点

Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异&#xff0c;它们的数据同步要求既要保持数据的准确性和一致性&#xff0c;又要处理好性能问题。以下是一些主要的技术要点&#xff1a; 数据结构差异 数据类型差异&#xff…...

【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验

系列回顾&#xff1a; 在上一篇中&#xff0c;我们成功地为应用集成了数据库&#xff0c;并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了&#xff01;但是&#xff0c;如果你仔细审视那些 API&#xff0c;会发现它们还很“粗糙”&#xff1a;有…...

leetcodeSQL解题:3564. 季节性销售分析

leetcodeSQL解题&#xff1a;3564. 季节性销售分析 题目&#xff1a; 表&#xff1a;sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...

服务器--宝塔命令

一、宝塔面板安装命令 ⚠️ 必须使用 root 用户 或 sudo 权限执行&#xff01; sudo su - 1. CentOS 系统&#xff1a; yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh2. Ubuntu / Debian 系统…...

短视频矩阵系统文案创作功能开发实践,定制化开发

在短视频行业迅猛发展的当下&#xff0c;企业和个人创作者为了扩大影响力、提升传播效果&#xff0c;纷纷采用短视频矩阵运营策略&#xff0c;同时管理多个平台、多个账号的内容发布。然而&#xff0c;频繁的文案创作需求让运营者疲于应对&#xff0c;如何高效产出高质量文案成…...

苹果AI眼镜:从“工具”到“社交姿态”的范式革命——重新定义AI交互入口的未来机会

在2025年的AI硬件浪潮中,苹果AI眼镜(Apple Glasses)正在引发一场关于“人机交互形态”的深度思考。它并非简单地替代AirPods或Apple Watch,而是开辟了一个全新的、日常可接受的AI入口。其核心价值不在于功能的堆叠,而在于如何通过形态设计打破社交壁垒,成为用户“全天佩戴…...

【SpringBoot自动化部署】

SpringBoot自动化部署方法 使用Jenkins进行持续集成与部署 Jenkins是最常用的自动化部署工具之一&#xff0c;能够实现代码拉取、构建、测试和部署的全流程自动化。 配置Jenkins任务时&#xff0c;需要添加Git仓库地址和凭证&#xff0c;设置构建触发器&#xff08;如GitHub…...

c# 局部函数 定义、功能与示例

C# 局部函数&#xff1a;定义、功能与示例 1. 定义与功能 局部函数&#xff08;Local Function&#xff09;是嵌套在另一个方法内部的私有方法&#xff0c;仅在包含它的方法内可见。 • 作用&#xff1a;封装仅用于当前方法的逻辑&#xff0c;避免污染类作用域&#xff0c;提升…...