I am wondering if an already trained convolutional neural network can be represented as a formula just like a perceptron can ($x_1w_1 + x_2w_2 + \ldots =$ PREDICTION). I know that the formula could be a lot more complicated like this, but in general would it be possible to train a CNN in Python or Matlab, get the weights and create an arithmetic, exponential, whatever formula made with those acquired weights?

I want to do this because I am trying classify in a PIC32 which I think does not have the capacity to perform a CNN within itself.

Or do you know if there is a "c library" that can do CNNs?


Implementing any of this from scratch is not for the faint of heart...

Well, I am not familiar with PIC32, but you can see a CNN as...

How to implement:

Simplified CNN Flowchart, click to see in Mermaid

Where I*CKN detones the convolution of the Convolution Kernel 1 and the input I $$I \circledast CK_N $$ Which can be implemented as $$ F\{I\} \times F\{CK_N\} $$ where $F\{ . \}$ denotes the 2D Fourier Transform which can be computed fast by the FFT2 function, FFT2 is just FFT in 2 axis and the transformation is separable so you can implement it with FFT, PIC32 has FFT implementations such as the ones is this forum. This is really fast for BIG KERNELS, for smaller kernels you should use the 2D convolution. That is simply: 2D Convolution Where $f$ would be your image and $g$ your convolution kernel.

The convolution can be seems as $$I \circledast CK_N = <B[x,y],H>_{dot}$$ where $B[x,y]$ is a flatted version of the a block of I centered in $(x,y)$ and $H$ is the flipped convolution kernel. This implementation is faster than FFT if you have a set of points $(x_i,y_i) \forall i \in [1,2,...,N]$ where $N$ is considerably smaller than the size of the images.

If your images are really small you can try doing it, I don't think there is a implementation for this in PIC32 or even if the processor is fast enough (it implements a MIPS instruction set at 80MHz, am I correct?) to do this in real time.

Note: a simple NN computational complexity is already huge for a punny PIC32.

On a signal processing mindset

Since you working with microcontrollers I will assumed you know a thing or two about Digital Signal Processing...

Well, you can see Convolution Kernel as 2-Dimensional FIR Filters and this probably has implementations for doing this on any significant microcontroller in the market.

Note on floats and integers:

Float processing is really slow on this kind of processors (any actually), you would take advantage of using integers for your CNN and Image. This can be done without much loss on accuracy and TensorFlow has many quantizing algorithms for their models.

I used a quantized version of a Caffe's YOLO face detector without much harm to accuracy.

I think it is easier to go another way...

Relatively cheap solutions for this lack of processing power (and also REALLY easier to implement) would be to use another processing unit such as Intel Movidius or their Neural Computing Stick (the version 2 has 100 GFLOPS of performance). Intel NCS can be programmed using OpenVINO and has suport to TensorFlow and Caffe NNs.

C++ Implementations of (C)NN:

Also, you can use OpenCV DNN that has implementations in Python, Java and C to load and run CNNs from Caffe,Darknet,Tensorflow and Torch. And then you can try using their implementation as a base, the source code can be found in here.


What you are looking for is often called an inference engine. Tensorflow Lite for microcontrollers was just announced. I have not seen anyone use it on PIC32, but it should be possible to port it to that platform.

For ARM Cortex-M microcontrollers there are a couple more options, for instance uTensor and CMSIS-NN. The latter is low level implementation using integer math only, which could be a good inspiration if your microcontroller does not have hardware support for floating point.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.