用c语言写出LeNet-5
时间: 2024-03-08 19:48:30 浏览: 121
基于YANN LECUN的论文设计的LeNet-5神经网络,C语言写成,不依赖任何第三方库
LeNet-5是一种经典的卷积神经网络结构,下面是用C语言实现的LeNet-5代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define WIDTH 32
#define HEIGHT 32
#define CHANNELS 1
#define CONV1_FILTER_SIZE 5
#define CONV1_FILTER_NUM 6
#define CONV1_PADDING 0
#define CONV1_STRIDE 1
#define POOL1_SIZE 2
#define POOL1_STRIDE 2
#define CONV2_FILTER_SIZE 5
#define CONV2_FILTER_NUM 16
#define CONV2_PADDING 0
#define CONV2_STRIDE 1
#define POOL2_SIZE 2
#define POOL2_STRIDE 2
#define FC1_SIZE 120
#define FC2_SIZE 84
#define OUTPUT_SIZE 10
float relu(float x) {
return x > 0 ? x : 0;
}
float sigmoid(float x) {
return 1 / (1 + expf(-x));
}
void conv(float *input, float *output, float *filter, int input_w, int input_h, int input_c, int filter_w, int filter_h, int filter_n, int stride, int padding) {
int output_w = (input_w - filter_w + 2 * padding) / stride + 1;
int output_h = (input_h - filter_h + 2 * padding) / stride + 1;
int output_size = output_w * output_h * filter_n;
for (int i = 0; i < output_size; i++) {
output[i] = 0;
}
for (int n = 0; n < filter_n; n++) {
for (int y = 0; y < output_h; y++) {
for (int x = 0; x < output_w; x++) {
for (int c = 0; c < input_c; c++) {
for (int fy = 0; fy < filter_h; fy++) {
for (int fx = 0; fx < filter_w; fx++) {
int ix = x * stride + fx - padding;
int iy = y * stride + fy - padding;
if (ix >= 0 && ix < input_w && iy >= 0 && iy < input_h) {
output[(n * output_h + y) * output_w + x] += input[(c * input_h + iy) * input_w + ix] * filter[(n * input_c + c) * filter_h * filter_w + fy * filter_w + fx];
}
}
}
}
}
}
}
}
void pool(float *input, float *output, int input_w, int input_h, int input_c, int size, int stride) {
int output_w = (input_w - size) / stride + 1;
int output_h = (input_h - size) / stride + 1;
int output_size = output_w * output_h * input_c;
for (int i = 0; i < output_size; i++) {
output[i] = 0;
}
for (int c = 0; c < input_c; c++) {
for (int y = 0; y < output_h; y++) {
for (int x = 0; x < output_w; x++) {
float max_val = input[(c * input_h + y * stride) * input_w + x * stride];
for (int fy = 0; fy < size; fy++) {
for (int fx = 0; fx < size; fx++) {
int ix = x * stride + fx;
int iy = y * stride + fy;
if (ix < input_w && iy < input_h) {
float val = input[(c * input_h + iy) * input_w + ix];
if (val > max_val) {
max_val = val;
}
}
}
}
output[(c * output_h + y) * output_w + x] = max_val;
}
}
}
}
void fc(float *input, float *output, float *weight, float *bias, int input_size, int output_size) {
for (int i = 0; i < output_size; i++) {
output[i] = 0;
}
for (int i = 0; i < output_size; i++) {
for (int j = 0; j < input_size; j++) {
output[i] += input[j] * weight[i * input_size + j];
}
output[i] += bias[i];
}
}
int argmax(float *output, int size) {
int max_index = 0;
float max_val = output[0];
for (int i = 1; i < size; i++) {
if (output[i] > max_val) {
max_index = i;
max_val = output[i];
}
}
return max_index;
}
int main() {
float input[WIDTH * HEIGHT * CHANNELS];
float conv1_output[(WIDTH - CONV1_FILTER_SIZE + 2 * CONV1_PADDING) / CONV1_STRIDE + 1][(HEIGHT - CONV1_FILTER_SIZE + 2 * CONV1_PADDING) / CONV1_STRIDE + 1][CONV1_FILTER_NUM];
float pool1_output[(WIDTH - CONV1_FILTER_SIZE + 2 * CONV1_PADDING) / CONV1_STRIDE / POOL1_STRIDE + 1][(HEIGHT - CONV1_FILTER_SIZE + 2 * CONV1_PADDING) / CONV1_STRIDE / POOL1_STRIDE + 1][CONV1_FILTER_NUM];
float conv2_output[(pool1_output_w - CONV2_FILTER_SIZE + 2 * CONV2_PADDING) / CONV2_STRIDE + 1][(pool1_output_h - CONV2_FILTER_SIZE + 2 * CONV2_PADDING) / CONV2_STRIDE + 1][CONV2_FILTER_NUM];
float pool2_output[(pool1_output_w - CONV2_FILTER_SIZE + 2 * CONV2_PADDING) / CONV2_STRIDE / POOL2_STRIDE + 1][(pool1_output_h - CONV2_FILTER_SIZE + 2 * CONV2_PADDING) / CONV2_STRIDE / POOL2_STRIDE + 1][CONV2_FILTER_NUM];
float fc1_output[FC1_SIZE];
float fc2_output[FC2_SIZE];
float output[OUTPUT_SIZE];
float conv1_filter[CONV1_FILTER_NUM * CHANNELS * CONV1_FILTER_SIZE * CONV1_FILTER_SIZE];
float conv1_bias[CONV1_FILTER_NUM];
float conv2_filter[CONV2_FILTER_NUM * CONV1_FILTER_NUM * CONV2_FILTER_SIZE * CONV2_FILTER_SIZE];
float conv2_bias[CONV2_FILTER_NUM];
float fc1_weight[FC1_SIZE * (pool2_output_w / POOL1_SIZE / POOL2_SIZE) * (pool2_output_h / POOL1_SIZE / POOL2_SIZE) * CONV2_FILTER_NUM];
float fc1_bias[FC1_SIZE];
float fc2_weight[FC2_SIZE * FC1_SIZE];
float fc2_bias[FC2_SIZE];
// load parameters
// ...
// forward pass
conv(input, conv1_output, conv1_filter, WIDTH, HEIGHT, CHANNELS, CONV1_FILTER_SIZE, CONV1_FILTER_SIZE, CONV1_FILTER_NUM, CONV1_STRIDE, CONV1_PADDING);
for (int i = 0; i < CONV1_FILTER_NUM; i++) {
for (int j = 0; j < pool1_output_h; j++) {
for (int k = 0; k < pool1_output_w; k++) {
pool1_output[j][k][i] = relu(conv1_output[j][k][i]);
}
}
}
conv(pool1_output, conv2_output, conv2_filter, pool1_output_w, pool1_output_h, CONV1_FILTER_NUM, CONV2_FILTER_SIZE, CONV2_FILTER_SIZE, CONV2_FILTER_NUM, CONV2_STRIDE, CONV2_PADDING);
for (int i = 0; i < CONV2_FILTER_NUM; i++) {
for (int j = 0; j < pool2_output_h; j++) {
for (int k = 0; k < pool2_output_w; k++) {
pool2_output[j][k][i] = relu(conv2_output[j][k][i]);
}
}
}
fc((float *)pool2_output, fc1_output, fc1_weight, fc1_bias, pool2_output_w / POOL1_SIZE / POOL2_SIZE * pool2_output_h / POOL1_SIZE / POOL2_SIZE * CONV2_FILTER_NUM, FC1_SIZE);
for (int i = 0; i < FC1_SIZE; i++) {
fc1_output[i] = relu(fc1_output[i]);
}
fc(fc1_output, fc2_output, fc2_weight, fc2_bias, FC1_SIZE, FC2_SIZE);
for (int i = 0; i < FC2_SIZE; i++) {
fc2_output[i] = sigmoid(fc2_output[i]);
}
fc(fc2_output, output, output_weight, output_bias, FC2_SIZE, OUTPUT_SIZE);
// get prediction
int prediction = argmax(output, OUTPUT_SIZE);
printf("Prediction: %d\n", prediction);
return 0;
}
```
注意:这只是一个示例代码,其中的参数和参数加载部分并没有实现,需要根据具体应用场景进行修改。
阅读全文