Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zyad max pooling #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions Hardware/HLS/max_pooling.cpp
Original file line number Diff line number Diff line change
@@ -1,97 +1,105 @@
#include <iostream>
#include<stdio.h>
#include<vector>
#include <stdlib.h>
#include<time.h>
using namespace std;

int main() {
int stride;
cout<<"enter the stride";
cin>>stride;
int stride=2;
cout<<endl;
int r, c;
cout<<"enter the input rows and columns";
cin >> r ;
cout<<"\n";
cin>>c;
cout<<endl;
std::vector< std::vector<int> > image;
image.resize(r);
for(int i = 0; i < r; i++)
image[i].resize(c);
srand(time(0));
int image[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> image[i][j];
image[i][j] = rand()%100;
}

}

for (int i = 0; i < r; i++) {
cout<<"\n";
for (int j = 0; j < c; j++) {
cout<<image[i][j]<<" ";
}

}

cout<< endl;
int r1=0;
int c1=0;
int out[1][1];
int out[r/2][c/2];
for (int i=0;i<=(r-2);i=i+stride)
{
for(int j=0;j<=(c-2);j=j+stride)
{
if((image[i][j]>image[i+1][j])&&(image[i][j]>image[i][j+1])&&(image[i][j]>image[i+1][j+1]))
if((image[i][j]>=image[i+1][j])&&(image[i][j]>=image[i][j+1])&&(image[i][j]>=image[i+1][j+1]))
{
out[r1][c1]=image[i][j];
c1++;
if (c1==(c/2) && r1<((r/2)-1))
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}


continue;
}
else if ((image[i+1][j]>image[i][j])&&(image[i+1][j]>image[i][j+1])&&(image[i+1][j]>image[i+1][j+1]))
else if ((image[i+1][j]>=image[i][j])&&(image[i+1][j]>=image[i][j+1])&&(image[i+1][j]>=image[i+1][j+1]))
{
out[r1][c1]=image[i+1][j];
c1++;
if (c1==(c/2) && r1<((r/2)-1))
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}


continue;
}

else if((image[i][j+1]>image[i+1][j])&&(image[i][j+1]>image[i][j])&&(image[i][j+1]>image[i+1][j+1]))
else if((image[i][j+1]>=image[i+1][j])&&(image[i][j+1]>=image[i][j])&&(image[i][j+1]>=image[i+1][j+1]))
{
out[r1][c1]=image[i][j+1];
c1++;
if (c1==(c/2) && r1<((r/2)-1))
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}


continue;
}

else if((image[i+1][j+1]>image[i][j])&&(image[i+1][j+1]>image[i][j+1])&&(image[i+1][j+1]>image[i+1][j]))
else if((image[i+1][j+1]>=image[i][j])&&(image[i+1][j+1]>=image[i][j+1])&&(image[i+1][j+1]>=image[i+1][j]))
{
out[r1][c1]=image[i+1][j+1];
c1++;
if (c1==(c/2) && r1<((r/2)-1))
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}


continue;
}

}
}
c1--;
for(int i=0;i<=r1;i++)
{
cout<<"\n";
for(int j=0;j<=c1;j++)
{
cout<<out[i][j];
cout<<endl;
cout<<out[i][j]<<" ";

}
}
return 0;
Expand Down
121 changes: 121 additions & 0 deletions Hardware/HLS/max_pooling_v3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <iostream>
#include<stdio.h>
#include<vector>
#include <stdlib.h>
#include<time.h>
using namespace std;
void Max_Pooling(int **image,int r,int c ,int stride)
{
int r1=0; // rows of output matrix
int c1=0; // columns of output matrix
int out[r/2][c/2];
// using 2x2 sub-window to compute the output
for (int i=0;i<=(r-2);i=i+stride)
{
for(int j=0;j<=(c-2);j=j+stride)
{
if((image[i][j]>=image[i+1][j])&&(image[i][j]>=image[i][j+1])&&(image[i][j]>=image[i+1][j+1]))
{
out[r1][c1]=image[i][j];
c1++;
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}

continue;
}
else if ((image[i+1][j]>=image[i][j])&&(image[i+1][j]>=image[i][j+1])&&(image[i+1][j]>=image[i+1][j+1]))
{
out[r1][c1]=image[i+1][j];
c1++;
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}

continue;
}

else if((image[i][j+1]>=image[i+1][j])&&(image[i][j+1]>=image[i][j])&&(image[i][j+1]>=image[i+1][j+1]))
{
out[r1][c1]=image[i][j+1];
c1++;
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}

continue;
}

else if((image[i+1][j+1]>=image[i][j])&&(image[i+1][j+1]>=image[i][j+1])&&(image[i+1][j+1]>=image[i+1][j]))
{
out[r1][c1]=image[i+1][j+1];
c1++;
if ((c1==(c/2)) && (r1<((r/2)-1)))
{
c1=0;
r1++;
}

continue;
}

}
}
c1--;
for(int i=0;i<=r1;i++)// print the output
{
cout<<"\n";
for(int j=0;j<=c1;j++)
{
cout<<out[i][j]<<" ";

}
}
}
int main() {
int stride=2;
//read the input rows and columns
int r;
int c;
cout<<endl;
cout<<"enter the input rows and columns";
cin >> r ;
cout<<"\n";
cin>>c;
cout<<endl;
//passing the 2D Array to the function
int **image =new int*[r];

//pointer initialization
for(int i=0;i<r;i++)
{
image[i]=new int[c];
}

//input array elements
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>image[i][j];
}
}

for (int i = 0; i < r; i++) {
cout<<"\n";
for (int j = 0; j < c; j++) {
cout<<image[i][j]<<" ";
}

}
cout<<endl;
//call the function with the required parameters
Max_Pooling(image,r,c,stride);
return 0;
}