Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
732ec57e7f | ||
|
|
47690eaa20 | ||
|
|
9468b6aea0 | ||
|
|
33d9ac7833 | ||
|
|
ff3ba69099 | ||
|
|
a4cf2c27e5 | ||
|
|
4e2bec6081 | ||
|
|
003d1820d9 | ||
|
|
d999786d02 | ||
|
|
9b1e4aab11 | ||
|
|
060b22f50e | ||
|
|
ebe3304c54 | ||
|
|
c0f73f9362 | ||
|
|
205513f901 | ||
|
|
d4e329e098 | ||
|
|
85369b3bad | ||
|
|
ddea2d5d72 | ||
|
|
f736d1743d | ||
|
|
69696064a5 | ||
|
|
37341ec9c3 | ||
|
|
5a285cf7e1 | ||
|
|
38dae6b132 | ||
|
|
3bb392a5f9 |
No files matched your search
+136
-2
@@ -7,16 +7,150 @@
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
//überprüft imageInput.h ob erfolgreich alle Elemente eingelesen oder Fehler
|
||||
static int validateFileHeader(FILE * file) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
size_t headerlength = strlen(FILE_HEADER_STRING);
|
||||
if (headerlength >= BUFFER_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
size_t readbytes = fread(buffer, sizeof (char), headerlength, file);
|
||||
if (readbytes != headerlength) {
|
||||
return 0;
|
||||
}
|
||||
buffer[headerlength] = '\0';
|
||||
return (strcmp(buffer, FILE_HEADER_STRING) == 0);
|
||||
}
|
||||
|
||||
GrayScaleImageSeries *pictureSeriesmemory(unsigned short numImagesShort);
|
||||
int PicturesLabelsimport(GrayScaleImageSeries *series, FILE *file, unsigned short widthShort, unsigned short heightShort);
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
return series;
|
||||
if (path == NULL) return NULL;
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (file == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//Header überprüfen in mainfunktion
|
||||
if (!validateFileHeader(file)) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
unsigned short numImagesShort = 0;
|
||||
unsigned short widthShort = 0;
|
||||
unsigned short heightShort = 0;
|
||||
|
||||
//Anzahl, Breite und Höhe einlesen, weiveile Elemente erfolgreich zurück
|
||||
if (fread(&numImagesShort, sizeof(unsigned short), 1, file) != 1 ||
|
||||
fread(&widthShort, sizeof(unsigned short), 1, file) != 1 ||
|
||||
fread(&heightShort, sizeof(unsigned short), 1, file) != 1){
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
series = pictureSeriesmemory(numImagesShort); //static kann nur innerhalb imageInput.c aufgerufen werden
|
||||
if (series == NULL) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!PicturesLabelsimport(series, file, widthShort, heightShort)) { //! -> inverter
|
||||
fclose(file);
|
||||
clearSeries(series);//Speicher bei Fehler direkt freigeben
|
||||
return NULL;
|
||||
}
|
||||
fclose(file);
|
||||
return series; //gibt fertige bildserie zurück
|
||||
|
||||
}
|
||||
|
||||
//Speicher für Bildserei
|
||||
GrayScaleImageSeries *pictureSeriesmemory(unsigned short numImagesShort) {
|
||||
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
if (series == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = (unsigned int)numImagesShort;
|
||||
series->images = (GrayScaleImage*)malloc(series->count * sizeof(GrayScaleImage));
|
||||
series->labels = (unsigned char*)malloc(series->count * sizeof(unsigned char));
|
||||
|
||||
if (series->images == NULL || (series->count > 0 && series->labels == NULL)) {
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
return NULL;
|
||||
}
|
||||
return series; //gibt zeiger (pictureSeriesmemory auf Objekt zurück
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Bilder und Labels einlesen
|
||||
int PicturesLabelsimport(GrayScaleImageSeries *series, FILE *file, unsigned short widthShort, unsigned short heightShort){
|
||||
|
||||
unsigned int pixelsPerImage = (unsigned int)widthShort * heightShort;
|
||||
for (unsigned int i = 0; i < series->count; i++) {
|
||||
series->images[i].width = (unsigned int)widthShort;
|
||||
series->images[i].height=(unsigned int)heightShort;
|
||||
series->images[i].buffer = (GrayScalePixelType *)malloc(pixelsPerImage * sizeof(GrayScalePixelType));
|
||||
|
||||
if (series->images[i].buffer == NULL) {
|
||||
//Fehler -> zugewiesenen Speicher freigeben
|
||||
for (unsigned int j=0;j<i; j++) {
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
return 0; //Fehler
|
||||
}
|
||||
|
||||
//Pixel einlesen
|
||||
if (fread(series->images[i].buffer, sizeof(GrayScalePixelType), pixelsPerImage, file) != pixelsPerImage) {
|
||||
for (unsigned int j = 0; j<= i; j++){
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Label einlesen
|
||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1 ) {
|
||||
for (unsigned int j=0; j<=i; j++) {
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
//Speicher von Bildserie komplett freigeben
|
||||
if (series != NULL) {
|
||||
if (series->images != NULL) {
|
||||
for (unsigned int i=0;i< series->count;i++) {
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
}
|
||||
free(series->labels);
|
||||
free(series);
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,7 @@ typedef struct
|
||||
GrayScaleImageSeries *readImages(const char *path);
|
||||
void clearSeries(GrayScaleImageSeries *series);
|
||||
|
||||
GrayScaleImageSeries *pictureSeriesmemory(unsigned short numImagesShort);
|
||||
int PicturesLabelsimport(GrayScaleImageSeries *series, FILE *file, unsigned short widthSHort, unsigned short heightShort);
|
||||
|
||||
#endif
|
||||
@@ -127,11 +127,66 @@ void tearDown(void) {
|
||||
// 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()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\nImage input tests\n============================\n");
|
||||
RUN_TEST(test_pictureSeriesmemoryAllocatesCorrectly);
|
||||
RUN_TEST(test_PicturesLabelsimportReadsDataCorrectly);
|
||||
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
||||
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
||||
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
||||
|
||||
+114
-5
@@ -6,30 +6,139 @@
|
||||
|
||||
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)
|
||||
{
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
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)
|
||||
{
|
||||
|
||||
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 struct Matrix{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType* buffer;
|
||||
} Matrix;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
|
||||
|
||||
@@ -123,6 +123,7 @@ void test_multiplyFailsOnWrongInputDimensions(void)
|
||||
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
||||
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
|
||||
|
||||
|
||||
Matrix result = multiply(matrix1, matrix2);
|
||||
|
||||
TEST_ASSERT_NULL(result.buffer);
|
||||
|
||||
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
||||
|
||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||
{
|
||||
Matrix matrix = {NULL, 0, 0};
|
||||
Matrix matrix = {0, 0, NULL};
|
||||
|
||||
if(count > 0 && images != NULL)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,37 @@
|
||||
|
||||
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)
|
||||
|
||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user