Merge remote-tracking branch 'origin/Development' into Development
This commit is contained in:
commit
b20a848e1b
6 changed files with 91 additions and 19 deletions
|
|
@ -3,4 +3,5 @@ project(c_net C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
|
||||||
add_executable(c_net main.c matrix.c)
|
add_executable(c_net main.c matrix.c image.c)
|
||||||
|
target_link_libraries(c_net m)
|
||||||
|
|
|
||||||
57
image.c
57
image.c
|
|
@ -1,39 +1,76 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
Img** import_images(char* image_file_string, char* label_file_string, int number_of_images) {
|
int endian_swap(int input) {
|
||||||
|
return ((input >> 24) & 0xff) | // move byte 3 to byte 0
|
||||||
|
((input << 8) & 0xff0000) | // move byte 1 to byte 2
|
||||||
|
((input >> 8) & 0xff00) | // move byte 2 to byte 1
|
||||||
|
((input << 24) & 0xff000000); // byte 0 to byte 3
|
||||||
|
}
|
||||||
|
|
||||||
|
int validate_files(FILE* image_file, FILE* label_file) {
|
||||||
|
|
||||||
|
// read magic number from files
|
||||||
|
int magic_number_label, magic_number_images;
|
||||||
|
fread(&magic_number_label, 4, 1, label_file);
|
||||||
|
fread(&magic_number_images, 4, 1, image_file);
|
||||||
|
|
||||||
|
// compare magic numbers with pre-defined value
|
||||||
|
if(endian_swap(magic_number_label) != 2049 || endian_swap(magic_number_images) != 2051) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Image** import_images(char* image_file_string, char* label_file_string, int number_of_images) {
|
||||||
|
|
||||||
// create file pointer for the image and label data
|
// create file pointer for the image and label data
|
||||||
FILE* image_file = fopen(image_file_string, 'r');
|
FILE* image_file = fopen(image_file_string, "r");
|
||||||
FILE* label_file = fopen(label_file_string, 'r');
|
FILE* label_file = fopen(label_file_string, "r");
|
||||||
|
|
||||||
// check if the file could be opened
|
// check if the file could be opened
|
||||||
if(image_file == NULL || label_file == NULL) {
|
if(image_file == NULL || label_file == NULL) {
|
||||||
printf("ERROR: File could not be opened! ()");
|
printf("ERROR: File could not be opened! (import_images)");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check magic number of the files
|
||||||
|
if(validate_files(image_file, label_file)) {
|
||||||
|
printf("ERROR: File validation failed! (validate_files)");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Jakob Section
|
||||||
|
|
||||||
|
|
||||||
|
// allocate memory for the storage of images
|
||||||
|
Image** images = malloc(sizeof(Image) * number_of_images);
|
||||||
|
|
||||||
|
fclose(image_file);
|
||||||
|
fclose(label_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void img_print (Img* img) {
|
void img_print (Image* img) {
|
||||||
|
|
||||||
//print the image
|
//print the image
|
||||||
matrix_print(img->pixel_values);
|
matrix_print(img->pixel_values);
|
||||||
//print the number of the image
|
//print the number of the image
|
||||||
printf("Number it is supposed to be: %d\n", img->image_label);
|
printf("Number it is supposed to be: %d\n", img->image_label);
|
||||||
}
|
}
|
||||||
|
|
||||||
void img_free (Img* img) {
|
void img_free (Image* img) {
|
||||||
//frees the matrix of image (deep free)
|
//frees the matrix of image (deep free)
|
||||||
matrix_free(img->pixel_values);
|
matrix_free(img->pixel_values);
|
||||||
//frees the rest of img
|
//frees the rest of img
|
||||||
free(img);
|
free(img);
|
||||||
}
|
}
|
||||||
|
|
||||||
void images_free (Img** images, int quantity){
|
void images_free (Image** images, int quantity){
|
||||||
//frees every single image
|
//frees every single image
|
||||||
for(int i=0;i<quantity;i++){
|
for(int i=0;i<quantity;i++){
|
||||||
img_free(images[i]);
|
img_free(images[i]);
|
||||||
|
|
|
||||||
9
image.h
9
image.h
|
|
@ -1,10 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Matrix* pixel_values;
|
Matrix* pixel_values;
|
||||||
int image_label;
|
int image_label;
|
||||||
} Image;
|
} Image;
|
||||||
|
|
||||||
Img** import_images(char* image_file_string, char* label_file_string, int number_of_images);
|
Image** import_images(char* image_file_string, char* label_file_string, int number_of_images);
|
||||||
void img_print (Img* img);
|
void img_print (Image* image);
|
||||||
void img_free (Img* img);
|
void img_free (Image* image);
|
||||||
5
main.c
5
main.c
|
|
@ -1,7 +1,10 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
#include <stdio.h>
|
#include "image.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", 20);
|
||||||
|
|
||||||
}
|
}
|
||||||
33
matrix.c
33
matrix.c
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#define MAX_BYTES 100
|
||||||
|
|
||||||
// operational functions
|
// operational functions
|
||||||
Matrix* matrix_create(int rows, int columns) {
|
Matrix* matrix_create(int rows, int columns) {
|
||||||
|
|
@ -252,12 +253,40 @@ void matrix_save(Matrix* matrix, char* file_string){
|
||||||
printf("Unable to get handle for \"%s\"", file_string);
|
printf("Unable to get handle for \"%s\"", file_string);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
fprintf(fptr, "%d\n", matrix->rows);
|
||||||
|
fprintf(fptr, "%d\n", matrix->columns);
|
||||||
|
|
||||||
for(int i = 0; i < matrix->rows; i++){
|
for(int i = 0; i < matrix->rows; i++){
|
||||||
for(int j = 0; j < matrix->columns; j++){
|
for(int j = 0; j < matrix->columns; j++){
|
||||||
fprintf(fptr, "%f.12 ", matrix->numbers[i][j]);
|
fprintf(fptr, "%.10f\n", matrix->numbers[i][j]);
|
||||||
}
|
}
|
||||||
fputc('\n', fptr);
|
|
||||||
}
|
}
|
||||||
|
printf("saved matrix to %s", file_string);
|
||||||
|
fclose(fptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix* matrix_load(char* file_string){
|
||||||
|
FILE *fptr = fopen(file_string, "r");
|
||||||
|
if(!fptr){
|
||||||
|
printf("Could not open \"%s\"", file_string);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
char buffer[MAX_BYTES];
|
||||||
|
|
||||||
|
fgets(buffer, MAX_BYTES, fptr);
|
||||||
|
int rows = (int)strtol(buffer, NULL, 10);
|
||||||
|
fgets(buffer, MAX_BYTES, fptr);
|
||||||
|
int cols = (int)strtol(buffer, NULL, 10);
|
||||||
|
|
||||||
|
Matrix *matrix = matrix_create(rows, cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < rows; i++){
|
||||||
|
for(int j = 0; j < cols; j++){
|
||||||
|
fgets(buffer, MAX_BYTES, fptr);
|
||||||
|
matrix->numbers[i][j] = strtod(buffer, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
3
matrix.h
3
matrix.h
|
|
@ -11,10 +11,9 @@ void matrix_fill(Matrix* matrix, double value);
|
||||||
void matrix_free(Matrix* matrix);
|
void matrix_free(Matrix* matrix);
|
||||||
void matrix_print(Matrix *matrix);
|
void matrix_print(Matrix *matrix);
|
||||||
Matrix* matrix_copy(Matrix *matrix);
|
Matrix* matrix_copy(Matrix *matrix);
|
||||||
|
|
||||||
// TODO:
|
|
||||||
void matrix_save(Matrix* matrix, char* file_string);
|
void matrix_save(Matrix* matrix, char* file_string);
|
||||||
Matrix* matrix_load(char* file_string);
|
Matrix* matrix_load(char* file_string);
|
||||||
|
|
||||||
void matrix_randomize(Matrix* matrix, int n); // don't understand the usage of the n
|
void matrix_randomize(Matrix* matrix, int n); // don't understand the usage of the n
|
||||||
int matrix_argmax(Matrix* matrix);
|
int matrix_argmax(Matrix* matrix);
|
||||||
Matrix* matrix_flatten(Matrix* matrix, int axis);
|
Matrix* matrix_flatten(Matrix* matrix, int axis);
|
||||||
|
|
|
||||||
Reference in a new issue