From ecbee8b24cc48fcda24428cbbcaf714346942570 Mon Sep 17 00:00:00 2001 From: xinyang Date: Thu, 25 Apr 2019 12:39:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E7=BB=93=E6=9E=84=EF=BC=8C=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0CNN=E8=AE=AD=E7=BB=83=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +- CMakeLists.txt | 14 +- .../armor_finder/classifier/classifier.h | 3 +- .../armor_finder/classifier/classifier.cpp | 35 ++++- main.cpp | 16 +-- .../include}/camera/camera_api.h | 0 .../include}/camera/camera_define.h | 0 .../include}/camera/camera_status.h | 0 .../include}/camera/camera_wrapper.h | 0 .../include}/camera/video_wrapper.h | 0 .../include}/camera/wrapper_head.h | 0 {include => others/include}/log.h | 0 .../include}/options/additions.h | 0 {include => others/include}/options/options.h | 0 {include => others/include}/uart/uart.h | 0 libMVSDK.so => others/libMVSDK.so | Bin {src => others/src}/camera/camera_wrapper.cpp | 0 {src => others/src}/camera/video_wrapper.cpp | 0 {src => others/src}/options/additions.cpp | 0 {src => others/src}/options/options.cpp | 0 {src => others/src}/uart/uart.cpp | 0 tools/TrainCNN/backward.py | 123 ++++++++++++++++++ tools/TrainCNN/forward.py | 98 ++++++++++++++ tools/TrainCNN/generate.py | 49 +++++++ monitor.sh => tools/monitor.sh | 0 25 files changed, 325 insertions(+), 19 deletions(-) rename {include => others/include}/camera/camera_api.h (100%) rename {include => others/include}/camera/camera_define.h (100%) rename {include => others/include}/camera/camera_status.h (100%) rename {include => others/include}/camera/camera_wrapper.h (100%) rename {include => others/include}/camera/video_wrapper.h (100%) rename {include => others/include}/camera/wrapper_head.h (100%) rename {include => others/include}/log.h (100%) rename {include => others/include}/options/additions.h (100%) rename {include => others/include}/options/options.h (100%) rename {include => others/include}/uart/uart.h (100%) rename libMVSDK.so => others/libMVSDK.so (100%) rename {src => others/src}/camera/camera_wrapper.cpp (100%) rename {src => others/src}/camera/video_wrapper.cpp (100%) rename {src => others/src}/options/additions.cpp (100%) rename {src => others/src}/options/options.cpp (100%) rename {src => others/src}/uart/uart.cpp (100%) create mode 100644 tools/TrainCNN/backward.py create mode 100644 tools/TrainCNN/forward.py create mode 100644 tools/TrainCNN/generate.py rename monitor.sh => tools/monitor.sh (100%) diff --git a/.gitignore b/.gitignore index 9ae13e4..69211d5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -cmake-build-debug/* -.idea/* +cmake-build-debug +.idea +tools/TrainCNN/.idea +tools/TrainCNN/__pycache__ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 99a0232..338501e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) -project(auto-aim) +project(AutoAim) set(CMAKE_CXX_STANDARD 11) SET(CMAKE_BUILD_TYPE RELEASE) @@ -18,11 +18,17 @@ FIND_PACKAGE(Threads) include_directories( ${EIGEN3_INCLUDE_DIR} ) include_directories( ${PROJECT_SOURCE_DIR}/energy/include ) include_directories( ${PROJECT_SOURCE_DIR}/armor/include ) -include_directories( ${PROJECT_SOURCE_DIR}/include ) +include_directories( ${PROJECT_SOURCE_DIR}/others/include ) -FILE(GLOB_RECURSE sourcefiles "src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp") +FILE(GLOB_RECURSE sourcefiles "others/src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp") add_executable(run main.cpp ${sourcefiles} ) TARGET_LINK_LIBRARIES(run ${CMAKE_THREAD_LIBS_INIT}) TARGET_LINK_LIBRARIES(run ${OpenCV_LIBS}) -TARGET_LINK_LIBRARIES(run ${PROJECT_SOURCE_DIR}/libMVSDK.so) +TARGET_LINK_LIBRARIES(run ${PROJECT_SOURCE_DIR}/others/libMVSDK.so) + +# Todo +# ADD_CUSTOM_TARGET(bind-monitor COMMAND "") + +# Todo +# ADD_CUSTOM_TARGET(train COMMAND "") \ No newline at end of file diff --git a/armor/include/armor_finder/classifier/classifier.h b/armor/include/armor_finder/classifier/classifier.h index 57b3bc6..baca8f6 100644 --- a/armor/include/armor_finder/classifier/classifier.h +++ b/armor/include/armor_finder/classifier/classifier.h @@ -32,7 +32,8 @@ private: MatrixXd relu(const MatrixXd &input); vector> apply_bias(const vector> &input, const vector &bias); vector> relu(const vector> &input); - vector> pool(const vector> &input, int size); + vector> max_pool(const vector> &input, int size); + vector> mean_pool(const vector> &input, int size); vector> pand(const vector> &input, int val); MatrixXd conv(const MatrixXd &filter, const MatrixXd &input); vector> conv2(const vector> &filter, const vector> &input); diff --git a/armor/src/armor_finder/classifier/classifier.cpp b/armor/src/armor_finder/classifier/classifier.cpp index 2c11ea5..c998116 100644 --- a/armor/src/armor_finder/classifier/classifier.cpp +++ b/armor/src/armor_finder/classifier/classifier.cpp @@ -2,6 +2,7 @@ // Created by xinyang on 19-4-19. // +//#define LOG_LEVEL LOG_NONE #include #include #include @@ -91,7 +92,33 @@ MatrixXd Classifier::softmax(const MatrixXd &input){ return tmp.array().exp() / tmp.array().exp().sum(); } -vector> Classifier::pool(const vector> &input, int size){ +vector> max_pool(const vector> &input, int size){ + vector> output; + for(int sample=0; sample sub; + for (int channel = 0; channel < input[0].size(); channel++) { + MatrixXd tmp(input[0][0].rows() / size, input[0][0].cols() / size); + for (int row = 0; row < input[0][0].rows() / size; row++) { + for (int col = 0; col < input[0][0].cols() / size; col++) { + double max = 0; + for (int x = 0; x < size; x++) { + for (int y = 0; y < size; y++) { + if(max < input[sample][channel](row * size + x, col * size + y)){ + max = input[sample][channel](row * size + x, col * size + y); + } + } + } + tmp(row, col) = max; + } + } + sub.emplace_back(tmp); + } + output.emplace_back(sub); + } + return output; +} + +vector> Classifier::mean_pool(const vector> &input, int size){ vector> output; for(int sample=0; sample sub; @@ -240,9 +267,9 @@ Classifier::Classifier(const string &folder) : state(true){ MatrixXd Classifier::calculate(const vector> &input) { vector> conv1_result = relu(apply_bias(conv2(conv1_w, input), conv1_b)); - vector> pool1_result = pool(conv1_result, 2); + vector> pool1_result = mean_pool(conv1_result, 2); vector> conv2_result = relu(apply_bias(conv2(conv2_w, pool1_result), conv2_b)); - vector> pool2_result = pool(conv2_result, 2); + vector> pool2_result = mean_pool(conv2_result, 2); MatrixXd flattened = flatten(pool2_result); MatrixXd y1 = fc1_w * flattened; y1.colwise() += fc1_b; @@ -260,7 +287,7 @@ Classifier::operator bool() const { int Classifier::operator()(const cv::Mat &image) { MatrixXd x; cv2eigen(image, x); - x /= 255; + x /= 255.0; vector sub = {x}; vector> in = {sub}; MatrixXd result = calculate(in); diff --git a/main.cpp b/main.cpp index 1bf0f83..becb9cd 100644 --- a/main.cpp +++ b/main.cpp @@ -4,14 +4,14 @@ #include #include -#include "energy/energy.h" -#include "uart/uart.h" -#include "energy/param_struct_define.h" -#include "energy/constant.h" -#include "camera/camera_wrapper.h" -#include "camera/video_wrapper.h" -#include "camera/wrapper_head.h" -#include "armor_finder/armor_finder.h" +#include +#include +#include +#include +#include +#include +#include +#include #include #include diff --git a/include/camera/camera_api.h b/others/include/camera/camera_api.h similarity index 100% rename from include/camera/camera_api.h rename to others/include/camera/camera_api.h diff --git a/include/camera/camera_define.h b/others/include/camera/camera_define.h similarity index 100% rename from include/camera/camera_define.h rename to others/include/camera/camera_define.h diff --git a/include/camera/camera_status.h b/others/include/camera/camera_status.h similarity index 100% rename from include/camera/camera_status.h rename to others/include/camera/camera_status.h diff --git a/include/camera/camera_wrapper.h b/others/include/camera/camera_wrapper.h similarity index 100% rename from include/camera/camera_wrapper.h rename to others/include/camera/camera_wrapper.h diff --git a/include/camera/video_wrapper.h b/others/include/camera/video_wrapper.h similarity index 100% rename from include/camera/video_wrapper.h rename to others/include/camera/video_wrapper.h diff --git a/include/camera/wrapper_head.h b/others/include/camera/wrapper_head.h similarity index 100% rename from include/camera/wrapper_head.h rename to others/include/camera/wrapper_head.h diff --git a/include/log.h b/others/include/log.h similarity index 100% rename from include/log.h rename to others/include/log.h diff --git a/include/options/additions.h b/others/include/options/additions.h similarity index 100% rename from include/options/additions.h rename to others/include/options/additions.h diff --git a/include/options/options.h b/others/include/options/options.h similarity index 100% rename from include/options/options.h rename to others/include/options/options.h diff --git a/include/uart/uart.h b/others/include/uart/uart.h similarity index 100% rename from include/uart/uart.h rename to others/include/uart/uart.h diff --git a/libMVSDK.so b/others/libMVSDK.so similarity index 100% rename from libMVSDK.so rename to others/libMVSDK.so diff --git a/src/camera/camera_wrapper.cpp b/others/src/camera/camera_wrapper.cpp similarity index 100% rename from src/camera/camera_wrapper.cpp rename to others/src/camera/camera_wrapper.cpp diff --git a/src/camera/video_wrapper.cpp b/others/src/camera/video_wrapper.cpp similarity index 100% rename from src/camera/video_wrapper.cpp rename to others/src/camera/video_wrapper.cpp diff --git a/src/options/additions.cpp b/others/src/options/additions.cpp similarity index 100% rename from src/options/additions.cpp rename to others/src/options/additions.cpp diff --git a/src/options/options.cpp b/others/src/options/options.cpp similarity index 100% rename from src/options/options.cpp rename to others/src/options/options.cpp diff --git a/src/uart/uart.cpp b/others/src/uart/uart.cpp similarity index 100% rename from src/uart/uart.cpp rename to others/src/uart/uart.cpp diff --git a/tools/TrainCNN/backward.py b/tools/TrainCNN/backward.py new file mode 100644 index 0000000..9cf7f44 --- /dev/null +++ b/tools/TrainCNN/backward.py @@ -0,0 +1,123 @@ +import tensorflow as tf +from progressive.bar import Bar +import generate +import forward + + +def save_kernal(fp, val): + print(val.shape[2], file=fp) + print(val.shape[3], file=fp) + print(val.shape[1], file=fp) + print(val.shape[0], file=fp) + for in_channel in range(val.shape[2]): + for out_channel in range(val.shape[3]): + for row in range(val.shape[0]): + for col in range(val.shape[1]): + print(val[row][col][in_channel][out_channel], file=fp) + + +def save_weight_mat(fp, val): + print(val.shape[0], file=fp) + print(val.shape[1], file=fp) + for row in range(val.shape[0]): + for col in range(val.shape[1]): + print(val[row][col], file=fp) + + +def save_bias(fp, val): + print(val.shape[0], file=fp) + for i in range(val.shape[0]): + print(val[i], file=fp) + + +def save_para(folder, paras): + with open(folder + "/conv1_w", "w") as fp: + save_kernal(fp, paras[0]) + with open(folder + "/conv1_b", "w") as fp: + save_bias(fp, paras[1]) + with open(folder + "/conv2_w", "w") as fp: + save_kernal(fp, paras[2]) + with open(folder + "/conv2_b", "w") as fp: + save_bias(fp, paras[3]) + with open(folder + "/fc1_w", "w") as fp: + save_weight_mat(fp, paras[4]) + with open(folder + "/fc1_b", "w") as fp: + save_bias(fp, paras[5]) + with open(folder + "/fc2_w", "w") as fp: + save_weight_mat(fp, paras[6]) + with open(folder + "/fc2_b", "w") as fp: + save_bias(fp, paras[7]) + + +STEPS = 30000 +BATCH = 10 +LEARNING_RATE_BASE = 0.01 +LEARNING_RATE_DECAY = 0.99 +MOVING_AVERAGE_DECAY = 0.99 + + +def train(dataset, show_bar=False): + test_images, test_labels = dataset.all_test_sets() + + x = tf.placeholder(tf.float32, [None, forward.SRC_ROWS, forward.SRC_COLS, forward.SRC_CHANNELS]) + y_= tf.placeholder(tf.float32, [None, forward.OUTPUT_NODES]) + nodes, vars = forward.forward(0.001) + y = nodes[-1] + + ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) + cem = tf.reduce_mean(ce) + loss= cem + tf.add_n(tf.get_collection("losses")) + + global_step = tf.Variable(0, trainable=False) + learning_rate = tf.train.exponential_decay( + LEARNING_RATE_BASE, + global_step, + len(dataset.train_sets) / BATCH, + LEARNING_RATE_DECAY, + staircase=False) + train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step) + + ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) + ema_op = ema.apply(tf.trainable_variables()) + with tf.control_dependencies([train_step, ema_op]): + train_op = tf.no_op(name='train') + + correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + acc = 0 + + with tf.Session() as sess: + init_op = tf.global_variables_initializer() + sess.run(init_op) + + if show_bar: + bar = Bar(max_value=STEPS, width=u'50%') + bar.cursor.clear_lines(1) + bar.cursor.save() + + for i in range(STEPS): + images_samples, labels_samples = dataset.sample_train_sets(BATCH) + + _, loss_value, step = sess.run( + [train_op, loss, global_step], + feed_dict={x: images_samples, y_: labels_samples} + ) + + if i % 100 == 0: + if i % 1000 == 0: + acc = sess.run(accuracy, feed_dict={x: test_images, y_: test_labels}) + + if show_bar: + bar.title = "step: %d, loss: %f, acc: %f" % (step, loss_value, acc) + bar.cursor.restore() + bar.draw(value=i+1) + + vars_val = sess.run(vars) + save_para("paras", vars_val) + # nodes_val = sess.run(nodes, feed_dict={x:test}) + # return vars_val, nodes_val + + +if __name__ == "__main__": + dataset = generate.DataSet("images") + train(dataset, show_bar=True) diff --git a/tools/TrainCNN/forward.py b/tools/TrainCNN/forward.py new file mode 100644 index 0000000..e1257c8 --- /dev/null +++ b/tools/TrainCNN/forward.py @@ -0,0 +1,98 @@ +import tensorflow as tf + + +def get_weight(shape, regularizer=None): + w = tf.Variable(tf.truncated_normal(shape, stddev=0.1)) + if regularizer is None: + tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w)) + return w + + +def get_bias(shape): + b = tf.Variable(tf.zeros(shape)) + return b + + +def conv2d(x, w): + return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding="VALID") + + +def avg_pool_2x2(x): + return tf.nn.avg_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID") + + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID") + + +# 原图像行数 +SRC_ROWS = 36 + +# 原图像列数 +SRC_COLS = 48 + +# 原图像通道数 +SRC_CHANNELS = 1 + +# 第一层卷积核大小 +CONV1_KERNAL_SIZE = 5 + +# 第一层卷积输出通道数 +CONV1_OUTPUT_CHANNELS = 8 + +# 第二层卷积核大小 +CONV2_KERNAL_SIZE = 3 + +# 第二层卷积输出通道数 +CONV2_OUTPUT_CHANNELS = 16 + +# 第一层全连接宽度 +FC1_OUTPUT_NODES = 32 + +# 第二层全连接宽度(输出标签类型数) +FC2_OUTPUT_NODES = 8 + +# 输出标签类型数 +OUTPUT_NODES = FC2_OUTPUT_NODES + + +def forward(x, regularizer=None): + vars = [] + nodes = [] + + conv1_w = get_weight( + [CONV1_KERNAL_SIZE, CONV1_KERNAL_SIZE, 1, CONV1_OUTPUT_CHANNELS] + ) + conv1_b = get_bias([CONV1_OUTPUT_CHANNELS]) + conv1 = tf.nn.relu(tf.nn.bias_add(conv2d(x, conv1_w), conv1_b)) + pool1 = avg_pool_2x2(conv1) + vars.extend([conv1_w, conv1_b]) + nodes.extend([conv1, pool1]) + + conv2_w = get_weight( + [CONV2_KERNAL_SIZE, CONV2_KERNAL_SIZE, CONV1_OUTPUT_CHANNELS, CONV2_OUTPUT_CHANNELS] + ) + conv2_b = get_bias([CONV2_OUTPUT_CHANNELS]) + conv2 = tf.nn.relu(tf.nn.bias_add(conv2d(pool1, conv2_w), conv2_b)) + pool2 = avg_pool_2x2(conv2) + vars.extend([conv2_w, conv2_b]) + nodes.extend([conv2, pool2]) + + pool_shape = pool2.get_shape().as_list() + node = pool_shape[1] * pool_shape[2] * pool_shape[3] + reshaped = tf.reshape(pool2, [-1, node]) + + fc1_w = get_weight([node, FC1_OUTPUT_NODES], regularizer) + fc1_b = get_bias([FC1_OUTPUT_NODES]) + fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b) + vars.extend([fc1_w, fc1_b]) + nodes.extend([fc1]) + + fc2_w = get_weight([FC1_OUTPUT_NODES, FC2_OUTPUT_NODES], regularizer) + fc2_b = get_bias([FC2_OUTPUT_NODES]) + fc2 = tf.nn.softmax(tf.matmul(fc1, fc2_w) + fc2_b) + vars.extend([fc2_w, fc2_b]) + nodes.extend([fc2]) + + return nodes, vars + diff --git a/tools/TrainCNN/generate.py b/tools/TrainCNN/generate.py new file mode 100644 index 0000000..e2153e2 --- /dev/null +++ b/tools/TrainCNN/generate.py @@ -0,0 +1,49 @@ +import numpy as np +import os +import cv2 +import random +from forward import OUTPUT_NODES + + +class DataSet: + def __init__(self, folder): + self.train_sets = [] + self.test_sets = [] + self.generate_data_sets(folder) + + def generate_data_sets(self, folder): + def file2nparray(name): + image = cv2.imread(name) + return image[:, :, 0] + + def id2label(id): + a = np.zeros([OUTPUT_NODES, 1]) + a[id] = 1 + return a[:] + + sets = [] + for i in range(OUTPUT_NODES): + dir = "%s/%d" % (folder, i) + files = os.listdir(dir) + for file in files: + sets.append([file2nparray("%s/%s" % (dir, file)), id2label(i)]) + sets = np.array(sets) + np.random.shuffle(sets) + length = len(sets) + self.train_sets = sets[:length*3//4] + self.test_sets = sets[length*3//4:] + + def sample_train_sets(self, length): + samples = [] + labels = [] + for i in range(length): + id = random.randint(0, length-1) + samples.append(self.train_sets[id][0]) + labels.append(self.train_sets[id][1]) + return np.array(samples), np.array(labels) + + def all_train_sets(self): + return self.train_sets[:, 0, :, :], self.train_sets[:, 1, :, :] + + def all_test_sets(self): + return self.test_sets[:, 0, :, :], self.test_sets[:, 1, :, :] diff --git a/monitor.sh b/tools/monitor.sh similarity index 100% rename from monitor.sh rename to tools/monitor.sh