-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.cpp
82 lines (68 loc) · 2.33 KB
/
src.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
#include <iostream>
#include <Eigen/Dense>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using Eigen::MatrixXd;
using cv::Mat;
using cv::imread;
using cv::imshow;
using cv::waitKey;
// Define the CNN architecture
const int NUM_CONV_LAYERS = 2;
const int NUM_POOL_LAYERS = 1;
const int NUM_FC_LAYERS = 1;
const int CONV_FILTER_SIZES[] = {3, 3};
const int CONV_NUM_FILTERS[] = {32, 64};
const int POOL_FILTER_SIZES[] = {2};
const int FC_SIZES[] = {100};
// Define the activation function and its derivative
double sigmoid(double x) {
return 1.0 / (1.0 + std::exp(-x));
}
double sigmoidDerivative(double x) {
double s = sigmoid(x);
return s * (1.0 - s);
}
// Define the CNN
class CNN {
public:
// Constructor
CNN(int width, int height, int depth, int numClasses) {
// Initialize the convolutional layer parameters
for (int i = 0; i < NUM_CONV_LAYERS; i++) {
conv_W.push_back(MatrixXd::Random(CONV_FILTER_SIZES[i], CONV_FILTER_SIZES[i], CONV_NUM_FILTERS[i], depth));
conv_b.push_back(MatrixXd::Random(1, 1, CONV_NUM_FILTERS[i]));
depth = CONV_NUM_FILTERS[i];
}
// Initialize the fully connected layer parameters
int inputSize = width * height * depth;
for (int i = 0; i < NUM_FC_LAYERS; i++) {
fc_W.push_back(MatrixXd::Random(FC_SIZES[i], inputSize));
fc_b.push_back(MatrixXd::Random(FC_SIZES[i], 1));
inputSize = FC_SIZES[i];
}
fc_W.push_back(MatrixXd::Random(numClasses, inputSize));
fc_b.push_back(MatrixXd::Random(numClasses, 1));
}
// Perform a forward pass through the CNN
MatrixXd forward(const MatrixXd& input) {
// Perform the convolutional layers
MatrixXd conv_out = input;
for (int i = 0; i < NUM_CONV_LAYERS; i++) {
conv_out = (conv_out.convolve(conv_W[i]) + conv_b[i]).unaryExpr(std::ptr_fun(sigmoid));
// Perform the pool
int main() {
// Load the input data
Mat input_image = imread("input.png", CV_LOAD_IMAGE_COLOR);
// Convert the input data to a matrix
MatrixXd input = MatrixXd::Map(input_image.data, input_image.rows, input_image.cols, input_image.channels());
// Create a CNN object with the desired configuration
CNN cnn(input_image.rows, input_image.cols, input_image.channels(), 10);
// Perform a forward pass through the CNN
MatrixXd output = cnn.forward(input);
// Print the output of the CNN
std::cout << "CNN output:\n" << output << std::endl;
// Wait for a key press before exiting
waitKey(0);
return 0;
}