Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
i-evi committed Aug 6, 2020
1 parent 11f99bd commit 2e2115c
Show file tree
Hide file tree
Showing 10 changed files with 1,135 additions and 202 deletions.
54 changes: 47 additions & 7 deletions demo/lenet.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "parg.h"
#include "catcoon.h"

#define INPUT_W 28
Expand All @@ -16,7 +18,48 @@ const char *parameters_files[]={
"fc2_w.bin",
"fc2_b.bin",
};
const char *parameters_path = "/home/evi/catcoon-pytorch-model/lenet/dump";

char image_path[128];
char parameters_path[128];

void arg_parser(int argc, char* const argv[])
{
struct parg_state ps;
int c;
parg_init(&ps);
while ((c = parg_getopt(&ps, argc, argv, "hp:v")) != -1) {
switch (c) {
case 1:
strcpy(image_path, ps.optarg);
break;
case 'h':
printf("Usage: [-h] -p parameters-path image-file\n");
printf("-h: Displays this message\n");
printf("-p: Choose parameters file path\n");
exit(EXIT_SUCCESS);
break;
case 'p':
strcpy(parameters_path, ps.optarg);
break;
case '?':
if (ps.optopt == 'p') {
printf("option -p requires an argument\n");
} else {
printf("unknown option -%c\n", ps.optopt);
}
exit(EXIT_FAILURE);
break;
default:
printf("error: unhandled option -%c\n", c);
exit(EXIT_FAILURE);
break;
}
}
if (!strlen(image_path) || !strlen(parameters_path)) {
printf("error: incomplete argument\n");
exit(EXIT_FAILURE);
}
}

int main(int argc, const char *argv[])
{
Expand All @@ -40,12 +83,9 @@ int main(int argc, const char *argv[])
cc_int32 shape_fc2_w[] = {1, 1, 128, 10, 0};
cc_int32 shape_fc2_b[] = {10, 0};

if (argc < 2) {
fprintf(stderr, "usage: lenet [filename]\n");
exit(255);
}
arg_parser(argc, (char**)argv);

img_read = utim_read(argv[1]);
img_read = utim_read(image_path);
utim_img2gray(img_read);
img = utim_resize(img_read, INPUT_H, INPUT_W, 0);
input = cc_image2tensor(img, "input");
Expand Down Expand Up @@ -129,7 +169,7 @@ int main(int argc, const char *argv[])
}
printf("[%d]: %f\n", i, *((cc_float32*)l4->data + i));
}
printf("Result of \"%s\": [%d]\n", argv[1], j);
printf("Result of \"%s\": [%d]\n", image_path, j);
cc_tsrmgr_list();
cc_clear();
return 0;
Expand Down
49 changes: 47 additions & 2 deletions demo/lenet_pack.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "parg.h"
#include "catcoon.h"

#define INPUT_W 28
Expand All @@ -16,8 +18,49 @@ const char *parameters_files[]={
"fc2_w.bin",
"fc2_b.bin",
};
const char *parameters_path = "/home/evi/catcoon-pytorch-model/lenet/dump";
const char *parameters_pack = "lenet_parameters.bin";

char parameters_path[128];
char parameters_pack[128];

void arg_parser(int argc, char* const argv[])
{
struct parg_state ps;
int c;
parg_init(&ps);
while ((c = parg_getopt(&ps, argc, argv, "hp:o:v")) != -1) {
switch (c) {
case 'h':
printf("Usage: [-h] -p parameters-path -o pack-name\n");
printf("-h: Displays this message\n");
printf("-p: Choose parameters file path\n");
printf("-o: Output file name\n");
exit(EXIT_SUCCESS);
break;
case 'p':
strcpy(parameters_path, ps.optarg);
break;
case 'o':
strcpy(parameters_pack, ps.optarg);
break;
case '?':
if (ps.optopt == 'p') {
printf("option -p requires an argument\n");
} else {
printf("unknown option -%c\n", ps.optopt);
}
exit(EXIT_FAILURE);
break;
default:
printf("error: unhandled option -%c\n", c);
exit(EXIT_FAILURE);
break;
}
}
if (!strlen(parameters_path) || !strlen(parameters_pack)) {
printf("error: incomplete argument\n");
exit(EXIT_FAILURE);
}
}

