Files
Info2-Pr-NeuroNetz/Startcode/main.c
T
Manuel GlockandClaude Opus 5.5 9356a19fb0 Optionale Aufgabe: eigene Ziffern malen
Taste D wechselt in den Malmodus (linke Maus malen, rechte Maus radieren,
C leeren). Die Zeichnung wird live klassifiziert; vor der Vorhersage wird
sie wie die MNIST-Daten ueber den Schwerpunkt zentriert. Da raylib.h selbst
einen Typ Matrix definiert, bekommt showMnist eine Klassifizierungsfunktion
aus main.c uebergeben statt neuralNetwork.h einzubinden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 12:19:07 +01:00

83 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "imageInput.h"
#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;
const unsigned int windowHeight = 600;
int exitCode = EXIT_FAILURE;
if(argc == 3)
{
const char *pathToMnist = argv[1];
const char *pathToModel = argv[2];
GrayScaleImageSeries *series = readImages(pathToMnist);
if(series != NULL)
{
NeuralNetwork model = {NULL, 0};
printf("Loaded %u images ... \n", series->count);
model = loadModel(pathToModel);
if(model.numberOfLayers > 0)
{
unsigned char *predictions = NULL;
printf("Processing %u images ...\n", series->count);
predictions = predict(model, series->images, series->count);
if(predictions != NULL)
{
showMnist(windowWidth, windowHeight, series, predictions, classifyImage, &model);
free(predictions);
exitCode = EXIT_SUCCESS;
}
else
{
fprintf(stderr, "Error while processing images ...\n");
exitCode = EXIT_FAILURE;
}
clearModel(&model);
}
else
{
fprintf(stderr, "Error while loading model from %s ...\n", pathToModel);
exitCode = EXIT_FAILURE;
}
}
else
{
fprintf(stderr, "Error while reading images from %s ...\n", pathToMnist);
exitCode = EXIT_FAILURE;
}
clearSeries(series);
}
else
{
fprintf(stderr, "Usage: %s <mnist file> <model file>\n", argv[0]);
exitCode = EXIT_FAILURE;
}
return exitCode;
}