forked from Mengjintao/FastCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv_layer.h
149 lines (117 loc) · 4.49 KB
/
conv_layer.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
//Tencent is pleased to support the open source community by making FeatherCNN available.
//Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
//Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
//in compliance with the License. You may obtain a copy of the License at
//
//https://opensource.org/licenses/BSD-3-Clause
//
//Unless required by applicable law or agreed to in writing, software distributed
//under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
//CONDITIONS OF ANY KIND, either express or implied. See the License for the
//specific language governing permissions and limitations under the License.
#pragma once
//#include "../layer.h"
#include "./utility/helper.h"
#include "./utility/common.h"
#include <assert.h>
#include <stdio.h>
#include <omp.h>
class ConvLayer
{
public:
ConvLayer(float *input, float *kernel, float *biasw, float *output_ref, size_t ic, size_t ih, size_t iw, size_t oc, size_t kh=3, size_t kw=3, size_t sh=1, size_t sw=1, size_t pad_left=1, size_t pad_right=1, size_t pad_top=1, size_t pad_bottom=1, size_t g=1, bool bias=0, size_t nt = 1, size_t iter = 10)
{
num_threads = nt;
iterations = iter;
omp_set_num_threads(num_threads);
//Input
input_channels = ic;
input_height = ih;
input_width = iw;
input_data = input;
//Kernel
output_channels=oc;
kernel_height = kh;
kernel_width = kw;
kernel_data = kernel;
//Bias
bias_term = bias;
if(bias_term) bias_data = biasw;
else bias_data = NULL;
group = g;
stride_height = sh;
stride_width = sw;
padding_left = pad_left;
padding_top = pad_top;
padding_right = pad_right;
padding_bottom = pad_bottom;
if(group == 0 || stride_width == 0 || stride_height == 0)
assert(!(group == 0 || stride_width == 0 || stride_height == 0));
//Output
this->output_ref = output_ref;
output_width = (input_width + padding_left + padding_right - kernel_width) / stride_width + 1;
output_height = (input_height + padding_top + padding_bottom - kernel_height) / stride_height + 1;
input_data = (float *) malloc(input_channels * input_width * input_height * sizeof(float));
kernel_data = (float *) malloc((input_channels * output_channels * kw * kh + 16) * sizeof(float));
bias_data = (float *) malloc(output_channels * output_width * output_height * sizeof(float));
memcpy(input_data, input, input_channels * input_width * input_height * sizeof(float));
memcpy(kernel_data, kernel, input_channels * output_channels * kw * kh * sizeof(float));
if(biasw)
memcpy(bias_data, biasw, output_channels * output_width * output_height * sizeof(float));
output_data = (float *) malloc(output_channels * output_width * output_height * sizeof(float));
//output_data should assert
}
~ConvLayer()
{
free(input_data);
free(kernel_data);
free(bias_data);
free(output_data);
input_data = NULL;
kernel_data = NULL;
output_data = NULL;
bias_data = NULL;
}
int get_output_height() {
return this->output_height;
}
int get_output_width() {
return this->output_width;
}
virtual int Init()
{
return -1;
}
virtual int Forward()
{
return -1;
}
virtual int Tuning()
{
return -1;
}
float *output_data;
protected:
size_t input_channels;
size_t input_width;
size_t input_height;
size_t output_channels;
size_t output_width;
size_t output_height;
size_t kernel_width;
size_t kernel_height;
size_t stride_width;
size_t stride_height;
size_t padding_left;
size_t padding_right;
size_t padding_top;
size_t padding_bottom;
size_t group;
bool bias_term;
size_t num_threads;
size_t iterations;
float *input_data;
float *kernel_data;
float *bias_data;
float *output_ref;
};