-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.h
205 lines (159 loc) · 4.68 KB
/
layers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//#include"common.h"
#include"dtype.h"
#include<iostream>
int hamming_weight[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int find_hammingweight(int value)
{
int sum=0;
// cout<<"First four bits"<<hamming_weight[value&0xF]<<"Middle one bit"<<hamming_weight[(value>>4)&1];
// cout<<"last 4 bits"<<hamming_weight[value>>1];
while(value>1)
{
sum=hamming_weight[value&0xF]+sum;
//cout<<(value&0xF)<<"value"<<endl;
value=value>>4;
// cout<<sum<<" ";
// cout<<"value"<<value;
}
sum+=value;
//cout<<sum;
return sum;
}
input_datatype findsum(input_datatype x ,input_datatype y)
{
return x+y;
}
input_datatype finddiff(input_datatype x ,input_datatype y)
{
return x-y;
}
template <class data,class res,typename config>
void Convolution(data input1[config::input_height][config::input_width][config::no_of_channels],
res output[config::size_of_output],float weight[config::size_of_weight])
{
//Converting data to 1 D
data input[6912];
for(int i=0;i<48;++i) //height of the input
for(int j=0;j<48;++j) //width of the input
for(int c=0;c<3;++c) //no of channels
input[i*48+j*3+c]=input1[i][j][c];
long int index=0;
for(int i=0;i<config::input_height-2;++i){ //Total no of the vertical traversal
for(int j=0;j<config::input_width-2;++j){ //Total number of horizontal traversal
for(int t=0;t<config::no_of_filters;++t){ //No of filters
for(int c=0;c<config::no_of_channels;++c){
input_datatype sum=0;
for(int ki=0;ki<config::kernel_size;++ki){ //Kernel
int kj=0;
while(kj<config::kernel_size) //kernel
{
if(weight[ki*3*3*32+kj*3*32+c*32+t]==1)
{
sum=findsum(sum,(input[i*48*3+j*3+c]));//weight[NO_OF_FILTERS*t+ki*K+kj];
//std::cout<<"input="<<input[i*48*3+j*3+c];
}
else if(weight[config::no_of_filters*t+ki*config::kernel_size+kj]==0)
sum=findsum(sum,(input[i*48*3+j*3+c]));//weight[NO_OF_FILTERS*t+ki*K+kj];
++kj;
}
}
++index;
//if(index<70)
sum=sum>1?1:0;
output[index]=sum;
index=index+1;
if(sum!=0)
std::cout<<"sum="<<sum;
}
}
}
}
std::cout<<"Index="<<index<<std::endl;
}
template <class data,class res,typename config>
void BinaryConvolution(data input1[config::input_height][config::input_width][config::no_of_channels],
res output[config::size_of_output],float weight[config::size_of_weight])
{
//Converting data to 1 D
data input[6912];
for(int i=0;i<48;++i) //height of the input
for(int j=0;j<48;++j) //width of the input
for(int c=0;c<3;++c) //no of channels
input[i*48+j*3+c]=input1[i][j][c];
long int index=0;
for(int i=0;i<config::input_height-2;++i){ //Total no of the vertical traversal
for(int j=0;j<config::input_width-2;++j){ //Total number of horizontal traversal
for(int t=0;t<config::no_of_filters;++t){ //No of filters
for(int c=0;c<config::no_of_channels;++c){
input_datatype sum=0;
for(int ki=0;ki<config::kernel_size;++ki){ //Kernel
int kj=0;
while(kj<config::kernel_size) //kernel
{
if(weight[ki*3*3*32+kj*3*32+c*32+t]==1)
{
sum=findsum(sum,(input[i*48*3+j*3+c]));//weight[NO_OF_FILTERS*t+ki*K+kj];
//std::cout<<"input="<<input[i*48*3+j*3+c];
}
else if(weight[config::no_of_filters*t+ki*config::kernel_size+kj]==0)
sum=findsum(sum,(input[i*48*3+j*3+c]));//weight[NO_OF_FILTERS*t+ki*K+kj];
++kj;
}
}
++index;
//if(index<70)
sum=sum>1?1:0;
output[index]=sum;
index=index+1;
if(sum!=0)
std::cout<<"sum="<<sum;
}
}
}
}
std::cout<<"Index="<<index<<std::endl;
}
/*
void Binary_Convolution(input_datatype input[INPUT_WIDTH],output[OUTPUT_LAYER1],weight)
{
int index=0;
input:for(int i=0;i<OUTPUT_LAYER1_WIDTH;++i){
for(int j=0;j<OUTPUT_LAYER1_HEIGHT;++j){
filter: for(int t=0;t<3;++t){
_9bit sum=0;
kernel: for(int ki=0;ki<KERNEL_SIZE;++ki){
int kj=0;
while(kj<KERNEL_SIZE)
{
_1bit temp=input_G[(i+ki)*INPUT_WIDTH+j+kj]^weight[NO_OF_FILTERS*t+ki*K+kj];
sum=sum|1;
sum=sum<<1;
++kj;
}
}
output[index++]=hamming_weight(sum);
}
}
}
void Fully_Connected_Layer(int *input,int* weight,int *output)
{
int index=-1;
for(int i=0;i<FC_OUT;++i)
{ output[++index]=0;
int sum=0;
for(int j=0;j<FC_IN;++j)
{
sum=input[i]^weight[i*FX_+j];
if(sum==1)
sum=sum<<1;
//cout<<sum<<endl;
}
// cout<<find_hammingweight(sum)<<" ";
output[index]+=find_hammingweight(sum);
// cout<<output[index]<<endl;
}
}
void batchNormalization()
{
}
*/