majianjia / nnom

A higher-level Neural Network library for microcontrollers.
Apache License 2.0
909 stars 245 forks source link

conv2d 1*1 with strides!=1 and cmsis-nn #84

Open BaptisteNguyen opened 4 years ago

BaptisteNguyen commented 4 years ago

Hello, I was trying to use a model wide-resnet (an implementation of the model resnet_v1 in https://keras.io/zh/examples/cifar10_resnet/ with the parameter n=1 and num_filters = 8). I was using the version of nnom with cmsis-nn. When i run the model (model_run(model);), it returns me an error NN_SIZE_MISMATCH. Using the debugger, i have found the issue. In the function nnom_status_t conv2d_run(nnom_layer_t *layer) of the file nnom_conv2d.c, the function arm_convolve_1x1_HWC_q7_fast_nonsquare is used. This function returns NN_SIZE_MISMATCH if the strides parameters are not equal to 1 (also if the pad parameters are not equals to 0 or the kernel size is not equal to 1x1). CMSIS-NN doesn't support 1x1 convolution with strides!=1. I suggest to modify the line of codes 293 of nnom_conv2d.c : if (cl->kernel.w == 1 && cl->kernel.h == 1) by: if (cl->kernel.w == 1 && cl->kernel.h == 1 && cl->stride.w == 1 && cl->stride.h == 1 && cl->pad.w == 0 && cl->pad.h == 0) in order to solve this issue.

majianjia commented 4 years ago

Hello, I was trying to use a model wide-resnet (an implementation of the model resnet_v1 in https://keras.io/zh/examples/cifar10_resnet/ with the parameter n=1 and num_filters = 8). I was using the version of nnom with cmsis-nn. When i run the model (model_run(model);), it returns me an error NN_SIZE_MISMATCH. Using the debugger, i have found the issue. In the function nnom_status_t conv2d_run(nnom_layer_t *layer) of the file nnom_conv2d.c, the function arm_convolve_1x1_HWC_q7_fast_nonsquare is used. This function returns NN_SIZE_MISMATCH if the strides parameters are not equal to 1 (also if the pad parameters are not equals to 0 or the kernel size is not equal to 1x1). CMSIS-NN doesn't support 1x1 convolution with strides!=1. I suggest to modify the line of codes 293 of nnom_conv2d.c : if (cl->kernel.w == 1 && cl->kernel.h == 1) by: if (cl->kernel.w == 1 && cl->kernel.h == 1 && cl->stride.w == 1 && cl->stride.h == 1 && cl->pad.w == 0 && cl->pad.h == 0) in order to solve this issue.

You are right! Thanks for digging into the problem and provided the solution. I will fix this bug in next version, or if you don't mind, you could PR and I will merge it.
Thanks Jianjia