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 |
+7
-1
@@ -1,4 +1,10 @@
|
|||||||
mnist
|
mnist
|
||||||
runTests
|
runTests
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
|
.vscode/settings.json
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/settings.json
|
||||||
|
.vscode/settings.json
|
||||||
|
runImageInputTests
|
||||||
|
testFile.info2
|
||||||
+125
-18
@@ -1,29 +1,136 @@
|
|||||||
|
#include "imageInput.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "imageInput.h"
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 100
|
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#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)
|
2. Meta-Daten lesen (unsigned short)
|
||||||
{
|
---------------------------------------------------------- */
|
||||||
GrayScaleImageSeries *series = NULL;
|
static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
|
||||||
FILE *file = fopen("mnist_test.info2","rb");
|
unsigned short *height) {
|
||||||
char headOfFile;
|
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||||
series = malloc();
|
return 0;
|
||||||
return series;
|
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
/* ----------------------------------------------------------
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|||||||
+183
-116
@@ -1,143 +1,210 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "unity.h"
|
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
#include "unity.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
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;
|
||||||
|
|
||||||
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
// Header
|
||||||
{
|
const char *fileTag = "__info2_image_file_format__";
|
||||||
FILE *file = fopen(path, "wb");
|
fwrite(fileTag, 1, strlen(fileTag), file);
|
||||||
|
|
||||||
if(file != NULL)
|
// Meta-Daten als unsigned short
|
||||||
{
|
unsigned short n = (unsigned short)numberOfImages;
|
||||||
const char *fileTag = "__info2_image_file_format__";
|
unsigned short w = (unsigned short)width;
|
||||||
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
|
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);
|
||||||
|
|
||||||
if(zeroBuffer != NULL)
|
// Pixelbuffer
|
||||||
{
|
GrayScalePixelType *buffer =
|
||||||
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
calloc(width * height, sizeof(GrayScalePixelType));
|
||||||
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
if (!buffer) {
|
||||||
fwrite(&width, sizeof(width), 1, file);
|
fclose(file);
|
||||||
fwrite(&height, sizeof(height), 1, file);
|
return;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < width * height; i++)
|
||||||
|
buffer[i] = (GrayScalePixelType)i;
|
||||||
|
|
||||||
for(int i = 0; i < numberOfImages; i++)
|
// Jedes Bild schreiben: Pixel + Label
|
||||||
{
|
for (unsigned int img = 0; img < numberOfImages; img++) {
|
||||||
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
|
fwrite(buffer, sizeof(GrayScalePixelType), width * height, file);
|
||||||
fwrite(&label, sizeof(unsigned char), 1, file);
|
fwrite(&label, sizeof(unsigned char), 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(zeroBuffer);
|
free(buffer);
|
||||||
}
|
fclose(file);
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
Unit Tests
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectNumberOfImages(void)
|
void test_readImagesReturnsCorrectNumberOfImages(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
GrayScaleImageSeries *series = NULL;
|
const unsigned int expectedNumberOfImages = 2;
|
||||||
const unsigned short expectedNumberOfImages = 2;
|
const char *path = "testFile.info2";
|
||||||
const char *path = "testFile.info2";
|
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
||||||
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
series = readImages(path);
|
||||||
series = readImages(path);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_EQUAL_UINT(expectedNumberOfImages, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
clearSeries(series);
|
||||||
clearSeries(series);
|
remove(path);
|
||||||
remove(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectImageWidth(void)
|
void test_readImagesReturnsCorrectImageWidth(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
GrayScaleImageSeries *series = NULL;
|
const unsigned int expectedWidth = 10;
|
||||||
const unsigned short expectedWidth = 10;
|
const char *path = "testFile.info2";
|
||||||
const char *path = "testFile.info2";
|
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
||||||
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
series = readImages(path);
|
||||||
series = readImages(path);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[0].width);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width);
|
TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[1].width);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width);
|
clearSeries(series);
|
||||||
clearSeries(series);
|
remove(path);
|
||||||
remove(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectImageHeight(void)
|
void test_readImagesReturnsCorrectImageHeight(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
GrayScaleImageSeries *series = NULL;
|
const unsigned int expectedHeight = 10;
|
||||||
const unsigned short expectedHeight = 10;
|
const char *path = "testFile.info2";
|
||||||
const char *path = "testFile.info2";
|
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
||||||
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
series = readImages(path);
|
||||||
series = readImages(path);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[0].height);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height);
|
TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[1].height);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height);
|
clearSeries(series);
|
||||||
clearSeries(series);
|
remove(path);
|
||||||
remove(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectLabels(void)
|
void test_readImagesReturnsCorrectLabels(void) {
|
||||||
{
|
const unsigned char expectedLabel = 15;
|
||||||
const unsigned char expectedLabel = 15;
|
|
||||||
|
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, 8, 2, expectedLabel);
|
prepareImageFile(path, 8, 8, 2, expectedLabel);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->labels);
|
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++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
|
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
|
||||||
}
|
}
|
||||||
clearSeries(series);
|
clearSeries(series);
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsNullOnNotExistingPath(void)
|
void test_readImagesReturnsNullOnNotExistingPath(void) {
|
||||||
{
|
const char *path = "testFile.txt";
|
||||||
const char *path = "testFile.txt";
|
remove(path);
|
||||||
remove(path);
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_readImagesFailsOnWrongFileTag(void) {
|
||||||
|
const char *path = "testFile.info2";
|
||||||
|
FILE *file = fopen(path, "w");
|
||||||
|
if (file != NULL) {
|
||||||
|
fprintf(file, "some_tag ");
|
||||||
|
fclose(file);
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
}
|
||||||
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesFailsOnWrongFileTag(void)
|
// Test
|
||||||
{
|
|
||||||
const char *path = "testFile.info2";
|
void test_read_GrayScale_Pixel(
|
||||||
FILE *file = fopen(path, "w");
|
void) { // testet das einlesen eines graustufenbildes von readImages()
|
||||||
if(file != NULL)
|
GrayScaleImageSeries *series = NULL; // enthält später das Bild
|
||||||
{
|
const char *path = "testFile.info2";
|
||||||
fprintf(file, "some_tag ");
|
|
||||||
fclose(file);
|
prepareImageFile(path, 8, 8, 1,
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
1); // Höhe x Breite in Pixel, Anzahl Bilder und Kategorie
|
||||||
}
|
series = readImages(path);
|
||||||
remove(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
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSeries(series);
|
||||||
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUp(void) {
|
/* ---------------------------------------------------------
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tearDown(void) {
|
/* ---------------------------------------------------------
|
||||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
Setup / Teardown
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
main()
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
@@ -59,7 +59,8 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
del /f *.o *.exe
|
|
||||||
else
|
|
||||||
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
||||||
endif
|
else
|
||||||
|
del /f *.o *.exe
|
||||||
|
endif
|
||||||
|
|
||||||
@@ -3,17 +3,15 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
/*typedef struct {
|
/*typedef struct {
|
||||||
unsigned int rows; //Zeilen
|
unsigned int rows; //Zeilen
|
||||||
unsigned int cols; //Spalten
|
unsigned int cols; //Spalten
|
||||||
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
||||||
} Matrix;*/
|
} Matrix;*/
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
|
||||||
|
|
||||||
Matrix errorMatrix = {0, 0, NULL};
|
|
||||||
if (rows == 0 || cols == 0) {
|
|
||||||
|
|
||||||
|
Matrix createMatrix(const unsigned int rows, const unsigned int cols) {
|
||||||
|
if (cols == 0 || rows == 0) {
|
||||||
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
MatrixType *buffer =
|
MatrixType *buffer =
|
||||||
@@ -23,202 +21,168 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
|||||||
return newMatrix;
|
return newMatrix;
|
||||||
}
|
}
|
||||||
void clearMatrix(Matrix *matrix) {
|
void clearMatrix(Matrix *matrix) {
|
||||||
matrix->buffer = UNDEFINED_MATRIX_VALUE;
|
|
||||||
matrix->rows = UNDEFINED_MATRIX_VALUE;
|
if (matrix->buffer != NULL) {
|
||||||
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
free((*matrix).buffer);
|
||||||
free((*matrix).buffer); // Speicher freigeben
|
matrix->buffer = NULL;
|
||||||
|
}
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(const MatrixType value, Matrix matrix,
|
void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||||
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
const unsigned int colIdx) {
|
const unsigned int colIdx) {
|
||||||
|
|
||||||
if (rowIdx >= matrix.rows ||
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
|
||||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
// Speichergröße nicht überschreiten
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
matrix.buffer[rowIdx * matrix.cols + colIdx] =
|
// rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
||||||
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
// innerhalb der Zeile
|
||||||
// innerhalb der Zeile
|
|
||||||
}
|
}
|
||||||
MatrixType getMatrixAt(const Matrix matrix,
|
MatrixType
|
||||||
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
getMatrixAt(const Matrix matrix,
|
||||||
unsigned int colIdx) {
|
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
if (rowIdx >= matrix.rows ||
|
const unsigned int colIdx) {
|
||||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
|
||||||
return 0;
|
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
Matrix broadCastCols(const Matrix matrix, const unsigned int cols) {
|
||||||
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
Matrix copy1 = createMatrix(matrix.rows, cols);
|
||||||
const unsigned int cols) {
|
for (int r = 0; r < matrix.rows; r++) {
|
||||||
|
MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0);
|
||||||
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);
|
|
||||||
for (int c = 0; c < cols; c++) {
|
for (int c = 0; c < cols; c++) {
|
||||||
|
setMatrixAt(valueMatrix1, copy1, r, c);
|
||||||
setMatrixAt(value, copy, r, c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return copy1;
|
||||||
return copy;
|
|
||||||
}
|
}
|
||||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
Matrix broadCastRows(const Matrix matrix, const unsigned int rows) {
|
||||||
const unsigned int cols) {
|
Matrix copy1 = createMatrix(rows, matrix.cols);
|
||||||
|
for (int c = 0; c < matrix.cols; c++) {
|
||||||
Matrix copy = createMatrix(rows, cols);
|
MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c);
|
||||||
|
|
||||||
for (int c = 0; c < cols; c++) {
|
|
||||||
MatrixType value = getMatrixAt(matrix, 0, c);
|
|
||||||
|
|
||||||
for (int r = 0; r < rows; r++) {
|
for (int r = 0; r < rows; r++) {
|
||||||
|
setMatrixAt(valueMatrix1, copy1, r, c);
|
||||||
setMatrixAt(value, copy, r, c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return copy1;
|
||||||
return copy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||||
|
|
||||||
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassenden
|
// Ergebnismatrix
|
||||||
// Matrizen
|
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 int rowsEqual = (matrix1.rows == matrix2.rows) ? 1 : 0;
|
||||||
const unsigned int rows2 = matrix2.rows;
|
const int colsEqual = (matrix1.cols == matrix2.cols) ? 1 : 0;
|
||||||
const unsigned int cols1 = matrix1.cols;
|
|
||||||
const unsigned int cols2 = matrix2.cols;
|
|
||||||
|
|
||||||
const int rowsEqual = ((rows1 == rows2) ? 1 : 0);
|
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender
|
||||||
|
// Matrix
|
||||||
const int colsEqual = ((cols1 == cols2) ? 1 : 0);
|
if (rowsEqual == 1 && colsEqual == 1) {
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
if (rowsEqual && colsEqual) // addieren
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
{
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} 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;
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result; // zurückgeben
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (rowsEqual && !colsEqual) {
|
else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) {
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
// add und return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
|
||||||
// Programmabbruch
|
|
||||||
Matrix error = {0, 0, NULL};
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (!rowsEqual && colsEqual) {
|
|
||||||
|
|
||||||
if (rows1 == 1) {
|
if (rows1 == 1) {
|
||||||
|
Matrix newMatrix = broadCastRows(matrix1, rows2);
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
// add
|
||||||
|
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||||
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
}
|
||||||
|
for (int i = 0; i < rows2; i++) {
|
||||||
result.buffer[i] =
|
for (int j = 0; j < cols1; j++) {
|
||||||
(copy1.buffer[i] +
|
int valueM1 = getMatrixAt(newMatrix, i, j);
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
int valueM2 = getMatrixAt(matrix2, i, j);
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
int sum = valueM1 + valueM2;
|
||||||
|
setMatrixAt(sum, result, i, j);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
} else {
|
||||||
// add und return
|
Matrix newMatrix2 = broadCastRows(matrix2, rows1);
|
||||||
|
// add
|
||||||
} else if (rows2 == 1) {
|
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
return (Matrix){0, 0, NULL};
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
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;
|
return result;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
|
||||||
// Programmabbruch
|
|
||||||
Matrix error = {0, 0, NULL};
|
|
||||||
return error;
|
|
||||||
}
|
}
|
||||||
|
} 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
||||||
// Spalten1 müssen gleich zeilen2 sein! dann multiplizieren
|
// Spalten1 müssen gleich zeilen2 sein! dann multiplizieren
|
||||||
if (matrix1.cols == matrix2.rows) {
|
if (matrix1.cols == matrix2.rows) {
|
||||||
@@ -245,4 +209,4 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
Matrix errorMatrix = {0, 0, NULL};
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,17 +13,15 @@ typedef struct {
|
|||||||
|
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(const unsigned int rows, const unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
|
void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||||
unsigned int colIdx);
|
const unsigned int rowIdx, const unsigned int colIdx);
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
MatrixType getMatrixAt(const Matrix matrix, const unsigned int rowIdx,
|
||||||
unsigned int colIdx);
|
const unsigned int colIdx);
|
||||||
|
|
||||||
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
Matrix broadCastCols(const Matrix matrix, const unsigned int cols);
|
||||||
const unsigned int cols);
|
Matrix broadCastRows(const Matrix matrix, const unsigned int rows);
|
||||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
|
||||||
const unsigned int cols);
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||||
|
|
||||||
|
|||||||
+58
-32
@@ -5,69 +5,95 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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) {
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
||||||
FILE *f = fopen(path, "wb");
|
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
|
||||||
if (f == NULL)
|
if (fptr == NULL)
|
||||||
return;
|
return; // file konnte nicht geöffnet werden
|
||||||
|
|
||||||
/* 1) Header: exakt das String, ohne '\n' oder abschließendes '\0' */
|
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
|
||||||
const char header[] = "__info2_neural_network_file_format__";
|
// Dateiformat
|
||||||
fwrite(header, sizeof(char), strlen(header), f);
|
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
|
// Wenn es keine Layer gibt, 0 eintragen, LoadModel erkennt, dass Datei leer
|
||||||
wird beim Lesen dann 0 zurückgeben). Aber wir können auch frühzeitig
|
// ist
|
||||||
mit einem 0-Int terminieren — beides ist in Ordnung. */
|
|
||||||
if (nn.numberOfLayers == 0) {
|
if (nn.numberOfLayers == 0) {
|
||||||
/* optional: schreibe ein 0 als next outputDimension (nicht nötig) */
|
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
fwrite(&zero, sizeof(int), 1, f);
|
fwrite(&zero, sizeof(int), 1, fptr);
|
||||||
fclose(f);
|
fclose(fptr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2) Für die erste Layer schreiben wir inputDimension und outputDimension */
|
// Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl
|
||||||
/* inputDimension == weights.cols, outputDimension == weights.rows */
|
// Output-Neuronen wird in Datei eingefügt
|
||||||
int inputDim = (int)nn.layers[0].weights.cols;
|
int inputDim = (int)nn.layers[0].weights.cols;
|
||||||
int outputDim = (int)nn.layers[0].weights.rows;
|
int outputDim = (int)nn.layers[0].weights.rows;
|
||||||
fwrite(&inputDim, sizeof(int), 1, f);
|
fwrite(&inputDim, sizeof(int), 1, fptr);
|
||||||
fwrite(&outputDim, sizeof(int), 1, f);
|
fwrite(&outputDim, sizeof(int), 1, fptr);
|
||||||
|
|
||||||
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
|
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
|
||||||
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
|
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
|
||||||
geschrieben. */
|
geschrieben. */
|
||||||
for (int i = 0; i < nn.numberOfLayers; i++) {
|
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 wrows = (int)layer.weights.rows;
|
||||||
int wcols = (int)layer.weights.cols;
|
int wcols = (int)layer.weights.cols;
|
||||||
int wcount = wrows * wcols;
|
int wcount = wrows * wcols; // Anzahl Gewichtseinträge
|
||||||
int bcount =
|
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) {
|
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) {
|
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) {
|
if (i + 1 < nn.numberOfLayers) {
|
||||||
int nextOutput = (int)nn.layers[i + 1].weights.rows;
|
int nextOutput = (int)nn.layers[i + 1].weights.rows;
|
||||||
fwrite(&nextOutput, sizeof(int), 1, f);
|
fwrite(&nextOutput, sizeof(int), 1, fptr);
|
||||||
} else {
|
} else {
|
||||||
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
|
// loadModel erkennt 0 als Ende der Datei
|
||||||
schreiben. loadModel liest dann outputDimension = 0 und beendet die
|
|
||||||
Schleife. */
|
|
||||||
int zero = 0;
|
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) {
|
void test_loadModelReturnsCorrectNumberOfLayers(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user