forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
25
Commits
56d59b1b50
...
Krisp2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a1ff310db | ||
|
|
3c4e4df496 | ||
|
|
fd1bc886a7 | ||
|
|
efa260ccbe | ||
|
|
801abc1b66 | ||
|
|
8e518a3bdd | ||
|
|
0baf646832 | ||
|
|
0f0f2f19c3 | ||
|
|
98dd789680 | ||
|
|
7aa57191da | ||
|
|
da4eaa718d | ||
|
|
21d9b5c01d | ||
|
|
e7930c7eb0 | ||
|
|
5075c34983 | ||
|
|
b187a13b17 | ||
|
|
4e2ee7078a | ||
|
|
35a598a276 | ||
|
|
e1ea9f33cd | ||
|
|
7f3c6d1d3f | ||
|
|
0886489d49 | ||
|
|
f9c46a6784 | ||
|
|
5fcc3cd042 | ||
|
|
3de79e2b83 | ||
|
|
ec54bdd951 | ||
|
|
0e3f03a03d |
@@ -2,3 +2,9 @@ mnist
|
||||
runTests
|
||||
*.o
|
||||
*.exe
|
||||
.vscode/settings.json
|
||||
.vscode/launch.json
|
||||
.vscode/settings.json
|
||||
.vscode/settings.json
|
||||
runImageInputTests
|
||||
testFile.info2
|
||||
+123
-16
@@ -1,29 +1,136 @@
|
||||
#include "imageInput.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "imageInput.h"
|
||||
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
// define BUFFER 100
|
||||
// 10x10 pixel
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
GrayScaleImage readImage()
|
||||
{
|
||||
|
||||
/* ----------------------------------------------------------
|
||||
1. Header prüfen
|
||||
---------------------------------------------------------- */
|
||||
static int readHeader(FILE *file) {
|
||||
char header[sizeof(FILE_HEADER_STRING)];
|
||||
if (fread(header, 1, sizeof(FILE_HEADER_STRING) - 1, file) !=
|
||||
sizeof(FILE_HEADER_STRING) - 1)
|
||||
return 0;
|
||||
header[sizeof(FILE_HEADER_STRING) - 1] = '\0';
|
||||
return strcmp(header, FILE_HEADER_STRING) == 0;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
FILE *file = fopen("mnist_test.info2","rb");
|
||||
char headOfFile;
|
||||
series = malloc();
|
||||
/* ----------------------------------------------------------
|
||||
2. Meta-Daten lesen (unsigned short)
|
||||
---------------------------------------------------------- */
|
||||
static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
|
||||
unsigned short *height) {
|
||||
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||
return 0;
|
||||
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||
return 0;
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------
|
||||
3. Einzelbild lesen
|
||||
---------------------------------------------------------- */
|
||||
static int readSingleImage(FILE *file, GrayScaleImage *img,
|
||||
unsigned short width, unsigned short height) {
|
||||
img->width = width;
|
||||
img->height = height;
|
||||
|
||||
size_t numPixels = (size_t)width * (size_t)height; // anzahl an pixeln
|
||||
img->buffer = malloc(numPixels);
|
||||
if (!img->buffer)
|
||||
return 0;
|
||||
|
||||
if (fread(img->buffer, 1, numPixels, file) != numPixels) {
|
||||
free(img->buffer);
|
||||
img->buffer = NULL; // fehler bei ungültiger eingabe
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------
|
||||
4. Label lesen
|
||||
---------------------------------------------------------- */
|
||||
static int readLabel(FILE *file, unsigned char *label) {
|
||||
return fread(label, 1, 1, file) == 1;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------
|
||||
5. Komplette Bildserie lesen
|
||||
---------------------------------------------------------- */
|
||||
GrayScaleImageSeries *readImages(const char *path) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (!file)
|
||||
return NULL;
|
||||
|
||||
if (!readHeader(file)) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned short count, width, height;
|
||||
if (!readMeta(file, &count, &width, &height)) {
|
||||
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
// printf("%d, %d, %d", count, width, height);
|
||||
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||
if (!series) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = count;
|
||||
series->images = malloc(count * sizeof(GrayScaleImage));
|
||||
series->labels = malloc(count * sizeof(unsigned char));
|
||||
if (!series->images || !series->labels) {
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (!readSingleImage(file, &series->images[i], width, height) ||
|
||||
!readLabel(file, &series->labels[i])) {
|
||||
// Aufräumen bei Fehler
|
||||
for (unsigned int j = 0; j < i; j++) {
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
/* ----------------------------------------------------------
|
||||
6. Speicher komplett freigeben
|
||||
---------------------------------------------------------- */
|
||||
void clearSeries(GrayScaleImageSeries *series) {
|
||||
if (!series)
|
||||
return;
|
||||
|
||||
for (unsigned int i = 0; i < series->count; i++) {
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
}
|
||||
+123
-56
@@ -1,88 +1,98 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "imageInput.h"
|
||||
#include "unity.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
||||
{
|
||||
/* ---------------------------------------------------------
|
||||
Hilfsfunktion: Testdatei vorbereiten
|
||||
--------------------------------------------------------- */
|
||||
static void prepareImageFile(const char *path, unsigned int width,
|
||||
unsigned int height, unsigned int numberOfImages,
|
||||
unsigned char label) {
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (!file)
|
||||
return;
|
||||
|
||||
if(file != NULL)
|
||||
{
|
||||
// Header
|
||||
const char *fileTag = "__info2_image_file_format__";
|
||||
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
|
||||
fwrite(fileTag, 1, strlen(fileTag), file);
|
||||
|
||||
if(zeroBuffer != NULL)
|
||||
{
|
||||
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
||||
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
||||
fwrite(&width, sizeof(width), 1, file);
|
||||
fwrite(&height, sizeof(height), 1, file);
|
||||
// Meta-Daten als unsigned short
|
||||
unsigned short n = (unsigned short)numberOfImages;
|
||||
unsigned short w = (unsigned short)width;
|
||||
unsigned short h = (unsigned short)height;
|
||||
fwrite(&n, sizeof(unsigned short), 1, file);
|
||||
fwrite(&w, sizeof(unsigned short), 1, file);
|
||||
fwrite(&h, sizeof(unsigned short), 1, file);
|
||||
|
||||
for(int i = 0; i < numberOfImages; i++)
|
||||
{
|
||||
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
|
||||
// Pixelbuffer
|
||||
GrayScalePixelType *buffer =
|
||||
calloc(width * height, sizeof(GrayScalePixelType));
|
||||
if (!buffer) {
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
for (unsigned int i = 0; i < width * height; i++)
|
||||
buffer[i] = (GrayScalePixelType)i;
|
||||
|
||||
// Jedes Bild schreiben: Pixel + Label
|
||||
for (unsigned int img = 0; img < numberOfImages; img++) {
|
||||
fwrite(buffer, sizeof(GrayScalePixelType), width * height, file);
|
||||
fwrite(&label, sizeof(unsigned char), 1, file);
|
||||
}
|
||||
|
||||
free(zeroBuffer);
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
Unit Tests
|
||||
--------------------------------------------------------- */
|
||||
|
||||
void test_readImagesReturnsCorrectNumberOfImages(void)
|
||||
{
|
||||
void test_readImagesReturnsCorrectNumberOfImages(void) {
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
const unsigned short expectedNumberOfImages = 2;
|
||||
const unsigned int expectedNumberOfImages = 2;
|
||||
const char *path = "testFile.info2";
|
||||
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
||||
series = readImages(path);
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
||||
TEST_ASSERT_EQUAL_UINT(expectedNumberOfImages, series->count);
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
void test_readImagesReturnsCorrectImageWidth(void)
|
||||
{
|
||||
void test_readImagesReturnsCorrectImageWidth(void) {
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
const unsigned short expectedWidth = 10;
|
||||
const unsigned int expectedWidth = 10;
|
||||
const char *path = "testFile.info2";
|
||||
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
||||
series = readImages(path);
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_NOT_NULL(series->images);
|
||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
||||
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width);
|
||||
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width);
|
||||
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||
TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[0].width);
|
||||
TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[1].width);
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
void test_readImagesReturnsCorrectImageHeight(void)
|
||||
{
|
||||
void test_readImagesReturnsCorrectImageHeight(void) {
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
const unsigned short expectedHeight = 10;
|
||||
const unsigned int expectedHeight = 10;
|
||||
const char *path = "testFile.info2";
|
||||
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
||||
series = readImages(path);
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_NOT_NULL(series->images);
|
||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
||||
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height);
|
||||
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height);
|
||||
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||
TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[0].height);
|
||||
TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[1].height);
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
void test_readImagesReturnsCorrectLabels(void)
|
||||
{
|
||||
void test_readImagesReturnsCorrectLabels(void) {
|
||||
const unsigned char expectedLabel = 15;
|
||||
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
@@ -91,7 +101,7 @@ void test_readImagesReturnsCorrectLabels(void)
|
||||
series = readImages(path);
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_NOT_NULL(series->labels);
|
||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
||||
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
|
||||
}
|
||||
@@ -99,19 +109,16 @@ void test_readImagesReturnsCorrectLabels(void)
|
||||
remove(path);
|
||||
}
|
||||
|
||||
void test_readImagesReturnsNullOnNotExistingPath(void)
|
||||
{
|
||||
void test_readImagesReturnsNullOnNotExistingPath(void) {
|
||||
const char *path = "testFile.txt";
|
||||
remove(path);
|
||||
TEST_ASSERT_NULL(readImages(path));
|
||||
}
|
||||
|
||||
void test_readImagesFailsOnWrongFileTag(void)
|
||||
{
|
||||
void test_readImagesFailsOnWrongFileTag(void) {
|
||||
const char *path = "testFile.info2";
|
||||
FILE *file = fopen(path, "w");
|
||||
if(file != NULL)
|
||||
{
|
||||
if (file != NULL) {
|
||||
fprintf(file, "some_tag ");
|
||||
fclose(file);
|
||||
TEST_ASSERT_NULL(readImages(path));
|
||||
@@ -119,25 +126,85 @@ void test_readImagesFailsOnWrongFileTag(void)
|
||||
remove(path);
|
||||
}
|
||||
|
||||
void setUp(void) {
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
// Test
|
||||
|
||||
void test_read_GrayScale_Pixel(
|
||||
void) { // testet das einlesen eines graustufenbildes von readImages()
|
||||
GrayScaleImageSeries *series = NULL; // enthält später das Bild
|
||||
const char *path = "testFile.info2";
|
||||
|
||||
prepareImageFile(path, 8, 8, 1,
|
||||
1); // Höhe x Breite in Pixel, Anzahl Bilder und Kategorie
|
||||
series = readImages(path);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(series); // Speicher reservieren
|
||||
TEST_ASSERT_NOT_NULL(series->images); // Inhalt ist da
|
||||
TEST_ASSERT_EQUAL_UINT(1, series->count); // Anzahl der Bilder stimmt
|
||||
|
||||
for (int i = 0; i < (8 * 8); i++) {
|
||||
TEST_ASSERT_EQUAL_UINT8(
|
||||
(GrayScalePixelType)i,
|
||||
series->images[0].buffer[i]); // alle Pixelwerte prüfen
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
/* ---------------------------------------------------------
|
||||
Optional: Mehrere Bilder gleichzeitig testen
|
||||
--------------------------------------------------------- */
|
||||
|
||||
void test_readImagesMultipleImagesContent(void) {
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
const char *path = "testFile.info2";
|
||||
const unsigned int numberOfImages = 3;
|
||||
const unsigned int width = 4;
|
||||
const unsigned int height = 4;
|
||||
const unsigned char label = 7;
|
||||
|
||||
prepareImageFile(path, width, height, numberOfImages, label);
|
||||
|
||||
series = readImages(path);
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_NOT_NULL(series->images);
|
||||
TEST_ASSERT_NOT_NULL(series->labels);
|
||||
TEST_ASSERT_EQUAL_UINT(numberOfImages, series->count);
|
||||
|
||||
for (unsigned int img = 0; img < numberOfImages; img++) {
|
||||
for (unsigned int i = 0; i < width * height; i++)
|
||||
TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i,
|
||||
series->images[img].buffer[i]);
|
||||
TEST_ASSERT_EQUAL_UINT8(label, series->labels[img]);
|
||||
}
|
||||
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
Setup / Teardown
|
||||
--------------------------------------------------------- */
|
||||
void setUp(void) {}
|
||||
void tearDown(void) {}
|
||||
|
||||
/* ---------------------------------------------------------
|
||||
main()
|
||||
--------------------------------------------------------- */
|
||||
int main(void) {
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\nImage input tests\n============================\n");
|
||||
printf("\n============================\nImage input "
|
||||
"tests\n============================\n");
|
||||
|
||||
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
||||
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
||||
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
||||
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
||||
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||
RUN_TEST(test_read_GrayScale_Pixel);
|
||||
RUN_TEST(test_readImagesMultipleImagesContent);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -59,7 +59,8 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
|
||||
# --------------------------
|
||||
clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
del /f *.o *.exe
|
||||
else
|
||||
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
||||
else
|
||||
del /f *.o *.exe
|
||||
endif
|
||||
|
||||
@@ -3,17 +3,15 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
/*typedef struct {
|
||||
unsigned int rows; //Zeilen
|
||||
unsigned int cols; //Spalten
|
||||
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
||||
} Matrix;*/
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||
|
||||
Matrix createMatrix(const unsigned int rows, const unsigned int cols) {
|
||||
if (cols == 0 || rows == 0) {
|
||||
Matrix errorMatrix = {0, 0, NULL};
|
||||
if (rows == 0 || cols == 0) {
|
||||
|
||||
return errorMatrix;
|
||||
}
|
||||
MatrixType *buffer =
|
||||
@@ -23,202 +21,168 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||
return newMatrix;
|
||||
}
|
||||
void clearMatrix(Matrix *matrix) {
|
||||
matrix->buffer = UNDEFINED_MATRIX_VALUE;
|
||||
matrix->rows = UNDEFINED_MATRIX_VALUE;
|
||||
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
||||
free((*matrix).buffer); // Speicher freigeben
|
||||
|
||||
if (matrix->buffer != NULL) {
|
||||
free((*matrix).buffer);
|
||||
matrix->buffer = NULL;
|
||||
}
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
|
||||
void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||
const unsigned int colIdx) {
|
||||
|
||||
if (rowIdx >= matrix.rows ||
|
||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
|
||||
// Speichergröße nicht überschreiten
|
||||
return;
|
||||
}
|
||||
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] =
|
||||
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
// rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
||||
// innerhalb der Zeile
|
||||
}
|
||||
MatrixType getMatrixAt(const Matrix matrix,
|
||||
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||
unsigned int colIdx) {
|
||||
if (rowIdx >= matrix.rows ||
|
||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
||||
return 0;
|
||||
MatrixType
|
||||
getMatrixAt(const Matrix matrix,
|
||||
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||
const unsigned int colIdx) {
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
|
||||
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
||||
const unsigned int cols) {
|
||||
|
||||
Matrix copy = createMatrix(
|
||||
rows, cols); // Matrix 1 Kopie erstellen mit Dimensionen von Matrix2
|
||||
for (int r = 0; r < rows; r++) {
|
||||
|
||||
MatrixType value = getMatrixAt(matrix, r, 0);
|
||||
Matrix broadCastCols(const Matrix matrix, const unsigned int cols) {
|
||||
Matrix copy1 = createMatrix(matrix.rows, cols);
|
||||
for (int r = 0; r < matrix.rows; r++) {
|
||||
MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0);
|
||||
for (int c = 0; c < cols; c++) {
|
||||
|
||||
setMatrixAt(value, copy, r, c);
|
||||
setMatrixAt(valueMatrix1, copy1, r, c);
|
||||
}
|
||||
}
|
||||
|
||||
return copy;
|
||||
return copy1;
|
||||
}
|
||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
||||
const unsigned int cols) {
|
||||
|
||||
Matrix copy = createMatrix(rows, cols);
|
||||
|
||||
for (int c = 0; c < cols; c++) {
|
||||
MatrixType value = getMatrixAt(matrix, 0, c);
|
||||
|
||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows) {
|
||||
Matrix copy1 = createMatrix(rows, matrix.cols);
|
||||
for (int c = 0; c < matrix.cols; c++) {
|
||||
MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c);
|
||||
for (int r = 0; r < rows; r++) {
|
||||
|
||||
setMatrixAt(value, copy, r, c);
|
||||
setMatrixAt(valueMatrix1, copy1, r, c);
|
||||
}
|
||||
}
|
||||
|
||||
return copy;
|
||||
return copy1;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||
|
||||
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassenden
|
||||
// Matrizen
|
||||
// Ergebnismatrix
|
||||
Matrix result;
|
||||
const int cols1 = matrix1.cols;
|
||||
const int rows1 = matrix1.rows;
|
||||
const int cols2 = matrix2.cols;
|
||||
const int rows2 = matrix2.rows;
|
||||
|
||||
const unsigned int rows1 = matrix1.rows;
|
||||
const unsigned int rows2 = matrix2.rows;
|
||||
const unsigned int cols1 = matrix1.cols;
|
||||
const unsigned int cols2 = matrix2.cols;
|
||||
const int rowsEqual = (matrix1.rows == matrix2.rows) ? 1 : 0;
|
||||
const int colsEqual = (matrix1.cols == matrix2.cols) ? 1 : 0;
|
||||
|
||||
const int rowsEqual = ((rows1 == rows2) ? 1 : 0);
|
||||
|
||||
const int colsEqual = ((cols1 == cols2) ? 1 : 0);
|
||||
|
||||
if (rowsEqual && colsEqual) // addieren
|
||||
|
||||
{
|
||||
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
|
||||
|
||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
||||
|
||||
result.buffer[i] =
|
||||
(matrix1.buffer[i] +
|
||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
||||
// Startadresse + (i * sizeof(MatrixType))
|
||||
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender
|
||||
// Matrix
|
||||
if (rowsEqual == 1 && colsEqual == 1) {
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){0, 0, NULL};
|
||||
}
|
||||
|
||||
return result; // zurückgeben
|
||||
for (int i = 0; i < rows1; i++) {
|
||||
for (int j = 0; j < cols1; j++) {
|
||||
int valueM1 = getMatrixAt(matrix1, i, j);
|
||||
int valueM2 = getMatrixAt(matrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
|
||||
else if (rowsEqual && !colsEqual) {
|
||||
|
||||
if (cols1 == 1) {
|
||||
|
||||
Matrix result = createMatrix(rows2, cols2);
|
||||
|
||||
Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
|
||||
|
||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
||||
|
||||
result.buffer[i] =
|
||||
(copy1.buffer[i] +
|
||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
||||
// Startadresse + (i * sizeof(MatrixType))
|
||||
}
|
||||
return result;
|
||||
|
||||
// add und return
|
||||
|
||||
} else if (cols2 == 1) {
|
||||
|
||||
Matrix result = createMatrix(rows1, cols1);
|
||||
|
||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
||||
|
||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
||||
|
||||
result.buffer[i] =
|
||||
(matrix1.buffer[i] +
|
||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
||||
// Startadresse + (i * sizeof(MatrixType))
|
||||
} else if (rowsEqual == 1 && (cols1 == 1 || cols2 == 1)) {
|
||||
if (cols1 == 1) { // broadcasting von vektor 1 zu matrix 1, add
|
||||
Matrix newMatrix = broadCastCols(matrix1, cols2);
|
||||
// add
|
||||
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){0, 0, NULL};
|
||||
}
|
||||
for (int i = 0; i < rows1; i++) {
|
||||
for (int j = 0; j < cols2; j++) {
|
||||
int valueM1 = getMatrixAt(newMatrix, i, j);
|
||||
int valueM2 = getMatrixAt(matrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
clearMatrix(&newMatrix);
|
||||
return result;
|
||||
// add und return
|
||||
|
||||
} else {
|
||||
Matrix newMatrix2 = broadCastCols(matrix2, cols1);
|
||||
// add
|
||||
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){0, 0, NULL};
|
||||
}
|
||||
for (int i = 0; i < rows1; i++) {
|
||||
for (int j = 0; j < cols1; j++) {
|
||||
int valueM1 = getMatrixAt(matrix1, i, j);
|
||||
int valueM2 = getMatrixAt(newMatrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
||||
// Programmabbruch
|
||||
Matrix error = {0, 0, NULL};
|
||||
return error;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else if (!rowsEqual && colsEqual) {
|
||||
|
||||
else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) {
|
||||
if (rows1 == 1) {
|
||||
|
||||
Matrix result = createMatrix(rows2, cols2);
|
||||
|
||||
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
|
||||
|
||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
||||
|
||||
result.buffer[i] =
|
||||
(copy1.buffer[i] +
|
||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
||||
// Startadresse + (i * sizeof(MatrixType))
|
||||
Matrix newMatrix = broadCastRows(matrix1, rows2);
|
||||
// add
|
||||
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){0, 0, NULL};
|
||||
}
|
||||
for (int i = 0; i < rows2; i++) {
|
||||
for (int j = 0; j < cols1; j++) {
|
||||
int valueM1 = getMatrixAt(newMatrix, i, j);
|
||||
int valueM2 = getMatrixAt(matrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
// add und return
|
||||
|
||||
} else if (rows2 == 1) {
|
||||
|
||||
Matrix result = createMatrix(rows1, cols1);
|
||||
|
||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
||||
// add und return
|
||||
|
||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
||||
|
||||
result.buffer[i] =
|
||||
(matrix1.buffer[i] +
|
||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
||||
// Startadresse + (i * sizeof(MatrixType))
|
||||
} else {
|
||||
Matrix newMatrix2 = broadCastRows(matrix2, rows1);
|
||||
// add
|
||||
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){0, 0, NULL};
|
||||
}
|
||||
for (int i = 0; i < rows1; i++) {
|
||||
for (int j = 0; j < cols1; j++) {
|
||||
int valueM1 = getMatrixAt(matrix1, i, j);
|
||||
int valueM2 = getMatrixAt(newMatrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
clearMatrix(&newMatrix2);
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
// kein add möglich
|
||||
Matrix errorMatrix = {0, 0, NULL};
|
||||
return errorMatrix;
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
||||
// Programmabbruch
|
||||
Matrix error = {0, 0, NULL};
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
printf(
|
||||
"Fehlermeldung"); // vielleicht Fehlermeldung ändern zu Programmabbruch
|
||||
Matrix error = {0, 0, NULL};
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
||||
// Spalten1 müssen gleich zeilen2 sein! dann multiplizieren
|
||||
if (matrix1.cols == matrix2.rows) {
|
||||
|
||||
@@ -13,17 +13,15 @@ typedef struct {
|
||||
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
Matrix createMatrix(const unsigned int rows, const unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
|
||||
unsigned int colIdx);
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
||||
unsigned int colIdx);
|
||||
void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||
const unsigned int rowIdx, const unsigned int colIdx);
|
||||
MatrixType getMatrixAt(const Matrix matrix, const unsigned int rowIdx,
|
||||
const unsigned int colIdx);
|
||||
|
||||
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
||||
const unsigned int cols);
|
||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
||||
const unsigned int cols);
|
||||
Matrix broadCastCols(const Matrix matrix, const unsigned int cols);
|
||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows);
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
|
||||
|
||||
+58
-32
@@ -5,69 +5,95 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*typedef struct
|
||||
{
|
||||
Matrix weights;
|
||||
Matrix biases;
|
||||
ActivationFunctionType activation;
|
||||
} Layer;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Layer *layers;
|
||||
unsigned int numberOfLayers;
|
||||
} NeuralNetwork;*/
|
||||
|
||||
/*Layer: Ebene im neuronalen Netzwerk, besteht aus mehreren Neuronen
|
||||
Input-Layer: Eingabedatei
|
||||
Hidden-Layer: verarbeiten die Daten
|
||||
Output-Layer: Ergebnis
|
||||
|
||||
|
||||
Gewichte: bestimmen, wie stark ein Eingangssignal auf ein Neuron wirkt
|
||||
|
||||
Dimension: Form der Matrizen für einen Layer*/
|
||||
|
||||
/* Gewichtsmatrix der Layer:
|
||||
*/
|
||||
|
||||
// speichert NeuralNetwork nn in binäre Datei->später kann es wieder geöffnet
|
||||
// werden
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (f == NULL)
|
||||
return;
|
||||
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
|
||||
if (fptr == NULL)
|
||||
return; // file konnte nicht geöffnet werden
|
||||
|
||||
/* 1) Header: exakt das String, ohne '\n' oder abschließendes '\0' */
|
||||
const char header[] = "__info2_neural_network_file_format__";
|
||||
fwrite(header, sizeof(char), strlen(header), f);
|
||||
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
|
||||
// Dateiformat
|
||||
const char header[] = "__info2_neural_network_file_format__"; // header string
|
||||
fwrite(header, sizeof(char), strlen(header),
|
||||
fptr); // der header wird am Anfang der Datei platziert
|
||||
|
||||
/* Wenn es keine Layer gibt, kein Dimensionspaar schreiben (loadModel
|
||||
wird beim Lesen dann 0 zurückgeben). Aber wir können auch frühzeitig
|
||||
mit einem 0-Int terminieren — beides ist in Ordnung. */
|
||||
// Wenn es keine Layer gibt, 0 eintragen, LoadModel erkennt, dass Datei leer
|
||||
// ist
|
||||
if (nn.numberOfLayers == 0) {
|
||||
/* optional: schreibe ein 0 als next outputDimension (nicht nötig) */
|
||||
int zero = 0;
|
||||
fwrite(&zero, sizeof(int), 1, f);
|
||||
fclose(f);
|
||||
fwrite(&zero, sizeof(int), 1, fptr);
|
||||
fclose(fptr);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 2) Für die erste Layer schreiben wir inputDimension und outputDimension */
|
||||
/* inputDimension == weights.cols, outputDimension == weights.rows */
|
||||
// Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl
|
||||
// Output-Neuronen wird in Datei eingefügt
|
||||
int inputDim = (int)nn.layers[0].weights.cols;
|
||||
int outputDim = (int)nn.layers[0].weights.rows;
|
||||
fwrite(&inputDim, sizeof(int), 1, f);
|
||||
fwrite(&outputDim, sizeof(int), 1, f);
|
||||
fwrite(&inputDim, sizeof(int), 1, fptr);
|
||||
fwrite(&outputDim, sizeof(int), 1, fptr);
|
||||
|
||||
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
|
||||
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
|
||||
geschrieben. */
|
||||
for (int i = 0; i < nn.numberOfLayers; i++) {
|
||||
Layer layer = nn.layers[i];
|
||||
Layer layer = nn.layers[i]; // kürzer, durch alle layer iterieren
|
||||
|
||||
int wrows = (int)layer.weights.rows;
|
||||
int wcols = (int)layer.weights.cols;
|
||||
int wcount = wrows * wcols;
|
||||
int wcount = wrows * wcols; // Anzahl Gewichtseinträge
|
||||
int bcount =
|
||||
layer.biases.rows * layer.biases.cols; /* normalerweise rows * 1 */
|
||||
layer.biases.rows * layer.biases.cols; // Anzahl der Bias-Einträge
|
||||
|
||||
/* Gewichte (MatrixType binär) */
|
||||
/* Gewichte */
|
||||
if (wcount > 0 && layer.weights.buffer != NULL) {
|
||||
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, f);
|
||||
}
|
||||
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, fptr);
|
||||
} // Gewichte werden als Matrix gespeichert
|
||||
|
||||
/* Biases (MatrixType binär) */
|
||||
/* Biases */
|
||||
if (bcount > 0 && layer.biases.buffer != NULL) {
|
||||
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, f);
|
||||
}
|
||||
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, fptr);
|
||||
} // Biases werden als Vektor gespeichert
|
||||
|
||||
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
|
||||
/* outputDimensionen der nächsten Layer */
|
||||
if (i + 1 < nn.numberOfLayers) {
|
||||
int nextOutput = (int)nn.layers[i + 1].weights.rows;
|
||||
fwrite(&nextOutput, sizeof(int), 1, f);
|
||||
fwrite(&nextOutput, sizeof(int), 1, fptr);
|
||||
} else {
|
||||
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
|
||||
schreiben. loadModel liest dann outputDimension = 0 und beendet die
|
||||
Schleife. */
|
||||
// loadModel erkennt 0 als Ende der Datei
|
||||
int zero = 0;
|
||||
fwrite(&zero, sizeof(int), 1, f);
|
||||
fwrite(&zero, sizeof(int), 1, fptr);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
fclose(fptr); // Datei schließen
|
||||
}
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void) {
|
||||
|
||||
Reference in New Issue
Block a user