-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn.h
124 lines (86 loc) · 3.37 KB
/
cnn.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
#ifndef VERSION_CONVOLUTIONNAL_CNN_H
#define VERSION_CONVOLUTIONNAL_CNN_H
#include <stdbool.h>
#include "mat.h"
#include "data_Images.h"
#define _USE_MATH_DEFINES
#define AvePool 0
#define MaxPool 1
#define MinPool 2
// convolution layer
typedef struct convolutional_layer
{
int inputWidth;
int inputHeight;
int mapSize; //size of the characteristic model
int inChannels; //imput number
int outChannels; //output number
// weight in the convolution kernel, a 4-dimension-array in case of being fully connected
// its size = inChannels*outChannels*mapSize*mapSize
// default : inChannels = outChannels = 1
// reference : DeapLearningToolboox
float**** mapData; //store this model
float**** dmapData; //store local gradient of the characteristic model
float* basicData; //biais(?), size = outChannels
bool isFullConnect; //whether is fully connected
bool* connectModel; //mode of connections (defalt = full connection)
float*** v; // input to activation function
float*** y; // outputs
// local gradient of the output pixel
float*** d; // sigma, the local gradient of the network
} CovLayer;
// pooling layer, used for layer S2 and S4
typedef struct pooling_layer
{
int inputWidth; //width of input image
int inputHeight; //height
int mapSize; //...
int inChannels; //...
int outChannels; //...
int poolType; //method of pooling
float* basicData; //biais(?)
float*** y; // output of neuron after pooling, no activation function
float*** d; // sigma, local gradient
} PoolLayer;
// single layer of the network, fully connected
typedef struct nn_layer
{
int inputNum; //number of input data
int outputNum; //number of output data
float** wData; // weight, size = inputNum*outputNum
float* basicData; //biais ,size = outputNum
float* v; // input to activation function
float* y; // output of activation function
float* d; // sigma, local gradient
bool isFullConnect; //whether is fully connected
} OutLayer;
typedef struct cnn_network
{
int layerNum;
CovLayer* C1;
PoolLayer* S2;
OutLayer* O3;
float* e; // error of training
} CNN;
typedef struct train_opts
{
int numepochs; // number of ieration
float alpha; // learning rate
} CNNOpts;
void cnnsetup(CNN* cnn,nSize inputSize,int outputSize);
void cnntrain(CNN* cnn, ImgArr inputData,LabelArr outputData,CNNOpts opts,int trainNum);
float cnntest(CNN* cnn, ImgArr inputData,LabelArr outputData,int testNum);
void savecnn(CNN* cnn, const char* filename);
void importcnn(CNN* cnn, const char* filename);
CovLayer* initCovLayer(int inputWidth,int inputHeight,int mapSize,int inChannels,int outChannels);
PoolLayer* initPoolLayer(int inputWidth,int inputHeigh,int mapSize,int inChannels,int outChannels,int poolType);
OutLayer* initOutLayer(int inputNum,int outputNum);
float activation_Sigma(float input,float bas);
void cnnff(CNN* cnn,float** inputData);
void cnnbp(CNN* cnn,float* outputData);
void cnnapplygrads(CNN* cnn,CNNOpts opts,float** inputData);
void cnnclear(CNN* cnn);
void avgPooling(float** output,nSize outputSize,float** input,nSize inputSize,int mapSize);
void nnff(float* output,float* input,float** wdata,float* bas,nSize nnSize);
void savecnndata(CNN* cnn,const char* filename,float** inputdata);
#endif //VERSION_CONVOLUTIONNAL_CNN_H