# Kneron KL520 guide ###### tags: `Kneron` ## Customized Model on flash memory reference: document -> ***KL520 model on flash memory tutorial*** To run customized model and get output in KL520, you need follow these steps: (1) Compile models: get ```all_models.bin```, ```fw_info.bin``` (2) Convert image into ```.bin``` file (optional) (3) Generate flash bin file (4) Upload bin file into board flash memory (5) Allocate memory address for model output and input, load image from flash (6) Config model environment and model run (7) Checkout network output ### Convert model to loadable bin file We will download all_models.bin onto KL520 board. To convert model into bin file, **ToolChain** is required. * Before compile * Convert model into .onnx file * Configure batch_compile_input_params.json * Convert Model ```SHELL=1 # Copy model to toolchain cp (your_model) (PATH_to_workspace_root)/ToolChain # Launch docker cd ../ sh ./script_run_toolchain_docker.sh #### Docker #### python /workspace/onnx-keras/generate_onnx.py -o /data1/(output_name).onnx /data1/(your_model) -O -C --duplicate-shared-weights ``` * Fill the input parameters Fill the simulator and emulator input parameters in the ```/data1/batch_compile_input_params.json``` in Interactive Folder. ```json=1 #### (1) Example: tiny_yolo_v3 #### "input_image_folder": ["/data1/caffe/images"], "img_channel": ["RGB"], "model_input_width": [224], "model_input_height": [224], "img_preprocess_method": ["yolo"], "input_onnx_file": ["/data1/yolov3-tiny-224.h5.onnx"], "command_addr": "0x30000000", "weight_addr": "0x40000000", "sram_addr": "0x50000000", "dram_addr": "0x60000000", "whether_encryption": "No", "encryption_key": "0x12345678", "model_id_list": [19], "model_version_list": [1] ``` * Compile ```SHELL=1 #### Docker #### cd /workspace/scripts ./fpAnalyserBatchCompiler.sh.x exit #### Docker exit #### ``` * Output file * *all_models.bin* * *fw_info.bin* * *fw_info.txt* * *temp_0_ioinfo.csv* ```fw_info.txt``` list readable info of model id, version etc. ```all_model.bin``` and ```fw_info.bin``` is for firmware to use ```temp_X_ioinfo.csv``` contains the information that cpu node and output node. If you find the cpu node in ```temp_X_ioinfo.csv```, whose format is “c,**,**”, you need to implement and register this function in SDK. Put all your bin file into a folder and we can go to next step. ### Load model to KL520 with DME mode We need three bin file. * *all_models.bin* * *fw_info.bin* * *config.bin* ## ```flash_fdfr_image.bin``` FDFR api introduction ```kdp_start_verify_mode_thresh(int dev_idx, uint32_t* img_size, float thresh):``` * Description: * start the user verification mode with specified threshold * It is same as calling ```kdp_start_sfid_mode``` with image width ```640```, image height ```480```, image format ```RGB565``` ## General note ### Convert RGB565 binary file to normal MAT in OpenCV Python ```python=1 nparr = np.fromfile('u1_f1_rgb.bin', dtype=np.uint16) pixels = [] for i in nparr: mask1 = 0b1111100000000000 mask2 = 0b0000011111100000 mask3 = 0b0000000000011111 value1 = (mask1 & i)>>8 value2 = (mask2 & i)>>3 value3 = (mask3 & i)<<3 pixels.append([value3,value2,value1]) data = np.array(pixels) face_data = np.reshape(data, (480, 640, 3)) # Output ``` ### Convert normal MAT to RGB565 binary file in OpenCV c++ ```c=1 void convert_BGR565(cv::Mat frame, uchar *dst){ cv::Mat tmp; cv::resize(frame, tmp, cv::Size(IMAGE_W,IMAGE_H)); cv::cvtColor(tmp, tmp, cv::COLOR_BGR2BGR565); uchar *src = tmp.isContinuous()? tmp.data: tmp.clone().data; std::memcpy(dst,(void*)src,IMAGE_SIZE); } ```