forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
28
Commits
56d59b1b50
...
RMax
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb72278d55 | ||
|
|
f1af6c1e4a | ||
|
|
9606b5a03e | ||
|
|
3c4e4df496 | ||
|
|
fd1bc886a7 | ||
|
|
efa260ccbe | ||
|
|
4a1b6cbb40 | ||
|
|
801abc1b66 | ||
|
|
8e518a3bdd | ||
|
|
0baf646832 | ||
|
|
0f0f2f19c3 | ||
|
|
98dd789680 | ||
|
|
7aa57191da | ||
|
|
da4eaa718d | ||
|
|
21d9b5c01d | ||
|
|
e7930c7eb0 | ||
|
|
5075c34983 | ||
|
|
b187a13b17 | ||
|
|
4e2ee7078a | ||
|
|
35a598a276 | ||
|
|
e1ea9f33cd | ||
|
|
7f3c6d1d3f | ||
|
|
0886489d49 | ||
|
|
f9c46a6784 | ||
|
|
5fcc3cd042 | ||
|
|
3de79e2b83 | ||
|
|
ec54bdd951 | ||
|
|
0e3f03a03d |
+4
-1
@@ -1,4 +1,7 @@
|
|||||||
mnist
|
mnist
|
||||||
runTests
|
runTests
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
|
.vscode/c_cpp_properties.json
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/settings.json
|
||||||
|
|||||||
Vendored
+18
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "windows-gcc-x64",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"compilerPath": "C:/ProgramData/mingw64/mingw64/bin/gcc.exe",
|
||||||
|
"cStandard": "${default}",
|
||||||
|
"cppStandard": "${default}",
|
||||||
|
"intelliSenseMode": "windows-gcc-x64",
|
||||||
|
"compilerArgs": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
Vendored
+24
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "C/C++ Runner: Debug Session",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"externalConsole": true,
|
||||||
|
"cwd": "c:/Users/Max-R/I2Pr/repoKachelto/I2-Pr_neuronalesNetz/info2Praktikum-NeuronalesNetz",
|
||||||
|
"program": "c:/Users/Max-R/I2Pr/repoKachelto/I2-Pr_neuronalesNetz/info2Praktikum-NeuronalesNetz/build/Debug/outDebug",
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"miDebuggerPath": "gdb",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Vendored
+57
-1
@@ -1,3 +1,59 @@
|
|||||||
{
|
{
|
||||||
"makefile.configureOnOpen": false
|
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||||
|
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||||
|
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||||
|
"C_Cpp_Runner.cStandard": "",
|
||||||
|
"C_Cpp_Runner.cppStandard": "",
|
||||||
|
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
|
||||||
|
"C_Cpp_Runner.useMsvc": false,
|
||||||
|
"C_Cpp_Runner.warnings": [
|
||||||
|
"-Wall",
|
||||||
|
"-Wextra",
|
||||||
|
"-Wpedantic",
|
||||||
|
"-Wshadow",
|
||||||
|
"-Wformat=2",
|
||||||
|
"-Wcast-align",
|
||||||
|
"-Wconversion",
|
||||||
|
"-Wsign-conversion",
|
||||||
|
"-Wnull-dereference"
|
||||||
|
],
|
||||||
|
"C_Cpp_Runner.msvcWarnings": [
|
||||||
|
"/W4",
|
||||||
|
"/permissive-",
|
||||||
|
"/w14242",
|
||||||
|
"/w14287",
|
||||||
|
"/w14296",
|
||||||
|
"/w14311",
|
||||||
|
"/w14826",
|
||||||
|
"/w44062",
|
||||||
|
"/w44242",
|
||||||
|
"/w14905",
|
||||||
|
"/w14906",
|
||||||
|
"/w14263",
|
||||||
|
"/w44265",
|
||||||
|
"/w14928"
|
||||||
|
],
|
||||||
|
"C_Cpp_Runner.enableWarnings": true,
|
||||||
|
"C_Cpp_Runner.warningsAsError": false,
|
||||||
|
"C_Cpp_Runner.compilerArgs": [],
|
||||||
|
"C_Cpp_Runner.linkerArgs": [],
|
||||||
|
"C_Cpp_Runner.includePaths": [],
|
||||||
|
"C_Cpp_Runner.includeSearch": [
|
||||||
|
"*",
|
||||||
|
"**/*"
|
||||||
|
],
|
||||||
|
"C_Cpp_Runner.excludeSearch": [
|
||||||
|
"**/build",
|
||||||
|
"**/build/**",
|
||||||
|
"**/.*",
|
||||||
|
"**/.*/**",
|
||||||
|
"**/.vscode",
|
||||||
|
"**/.vscode/**"
|
||||||
|
],
|
||||||
|
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||||
|
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||||
|
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||||
|
"C_Cpp_Runner.showCompilationTime": false,
|
||||||
|
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||||
|
"C_Cpp_Runner.msvcSecureNoWarnings": false
|
||||||
}
|
}
|
||||||
+123
-18
@@ -1,29 +1,134 @@
|
|||||||
|
#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__"
|
||||||
|
|
||||||
// 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;
|
||||||
|
img->buffer = malloc(numPixels);
|
||||||
|
if (!img->buffer)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(img->buffer, 1, numPixels, file) != numPixels) {
|
||||||
|
free(img->buffer);
|
||||||
|
img->buffer = NULL;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|||||||
+177
-116
@@ -1,143 +1,204 @@
|
|||||||
|
|
||||||
#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)
|
void test_read_GrayScale_Pixel(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
FILE *file = fopen(path, "w");
|
|
||||||
if(file != NULL)
|
prepareImageFile(path, 8, 8, 1, 1);
|
||||||
{
|
series = readImages(path);
|
||||||
fprintf(file, "some_tag ");
|
|
||||||
fclose(file);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
}
|
TEST_ASSERT_EQUAL_UINT(1, series->count);
|
||||||
remove(path);
|
|
||||||
|
for (int i = 0; i < (8 * 8); i++) {
|
||||||
|
TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, series->images[0].buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
|
||||||
}
|
|
||||||
@@ -62,4 +62,5 @@ ifeq ($(OS),Windows_NT)
|
|||||||
del /f *.o *.exe
|
del /f *.o *.exe
|
||||||
else
|
else
|
||||||
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
||||||
endif
|
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 createMatrix(unsigned int rows, unsigned int cols) {
|
||||||
|
if (cols == 0 || rows == 0) {
|
||||||
Matrix errorMatrix = {0, 0, NULL};
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
if (rows == 0 || cols == 0) {
|
|
||||||
|
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
MatrixType *buffer =
|
MatrixType *buffer =
|
||||||
@@ -23,202 +21,222 @@ 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 getMatrixAt(const Matrix matrix,
|
||||||
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
unsigned int colIdx) {
|
unsigned int colIdx) {
|
||||||
if (rowIdx >= matrix.rows ||
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
|
||||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
|
||||||
return 0;
|
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 broadcastingCols(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 broadcastingRows(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
|
<<<<<<< HEAD
|
||||||
|
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++) {
|
||||||
return result; // zurückgeben
|
for (int j= 0; j< cols1; j++){
|
||||||
}
|
int valueM1= getMatrixAt(matrix1, i, j);
|
||||||
|
int valueM2= getMatrixAt(matrix2, i, j);
|
||||||
else if (rowsEqual && !colsEqual) {
|
=======
|
||||||
|
if (result.buffer == NULL) {
|
||||||
if (cols1 == 1) {
|
return (Matrix){0, 0, NULL};
|
||||||
|
}
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
for (int i = 0; i < rows1; i++) {
|
||||||
|
for (int j = 0; j < cols1; j++) {
|
||||||
Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
|
int valueM1 = getMatrixAt(matrix1, i, j);
|
||||||
|
int valueM2 = getMatrixAt(matrix2, i, j);
|
||||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
>>>>>>> main
|
||||||
|
int sum = valueM1 + valueM2;
|
||||||
result.buffer[i] =
|
setMatrixAt(sum, result, i, j);
|
||||||
(copy1.buffer[i] +
|
}
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
}
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
return result;
|
||||||
|
} else if (rowsEqual == 1 && (cols1 == 1 || cols2 == 1)) {
|
||||||
|
if (cols1 == 1) { // broadcasting von vektor 1 zu matrix 1, add
|
||||||
|
Matrix newMatrix = broadcastingCols(matrix1, cols2);
|
||||||
|
// add
|
||||||
|
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||||
|
<<<<<<< HEAD
|
||||||
|
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);
|
||||||
|
=======
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
>>>>>>> main
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
} else {
|
||||||
// add und return
|
Matrix newMatrix2 = broadcastingCols(matrix2, cols1);
|
||||||
|
// add
|
||||||
} else if (cols2 == 1) {
|
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||||
|
<<<<<<< HEAD
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
if (result.buffer == NULL){
|
||||||
|
return (Matrix){0,0,NULL};
|
||||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
}
|
||||||
|
for (int i = 0; i< rows1; i++) {
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
for (int j= 0; j< cols1; j++){
|
||||||
|
int valueM1= getMatrixAt(matrix1, i, j);
|
||||||
result.buffer[i] =
|
int valueM2= getMatrixAt(newMatrix2, i, j);
|
||||||
(matrix1.buffer[i] +
|
int sum = valueM1 + valueM2;
|
||||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
setMatrixAt(sum, result, i, j);
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
=======
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
>>>>>>> main
|
||||||
|
}
|
||||||
|
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;
|
return result;
|
||||||
// add und return
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
|
||||||
// Programmabbruch
|
|
||||||
Matrix error = {0, 0, NULL};
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!rowsEqual && colsEqual) {
|
else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) {
|
||||||
|
|
||||||
if (rows1 == 1) {
|
if (rows1 == 1) {
|
||||||
|
Matrix newMatrix = broadcastingRows(matrix1, rows2);
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
// add
|
||||||
|
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||||
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
|
<<<<<<< HEAD
|
||||||
|
if (result.buffer == NULL){
|
||||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
return (Matrix){0,0,NULL};
|
||||||
|
}
|
||||||
result.buffer[i] =
|
for (int i = 0; i< rows2; i++) {
|
||||||
(copy1.buffer[i] +
|
for (int j= 0; j< cols1; j++){
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
int valueM1= getMatrixAt(newMatrix, i, j);
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
int valueM2= getMatrixAt(matrix2, i, j);
|
||||||
|
int sum = valueM1 + valueM2;
|
||||||
|
setMatrixAt(sum, result, i, j);
|
||||||
|
=======
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
>>>>>>> main
|
||||||
|
}
|
||||||
|
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;
|
return result;
|
||||||
|
} else {
|
||||||
// add und return
|
Matrix newMatrix2 = broadcastingRows(matrix2, rows1);
|
||||||
|
// add
|
||||||
} else if (rows2 == 1) {
|
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||||
|
<<<<<<< HEAD
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
if (result.buffer == NULL){
|
||||||
|
return (Matrix){0,0,NULL};
|
||||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
}
|
||||||
// add und return
|
for (int i = 0; i< rows1; i++) {
|
||||||
|
for (int j= 0; j< cols1; j++){
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
int valueM1= getMatrixAt(matrix1, i, j);
|
||||||
|
int valueM2= getMatrixAt(newMatrix2, i, j);
|
||||||
result.buffer[i] =
|
int sum = valueM1 + valueM2;
|
||||||
(matrix1.buffer[i] +
|
setMatrixAt(sum, result, i, j);
|
||||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
=======
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
>>>>>>> main
|
||||||
|
}
|
||||||
|
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;
|
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 +263,4 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
Matrix errorMatrix = {0, 0, NULL};
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-21
@@ -5,32 +5,55 @@
|
|||||||
#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*/
|
||||||
|
|
||||||
|
// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
|
||||||
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 vor jedem Layer
|
||||||
|
fwrite(header, sizeof(char), strlen(header), fptr);
|
||||||
|
|
||||||
/* Wenn es keine Layer gibt, kein Dimensionspaar schreiben (loadModel
|
// Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
|
||||||
wird beim Lesen dann 0 zurückgeben). Aber wir können auch frühzeitig
|
|
||||||
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
|
||||||
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)
|
||||||
@@ -46,28 +69,28 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
|||||||
|
|
||||||
/* Gewichte (MatrixType binär) */
|
/* Gewichte (MatrixType binär) */
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Biases (MatrixType binär) */
|
/* Biases (MatrixType binär) */
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
|
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
|
||||||
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
|
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
|
||||||
schreiben. loadModel liest dann outputDimension = 0 und beendet die
|
schreiben. loadModel liest dann outputDimension = 0 und beendet die
|
||||||
Schleife. */
|
Schleife. */
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
fwrite(&zero, sizeof(int), 1, f);
|
fwrite(&zero, sizeof(int), 1, fptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(fptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void) {
|
void test_loadModelReturnsCorrectNumberOfLayers(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user