Call ncnn from Fortran via mix compiling.
How this demo works?
-
Image
As Fortran is mainly used in scientific computation, there seems to be no workable OpenCV binding for Fortran or other computer vision libraries. The repo ncnn_on_xr806 inspired me for the image was stored in a header file in the format of
const unsigned chararray andncnn::Mat::from_pixelsis ready for it. In this repo the image is stored in a ASCII-text file with 28 rows and 28 columns, it should be incsvortsvlike format. Here are some so-called images inimage/directory. -
Main Program
The main program
main.f90is written in Fortran, it parses command line arguments, loads image, call ncnn binding, and output the most possible label of input image. -
ncnn Binding
ncnn Binding inclueds 4 files:
ncnn_mnist.f90predict.hpredict.cppmnist-12-bin.h
ncnn_mnist.f90is the wrapper of functionpredict, it interacts withpredict.cppviaextern "C"inpredict.h.mnist-12-bin.his the model waits ncnn to load. Inpredict.cpp, functionpreductinitializes anncnn::Matfrom a 2d Fortran array, loads the model, and predicts the label. Finally, the predicted label is passed to main program.
Modify the CMakeLists.txt, make sure all paths of ncnn is correct for you.
Then
$ mkdir build
$ cmake ..
$ make
$ cd ..
$ ./build/mnist image/4.dat
Predict: 4
GNU and Intel compilers have been tested.