forked from freudenreichan/info2Praktikum-NeuronalesNetz
optionale Aufgabe
This commit is contained in:
1 parent
d5753d1510
commit
c6053f61bc
2 files changed
+66
-5
No files matched your search
@@ -1,8 +1,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "mnistVisualization.h"
|
|
||||||
|
#define Matrix RaylibMatrix
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#undef Matrix
|
||||||
|
|
||||||
|
#include "mnistVisualization.h"
|
||||||
|
|
||||||
#define MAX_TEXT_LEN 100
|
#define MAX_TEXT_LEN 100
|
||||||
|
|
||||||
@@ -13,6 +17,9 @@ typedef struct
|
|||||||
const unsigned char *predictions;
|
const unsigned char *predictions;
|
||||||
unsigned int currentIdx;
|
unsigned int currentIdx;
|
||||||
Vector2 pixelSize;
|
Vector2 pixelSize;
|
||||||
|
int isDrawing;
|
||||||
|
GrayScaleImage canvas;
|
||||||
|
NeuralNetwork model;
|
||||||
} MnistVisualization;
|
} MnistVisualization;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
@@ -55,16 +62,23 @@ static MnistVisualization *createVisualizationContainer(const GrayScaleImageSeri
|
|||||||
|
|
||||||
if(container != NULL)
|
if(container != NULL)
|
||||||
{
|
{
|
||||||
|
container->canvas.width = series->images[0].width;
|
||||||
|
container->canvas.height = series->images[0].height;
|
||||||
|
container->canvas.buffer = (unsigned char *)calloc(container->canvas.width * container->canvas.height, sizeof(unsigned char));
|
||||||
|
container->model = loadModel("mnist_model.info2");
|
||||||
|
|
||||||
Vector2 pixelSize = {(int)(size.x / series->images[0].width), (int)(size.y / series->images[0].height)};
|
Vector2 pixelSize = {(int)(size.x / series->images[0].width), (int)(size.y / series->images[0].height)};
|
||||||
container->pixelSize = pixelSize;
|
container->pixelSize = pixelSize;
|
||||||
container->series = series;
|
container->series = series;
|
||||||
container->predictions = predictions;
|
container->predictions = predictions;
|
||||||
|
container->isDrawing = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void drawTextLabel(const TextLabel *label)
|
static void drawTextLabel(const TextLabel *label)
|
||||||
{
|
{
|
||||||
DrawText(label->text, label->position.x, label->position.y, label->fontSize, label->color);
|
DrawText(label->text, label->position.x, label->position.y, label->fontSize, label->color);
|
||||||
@@ -99,7 +113,11 @@ static void drawAll(const MnistVisualization *container, const TextLabel *naviga
|
|||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
drawDigit(container->series->images[container->currentIdx], container->position, container->pixelSize);
|
if (container->isDrawing) {
|
||||||
|
drawDigit(container->canvas, container->position, container->pixelSize);
|
||||||
|
} else {
|
||||||
|
drawDigit(container->series->images[container->currentIdx], container->position, container->pixelSize);
|
||||||
|
}
|
||||||
drawTextLabel(navigationLabel);
|
drawTextLabel(navigationLabel);
|
||||||
drawTextLabel(predictionLabel);
|
drawTextLabel(predictionLabel);
|
||||||
|
|
||||||
@@ -114,6 +132,10 @@ static int checkUserInput()
|
|||||||
inputResult = -1;
|
inputResult = -1;
|
||||||
else if(IsKeyReleased(KEY_RIGHT))
|
else if(IsKeyReleased(KEY_RIGHT))
|
||||||
inputResult = 1;
|
inputResult = 1;
|
||||||
|
else if(IsKeyReleased(KEY_UP))
|
||||||
|
inputResult = 2;
|
||||||
|
else if(IsKeyReleased(KEY_DOWN))
|
||||||
|
inputResult = 3;
|
||||||
|
|
||||||
return inputResult;
|
return inputResult;
|
||||||
}
|
}
|
||||||
@@ -137,8 +159,41 @@ static void updatePredictionLabel(TextLabel *predictionLabel, unsigned char true
|
|||||||
|
|
||||||
static void update(MnistVisualization *container, TextLabel *predictionLabel, int updateDirection)
|
static void update(MnistVisualization *container, TextLabel *predictionLabel, int updateDirection)
|
||||||
{
|
{
|
||||||
updateDisplayContainer(container, updateDirection);
|
if (updateDirection == 2)
|
||||||
updatePredictionLabel(predictionLabel, container->series->labels[container->currentIdx], container->predictions[container->currentIdx]);
|
{
|
||||||
|
container->isDrawing = !container->isDrawing;
|
||||||
|
}
|
||||||
|
else if (updateDirection == 3)
|
||||||
|
{
|
||||||
|
memset(container->canvas.buffer, 0, container->canvas.width * container->canvas.height);
|
||||||
|
}
|
||||||
|
else if (!container->isDrawing)
|
||||||
|
{
|
||||||
|
updateDisplayContainer(container, updateDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (container->isDrawing == 1 && IsMouseButtonDown(MOUSE_BUTTON_LEFT))
|
||||||
|
{
|
||||||
|
int col = GetMouseX() / (int)container->pixelSize.x;
|
||||||
|
int row = GetMouseY() / (int)container->pixelSize.y;
|
||||||
|
|
||||||
|
if (col >= 0 && col < container->canvas.width && row >= 0 && row < container->canvas.height)
|
||||||
|
{
|
||||||
|
container->canvas.buffer[row * container->canvas.width + col] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (container->isDrawing)
|
||||||
|
{
|
||||||
|
unsigned char *liveResult = predict(container->model, &container->canvas, 1);
|
||||||
|
unsigned char predictedDigit = liveResult[0];
|
||||||
|
free(liveResult);
|
||||||
|
updatePredictionLabel(predictionLabel, 0, predictedDigit);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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[])
|
||||||
@@ -146,7 +201,7 @@ void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GraySc
|
|||||||
const Vector2 windowSize = {windowWidth, windowHeight};
|
const Vector2 windowSize = {windowWidth, windowHeight};
|
||||||
|
|
||||||
MnistVisualization *container = createVisualizationContainer(series, predictions, windowSize);
|
MnistVisualization *container = createVisualizationContainer(series, predictions, windowSize);
|
||||||
TextLabel *navigationLabel = createTextLabel("Use left and right key to navigate ...", 20, WHITE);
|
TextLabel *navigationLabel = createTextLabel("Use left and right key to navigate\n Use up and down key to enable free-Drawing and reset", 20, WHITE);
|
||||||
TextLabel *predictionLabel = createTextLabel("", 20, WHITE);
|
TextLabel *predictionLabel = createTextLabel("", 20, WHITE);
|
||||||
predictionLabel->position.x = 10;
|
predictionLabel->position.x = 10;
|
||||||
predictionLabel->position.y = windowSize.y - 50;
|
predictionLabel->position.y = windowSize.y - 50;
|
||||||
@@ -167,6 +222,11 @@ void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GraySc
|
|||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|
||||||
|
if(container != NULL)
|
||||||
|
{
|
||||||
|
free(container->canvas.buffer);
|
||||||
|
clearModel(&container->model);
|
||||||
|
}
|
||||||
free(container);
|
free(container);
|
||||||
free(navigationLabel);
|
free(navigationLabel);
|
||||||
free(predictionLabel);
|
free(predictionLabel);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define MNISTVISUALIZATION_H
|
#define MNISTVISUALIZATION_H
|
||||||
|
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
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[]);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user