Compare commits

...

7 commits

Author SHA1 Message Date
Jakob Stornig
aa58f99045 minor changes 2023-09-25 11:11:07 +02:00
Jakob Stornig
ac35897396 Merge remote-tracking branch 'origin/main' 2023-09-24 22:36:51 +02:00
Jakob Stornig
746f4cf3bd deleted unused functions 2023-09-24 22:36:40 +02:00
jastornig
754d170616 Update .gitlab-ci.yml file 2023-09-24 20:36:11 +00:00
jastornig
8b4f2e2ccd Merge branch 'ci-testing' into 'main'
Ci testing

See merge request jastornig/c-net!12
2023-09-24 20:31:36 +00:00
Jakob Stornig
1cfdf93f7c (fix) evaluation
trained with 50000 images
evaluated with 10000
2023-09-24 22:29:41 +02:00
Jakob Stornig
38fafa672c minor changes for pgm files 2023-09-24 21:27:59 +02:00
6 changed files with 23 additions and 47 deletions

View file

@ -48,7 +48,7 @@ release:
assets: assets:
links: links:
- name: c_net - name: c_net linux download (precompiled)
url: '${CI_PROJECT_URL}/-/jobs/${BUILD_JOB_ID}/artifacts/file/c_net' url: '${CI_PROJECT_URL}/-/jobs/${BUILD_JOB_ID}/artifacts/file/c_net'
script: echo "Define your deployment script!" script: echo "Define your deployment script!"

21
image.c
View file