int main(int argc, const char *argv[])
{
Expand All @@ -32,6 +75,8 @@ int main(int argc, const char *argv[])
cc_int32 shape_fc2_w[] = {1, 1, 128, 10, 0};
cc_int32 shape_fc2_b[] = {10, 0};

arg_parser(argc, (char**)argv);

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
cc_load_bin(filepath,
Expand Down
53 changes: 46 additions & 7 deletions demo/lenet_unpack.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "parg.h"
#include "catcoon.h"

#define INPUT_W 28
#define INPUT_H 28

const char *parameters_path = "./lenet_parameters.bin";
char image_path[128];
char parameters_path[128];

void arg_parser(int argc, char* const argv[])
{
struct parg_state ps;
int c;
parg_init(&ps);
while ((c = parg_getopt(&ps, argc, argv, "hp:v")) != -1) {
switch (c) {
case 1:
strcpy(image_path, ps.optarg);
break;
case 'h':
printf("Usage: [-h] -p parameters-pack image-file\n");
printf("-h: Displays this message\n");
printf("-p: Choose parameters file path\n");
exit(EXIT_SUCCESS);
break;
case 'p':
strcpy(parameters_path, ps.optarg);
break;
case '?':
if (ps.optopt == 'p') {
printf("option -p requires an argument\n");
} else {
printf("unknown option -%c\n", ps.optopt);
}
exit(EXIT_FAILURE);
break;
default:
printf("error: unhandled option -%c\n", c);
exit(EXIT_FAILURE);
break;
}
}
if (!strlen(image_path) || !strlen(parameters_path)) {
printf("error: incomplete argument\n");
exit(EXIT_FAILURE);
}
}

int main(int argc, const char *argv[])
{
Expand All @@ -21,10 +63,7 @@ int main(int argc, const char *argv[])
*conv2_w, *conv2_b, *fc1_w, *fc1_b, *fc2_w, *fc2_b;
cc_int32 shape_flat[] = {-1, 1, 1, 0};

if (argc < 2) {
fprintf(stderr, "usage: lenet [filename]\n");
exit(255);
}
arg_parser(argc, (char**)argv);

/* load parameters */
cc_tsrmgr_import(parameters_path);
Expand All @@ -37,7 +76,7 @@ int main(int argc, const char *argv[])
fc2_w = cc_tsrmgr_get("fc2_w");
fc2_b = cc_tsrmgr_get("fc2_b");

img_read = utim_read(argv[1]);
img_read = utim_read(image_path);
utim_img2gray(img_read);
img = utim_resize(img_read, INPUT_H, INPUT_W, 0);
input = cc_image2tensor(img, "input");
Expand Down Expand Up @@ -78,7 +117,7 @@ int main(int argc, const char *argv[])
}
printf("[%d]: %f\n", i, *((cc_float32*)l4->data + i));
}
printf("Result of \"%s\": [%d]\n", argv[1], j);
printf("Result of \"%s\": [%d]\n", image_path, j);
cc_tsrmgr_list();
cc_clear();
return 0;
Expand Down
60 changes: 39 additions & 21 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
CC = gcc
cc = gcc

# Build Ctrl Flag
BCTRL =

INC += -I ./src/
LINK += -lm

# Debug flag, cc_assert
DFLAG += -DENABLE_CC_ASSERT
# AddressSanitizer for gcc/clang
Expand All @@ -22,15 +25,19 @@ CFLAG += $(DFLAG) $(WFLAG) $(OFLAG)
CFLAG += -DAUTO_TSRMGR

# 3rd party source/library configurations
# stb for jpg, png, tga format images,
# will only support bmp image if disabled
# stb: read/write jpg, png, tga format images.
# Will only support bmp image if disabled
3RDSRC_CF += stb

# parg: argv parser written in ANSI C
# Argv parser for demo apps
3RDSRC_CF += parg

ifneq ($(findstring MINI, $(BCTRL)),)
CC = tcc
CFLAG = -std=c89
CFLAG += -DAUTO_TSRMGR
3RDSRC_CF =
3RDSRC_CF = parg
endif

ifneq ($(findstring -std=c89, $(CFLAG)),)
Expand All @@ -40,16 +47,26 @@ ifneq ($(findstring -std=c89, $(CFLAG)),)
endif
endif

LINK += -lm
INC += -I ./src/
ALL_O = catcoon.o cc_tensor.o cc_dtype.o cc_tsrmgr.o cc_fmap2d.o cc_pool2d.o \
cc_array.o cc_basic.o cc_actfn.o cc_fullycon.o cc_pad2d.o cc_cpufn.o \
cc_conv2d.o cc_normfn.o cc_image.o util_rbt.o util_list.o util_log.o \
util_image.o global_fn_cfg.o
ifneq ($(findstring stb, $(3RDSRC_CF)),)
UTIM_COND += -DUSE3RD_STB_IMAGE -I ./src/3rd_party/stb/
endif

ifneq ($(findstring parg, $(3RDSRC_CF)),)
ALL_O += parg.o
APP_INC += -I ./src/3rd_party/parg/
endif

ALL_O += \
catcoon.o cc_tensor.o cc_dtype.o cc_tsrmgr.o cc_fmap2d.o cc_pool2d.o \
cc_array.o cc_basic.o cc_actfn.o cc_fullycon.o cc_pad2d.o cc_cpufn.o \
cc_conv2d.o cc_normfn.o cc_image.o util_rbt.o util_list.o util_log.o \
util_image.o global_fn_cfg.o

CATCOON_A = libcatcoon.a

APP_NAMES = simple lenet lenet_pack lenet_unpack
APP_NAMES = simple lenet lenet_pack lenet_unpack
APP_INC += $(INC)
APP_LINK += $(LINK)

ifeq ($(OS),Windows_NT)
RM = del
Expand All @@ -63,12 +80,13 @@ all: $(APPS) # $(CATCOON_A)

%.o: ./src/%.c
$(CC) -c -o $@ $< $(CFLAG) $(INC)
# Targets For Linux

# Apps For Linux
%: ./demo/%.c $(ALL_O)
$(CC) -o $@ $< $(ALL_O) $(CFLAG) $(INC) $(LINK)
# Targets For Windows
$(CC) -o $@ $< $(ALL_O) $(CFLAG) $(APP_INC) $(APP_LINK)
# Apps For Windows
%.exe: ./demo/%.c $(ALL_O)
$(CC) -o $@ $< $(ALL_O) $(CFLAG) $(INC) $(LINK)
$(CC) -o $@ $< $(ALL_O) $(CFLAG) $(APP_INC) $(APP_LINK)

global_fn_cfg.o : $(patsubst %, ./src/%, global_fn_cfg.h global_fn_cfg.c)

Expand All @@ -90,15 +108,15 @@ cc_tensor.o : $(patsubst %, ./src/%, cc_tensor.h cc_tensor.c)

util_log.o : $(patsubst %, ./src/%, util_log.h util_log.c)
util_rbt.o : $(patsubst %, ./src/%, util_rbt.h util_rbt.c)
util_list.o : ./src/util_list.h ./src/util_list.c
util_list.o : $(patsubst %, ./src/%, util_list.h util_list.c)
$(CC) -c -o $@ ./src/util_list.c $(CFLAG) -DENABLE_FOPS
UTIM_COND =
ifneq ($(findstring stb, $(3RDSRC_CF)),)
UTIM_COND += -DUSE3RD_STB_IMAGE -I ./src/3rd_party/stb/
endif
util_image.o : ./src/util_image.h ./src/util_image.c
util_image.o : $(patsubst %, ./src/%, util_image.h util_image.c)
$(CC) -c -o $@ ./src/util_image.c $(CFLAG) $(UTIM_COND)

# 3rd party objs
parg.o: ./src/3rd_party/parg/parg*
$(CC) -c -o $@ ./src/3rd_party/parg/parg.c $(CFLAG)

minimal:
$(MAKE) "BCTRL = MINI"

Expand Down
Loading

0 comments on commit 2e2115c

Please sign in to comment.