// curve_conv_nn2_axis3.cpp
// 2017/09/09 by marsee
// 畳み込み層のカーネル数 2
// AXI4 Stream入力 番号出力
// 2017/09/18 : dot2[3]の出力も追加
// 2017/12/13 : 直線に加えてカーブのデータも使用して学習した
//
#include <ap_fixed.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "conv1_weight.h"
#include "conv1_bias.h"
#include "af1_weight.h"
#include "af1_bias.h"
#include "af2_weight.h"
#include "af2_bias.h"
#define REDUSED_ROW 45
#define REDUSED_COULMN 60
#define NUM_OF_KERNELS 2
#define COULMN_PIXELS 56
#define ROW_PIXELS 10
#define ALL_PIXELS 560
#define NUM_OF_OUTPUT 3
int max_ap_fixed(ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> out[NUM_OF_OUTPUT], ap_uint<2> &out_num);
int curve_conv_nn2_axis3(hls::stream<ap_axiu<32,1,1,1> >& ins, ap_uint<2> &outs,
ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> dot2[NUM_OF_OUTPUT]){
#pragma HLS INTERFACE s_axilite port=dot2
#pragma HLS INTERFACE s_axilite port=return
#pragma HLS INTERFACE s_axilite port=outs
#pragma HLS INTERFACE axis register both port=ins
ap_ufixed<8, 0, AP_TRN_ZERO, AP_SAT> buf[ROW_PIXELS][COULMN_PIXELS];
ap_fixed<13, 6, AP_TRN_ZERO, AP_SAT> conv_out[NUM_OF_KERNELS][ROW_PIXELS-4][COULMN_PIXELS-4];
ap_fixed<13, 6, AP_TRN_ZERO, AP_SAT> pool_out[NUM_OF_KERNELS][(ROW_PIXELS-4)/2][(COULMN_PIXELS-4)/2];
ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> dot1[100];
ap_axiu<32,1,1,1> pix;
do {
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
// user が 1になった時にフレームがスタートする
ins >> pix;
} while(pix.user == 0);
// 10 x 56 に整形
buf_copy1: for(int i=0; i<REDUSED_ROW; i++){
buf_copy2: for(int j=0; j<REDUSED_COULMN; j++){
if (!(i==0 && j==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
if((i>=33 && i<33+ROW_PIXELS) && (j>=2 && j<2+COULMN_PIXELS)){
buf[i-33][j-2] = (ap_ufixed<8, 0, AP_TRN_ZERO, AP_SAT>)((ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)(pix.data & 0xff) / 256);
}
}
}
// Convolutional Neural Network 5x5 kernel, Stride = 1, Padding = 0
// + ReLU
CONV1: for(int i=0; i<NUM_OF_KERNELS; i++){ // カーネルの個数
CONV2: for(int j=0; j<ROW_PIXELS-4; j++){
CONV3: for(int k=0; k<COULMN_PIXELS-4; k++){
conv_out[i][j][k] = 0;
CONV4: for(int m=0; m<5; m++){
CONV5: for(int n=0; n<5; n++){
conv_out[i][j][k] += buf[j+m][k+n] * conv1_weight[i][0][m][n];
}
}
conv_out[i][j][k] += conv1_bias[i];
if(conv_out[i][j][k]<0) // ReLU
conv_out[i][j][k] = 0;
}
}
}
// Pooling Kernel = 2 x 2, Stride = 2
POOL1: for(int i=0; i<NUM_OF_KERNELS; i++){
POOL2: for(int j=0; j<ROW_PIXELS-4; j += 2){
POOL3: for(int k=0; k<COULMN_PIXELS-4; k += 2){
POOL4: for(int m=0; m<2; m++){
POOL5: for(int n=0; n<2; n++){
if(m==0 && n==0){
pool_out[i][j/2][k/2] = conv_out[i][j][k];
} else if(pool_out[i][j/2][k/2] < conv_out[i][j+m][k+n]){
pool_out[i][j/2][k/2] = conv_out[i][j+m][k+n];
}
}
}
}
}
}
af1_dot1: for(int col=0; col<100; col++){
dot1[col] = 0;
af1_dot2: for(int i=0; i<NUM_OF_KERNELS; i++){
af1_dot3: for(int j=0; j<(ROW_PIXELS-4)/2; j++){
af1_dot4: for(int k=0; k<(COULMN_PIXELS-4)/2; k++){
dot1[col] += pool_out[i][j][k]*af1_weight[i*((ROW_PIXELS-4)/2)*((COULMN_PIXELS-4)/2)+j*((COULMN_PIXELS-4)/2)+k][col];
}
}
}
dot1[col] += af1_bias[col];
if(dot1[col] < 0) // ReLU
dot1[col] = 0;
}
af2_dot1: for(int col=0; col<NUM_OF_OUTPUT; col++){
dot2[col] = 0;
af2_dot2: for(int row=0; row<100; row++){
dot2[col] += dot1[row]*af2_weight[row][col];
}
dot2[col] += af2_bias[col];
}
max_ap_fixed(dot2, outs);
return(0);
}
int max_ap_fixed(ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> out[NUM_OF_OUTPUT], ap_uint<2> &out_num){
int max_id;
ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> max;
for(int i=0; i<NUM_OF_OUTPUT; i++){
if(i == 0){
max = out[0];
max_id = 0;
}else if(out[i]>max){
max = out[i];
max_id = i;
}
}
out_num = (ap_uint<2>)max_id;
return(0);
}
// curve_conv_nn2_axis3_tb.cpp
// 2017/09/09 by marsee
//
// 2017/09/18 : straight_conv_nn2_axis3.cpp に dot2[3]の出力も追加
// 2017/12/13 : 直線に加えてカーブのデータも使用して学習した
//
#include <iostream>
#include "hls_opencv.h"
#include "ap_axi_sdata.h"
#include "hls_video.h"
#define MAX_HEIGHT 600
#define MAX_WIDTH 800
typedef hls::stream<ap_axiu<32,1,1,1> > AXI_STREAM;
typedef hls::Mat<MAX_HEIGHT, MAX_WIDTH, HLS_8UC3> RGB_IMAGE;
typedef hls::Mat<MAX_HEIGHT, MAX_WIDTH, HLS_8UC1> GRAY_IMAGE;
using namespace cv;
#define NUM_OF_OUTPUT 3
//#define STRAIGHT_LOOP_COUNT 41 // train_images
//#define LR_LOOP_COUNT 18 // train_images
#define STRAIGHT_LOOP_COUNT 35 // test_images
#define LR_LOOP_COUNT 12 // test_images
//#define STRAIGHT_LOOP_COUNT 1 // for C/RTL Co-Simulation
//#define LR_LOOP_COUNT 1 // for C/RTL Co-Simulation
//#define STRAIGHT_IMAGE_NAME "train_images_171129/straight"
//#define LEFT_TURN_IMAGE_NAME "train_images_171129/left_turn"
//#define RIGHT_TURN_IMAGE_NAME "train_images_171129/right_turn"
#define STRAIGHT_IMAGE_NAME "test_images_171129/straight_test"
#define LEFT_TURN_IMAGE_NAME "test_images_171129/left_turn_test"
#define RIGHT_TURN_IMAGE_NAME "test_images_171129/right_turn_test"
int curve_conv_nn2_axis3(hls::stream<ap_axiu<32,1,1,1> >& ins, ap_uint<2> &outs,
ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> dot2[NUM_OF_OUTPUT]);
int resize_gray(AXI_STREAM& ins, AXI_STREAM& outs);
int main_output_loop(char *buf, int loop_count, int correct_data);
int main () {
char buf[200];
sprintf(buf, "%s", STRAIGHT_IMAGE_NAME);
main_output_loop(buf, STRAIGHT_LOOP_COUNT, 1);
sprintf(buf, "%s", LEFT_TURN_IMAGE_NAME);
main_output_loop(buf, LR_LOOP_COUNT, 0);
sprintf(buf, "%s", RIGHT_TURN_IMAGE_NAME);
main_output_loop(buf, LR_LOOP_COUNT, 2);
return(0);
}
int main_output_loop(char *buf, int loop_count, int correct_data){
char bmp_file_name[200];
ap_uint<2> outs;
AXI_STREAM src_axi, dst_axi;
Mat src;
ap_fixed<16, 7, AP_TRN_ZERO, AP_SAT> dot2[NUM_OF_OUTPUT];
int err_num = 0;
for(int i=0; i<loop_count; i++){
sprintf(bmp_file_name, "%s%d.bmp", buf, i);
// OpenCV で 画像を読み込む
src = imread(bmp_file_name);
// BGR から RGBへ変換
Mat src_rgb;
cvtColor(src, src_rgb, CV_BGR2RGB);
// Mat フォーマットから AXI4 Stream へ変換
cvMat2AXIvideo(src_rgb, src_axi);
// resize_gray() 関数をコール
resize_gray(src_axi, dst_axi);
curve_conv_nn2_axis3(dst_axi, outs, dot2);
if((int)outs != correct_data){
printf("*%s\n", bmp_file_name);
printf("correct data = %d, outs = %d\n", correct_data, (int)outs);
for(int i=0; i<NUM_OF_OUTPUT; i++)
printf("dot2[%d] = %f ", i, (float)dot2[i]);
printf("\n");
err_num++;
}
}
if(correct_data == 1)
printf("Straight error is %d\n\n", err_num);
else if(correct_data == 0)
printf("Left error is %d\n\n", err_num);
else // if(correct_data == 2)
printf("Right error is %d\n\n", err_num);
return(0);
}
int resize_gray(AXI_STREAM& ins, AXI_STREAM& outs){
RGB_IMAGE org_img(600, 800);
GRAY_IMAGE org_img_g(600, 800);
GRAY_IMAGE resize_img_g(45, 60);
RGB_IMAGE resize_img(45, 60);
hls::AXIvideo2Mat(ins, org_img);
hls::CvtColor<HLS_RGB2GRAY>(org_img, org_img_g);
hls::Resize(org_img_g, resize_img_g);
hls::CvtColor<HLS_GRAY2RGB>(resize_img_g, resize_img);
hls::Mat2AXIvideo(resize_img, outs);
return(0);
}
INFO: [SIM 2] *************** CSIM start ***************
INFO: [SIM 4] CSIM will launch GCC as the compiler.
Compiling ../../../curve_conv_nn2_axis3_tb.cpp in debug mode
Generating csim.exe
Straight error is 0
*test_images_171129/left_turn_test8.bmp
correct data = 0, outs = 1
dot2[0] = -0.435547 dot2[1] = 1.232422 dot2[2] = -4.697266
Left error is 1
Right error is 0
INFO: [SIM 1] CSim done with 0 errors.
INFO: [SIM 3] *************** CSIM finish ***************
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | - | - | - | - | - | - |