Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
i-evi committed May 22, 2020
0 parents commit 8ee8a85
Show file tree
Hide file tree
Showing 44 changed files with 15,546 additions and 0 deletions.
Binary file added catcoon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 136 additions & 0 deletions demo/lenet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <stdio.h>
#include <stdlib.h>

#include "catcoon.h"

#define INPUT_W 28
#define INPUT_H 28

const char *parameters_files[]={
"conv1_w.bin",
"conv1_b.bin",
"conv2_w.bin",
"conv2_b.bin",
"fc1_w.bin",
"fc1_b.bin",
"fc2_w.bin",
"fc2_b.bin",
};
const char *parameters_path = "/home/evi/catcoon-pytorch-model/lenet/dump";

int main(int argc, const char *argv[])
{
cc_int32 i, j;
cc_float32 v;
char filepath[256];
/* Image */
utim_image_t *img, *img_read;
/* Layers */
cc_tensor_t *input,*l1, *l1_pool, *l2, *l2_pool, *l2_flat, *l3, *l4;
/* Parameters */
cc_tensor_t *conv1_w, *conv1_b,
*conv2_w, *conv2_b, *fc1_w, *fc1_b, *fc2_w, *fc2_b;
cc_int32 shape_conv1_w[] = {5, 5, 1, 32, 0};
cc_int32 shape_conv1_b[] = {32, 0};
cc_int32 shape_conv2_w[] = {5, 5, 32, 64, 0};
cc_int32 shape_conv2_b[] = {64, 0};
cc_int32 shape_flat[] = {-1, 1, 1, 0};
cc_int32 shape_fc1_w[] = {1, 1, 3136, 128, 0};
cc_int32 shape_fc1_b[] = {128, 0};
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);
}

img_read = utim_read(argv[1]);
utim_img2gray(img_read);
img = utim_resize(img_read, INPUT_H, INPUT_W, 0);
input = cc_image2tensor(img, "input");
utim_free_image(img);
utim_free_image(img_read);
input = cc_cast_tensor(input, CC_FLOAT32, "input");
v = 255.;
input = cc_tensor_by_scalar(input, '/', &v, "input");

i = 0;
sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
conv1_w = cc_load_bin(filepath,
shape_conv1_w, CC_FLOAT32, "conv1_w");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
conv1_b = cc_load_bin(filepath,
shape_conv1_b, CC_FLOAT32, "conv1_b");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
conv2_w = cc_load_bin(filepath,
shape_conv2_w, CC_FLOAT32, "conv2_w");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
conv2_b = cc_load_bin(filepath,
shape_conv2_b, CC_FLOAT32, "conv2_b");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
fc1_w = cc_load_bin(filepath,
shape_fc1_w, CC_FLOAT32, "fc1_w");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
fc1_b = cc_load_bin(filepath,
shape_fc1_b, CC_FLOAT32, "fc1_b");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
fc2_w = cc_load_bin(filepath,
shape_fc2_w, CC_FLOAT32, "fc2_w");

sprintf(filepath, "%s/%s",
parameters_path, parameters_files[i++]);
fc2_b = cc_load_bin(filepath,
shape_fc2_b, CC_FLOAT32, "fc2_b");

l1 = cc_conv2d(input, conv1_w, conv1_b, 1, 2, 0, "l1_conv");

l1 = cc_relu(l1, NULL);

l1_pool = cc_max_pool2d(l1, 2, "l1_pool");

l2 = cc_conv2d(l1_pool, conv2_w, conv2_b, 1, 2, 0, "l2_conv");

l2 = cc_relu(l2, NULL);

l2_pool = cc_max_pool2d(l2, 2, "l2_pool");

/* l2_flat = cc_fmap2d_flat(l2_pool, "l2_flat"); */
l2_flat = cc_tensor_reshape(l2_pool, shape_flat);

/* l3 = cc_conv2d(l2_flat, fc1_w, fc1_b, 1, 0, 0, "fc1"); */
l3 = cc_fully_connected(l2_flat, fc1_w, fc1_b, "fc1");

l3 = cc_relu(l3, NULL);

/* l4 = cc_conv2d(l3, fc2_w, fc2_b, 1, 0, 0, "fc2"); */
l4 = cc_fully_connected(l3, fc2_w, fc2_b, "fc2");

l4 = cc_softmax(l4, NULL);