@ -14,23 +14,27 @@ void big_endian_to_c_uint(const char * bytes, void * target, int size) {
void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){ void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){
int bufferOffset = 0; int bufferOffset = 0;
char c = -1; char c;
int counter = 0;
do{ do{
c = (char)getc(fptr); c = (char)getc(fptr);
buff[bufferOffset++] = c; buff[bufferOffset++] = c;
}while(!feof(fptr) && c != 0 && c != ' ' && c !='\n'); }while(!feof(fptr) && c != 0 && c != ' ' && c !='\n' && counter++ < maxCount);
buff[bufferOffset-1] = 0; buff[bufferOffset-1] = 0;
} }
Image * load_pgm_image(char * image_file_string){ Image * load_pgm_image(char * image_file_string){
FILE * fptr = fopen(image_file_string, "r"); FILE * fptr = fopen(image_file_string, "r");
if(!fptr){
printf("could not open image file. exit\n");
exit(1);
}
Image *image = malloc(sizeof(Image)); Image *image = malloc(sizeof(Image));
image->label = -1; image->label = -1;
char buffer[100]; char buffer[2048];
int magic_number = 0;
fgets(buffer, 4, fptr); fgets(buffer, 4, fptr);
if(buffer[0] != 'P' || buffer[1] != '5'){ if(buffer[0] != 'P' || buffer[1] != '5'){
printf("Wrong file Format"); printf("Wrong file Format");
@ -40,17 +44,16 @@ Image * load_pgm_image(char * image_file_string){
fgets(buffer, 1024, fptr); fgets(buffer, 1024, fptr);
} }
int image_width, image_height, image_length, image_white ; int image_width, image_height, image_white ;
read_until_space_or_newline(buffer, 10, fptr); read_until_space_or_newline(buffer, 10, fptr);
image_width = strtol(buffer, NULL, 10); image_width = (int)strtol(buffer, NULL, 10);
read_until_space_or_newline(buffer, 10, fptr); read_until_space_or_newline(buffer, 10, fptr);
image_height = strtol(buffer, NULL, 10); image_height = (int)strtol(buffer, NULL, 10);
read_until_space_or_newline(buffer, 10, fptr); read_until_space_or_newline(buffer, 10, fptr);
image_white = strtol(buffer, NULL, 10); image_white = (int)strtol(buffer, NULL, 10);
image_length = image_width * image_height;
image->pixel_values = matrix_create(image_height, image_width); image->pixel_values = matrix_create(image_height, image_width);
for(int i = 0; i < image_height; i++){ for(int i = 0; i < image_height; i++){

17
main.c
View file

@ -8,7 +8,7 @@
#include "util.h" #include "util.h"
void parsingErrorPrintHelp(){ void parsingErrorPrintHelp(){
printf("Syntax: c_net [train | detect]\n"); printf("Syntax: c_net [train | predict]\n");
printf("commands:\n"); printf("commands:\n");
printf("train\t train the network\n"); printf("train\t train the network\n");
printf("predict\t load a pgm image and predict_demo the number\n"); printf("predict\t load a pgm image and predict_demo the number\n");
@ -23,7 +23,7 @@ void parsingErrorTrain(){
void parsingErrorDetect(){ void parsingErrorDetect(){
printf("invalid syntax\n"); printf("invalid syntax\n");
printf("Syntax: c_net predict_demo [path_to_network] [image_file]"); printf("Syntax: c_net predict_demo [path_to_network] [image_file]\n");
} }
void predict_demo(int argc, char** arguments){ void predict_demo(int argc, char** arguments){
@ -58,19 +58,18 @@ void train(int argc, char** arguments) {
} }
char *save_path = arguments[6]; char *save_path = arguments[6];
int imported = 0; int imported = 0;
Image **images = import_images(image_file, label_file, &imported, 50000); Image ** images = import_images(image_file, label_file, &imported, 60000);
Image ** evaluation_images = images+50000;
// for(int i = 0; i < imported; i++){ int training_image_count = 50000;
// matrix_save(images[i]->pixel_values, "images.txt"); int testing_image_count = 10000;
// }
// exit(1);
Neural_Network *nn = new_network(28 * 28, neurons_per_layer, hidden_count, 10, learning_rate); Neural_Network *nn = new_network(28 * 28, neurons_per_layer, hidden_count, 10, learning_rate);
randomize_network(nn, 1); randomize_network(nn, 1);
printf("training_network\n"); printf("training_network\n");
for(int epoch = 1; epoch <= epochs; epoch++){ for(int epoch = 1; epoch <= epochs; epoch++){
printf("epoch %d\n", epoch); printf("epoch %d\n", epoch);
for (int i = 0; i < imported; i++) { for (int i = 0; i < training_image_count; i++) {
if (i % 1000 == 0) { if (i % 1000 == 0) {
updateBar(i * 100 / imported); updateBar(i * 100 / imported);
} }
@ -78,7 +77,7 @@ void train(int argc, char** arguments) {
} }
updateBar(100); updateBar(100);
printf("\n"); printf("\n");
printf("accuracy %lf\n", measure_network_accuracy(nn, images, 10000)); printf("accuracy %lf\n", measure_network_accuracy(nn, evaluation_images, testing_image_count));
} }
printf("done training!\n"); printf("done training!\n");
save_network(nn, save_path); save_network(nn, save_path);

View file

@ -216,21 +216,6 @@ Matrix* scale(Matrix* matrix, double value) {
return result_matrix; return result_matrix;
} }
Matrix* addScalar(Matrix* matrix, double value) {
// create a copy of the original matrix
Matrix* result_matrix = matrix_copy(matrix);
// iterate over all numbers in the matrix and add the scalar value
for (int i = 0; i < result_matrix->rows; i++) {
for (int j = 0; j < result_matrix->columns; j++) {
result_matrix->numbers[i][j] += value;
}
}
// return the copy
return result_matrix;
}
Matrix* transpose(Matrix* matrix) { Matrix* transpose(Matrix* matrix) {
@ -249,16 +234,6 @@ Matrix* transpose(Matrix* matrix) {
} }
double matrix_sum(Matrix* matrix) {
double sum = 0;
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->columns; j++) {
sum += matrix->numbers[i][j];
}
}
return sum;
}
void matrix_save(Matrix* matrix, char* file_string){ void matrix_save(Matrix* matrix, char* file_string){
// open the file in append mode // open the file in append mode

View file

@ -36,6 +36,4 @@ Matrix* subtract(Matrix* matrix1, Matrix* matrix2);
Matrix* dot(Matrix* matrix1, Matrix* matrix2); Matrix* dot(Matrix* matrix1, Matrix* matrix2);
Matrix* apply(double (*function)(double), Matrix* matrix); Matrix* apply(double (*function)(double), Matrix* matrix);
Matrix* scale(Matrix* matrix, double value); Matrix* scale(Matrix* matrix, double value);
Matrix* addScalar(Matrix* matrix, double value);
Matrix* transpose(Matrix* matrix); Matrix* transpose(Matrix* matrix);
double matrix_sum(Matrix* matrix);

View file

@ -117,7 +117,8 @@ double measure_network_accuracy(Neural_Network* network, Image** images, int amo
int num_correct = 0; int num_correct = 0;
printf("evaluating network\n"); printf("evaluating network\n");
for (int i = 50001; i < amount; i++) { if(amount > 10000) amount = 10000;
for (int i = 0; i < amount; i++) {
updateBar(i*100/amount); updateBar(i*100/amount);
Matrix* prediction = predict_image(network, images[i]); Matrix* prediction = predict_image(network, images[i]);