forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
732ec57e7f | ||
|
|
47690eaa20 | ||
|
|
9468b6aea0 | ||
|
|
33d9ac7833 | ||
|
|
ff3ba69099 | ||
|
|
a4cf2c27e5 | ||
|
|
4e2bec6081 | ||
|
|
003d1820d9 | ||
|
|
d999786d02 | ||
|
|
9b1e4aab11 | ||
|
|
060b22f50e | ||
|
|
ebe3304c54 | ||
|
|
c0f73f9362 | ||
|
|
205513f901 | ||
|
|
d4e329e098 | ||
|
|
85369b3bad | ||
|
|
ddea2d5d72 | ||
|
|
37341ec9c3 |
No files matched your search
@@ -53,7 +53,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
GrayScaleImageSeries *pictureSeriesmemory(unsigned short numImagesShort); //static kann nur innerhalb imageInput.c aufgerufen werden
|
series = pictureSeriesmemory(numImagesShort); //static kann nur innerhalb imageInput.c aufgerufen werden
|
||||||
if (series == NULL) {
|
if (series == NULL) {
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -61,10 +61,11 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
|
|
||||||
if (!PicturesLabelsimport(series, file, widthShort, heightShort)) { //! -> inverter
|
if (!PicturesLabelsimport(series, file, widthShort, heightShort)) { //! -> inverter
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
clearSeries(series);//Speicher bei Fehler direkt freigeben
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return series;
|
return series; //gibt fertige bildserie zurück
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +78,7 @@ GrayScaleImageSeries *pictureSeriesmemory(unsigned short numImagesShort) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
series->count *= (unsigned int)numImagesShort;
|
series->count = (unsigned int)numImagesShort;
|
||||||
series->images = (GrayScaleImage*)malloc(series->count * sizeof(GrayScaleImage));
|
series->images = (GrayScaleImage*)malloc(series->count * sizeof(GrayScaleImage));
|
||||||
series->labels = (unsigned char*)malloc(series->count * sizeof(unsigned char));
|
series->labels = (unsigned char*)malloc(series->count * sizeof(unsigned char));
|
||||||
|
|
||||||
@@ -96,7 +97,7 @@ GrayScaleImageSeries *pictureSeriesmemory(unsigned short numImagesShort) {
|
|||||||
int PicturesLabelsimport(GrayScaleImageSeries *series, FILE *file, unsigned short widthShort, unsigned short heightShort){
|
int PicturesLabelsimport(GrayScaleImageSeries *series, FILE *file, unsigned short widthShort, unsigned short heightShort){
|
||||||
|
|
||||||
unsigned int pixelsPerImage = (unsigned int)widthShort * heightShort;
|
unsigned int pixelsPerImage = (unsigned int)widthShort * heightShort;
|
||||||
for (unsigned int i = 0 < series->count; i++;) {
|
for (unsigned int i = 0; i < series->count; i++) {
|
||||||
series->images[i].width = (unsigned int)widthShort;
|
series->images[i].width = (unsigned int)widthShort;
|
||||||
series->images[i].height=(unsigned int)heightShort;
|
series->images[i].height=(unsigned int)heightShort;
|
||||||
series->images[i].buffer = (GrayScalePixelType *)malloc(pixelsPerImage * sizeof(GrayScalePixelType));
|
series->images[i].buffer = (GrayScalePixelType *)malloc(pixelsPerImage * sizeof(GrayScalePixelType));
|
||||||
|
|||||||
@@ -127,11 +127,66 @@ void tearDown(void) {
|
|||||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_pictureSeriesmemoryAllocatesCorrectly(void) //testet ob arbeitsspeicher fehlerfrei anfordert
|
||||||
|
{
|
||||||
|
unsigned short expectedCount = 3;
|
||||||
|
GrayScaleImageSeries *series = pictureSeriesmemory(expectedCount);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(expectedCount, series->count);
|
||||||
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
|
TEST_ASSERT_NOT_NULL(series->labels);
|
||||||
|
|
||||||
|
clearSeries(series);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_PicturesLabelsimportReadsDataCorrectly(void)
|
||||||
|
{
|
||||||
|
const char *path = "testImport.info2";
|
||||||
|
const unsigned short count = 2;
|
||||||
|
const unsigned short width = 4;
|
||||||
|
const unsigned short height = 4;
|
||||||
|
const unsigned char expectedLabel = 7;
|
||||||
|
|
||||||
|
// Testdatei über die bestehende Hilfsfunktion erzeugen
|
||||||
|
prepareImageFile(path, width, height, count, expectedLabel);
|
||||||
|
|
||||||
|
FILE *file = fopen(path, "rb");
|
||||||
|
TEST_ASSERT_NOT_NULL(file);
|
||||||
|
|
||||||
|
// Header-String + 3x unsigned short (Anzahl, Breite, Höhe) überspringen,
|
||||||
|
// damit der Dateizeiger exakt vor dem ersten Bild steht
|
||||||
|
long headerOffset = (long)strlen("__info2_image_file_format__") + 3 * sizeof(unsigned short);
|
||||||
|
fseek(file, headerOffset, SEEK_SET);
|
||||||
|
|
||||||
|
// Speicher mit deiner ersten Hilfsfunktion vorbereiten
|
||||||
|
GrayScaleImageSeries *series = pictureSeriesmemory(count);
|
||||||
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
|
|
||||||
|
// Deine zweite Hilfsfunktion direkt testen
|
||||||
|
int result = PicturesLabelsimport(series, file, width, height);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
// Prüfen, ob alles erfolgreich eingelesen wurde
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, result);
|
||||||
|
for (unsigned int i = 0; i < count; i++) {
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(width, series->images[i].width);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(height, series->images[i].height);
|
||||||
|
TEST_ASSERT_NOT_NULL(series->images[i].buffer);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSeries(series);
|
||||||
|
remove(path);
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
printf("\n============================\nImage input tests\n============================\n");
|
printf("\n============================\nImage input tests\n============================\n");
|
||||||
|
RUN_TEST(test_pictureSeriesmemoryAllocatesCorrectly);
|
||||||
|
RUN_TEST(test_PicturesLabelsimportReadsDataCorrectly);
|
||||||
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
||||||
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
||||||
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
||||||
|
|||||||
+114
-5
@@ -6,30 +6,139 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix matrix;
|
||||||
|
|
||||||
|
if (rows == 0 || cols == 0) {
|
||||||
|
matrix.rows = 0;
|
||||||
|
matrix.cols = 0;
|
||||||
|
matrix.buffer = NULL;
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix.buffer = malloc(rows*cols*sizeof(MatrixType));
|
||||||
|
|
||||||
|
if (matrix.buffer == NULL) {
|
||||||
|
matrix.rows = 0;
|
||||||
|
matrix.cols = 0;
|
||||||
|
matrix.buffer = NULL;
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(matrix.rows <= rowIdx || matrix.cols <= colIdx) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsigned int index = rowIdx * matrix.cols + colIdx;
|
||||||
|
|
||||||
|
matrix.buffer[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if(matrix.rows <= rowIdx || matrix.cols <= colIdx){
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
unsigned int index = rowIdx * matrix.cols + colIdx;
|
||||||
|
|
||||||
|
return matrix.buffer[index];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
}
|
Matrix resultMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
if (resultMatrix.buffer == NULL) {
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows*matrix1.cols; i++) {
|
||||||
|
resultMatrix.buffer[i]=matrix1.buffer[i]+matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (matrix1.rows == matrix2.rows && (matrix1.cols == 1 || matrix2.cols == 1)) {
|
||||||
|
Matrix resultMatrix;
|
||||||
|
|
||||||
|
if (matrix2.cols == 1) {
|
||||||
|
resultMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
if (resultMatrix.buffer == NULL) {
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows*matrix1.cols; i++) {
|
||||||
|
unsigned int rowIdx = i / matrix1.cols;
|
||||||
|
resultMatrix.buffer[i] = matrix1.buffer[i] + matrix2.buffer[rowIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
resultMatrix = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
if (resultMatrix.buffer == NULL) {
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < matrix2.rows*matrix2.cols; i++) {
|
||||||
|
unsigned int rowIdx = i / matrix2.cols;
|
||||||
|
resultMatrix.buffer[i] = matrix2.buffer[i] + matrix1.buffer[rowIdx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if(matrix1.cols !=matrix2.rows){
|
||||||
|
Matrix result;
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix resultMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
if (resultMatrix.buffer == NULL) {
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
||||||
|
MatrixType sum = 0;
|
||||||
|
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
||||||
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
setMatrixAt(sum, resultMatrix, i, j);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultMatrix;
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,12 @@
|
|||||||
|
|
||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
|
typedef struct Matrix{
|
||||||
|
unsigned int rows;
|
||||||
|
unsigned int cols;
|
||||||
|
MatrixType* buffer;
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ void test_multiplyFailsOnWrongInputDimensions(void)
|
|||||||
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
||||||
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
|
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
|
||||||
|
|
||||||
|
|
||||||
Matrix result = multiply(matrix1, matrix2);
|
Matrix result = multiply(matrix1, matrix2);
|
||||||
|
|
||||||
TEST_ASSERT_NULL(result.buffer);
|
TEST_ASSERT_NULL(result.buffer);
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
|
|
||||||
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 = {0, 0, NULL};
|
||||||
|
|
||||||
if(count > 0 && images != NULL)
|
if(count > 0 && images != NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,37 @@
|
|||||||
|
|
||||||
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 != NULL){
|
||||||
|
|
||||||
|
const char *fileTag = "__info2_neural_network_file_format__";
|
||||||
|
|
||||||
|
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||||
|
if(nn.numberOfLayers > 0){
|
||||||
|
int dimension;
|
||||||
|
|
||||||
|
dimension = nn.layers[0].weights.cols;
|
||||||
|
fwrite(&dimension, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
for(unsigned int i= 0; i<nn.numberOfLayers; i++){
|
||||||
|
dimension = nn.layers[i].weights.rows;
|
||||||
|
fwrite(&dimension, sizeof(int), 1, file);
|
||||||
|
unsigned int nWeights = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||||
|
|
||||||
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), nWeights, file);
|
||||||
|
|
||||||
|
unsigned int nBiases = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||||
|
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), nBiases, file);
|
||||||
|
}
|
||||||
|
dimension = 0;
|
||||||
|
fwrite(&dimension, sizeof(int), 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user