From 635b46bdf3990bc45999c7046746b7e1ddd153e0 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 5 Feb 2023 16:21:41 +0100 Subject: [PATCH] Add export.c --- Makefile | 9 +++-- src/cnn/export.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 src/cnn/export.c diff --git a/Makefile b/Makefile index e0b497e..6c54ca3 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,8 @@ LD_CFLAGS = -lm -lpthread -ljpeg -fopenmp LD_NVCCFLAGS = -ljpeg -Xcompiler -fopenmp # Compilation flag -CFLAGS = -Wall -Wextra -std=gnu99 -NVCCFLAGS = +CFLAGS = -Wall -Wextra -std=gnu99 -g +NVCCFLAGS = -g # Remove warnings about unused variables, functions, ... # -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable # Compile with debug @@ -69,7 +69,7 @@ $(BUILDDIR)/mnist_%.o: $(MNIST_SRCDIR)/%.c $(MNIST_SRCDIR)/include/%.h # # Build cnn # -cnn: $(BUILDDIR)/cnn-main $(BUILDDIR)/cnn-main-cuda $(BUILDDIR)/cnn-preview; +cnn: $(BUILDDIR)/cnn-main $(BUILDDIR)/cnn-main-cuda $(BUILDDIR)/cnn-preview $(BUILDDIR)/cnn-export; $(BUILDDIR)/cnn-main: $(CNN_SRCDIR)/main.c \ $(BUILDDIR)/cnn_train.o \ @@ -119,6 +119,9 @@ endif $(BUILDDIR)/cnn-preview: $(CNN_SRCDIR)/preview.c $(BUILDDIR)/cnn_jpeg.o $(BUILDDIR)/colors.o $(BUILDDIR)/utils.o $(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS) +$(BUILDDIR)/cnn-export: $(CNN_SRCDIR)/export.c $(BUILDDIR)/cnn_free.o $(BUILDDIR)/cnn_neuron_io.o $(BUILDDIR)/utils.o + $(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS) + $(BUILDDIR)/cnn_%.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h $(CC) -c $< -o $@ $(CFLAGS) diff --git a/src/cnn/export.c b/src/cnn/export.c new file mode 100644 index 0000000..29a3f71 --- /dev/null +++ b/src/cnn/export.c @@ -0,0 +1,96 @@ +#include +#include +#include + +#include "include/free.h" +#include "include/struct.h" +#include "include/neuron_io.h" + + +void help(char* call) { + printf("Usage: %s ( print-poids-kernel-cnn ) [OPTIONS]\n\n", call); +} + + +void print_poids_ker_cnn(char* modele) { + Network* network = read_network(modele); + int vus = 0; + + printf("{\n"); + for (int i=0; i < network->max_size-1; i++) { + Kernel_cnn* kernel_cnn = network->kernel[i]->cnn; + if (!(!kernel_cnn)) { + if (vus != 0) { + printf(","); + } + vus++; + printf("\t\"%d\":[\n", i); + for (int i=0; i < kernel_cnn->rows; i++) { + printf("\t\t[\n"); + for (int j=0; j < kernel_cnn->columns; j++) { + printf("\t\t\t[\n"); + for (int k=0; k < kernel_cnn->k_size; k++) { + printf("\t\t\t\t["); + for (int l=0; l < kernel_cnn->k_size; l++) { + printf("%lf", kernel_cnn->w[i][j][k][l]); + if (l != kernel_cnn->k_size-1) { + printf(", "); + } + } + printf(" ]"); + if (k != kernel_cnn->k_size-1) { + printf(","); + } + printf("\n"); + } + printf("\t\t\t]"); + if (j != kernel_cnn->columns-1) { + printf(","); + } + printf("\n"); + } + printf("\t\t]"); + if (i != kernel_cnn->rows-1) { + printf(","); + } + printf("\n"); + } + printf("\t]\n"); + } + } + printf("}\n"); + + free_network(network); +} + + + +int main(int argc, char* argv[]) { + if (argc < 2) { + printf("Pas d'action spécifiée\n"); + help(argv[0]); + return 1; + } + if (! strcmp(argv[1], "print-poids-kernel-cnn")) { + char* modele = NULL; // Fichier contenant le modèle + int i = 2; + while (i < argc) { + if ((! strcmp(argv[i], "--modele"))||(! strcmp(argv[i], "-m"))) { + modele = argv[i+1]; + i += 2; + } else { + printf("Option choisie inconnue: %s\n", argv[i]); + i++; + } + } + if (!modele) { + printf("Pas de modèle à utiliser spécifié.\n"); + return 1; + } + print_poids_ker_cnn(modele); + return 0; + } + printf("Option choisie non reconnue: %s\n", argv[1]); + help(argv[0]); + return 1; +} \ No newline at end of file