2022-07-05 08:13:25 +02:00
|
|
|
#include <stdio.h>
|
2022-09-28 10:20:08 +02:00
|
|
|
|
2022-10-24 12:54:51 +02:00
|
|
|
#include "../include/colors.h"
|
2022-09-16 14:53:35 +02:00
|
|
|
#include "include/make.h"
|
2022-07-05 08:13:25 +02:00
|
|
|
|
2022-10-02 20:31:20 +02:00
|
|
|
void make_convolution(Kernel_cnn* kernel, float*** input, float*** output, int output_dim) {
|
2022-07-05 08:13:25 +02:00
|
|
|
float f;
|
2022-09-09 17:39:07 +02:00
|
|
|
int n = kernel->k_size;
|
|
|
|
for (int i=0; i < kernel->columns; i++) {
|
|
|
|
for (int j=0; j < output_dim; j++) {
|
|
|
|
for (int k=0; k < output_dim; k++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
f = kernel->bias[i][j][k];
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int a=0; a < kernel->rows; a++) {
|
|
|
|
for (int b=0; b < n; b++) {
|
|
|
|
for (int c=0; c < n; c++) {
|
2022-10-02 20:31:20 +02:00
|
|
|
f += kernel->w[a][i][b][c]*input[a][j+b][k+c];
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 20:31:20 +02:00
|
|
|
output[i][j][k] = f/n; // Average
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_average_pooling(float*** input, float*** output, int size, int output_depth, int output_dim) {
|
|
|
|
float average;
|
2022-09-09 17:39:07 +02:00
|
|
|
int n = size*size;
|
|
|
|
for (int i=0; i < output_depth; i++) {
|
|
|
|
for (int j=0; j < output_dim; j++) {
|
|
|
|
for (int k=0; k < output_dim; k++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
average = 0.;
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int a=0; a < size; a++) {
|
|
|
|
for (int b=0; b < size; b++) {
|
2022-10-02 20:31:20 +02:00
|
|
|
average += input[i][size*j +a][size*k +b];
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-19 18:39:49 +02:00
|
|
|
output[i][j][k] = average/n;
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 20:31:20 +02:00
|
|
|
void make_dense(Kernel_nn* kernel, float* input, float* output, int size_input, int size_output) {
|
2022-07-05 08:13:25 +02:00
|
|
|
float f;
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int i=0; i < size_output; i++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
f = kernel->bias[i];
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int j=0; j < size_input; j++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
f += kernel->weights[i][j]*input[j];
|
|
|
|
}
|
|
|
|
output[i] = f;
|
|
|
|
}
|
2022-10-02 20:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void make_dense_linearised(Kernel_nn* kernel, float*** input, float* output, int depth_input, int dim_input, int size_output) {
|
|
|
|
int n = depth_input*dim_input*dim_input;
|
|
|
|
float f;
|
|
|
|
for (int l=0; l<size_output; l++) {
|
|
|
|
f = 0;
|
|
|
|
for (int i=0; i<depth_input; i++) {
|
|
|
|
for (int j=0; j<dim_input; j++) {
|
|
|
|
for (int k=0; k<dim_input; k++) {
|
|
|
|
f += input[i][j][k]*kernel->weights[k + j*dim_input + i*depth_input][l];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output[l] = f/size_output;
|
|
|
|
}
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|