Compare commits
10
Commits
150cce7f8c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5f493bd43 | ||
|
|
53a7828cf0 | ||
|
|
28a968e2c9 | ||
|
|
86d225f2d8 | ||
|
|
c212109a27 | ||
|
|
5f068f1337 | ||
|
|
05fbd80b8c | ||
|
|
6d162b313c | ||
|
|
c8b14cefae | ||
|
|
47a54fb567 |
@@ -6,7 +6,6 @@
|
|||||||
#define BUFFER_SIZE 100
|
#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
|
|
||||||
// Lädt eine .info2-Datei und prüft, ob das Dateiformat korrekt ist
|
// Lädt eine .info2-Datei und prüft, ob das Dateiformat korrekt ist
|
||||||
static FILE* openImageFile(const char* path) {
|
static FILE* openImageFile(const char* path) {
|
||||||
FILE* imageFile = NULL;
|
FILE* imageFile = NULL;
|
||||||
@@ -95,11 +94,9 @@ static char getLabelOfImage(FILE* imageFile, GrayScaleImage* image) {
|
|||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
// Datei laden und Dateiformat prüfen
|
// Datei laden und Dateiformat prüfen
|
||||||
// setzt außerdem den Pointer an der Stelle nach dem Prestring
|
|
||||||
FILE* imageFile = openImageFile(path);
|
FILE* imageFile = openImageFile(path);
|
||||||
|
|
||||||
if (imageFile == NULL) {
|
if (imageFile == NULL) {
|
||||||
@@ -180,20 +177,36 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// für jedes Bild gemäß GreyScaleImageSeries.count:
|
|
||||||
// GreyScaleImage.width = width
|
|
||||||
// GreyScaleImage.height = height
|
|
||||||
// GreyScaleImageSeries.buffer = Pixelwerte
|
|
||||||
// labels = Label des Bilds
|
|
||||||
// series.labels[i] = (unsigned char)(i % 256);
|
|
||||||
// Springe in der Datei an die Stelle nach dem i. Bild
|
|
||||||
|
|
||||||
fclose(imageFile);
|
fclose(imageFile);
|
||||||
|
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
|
//prüfen, ob series überhaupt bereinigt werden muss
|
||||||
|
if(series == NULL){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//images freigeben und NULL setzen -> jeden Index der Images durchgehen und buffer freigeben
|
||||||
|
if(series->images != NULL){
|
||||||
|
for(unsigned int i = 0; i < series->count; i++){
|
||||||
|
if (series->images[i].buffer != NULL){
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
series->images[i].buffer = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(series->images); // wenn alle images bereinigt sind, den Zeiger Images selbst bereinigen
|
||||||
|
series->images = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//labels freigeben und NULL setzen
|
||||||
|
if(series->labels != NULL){
|
||||||
|
free(series->labels);
|
||||||
|
series->labels = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//series freigeben
|
||||||
|
free(series);
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
|
||||||
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
||||||
@@ -64,6 +65,19 @@ static void prepareImageFileIncorrectTag(const char *path, unsigned short int wi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_readImagesFailsOnIncompleteHeader(void) {
|
||||||
|
const char *path = "testIncompleteHeader.info2";
|
||||||
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (file) {
|
||||||
|
fwrite(FILE_HEADER_STRING, 1, strlen(FILE_HEADER_STRING), file);
|
||||||
|
|
||||||
|
unsigned char incompleteHeader[2] = {0x01, 0x00};
|
||||||
|
fwrite(incompleteHeader, 1, 2, file);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
remove(path);
|
||||||
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectNumberOfImages(void)
|
void test_readImagesReturnsCorrectNumberOfImages(void)
|
||||||
{
|
{
|
||||||
@@ -159,6 +173,37 @@ void test_openImageFileIncorrectTag(void)
|
|||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_clearSeriesFreesMemory(void)
|
||||||
|
{
|
||||||
|
// Dummy-Series erstellen
|
||||||
|
GrayScaleImageSeries *series = (GrayScaleImageSeries*)malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
series->count = 2;
|
||||||
|
|
||||||
|
// Labels allokieren
|
||||||
|
series->labels = (unsigned char*)malloc(series->count * sizeof(unsigned char));
|
||||||
|
for (unsigned int i = 0; i < series->count; i++) {
|
||||||
|
series->labels[i] = (unsigned char)(i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Images allokieren
|
||||||
|
series->images = (GrayScaleImage*)malloc(series->count * sizeof(GrayScaleImage));
|
||||||
|
for (unsigned int i = 0; i < series->count; i++) {
|
||||||
|
series->images[i].width = 4;
|
||||||
|
series->images[i].height = 4;
|
||||||
|
series->images[i].buffer = (GrayScalePixelType*)malloc(series->images[i].width * series->images[i].height);
|
||||||
|
|
||||||
|
for (unsigned int j = 0; j < series->images[i].width * series->images[i].height; j++){
|
||||||
|
series->images[i].buffer[j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clearSeries aufrufen
|
||||||
|
clearSeries(series);
|
||||||
|
|
||||||
|
// Test
|
||||||
|
TEST_PASS();
|
||||||
|
}
|
||||||
|
|
||||||
void setUp(void) {
|
void setUp(void) {
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
}
|
}
|
||||||
@@ -179,6 +224,8 @@ int main()
|
|||||||
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||||
RUN_TEST(test_openImageFileIncorrectTag);
|
RUN_TEST(test_openImageFileIncorrectTag);
|
||||||
|
RUN_TEST(test_readImagesFailsOnIncompleteHeader);
|
||||||
|
RUN_TEST(test_clearSeriesFreesMemory);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
@@ -10,12 +10,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
Matrix matrix;
|
Matrix matrix;
|
||||||
matrix.rows = rows;
|
matrix.rows = rows;
|
||||||
matrix.cols = cols;
|
matrix.cols = cols;
|
||||||
if(matrix.rows == 0 || matrix.cols == 0){
|
if(matrix.rows == 0 || matrix.cols == 0){ // prüfen, ob ungültige Dimensionen übergeben wurden, wenn ja, "error matrix" erstellen
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
matrix.buffer = NULL;
|
matrix.buffer = NULL;
|
||||||
}
|
}
|
||||||
else {
|
else { // für gültige Dimensionen: Speicher allokieren
|
||||||
matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
|
matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,50 +36,60 @@ void clearMatrix(Matrix *matrix)
|
|||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
|
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // value an vorgebenener Stelle setzen
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
|
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
MatrixType value;
|
MatrixType value;
|
||||||
value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // value an vorgegebener Stelle auslesen
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben
|
size_t rows = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows;
|
||||||
Matrix errorMatrix = createMatrix(0, 0);
|
size_t cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
|
||||||
errorMatrix.buffer = NULL;
|
|
||||||
return errorMatrix;
|
// Prüfen, ob Broadcasting möglich ist
|
||||||
|
if (!((matrix1.rows == rows || matrix1.rows == 1) &&
|
||||||
|
(matrix2.rows == rows || matrix2.rows == 1) &&
|
||||||
|
(matrix1.cols == cols || matrix1.cols == 1) &&
|
||||||
|
(matrix2.cols == cols || matrix2.cols == 1))) {
|
||||||
|
Matrix errorMatrix = createMatrix(0, 0);
|
||||||
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
Matrix result = createMatrix(rows, cols);
|
||||||
for (size_t i = 0; i < matrix1.rows; i++) {
|
|
||||||
for (size_t j = 0; j < matrix1.cols; j++) {
|
for (size_t i = 0; i < rows; i++) {
|
||||||
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
for (size_t j = 0; j < cols; j++) {
|
||||||
setMatrixAt(sum, result, i, j);
|
MatrixType val1 = matrix1.buffer[(i % matrix1.rows) * matrix1.cols + (j % matrix1.cols)];
|
||||||
}
|
MatrixType val2 = matrix2.buffer[(i % matrix2.rows) * matrix2.cols + (j % matrix2.cols)];
|
||||||
|
result.buffer[i * cols + j] = val1 + val2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.cols != matrix2.rows){
|
if (matrix1.cols != matrix2.rows){ // prüfen, ob Matrizen multipliziert werden dürfen
|
||||||
Matrix errorMatrix = createMatrix(0, 0);
|
Matrix errorMatrix = createMatrix(0, 0);
|
||||||
errorMatrix.buffer = NULL;
|
errorMatrix.buffer = NULL;
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols);
|
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); // Ergebnis-Matrix erstellen
|
||||||
|
|
||||||
|
// Algorithmus für Multiplikation
|
||||||
for( size_t i = 0; i < matrix1.rows; i++){
|
for( size_t i = 0; i < matrix1.rows; i++){
|
||||||
for(size_t j = 0; j < matrix2.cols; j++){
|
for(size_t j = 0; j < matrix2.cols; j++){
|
||||||
MatrixType sum = 0;
|
MatrixType sum = 0;
|
||||||
|
|||||||
@@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void)
|
|||||||
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_addSupportsBroadcasting(void)
|
||||||
|
{
|
||||||
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
|
MatrixType buffer2[] = {7, 8};
|
||||||
|
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
||||||
|
Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
|
||||||
|
|
||||||
|
Matrix result1 = add(matrix1, matrix2);
|
||||||
|
Matrix result2 = add(matrix2, matrix1);
|
||||||
|
|
||||||
|
float expectedResults[] = {8, 9, 10, 12, 13, 14};
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows);
|
||||||
|
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows);
|
||||||
|
|
||||||
|
free(result1.buffer);
|
||||||
|
free(result2.buffer);
|
||||||
|
}
|
||||||
|
|
||||||
void test_multiplyReturnsCorrectResults(void)
|
void test_multiplyReturnsCorrectResults(void)
|
||||||
{
|
{
|
||||||
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
@@ -159,6 +185,7 @@ int main()
|
|||||||
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
||||||
RUN_TEST(test_addReturnsCorrectResult);
|
RUN_TEST(test_addReturnsCorrectResult);
|
||||||
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
||||||
|
RUN_TEST(test_addSupportsBroadcasting);
|
||||||
RUN_TEST(test_multiplyReturnsCorrectResults);
|
RUN_TEST(test_multiplyReturnsCorrectResults);
|
||||||
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
||||||
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
||||||
|
|||||||
@@ -118,6 +118,10 @@ static void assignActivations(NeuralNetwork model)
|
|||||||
if(model.numberOfLayers > 0)
|
if(model.numberOfLayers > 0)
|
||||||
model.layers[model.numberOfLayers-1].activation = softmax;
|
model.layers[model.numberOfLayers-1].activation = softmax;
|
||||||
}
|
}
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
NeuralNetwork loadModel(const char *path)
|
NeuralNetwork loadModel(const char *path)
|
||||||
{
|
{
|
||||||
@@ -168,6 +172,7 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||||
{
|
{
|
||||||
Matrix matrix = {NULL, 0, 0};
|
Matrix matrix = {NULL, 0, 0};
|
||||||
@@ -254,6 +259,7 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearModel(NeuralNetwork *model)
|
void clearModel(NeuralNetwork *model)
|
||||||
{
|
{
|
||||||
if(model != NULL)
|
if(model != NULL)
|
||||||
|
|||||||
@@ -8,7 +8,48 @@
|
|||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (!file) {
|
||||||
|
perror("Fehler beim Öffnen der Datei");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header schreiben
|
||||||
|
const char *header = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(header, sizeof(char), strlen(header), file);
|
||||||
|
|
||||||
|
// Alle Layer schreiben
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
Layer layer = nn.layers[i];
|
||||||
|
|
||||||
|
|
||||||
|
if(i == 0){
|
||||||
|
// beim ersten Layer inputDim und outputDim schreiben
|
||||||
|
unsigned int inputDim = layer.weights.cols; // Anzahl Eingänge
|
||||||
|
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
||||||
|
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
|
||||||
|
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// bei allen anderen Layern nur outputDim schreiben
|
||||||
|
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
|
||||||
|
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gewichte schreiben
|
||||||
|
size_t weightCount = layer.weights.rows * layer.weights.cols;
|
||||||
|
fwrite(layer.weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
|
|
||||||
|
// Biases schreiben
|
||||||
|
size_t biasCount = layer.biases.rows * layer.biases.cols;
|
||||||
|
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// End-Marker (outputDim = 0)
|
||||||
|
unsigned int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Reference in New Issue
Block a user