v = 0.;
for (i = 0, j = 0; i < 10; ++i) {
if (*((cc_float32*)l4->data + i) > v) {
v = *((cc_float32*)l4->data + i);
j = i;
}
printf("[%d]: %f\n", i, *((cc_float32*)l4->data + i));
}
printf("Result of \"%s\": [%d]\n", argv[1], j);
cc_tsrmgr_list();
cc_clear();
return 0;
}
11 changes: 11 additions & 0 deletions demo/simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "catcoon.h"

int main(int argc, const char *argv[])
{
cc_tensor_t *tensor;
cc_int32 shape[] = {3, 3, 3, 0};
tensor = cc_create_tensor(shape, CC_FLOAT32, "tensor0");
cc_print_tensor_property(tensor);
cc_free_tensor(tensor);
return 0;
}
89 changes: 89 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
cc = gcc

# Debug flag, cc_assert
DFLAG += -DENABLE_CC_ASSERT
# AddressSanitizer for gcc/clang
DFLAG += # -g -fsanitize=address -fno-omit-frame-pointer

CFLAG += # -std=c89
CFLAG += -Wall # -Wpedantic

OFLAG += -Os

# Enable OpenMP
OFLAG += -DENABLE_OPENMP -fopenmp

CFLAG += $(DFLAG) $(WFLAG) $(OFLAG)

# Enable automatic tensor manager
CFLAG += -DAUTO_TSRMGR

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

ifneq ($(findstring -std=c89, $(CFLAG)),)
CFLAG += -DCONFIG_STD_C89
ifneq ($(findstring $(cc), gcc clang),)
WFLAG += -Wno-long-long
endif
endif

LINK += -lm
INC += -I ./src/
ALL_O = catcoon.o cc_tensor.o cc_dtype.o cc_image.o cc_array.o cc_tsrmgr.o \
cc_basic.o cc_fmap2d.o cc_pad2d.o cc_conv2d.o cc_pool2d.o cc_cpufn.o \
cc_fullycon.o global_fn_cfg.o util_rbt.o util_list.o util_log.o \
util_image.o

APP_NAMES = simple lenet

ifeq ($(OS),Windows_NT)
RM = del
APPS = $(foreach v, $(APP_NAMES), $(v).exe)
else
RM = rm
APPS = $(APP_NAMES)
endif

all: $(APPS)

%.o: ./src/%.c
$(cc) -c -o $@ $< $(CFLAG) $(INC)
# APPS For Linux
%: ./demo/%.c $(ALL_O)
$(cc) -o $@ $< $(ALL_O) $(CFLAG) $(INC) $(LINK)
# APPS For Windows
%.exe: ./demo/%.c $(ALL_O)
$(cc) -o $@ $< $(ALL_O) $(CFLAG) $(INC) $(LINK)

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

catcoon.o : $(patsubst %, ./src/%, catcoon.h catcoon.c)
cc_array.o : $(patsubst %, ./src/%, cc_array.h cc_array.c)
cc_basic.o : $(patsubst %, ./src/%, cc_basic.h cc_basic.c)
cc_conv2d.o : $(patsubst %, ./src/%, cc_conv2d.h cc_conv2d.c)
cc_cpufn.o : $(patsubst %, ./src/%, cc_cpufn.h cc_cpufn.c)
cc_dtype.o : $(patsubst %, ./src/%, cc_dtype.h cc_dtype.c)
cc_fmap2d.o : $(patsubst %, ./src/%, cc_fmap2d.h cc_fmap2d.c)
cc_fullycon.o : $(patsubst %, ./src/%, cc_fullycon.h cc_fullycon.c)
cc_image.o : $(patsubst %, ./src/%, cc_image.h cc_image.c)
cc_pad2d.o : $(patsubst %, ./src/%, cc_pad2d.h cc_pad2d.c)
cc_pool2d.o : $(patsubst %, ./src/%, cc_pool2d.h cc_pool2d.c)
cc_tsrmgr.o : $(patsubst %, ./src/%, cc_tsrmgr.h cc_tsrmgr.c)
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
$(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
$(cc) -c -o $@ ./src/util_image.c $(CFLAG) $(UTIM_COND)

clean:
$(RM) *.o && $(RM) $(APPS)
Loading

0 comments on commit 8ee8a85

Please sign in to comment.