-
Notifications
You must be signed in to change notification settings - Fork 18
/
conv2d.cpp
208 lines (191 loc) · 6.48 KB
/
conv2d.cpp
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
206
207
208
#define INCLUDE_PYTHON_INTERFACE 1
#define INCLUDE_PYBIND_PYTHON_INTERFACE 1
#include "pybind11/pybind11.h"
#include "pybind11/numpy.h"
#include "NumCpp.hpp"
#include <iostream>
#include <string>
#include<thread>
using namespace std;
namespace py=pybind11;
template<typename T>
py::array_t<double> cpp_dot(py::array_t<T, py::array::c_style> inArray1, py::array_t<T, py::array::c_style> inArray2)
{
auto array1 = nc::pybindInterface::pybind2nc(inArray1);
auto array2 = nc::pybindInterface::pybind2nc(inArray2);
auto dotProduct = nc::dot<double>(array1, array2);
return nc::pybindInterface::nc2pybind(dotProduct);
}
template<typename T>
py::array_t<double> conv2d_pure(int s,string mode, py::array_t<T, py::array::c_style> kernel_py, py::array_t<T, py::array::c_style> input_py)
{
auto kernel = nc::pybindInterface::pybind2nc(kernel_py);
auto image = nc::pybindInterface::pybind2nc(input_py);
int k=kernel.shape().rows;
int h=image.shape().rows;
int w=image.shape().cols;
int p_h,p_w;
if(mode=="same"){
p_h=(s*(h-1)+k-h)/2;
p_w=(s*(w-1)+k-w)/2;
}
else if(mode=="valid"){
p_h=0;
p_w=0;
}
else if(mode=="full"){
p_h=k-1;
p_w=k-1;
}
else{
assert(false);
}
int out_h = (h + 2 * p_h - k) /s + 1;
int out_w = (w + 2 * p_w - k) /s + 1;
//填充后的image
auto padded_img = nc::zeros<double>(h + 2 * p_h, w + 2 * p_w);
padded_img.put(nc::Slice(p_h,p_h + h), nc::Slice(p_w,p_w + w),image) ;
auto image_mat=nc::zeros<double>(out_h ,out_w);
// nc::NdArray<double> shapedKernel=kernel.reshape(k*k,1);
for(int i=0;i<out_h;++i){
for(int j=0;j<out_w;++j){
double sum=0;
for(int x=i*s;x<i*s+k;++x){
for(int y=j*s;y<j*s+k;++y){
sum+=static_cast<double>(padded_img(x,y)*kernel(x-i*s,y-j*s));
}
}
image_mat.put(i,j,sum);
// auto window=padded_img(nc::Slice(i * s,(i * s +k)),nc::Slice(j*s,j*s+k));
// image_mat.put(i,j,nc::dot<double>(window.reshape(1,k*k),shapedKernel).item());
}
}
return nc::pybindInterface::nc2pybind(image_mat);
}
template<typename T>
py::array_t<double> conv2d(int s,string mode, py::array_t<T, py::array::c_style> kernel_py, py::array_t<T, py::array::c_style> input_py)
{
auto kernel = nc::pybindInterface::pybind2nc(kernel_py);
auto image = nc::pybindInterface::pybind2nc(input_py);
int k=kernel.shape().rows;
int h=image.shape().rows;
int w=image.shape().cols;
int p_h,p_w;
if(mode=="same"){
p_h=(s*(h-1)+k-h)/2;
p_w=(s*(w-1)+k-w)/2;
}
else if(mode=="valid"){
p_h=0;
p_w=0;
}
else if(mode=="full"){
p_h=k-1;
p_w=k-1;
}
else{
assert(false);
}
int out_h = (h + 2 * p_h - k) /s + 1;
int out_w = (w + 2 * p_w - k) /s + 1;
//填充后的image
auto padded_img = nc::zeros<double>(h + 2 * p_h, w + 2 * p_w);
padded_img.put(nc::Slice(p_h,p_h + h), nc::Slice(p_w,p_w + w),image) ;
auto image_mat=nc::zeros<double>(out_h * out_w, k*k);
int row=0;
nc::NdArray<double> window;
for(int i=0;i<out_h;++i){
for(int j=0;j<out_w;++j){
window=padded_img(nc::Slice(i * s,(i * s +k)),nc::Slice(j*s,j*s+k));
image_mat.put(row,image_mat.cSlice(),window.flatten());
++row;
}
}
auto dotProduct = nc::dot<double>(image_mat, kernel.reshape(k*k,1)).reshape(out_h,out_w);
return nc::pybindInterface::nc2pybind(dotProduct);
}
template<typename T>
void f(nc::NdArray<T>* image_mat,nc::NdArray<T> * padded_img,int out_h_start,int out_h,int out_w,int row,int s,int k){
nc::NdArray<T> window;
for(int i=out_h_start;i<out_h;++i){
for(int j=0;j<out_w;++j){
window=(*padded_img)(nc::Slice(i * s,(i * s +k)),nc::Slice(j*s,j*s+k));
image_mat->put(row,image_mat->cSlice(),window.flatten());
++row;
}
}
}
template<typename T>
py::array_t<double> conv2d_multi(int s,string mode, py::array_t<T, py::array::c_style> kernel_py, py::array_t<T, py::array::c_style> input_py)
{
auto kernel = nc::pybindInterface::pybind2nc(kernel_py);
auto image = nc::pybindInterface::pybind2nc(input_py);
int k=kernel.shape().rows;
int h=image.shape().rows;
int w=image.shape().cols;
int p_h,p_w;
if(mode=="same"){
p_h=(s*(h-1)+k-h)/2;
p_w=(s*(w-1)+k-w)/2;
}
else if(mode=="valid"){
p_h=0;
p_w=0;
}
else if(mode=="full"){
p_h=k-1;
p_w=k-1;
}
else{
assert(false);
}
int out_h = (h + 2 * p_h - k) /s + 1;
int out_w = (w + 2 * p_w - k) /s + 1;
//填充后的image
auto padded_img = nc::zeros<double>(h + 2 * p_h, w + 2 * p_w);
padded_img.put(nc::Slice(p_h,p_h + h), nc::Slice(p_w,p_w + w),image) ;
auto image_mat=nc::zeros<double>(out_h * out_w, k*k);
thread t1(f<double>,&image_mat,&padded_img,0,out_h/2,out_w,0,s,k);
thread t2(f<double>,&image_mat,&padded_img,out_h/2,out_h,out_w,out_h * out_w/2,s,k);
t1.join();
t2.join();
auto dotProduct = nc::dot<double>(image_mat, kernel.reshape(k*k,1)).reshape(out_h,out_w);
return nc::pybindInterface::nc2pybind(dotProduct);
}
PYBIND11_MODULE(example, m)
{
m.doc() = "This is an example of using NumCpp with python and NumPy.";
m.def("cpp_dot_double", &cpp_dot<double>,
pybind11::arg("inArray1"),
pybind11::arg("inArray2"),
"Returns the dot project of the two arrays.");
m.def("conv2d", &conv2d<double>,
pybind11::arg("s"),
pybind11::arg("mode"),
pybind11::arg("kernel_py"),
pybind11::arg("input_py"),
"Returns the dot project of the two arrays.");
m.def("conv2d_multi", &conv2d_multi<double>,
pybind11::arg("s"),
pybind11::arg("mode"),
pybind11::arg("kernel_py"),
pybind11::arg("input_py"),
"Returns the dot project of the two arrays.");
m.def("conv2d_pure", &conv2d_pure<double>,
pybind11::arg("s"),
pybind11::arg("mode"),
pybind11::arg("kernel_py"),
pybind11::arg("input_py"),
"Returns the dot project of the two arrays.");
}
//#include <pybind11/pybind11.h>
//
//int add(int i, int j) {
// return i + j;
//}
//
//PYBIND11_MODULE(example, m) {
// m.doc() = "pybind11 example plugin"; // optional module docstring
//
// m.def("add", &add, "A function which adds two numbers");
//}