diff --git a/Startcode/main.c b/Startcode/main.c index a95cba3..04139e7 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -4,6 +4,21 @@ #include "mnistVisualization.h" #include "neuralNetwork.h" +static int classifyImage(const GrayScaleImage *image, const void *context) +{ + const NeuralNetwork *model = (const NeuralNetwork *)context; + unsigned char *prediction = predict(*model, image, 1); + int label = -1; + + if(prediction != NULL) + { + label = prediction[0]; + free(prediction); + } + + return label; +} + int main(int argc, char *argv[]) { const unsigned int windowWidth = 800; @@ -34,7 +49,7 @@ int main(int argc, char *argv[]) if(predictions != NULL) { - showMnist(windowWidth, windowHeight, series, predictions); + showMnist(windowWidth, windowHeight, series, predictions, classifyImage, &model); free(predictions); exitCode = EXIT_SUCCESS; } diff --git a/Startcode/mnistVisualization.c b/Startcode/mnistVisualization.c index b0a29f5..f0ea9d0 100644 --- a/Startcode/mnistVisualization.c +++ b/Startcode/mnistVisualization.c @@ -1,10 +1,21 @@ #include #include #include +#include #include "mnistVisualization.h" #include "raylib.h" #define MAX_TEXT_LEN 100 +#define BRUSH_RADIUS 2.0f +#define BRUSH_STEP 0.25f +#define BROWSE_TEXT "Use left and right key to navigate ... (D: draw mode)" +#define DRAW_TEXT "Left mouse: draw Right mouse: erase C: clear D: browse" + +typedef enum +{ + BROWSE_MODE, + DRAW_MODE +} VisualizationMode; typedef struct { @@ -23,6 +34,15 @@ typedef struct Color color; } TextLabel; +typedef struct +{ + GrayScaleImage image; + Vector2 lastPosition; + int isStroking; + int isEmpty; + int prediction; +} DrawingCanvas; + static TextLabel *createTextLabel(const char *text, unsigned int fontSize, Color color) { TextLabel *label = NULL; @@ -65,6 +85,185 @@ static MnistVisualization *createVisualizationContainer(const GrayScaleImageSeri return container; } +static void setLabelText(TextLabel *label, const char *text) +{ + strncpy(label->text, text, MAX_TEXT_LEN); + label->text[MAX_TEXT_LEN-1] = '\0'; +} + +static DrawingCanvas *createDrawingCanvas(unsigned int width, unsigned int height) +{ + DrawingCanvas *canvas = (DrawingCanvas *)calloc(1, sizeof(DrawingCanvas)); + + if(canvas != NULL) + { + canvas->image.buffer = (GrayScalePixelType *)calloc(width * height, sizeof(GrayScalePixelType)); + + if(canvas->image.buffer != NULL) + { + canvas->image.width = width; + canvas->image.height = height; + canvas->isEmpty = 1; + } + else + { + free(canvas); + canvas = NULL; + } + } + + return canvas; +} + +static void freeDrawingCanvas(DrawingCanvas *canvas) +{ + if(canvas != NULL) + { + free(canvas->image.buffer); + free(canvas); + } +} + +static void clearDrawingCanvas(DrawingCanvas *canvas) +{ + memset(canvas->image.buffer, 0, canvas->image.width * canvas->image.height * sizeof(GrayScalePixelType)); + canvas->isEmpty = 1; +} + +// Weicher Pinsel: innerer Bereich voll weiss, nach aussen linear auslaufend (aehnlich den MNIST-Strichen) +static void paintAt(DrawingCanvas *canvas, Vector2 position, int erase) +{ + int minX = (int)floorf(position.x - BRUSH_RADIUS); + int maxX = (int)ceilf(position.x + BRUSH_RADIUS); + int minY = (int)floorf(position.y - BRUSH_RADIUS); + int maxY = (int)ceilf(position.y + BRUSH_RADIUS); + + for(int y = minY; y <= maxY; y++) + { + for(int x = minX; x <= maxX; x++) + { + if(x >= 0 && y >= 0 && x < (int)canvas->image.width && y < (int)canvas->image.height) + { + float dx = x + 0.5f - position.x; + float dy = y + 0.5f - position.y; + float strength = 2.0f * (1.0f - sqrtf(dx * dx + dy * dy) / BRUSH_RADIUS); + GrayScalePixelType *pixel = &canvas->image.buffer[y * canvas->image.width + x]; + + if(strength > 0) + { + if(erase) + *pixel = 0; + else + { + float value = strength >= 1.0f ? 255.0f : 255.0f * strength; + + if(value > *pixel) + *pixel = (GrayScalePixelType)value; + } + } + } + } + } +} + +// Zwischenpunkte interpolieren, damit bei schnellen Mausbewegungen keine Luecken entstehen +static void paintLine(DrawingCanvas *canvas, Vector2 from, Vector2 to, int erase) +{ + float dx = to.x - from.x; + float dy = to.y - from.y; + int steps = (int)ceilf(sqrtf(dx * dx + dy * dy) / BRUSH_STEP); + + for(int i = 0; i <= steps; i++) + { + float t = steps > 0 ? (float)i / steps : 0.0f; + Vector2 point = {from.x + t * dx, from.y + t * dy}; + paintAt(canvas, point, erase); + } +} + +// MNIST-Ziffern sind ueber ihren Schwerpunkt zentriert; die Zeichnung wird fuer die Vorhersage genauso verschoben +static void centerByMass(const GrayScaleImage source, GrayScaleImage target) +{ + double sum = 0; + double centerX = 0; + double centerY = 0; + + memset(target.buffer, 0, target.width * target.height * sizeof(GrayScalePixelType)); + + for(unsigned int i = 0; i < source.width * source.height; i++) + { + sum += source.buffer[i]; + centerX += (double)(i % source.width) * source.buffer[i]; + centerY += (double)(i / source.width) * source.buffer[i]; + } + + if(sum > 0) + { + int shiftX = (int)lround((source.width - 1) / 2.0 - centerX / sum); + int shiftY = (int)lround((source.height - 1) / 2.0 - centerY / sum); + + for(int y = 0; y < (int)source.height; y++) + { + for(int x = 0; x < (int)source.width; x++) + { + int newX = x + shiftX; + int newY = y + shiftY; + + if(newX >= 0 && newY >= 0 && newX < (int)target.width && newY < (int)target.height) + target.buffer[newY * target.width + newX] = source.buffer[y * source.width + x]; + } + } + } +} + +static void predictDrawing(DrawingCanvas *canvas, ClassifierFunctionType classify, const void *classifierContext) +{ + GrayScaleImage centered = canvas->image; + centered.buffer = (GrayScalePixelType *)malloc(canvas->image.width * canvas->image.height * sizeof(GrayScalePixelType)); + canvas->prediction = -1; + + if(centered.buffer != NULL) + { + centerByMass(canvas->image, centered); + canvas->prediction = classify(¢ered, classifierContext); + free(centered.buffer); + } +} + +static void updateDrawingCanvas(DrawingCanvas *canvas, const MnistVisualization *container, ClassifierFunctionType classify, const void *classifierContext, TextLabel *predictionLabel) +{ + int isDrawing = IsMouseButtonDown(MOUSE_BUTTON_LEFT); + int isErasing = IsMouseButtonDown(MOUSE_BUTTON_RIGHT); + + if(IsKeyReleased(KEY_C)) + clearDrawingCanvas(canvas); + + if(isDrawing || isErasing) + { + Vector2 mouse = GetMousePosition(); + Vector2 position = {(mouse.x - container->position.x) / container->pixelSize.x, (mouse.y - container->position.y) / container->pixelSize.y}; + + if(canvas->isStroking) + paintLine(canvas, canvas->lastPosition, position, isErasing); + else + paintAt(canvas, position, isErasing); + + canvas->lastPosition = position; + canvas->isStroking = 1; + canvas->isEmpty = 0; + predictDrawing(canvas, classify, classifierContext); + } + else + canvas->isStroking = 0; + + if(canvas->isEmpty) + setLabelText(predictionLabel, "Draw a digit ..."); + else if(canvas->prediction < 0) + setLabelText(predictionLabel, "Prediction failed ..."); + else + snprintf(predictionLabel->text, MAX_TEXT_LEN, "Predicted label: %d", canvas->prediction); +} + static void drawTextLabel(const TextLabel *label) { DrawText(label->text, label->position.x, label->position.y, label->fontSize, label->color); @@ -106,6 +305,24 @@ static void drawAll(const MnistVisualization *container, const TextLabel *naviga EndDrawing(); } +static void drawCanvasAll(const DrawingCanvas *canvas, const MnistVisualization *container, const TextLabel *navigationLabel, const TextLabel *predictionLabel) +{ + // Hilfsrahmen: MNIST-Ziffern fuellen etwa die inneren 20x20 von 28x28 Pixeln + const float margin = canvas->image.width / 7.0f; + Rectangle guide = {container->position.x + margin * container->pixelSize.x, container->position.y + margin * container->pixelSize.y, + (canvas->image.width - 2 * margin) * container->pixelSize.x, (canvas->image.height - 2 * margin) * container->pixelSize.y}; + + BeginDrawing(); + ClearBackground(BLACK); + + drawDigit(canvas->image, container->position, container->pixelSize); + DrawRectangleLinesEx(guide, 1, DARKGRAY); + drawTextLabel(navigationLabel); + drawTextLabel(predictionLabel); + + EndDrawing(); +} + static int checkUserInput() { int inputResult = 0; @@ -141,17 +358,19 @@ static void update(MnistVisualization *container, TextLabel *predictionLabel, in updatePredictionLabel(predictionLabel, container->series->labels[container->currentIdx], container->predictions[container->currentIdx]); } -void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[]) +void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[], ClassifierFunctionType classify, const void *classifierContext) { const Vector2 windowSize = {windowWidth, windowHeight}; + VisualizationMode mode = BROWSE_MODE; MnistVisualization *container = createVisualizationContainer(series, predictions, windowSize); - TextLabel *navigationLabel = createTextLabel("Use left and right key to navigate ...", 20, WHITE); + DrawingCanvas *canvas = container != NULL ? createDrawingCanvas(series->images[0].width, series->images[0].height) : NULL; + TextLabel *navigationLabel = createTextLabel(BROWSE_TEXT, 20, WHITE); TextLabel *predictionLabel = createTextLabel("", 20, WHITE); predictionLabel->position.x = 10; predictionLabel->position.y = windowSize.y - 50; - if(container != NULL && navigationLabel != NULL && predictionLabel != NULL) + if(container != NULL && canvas != NULL && classify != NULL && navigationLabel != NULL && predictionLabel != NULL) { InitWindow(windowSize.x, windowSize.y, "MNIST Browser"); @@ -159,15 +378,30 @@ void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GraySc while (!WindowShouldClose()) { - int updateDirection = checkUserInput(); - update(container, predictionLabel, updateDirection); - drawAll(container, navigationLabel, predictionLabel); + if(IsKeyReleased(KEY_D)) + { + mode = mode == BROWSE_MODE ? DRAW_MODE : BROWSE_MODE; + setLabelText(navigationLabel, mode == BROWSE_MODE ? BROWSE_TEXT : DRAW_TEXT); + } + + if(mode == BROWSE_MODE) + { + int updateDirection = checkUserInput(); + update(container, predictionLabel, updateDirection); + drawAll(container, navigationLabel, predictionLabel); + } + else + { + updateDrawingCanvas(canvas, container, classify, classifierContext, predictionLabel); + drawCanvasAll(canvas, container, navigationLabel, predictionLabel); + } } } CloseWindow(); free(container); + freeDrawingCanvas(canvas); free(navigationLabel); free(predictionLabel); } \ No newline at end of file diff --git a/Startcode/mnistVisualization.h b/Startcode/mnistVisualization.h index 050ac97..066d490 100644 --- a/Startcode/mnistVisualization.h +++ b/Startcode/mnistVisualization.h @@ -3,6 +3,10 @@ #include "imageInput.h" -void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[]); +// Liefert das erkannte Label (0-9) eines Bildes oder -1 im Fehlerfall; context wird unveraendert durchgereicht. +// (Die Visualisierung kann neuralNetwork.h nicht einbinden, da raylib.h selbst einen Typ "Matrix" definiert.) +typedef int (*ClassifierFunctionType)(const GrayScaleImage *image, const void *context); + +void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[], ClassifierFunctionType classify, const void *classifierContext); #endif \ No newline at end of file