From cfb3cb28d737a18efd0a9d996dd025b895c00086 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 19 Nov 2022 17:39:56 +0100 Subject: [PATCH] Add cnn/preview.c --- Makefile | 5 ++++- src/cnn/preview.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/cnn/preview.c diff --git a/Makefile b/Makefile index 288a7d8..a22b6c5 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ $(BUILDDIR)/mnist_%.o: $(MNIST_SRCDIR)/%.c $(MNIST_SRCDIR)/include/%.h # # Build cnn # -cnn: $(BUILDDIR)/cnn-main $(BUILDDIR)/cnn-main-cuda; +cnn: $(BUILDDIR)/cnn-main $(BUILDDIR)/cnn-main-cuda $(BUILDDIR)/cnn-preview; $(BUILDDIR)/cnn-main: $(CNN_SRCDIR)/main.c $(BUILDDIR)/cnn_train.o $(BUILDDIR)/cnn_cnn.o $(BUILDDIR)/cnn_creation.o $(BUILDDIR)/cnn_initialisation.o $(BUILDDIR)/cnn_make.o $(BUILDDIR)/cnn_neuron_io.o $(BUILDDIR)/cnn_function.o $(BUILDDIR)/cnn_utils.o $(BUILDDIR)/cnn_update.o $(BUILDDIR)/cnn_free.o $(BUILDDIR)/cnn_jpeg.o $(BUILDDIR)/cnn_convolution.o $(BUILDDIR)/cnn_backpropagation.o $(BUILDDIR)/colors.o $(BUILDDIR)/mnist.o $(CC) $^ -o $@ $(CFLAGS) @@ -75,6 +75,9 @@ else $(NVCC) $(NVCCFLAGS) $^ -o $@ endif +$(BUILDDIR)/cnn-preview: $(CNN_SRCDIR)/preview.c $(BUILDDIR)/cnn_jpeg.o $(BUILDDIR)/colors.o + $(CC) $^ -o $@ $(CFLAGS) + $(BUILDDIR)/cnn_%.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h $(CC) -c $< -o $@ $(CFLAGS) diff --git a/src/cnn/preview.c b/src/cnn/preview.c new file mode 100644 index 0000000..5e0da6a --- /dev/null +++ b/src/cnn/preview.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include "include/jpeg.h" + + +void print_image(unsigned char* image, int height, int width) { + + for (int i=0; i < (int)width; i++) { + for (int j=0; j < (int)height; j++) { + printf("\x1b[38;2;%d;%d;%dm#\x1b[0m", image[((i*width)+j)*3 + 0], image[((i*width)+j)*3 + 1], image[((i*width)+j)*3 + 2]); + } + printf("\n"); + } +} + +void preview_images(char* path, int limit) { + jpegDataset* dataset = loadJpegDataset(path); + imgRawImage* image; + + if (limit == -1) { + limit = dataset->numImages; + } + for (int i=0; i < limit; i++) { + printf("--- Image %d : %d ---\n", i, dataset->labels[i]); + + if (!dataset->images[i]) { + image = loadJpegImageFile(dataset->fileNames[i]); + dataset->images[i] = image->lpData; + free(image); + } + print_image(dataset->images[i], dataset->height, dataset->width); + + free(dataset->images[i]); + } +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + printf("Utilisation: %s [DIRECTORY] (opt:nombre d'images)\n", argv[0]); + return 1; + } + int limit = -1; + if (argc > 2) { + limit = strtol(argv[2], NULL, 10); + } + preview_images(argv[1], limit); + return 0; +}