├── .gitignore ├── LICENSE ├── README.md ├── data.zip ├── include ├── convolution_layer.h ├── flow.h ├── fully_connected_layer.h ├── lazy_net.h ├── math_function.h ├── maxpool_layer.h ├── relu_layer.h ├── softmax_layer.h └── softmax_loss_layer.h ├── openblas.zip ├── opencv.zip ├── result.jpg └── src ├── convolution_layer.cpp ├── flow.cpp ├── fully_connected_layer.cpp ├── lazy_net.cpp ├── lazy_net_function.cpp ├── main.cpp ├── math_function.cpp ├── maxpool_layer.cpp ├── relu_layer.cpp ├── softmax_layer.cpp └── softmax_loss_layer.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LazyNet 2 | # 前言 3 | 很多小伙伴还在为弄不懂深度学习整个流程而烦恼,比如图像数据如何读入?数据在网络之间如何传递?如何前向计算?如何反向计算?如何更新权重?如何预测输出?博主利用每天晚上的时间,历经两个星期,为大家带来这款为理解深度学习而生的学习框架,命名为LazyNet,为什么呢?因为此框架真的很简单,动动手指头就能明白整个深度学习流程了! 4 | 5 | # LazyNet优势 6 | * 不依赖GPU,任何CPU均可 7 | * 除OpenBLAS、OpenCV外,不依赖任何第三方 8 | * 纯C++代码 9 | * 代码简介明了,数据流简单易懂 10 | * 相关原理在代码里面做了详细说明 11 | 12 | # LazyNet简介 13 | ## 一、支持系统 14 | * Windows7/10(强烈推荐,因为可以直观调试) 15 | * Linux(可自己写简易makefile) 16 | * Mac(暂未尝试) 17 | ## 二、环境搭建 18 | * CPU 19 | * VS2015(Windows强烈推荐) 20 | ## 三、示例数据 21 | mnist400(train200-0,train200-1,test20-0,test20-1) 22 | ## 四、示例网络 23 | | Layers | channel | Kernel_size | stride | pad | Input_size | Output_size | 24 | |:------:|:------:|:------:|:------:|:------:|:------:|:------:| 25 | | Conv1 | 16 | 3 | 1 | 0 | 28x28 | 26x26 | 26 | | Relu1 | 16 | - | - | - | 26x26 | 26x26 | 27 | | Maxpool1 | 16 | 2 | 2 | 0 | 26x26 | 13x13 | 28 | | Conv2 | 32 | 3 | 1 | 0 | 13x13 | 11x11 | 29 | | Relu2 | 32 | - | - | - | 11x11 | 11x11 | 30 | | Maxpool2 | 32 | 2 | 2 | 0 | 11x11 | 6x6 | 31 | | Conv3 | 64 | 3 | 1 | 0 | 6x6 | 4x4 | 32 | | Relu3 | 64 | - | - | - | 4x4 | 4x4 | 33 | | Ip1 | 128 | - | - | - | 4x4 | 1x1 | 34 | | Relu4 | 128 | -| - | - | 1x1 | 1x1 | 35 | | Ip2 | 2 | - | - | - | 1x1 | 1x1 | 36 | | softmax | 2 | - | - | - | 1x1 | 1x1 | 37 | 38 | ## 五、示例代码 39 | ```cpp 40 | #include "lazy_net.h" 41 | 42 | int main() 43 | { 44 | //data information 45 | string data_path = "data/"; 46 | int data_batch = 20; 47 | int data_channel = 3; 48 | int data_size = 28; 49 | 50 | //hyperparameter 51 | int max_iter = 1000; 52 | int disp_iter = 10; 53 | float weight_decay = 0.0005; 54 | float base_lr = 0.01; 55 | float momentum = 0.9; 56 | string lr_policy = "inv"; 57 | float power = 0.75; 58 | float gamma = 0.0001; 59 | int test_iter = 50; 60 | 61 | LazyNet lazy_net(data_path, data_batch, data_channel, 62 | data_size, max_iter, disp_iter, 63 | weight_decay, base_lr, momentum, 64 | lr_policy, power, gamma, test_iter); 65 | 66 | lazy_net.TrainNet(); 67 | 68 | return 0; 69 | } 70 | ``` 71 | 72 | ## 六、示例结果 73 | ![result](https://github.com/samylee/LazyNet/blob/master/result.jpg) 74 | 75 | # 尾注 76 | 虽然这款学习框架很好理解,有利于梳理整个深度学习流程,但是也有几点不足之处,后面有时间会做相应调整 77 | 78 | * 网络重塑性不好,网络已嵌入代码,后面考虑用文本的形式写入 79 | * 训练数据灵活性不好,数据形式已嵌入代码,后面考虑利用random复写 80 | * 主要layers基于caffe修改而成,后面考虑去除OpenBLAS 81 | 82 | 83 | # 参考 84 | * https://blog.csdn.net/samylee/article/details/87194376 85 | -------------------------------------------------------------------------------- /data.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/data.zip -------------------------------------------------------------------------------- /include/convolution_layer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "math_function.h" 4 | 5 | class ConvolutionLayer 6 | { 7 | public: 8 | ConvolutionLayer(); 9 | ~ConvolutionLayer(); 10 | 11 | void LayerSetUp(Flow &bottom, Flow &top, vector &weight_shape, vector &bias_shape); 12 | 13 | void ForwardNet(Flow &bottom, Flow &top); 14 | void BackwardNet(Flow &top, Flow &bottom); 15 | 16 | Flow weight_; 17 | Flow bias_; 18 | 19 | private: 20 | void ForwardWeightGemm(float *input, float *weights, float *output); 21 | void ForwardBiasGemm(float* output, const float* bias); 22 | 23 | void BackwardWeightGemm(float* output, float* weights, float* input); 24 | void BackwardBiasGemm(float* bias, float* input); 25 | 26 | void WeightGemm(float* input, float* output, float* weights); 27 | 28 | void ConvIm2Col(const float* data_im, const int channels, 29 | const int height, const int width, const int kernel_h, const int kernel_w, 30 | const int pad_h, const int pad_w, 31 | const int stride_h, const int stride_w, 32 | const int dilation_h, const int dilation_w, 33 | float* data_col); 34 | 35 | void ConvCol2Im(const float* data_col, const int channels, 36 | const int height, const int width, const int kernel_h, const int kernel_w, 37 | const int pad_h, const int pad_w, 38 | const int stride_h, const int stride_w, 39 | const int dilation_h, const int dilation_w, 40 | float* data_im); 41 | 42 | private: 43 | Flow col_buffer_; 44 | Flow bias_multiplier_; 45 | 46 | int weight_dim_; 47 | int conv_in_channel_; 48 | int conv_out_channels_; 49 | int conv_out_spatial_dim_; 50 | int num_output_; 51 | int out_spatial_dim_; 52 | vector conv_input_shape_; 53 | vector kernel_shape_; 54 | }; -------------------------------------------------------------------------------- /include/flow.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Flow 15 | { 16 | public: 17 | Flow(); 18 | void SetShapeData(vector &shape); 19 | vector FlowShape(); 20 | int WhichDimensionShape(int nchw); 21 | int ShapeCount(int start_axis, int end_axis); 22 | int offset(int n, int c = 0, int h = 0, int w = 0); 23 | 24 | void InitData(float alpha); 25 | void InitDiff(float alpha); 26 | void SetData(float *data); 27 | void SetDiff(float *diff); 28 | float* GetData(); 29 | float* GetDiff(); 30 | int GetCounts(); 31 | void Release(); 32 | 33 | private: 34 | float *data_; 35 | float *diff_; 36 | 37 | private: 38 | int number_; 39 | int channel_; 40 | int height_; 41 | int width_; 42 | int counts_; 43 | }; -------------------------------------------------------------------------------- /include/fully_connected_layer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "math_function.h" 4 | 5 | class FullyConnectedLayer 6 | { 7 | public: 8 | FullyConnectedLayer(); 9 | ~FullyConnectedLayer(); 10 | 11 | void LayerSetUp(Flow &bottom, Flow &top, vector &weight_shape, vector &bias_shape); 12 | 13 | void ForwardNet(Flow &bottom, Flow &top); 14 | void BackwardNet(Flow &top, Flow &bottom); 15 | 16 | Flow weight_; 17 | Flow bias_; 18 | 19 | private: 20 | int M_; 21 | int K_; 22 | int N_; 23 | Flow bias_multiplier_; 24 | }; 25 | -------------------------------------------------------------------------------- /include/lazy_net.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "convolution_layer.h" 8 | #include "relu_layer.h" 9 | #include "maxpool_layer.h" 10 | #include "fully_connected_layer.h" 11 | #include "softmax_layer.h" 12 | #include "softmax_loss_layer.h" 13 | 14 | using namespace cv; 15 | 16 | class LazyNet 17 | { 18 | public: 19 | LazyNet(string &data_path, int &data_batch, int &data_channel, 20 | int &data_size, int &max_iter, int &disp_iter, 21 | float &weight_decay, float &base_lr, float &momentum, 22 | string &lr_policy, float &power, float &gamma, int &test_iter); 23 | ~LazyNet(); 24 | 25 | int TrainNet(); 26 | 27 | private: 28 | void LoadNetWork(); 29 | void ApplyHistoryData(Flow &history, Flow ¤t_data); 30 | void LoadDataLabel(int img_count, string phase); 31 | Mat SetMean(int target_width, int target_height); 32 | void ImageToDataTmp(Mat &img_norm, float label, int batch_count); 33 | Mat ImageNormalize(Mat &intput, string norm_type); 34 | 35 | void NetForward(int &iter_count, string phase); 36 | void NetBackward(); 37 | 38 | void NetUpdate(int iter_count); 39 | void UpdateRegularize(); 40 | void UpdateValue(int iter_count); 41 | void UpdateWeightBias(); 42 | 43 | void TestInOutFlow(Flow &in_out_data, string flow_name); 44 | 45 | private: 46 | int data_batch_; 47 | int data_channel_; 48 | int data_size_; 49 | 50 | int max_iter_; 51 | int disp_iter_; 52 | float weight_decay_; 53 | float base_lr_; 54 | float momentum_; 55 | string lr_policy_; 56 | float power_; 57 | float gamma_; 58 | int test_iter_; 59 | 60 | float *input_data_tmp_; 61 | float *label_data_tmp_; 62 | 63 | vector data0_train_path; 64 | vector data1_train_path; 65 | 66 | vector data0_test_path; 67 | vector data1_test_path; 68 | 69 | private: 70 | Flow input_data_; 71 | Flow label_data_; 72 | 73 | Flow conv1_data_; 74 | Flow relu1_data_; 75 | Flow pool1_data_; 76 | 77 | Flow conv2_data_; 78 | Flow relu2_data_; 79 | Flow pool2_data_; 80 | 81 | Flow conv3_data_; 82 | Flow relu3_data_; 83 | 84 | Flow fc1_data_; 85 | Flow relu4_data_; 86 | Flow fc2_data_; 87 | 88 | Flow softmax_data_; 89 | Flow softmax_loss_data_; 90 | 91 | vector history_data_; 92 | 93 | private: 94 | ConvolutionLayer conv1_; 95 | ReLULayer relu1_; 96 | MaxPoolLayer pool1_; 97 | 98 | ConvolutionLayer conv2_; 99 | ReLULayer relu2_; 100 | MaxPoolLayer pool2_; 101 | 102 | ConvolutionLayer conv3_; 103 | ReLULayer relu3_; 104 | 105 | FullyConnectedLayer fc1_; 106 | ReLULayer relu4_; 107 | FullyConnectedLayer fc2_; 108 | 109 | SoftmaxLayer softmax_; 110 | SoftmaxWithLossLayer softmax_loss_; 111 | }; -------------------------------------------------------------------------------- /include/math_function.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "string.h" 4 | #include 5 | #include "flow.h" 6 | 7 | extern "C" { 8 | #include 9 | } 10 | 11 | void OpenblasGemm(const CBLAS_TRANSPOSE TransA, 12 | const CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, 13 | const float alpha, const float* A, const float* B, const float beta, 14 | float* C); 15 | 16 | void OpenblasGemv(const CBLAS_TRANSPOSE TransA, const int M, 17 | const int N, const float alpha, const float* A, const float* x, 18 | const float beta, float* y); 19 | 20 | void InitWeightBias(Flow &weight_, Flow &bias_); 21 | 22 | //Y[i] = X[i] 23 | void CopyData(const int N, const float* X, float* Y); 24 | 25 | //y[i] = exp(a[i]) 26 | void ExpData(const int n, const float* a, float* y); 27 | 28 | //y[i] = a[i]\b[i] 29 | void DivData(const int n, const float* a, const float* b, float* y); 30 | 31 | //X = alpha*X 32 | void ScalData(const int N, const float alpha, float *X); 33 | 34 | //Y=alpha*X+Y 35 | void AxpyData(const int N, const float alpha, const float* X, float* Y); 36 | 37 | //Y= alpha*X+beta*Y 38 | void AxpbyData(const int N, const float alpha, const float* X, const float beta, float* Y); -------------------------------------------------------------------------------- /include/maxpool_layer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "math_function.h" 4 | 5 | class MaxPoolLayer 6 | { 7 | public: 8 | MaxPoolLayer(); 9 | ~MaxPoolLayer(); 10 | 11 | void LayerSetUp(Flow &bottom, Flow &top, vector &pool_shape); 12 | 13 | void ForwardNet(Flow &bottom, Flow &top); 14 | void BackwardNet(Flow &top, Flow &bottom); 15 | 16 | private: 17 | int kernel_h_; 18 | int kernel_w_; 19 | 20 | Flow max_idx_; 21 | }; 22 | -------------------------------------------------------------------------------- /include/relu_layer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "math_function.h" 4 | 5 | class ReLULayer 6 | { 7 | public: 8 | ReLULayer(); 9 | 10 | void LayerSetUp(Flow &bottom, Flow &top); 11 | 12 | void ForwardNet(Flow &bottom, Flow &top); 13 | void BackwardNet(Flow &top, Flow &bottom); 14 | }; 15 | 16 | -------------------------------------------------------------------------------- /include/softmax_layer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "math_function.h" 4 | 5 | class SoftmaxLayer 6 | { 7 | public: 8 | SoftmaxLayer(); 9 | ~SoftmaxLayer(); 10 | 11 | void LayerSetUp(Flow &bottom, Flow &top); 12 | 13 | void ForwardNet(Flow &bottom, Flow &top); 14 | void BackwardNet(Flow &top, Flow &bottom); 15 | 16 | private: 17 | int outer_num_; 18 | int inner_num_; 19 | int softmax_axis_; 20 | /// sum_multiplier is used to carry out sum using BLAS 21 | Flow sum_multiplier_; 22 | /// scale is an intermediate Blob to hold temporary results. 23 | Flow scale_; 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /include/softmax_loss_layer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "math_function.h" 4 | 5 | class SoftmaxWithLossLayer 6 | { 7 | public: 8 | SoftmaxWithLossLayer(); 9 | 10 | void LayerSetUp(int outer_num, Flow &top); 11 | 12 | //bottom: 0(feature map), 1(label) 13 | void ForwardNet(vector &bottom, Flow &top); 14 | void BackwardNet(Flow &top, vector &bottom); 15 | 16 | private: 17 | int outer_num_; 18 | int inner_num_; 19 | }; 20 | -------------------------------------------------------------------------------- /openblas.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/openblas.zip -------------------------------------------------------------------------------- /opencv.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/opencv.zip -------------------------------------------------------------------------------- /result.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/result.jpg -------------------------------------------------------------------------------- /src/convolution_layer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/convolution_layer.cpp -------------------------------------------------------------------------------- /src/flow.cpp: -------------------------------------------------------------------------------- 1 | #include "flow.h" 2 | 3 | Flow::Flow() 4 | { 5 | 6 | } 7 | 8 | void Flow::SetShapeData(vector &shape) 9 | { 10 | switch (shape.size()) 11 | { 12 | case 1: 13 | number_ = shape[0]; 14 | counts_ = number_; 15 | break; 16 | case 2: 17 | number_ = shape[0]; 18 | channel_ = shape[1]; 19 | counts_ = number_*channel_; 20 | break; 21 | case 3: 22 | number_ = shape[0]; 23 | channel_ = shape[1]; 24 | height_ = shape[2]; 25 | counts_ = number_*channel_*height_; 26 | break; 27 | case 4: 28 | number_ = shape[0]; 29 | channel_ = shape[1]; 30 | height_ = shape[2]; 31 | width_ = shape[3]; 32 | counts_ = number_*channel_*height_*width_; 33 | break; 34 | default: 35 | break; 36 | } 37 | 38 | if (data_ != NULL) 39 | free(data_); 40 | if (diff_ != NULL) 41 | free(diff_); 42 | 43 | data_ = (float*)malloc(sizeof(float) * counts_); 44 | diff_ = (float*)malloc(sizeof(float) * counts_); 45 | } 46 | 47 | vector Flow::FlowShape() 48 | { 49 | vector shape; 50 | if (number_ != NULL) 51 | shape.push_back(number_); 52 | if (channel_ != NULL) 53 | shape.push_back(channel_); 54 | if (height_ != NULL) 55 | shape.push_back(height_); 56 | if (width_ != NULL) 57 | shape.push_back(width_); 58 | return shape; 59 | } 60 | 61 | int Flow::WhichDimensionShape(int nchw) 62 | { 63 | vector shape = FlowShape(); 64 | if (nchw > shape.size() - 1) 65 | return 0; 66 | 67 | int dimension = 0; 68 | switch (nchw) 69 | { 70 | case 0: 71 | dimension = number_; 72 | break; 73 | case 1: 74 | dimension = channel_; 75 | break; 76 | case 2: 77 | dimension = height_; 78 | break; 79 | case 3: 80 | dimension = width_; 81 | break; 82 | default: 83 | break; 84 | } 85 | return dimension; 86 | } 87 | 88 | int Flow::ShapeCount(int start_axis, int end_axis) 89 | { 90 | int count = 1; 91 | for (int i = start_axis; i < end_axis; ++i) { 92 | count *= WhichDimensionShape(i); 93 | } 94 | return count; 95 | } 96 | 97 | int Flow::offset(int n, int c, int h, int w) 98 | { 99 | return ((n * channel_ + c) * height_ + h) * width_ + w; 100 | } 101 | 102 | void Flow::InitData(float alpha) 103 | { 104 | float *data_tmp = (float *)malloc(sizeof(float)*counts_); 105 | for (int i = 0; i < counts_; i++) 106 | { 107 | data_tmp[i] = alpha; 108 | } 109 | SetData(data_tmp); 110 | free(data_tmp); 111 | } 112 | 113 | void Flow::InitDiff(float alpha) 114 | { 115 | float *diff_tmp = (float *)malloc(sizeof(float)*counts_); 116 | for (int i = 0; i < counts_; i++) 117 | { 118 | diff_tmp[i] = alpha; 119 | } 120 | SetDiff(diff_tmp); 121 | free(diff_tmp); 122 | } 123 | 124 | void Flow::SetData(float *data) 125 | { 126 | if (data_ != data) 127 | { 128 | memcpy(data_, data, sizeof(float) * counts_); // NOLINT(caffe/alt_fn) 129 | } 130 | } 131 | 132 | void Flow::SetDiff(float *diff) 133 | { 134 | if (diff_ != diff) 135 | { 136 | memcpy(diff_, diff, sizeof(float) * counts_); // NOLINT(caffe/alt_fn) 137 | } 138 | } 139 | 140 | float* Flow::GetData() 141 | { 142 | return data_; 143 | } 144 | 145 | float* Flow::GetDiff() 146 | { 147 | return diff_; 148 | } 149 | 150 | int Flow::GetCounts() 151 | { 152 | return counts_; 153 | } 154 | 155 | void Flow::Release() 156 | { 157 | if (data_ != NULL) 158 | { 159 | free(data_); 160 | data_ = NULL; 161 | } 162 | if (diff_ != NULL) 163 | { 164 | free(diff_); 165 | diff_ = NULL; 166 | } 167 | } -------------------------------------------------------------------------------- /src/fully_connected_layer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/fully_connected_layer.cpp -------------------------------------------------------------------------------- /src/lazy_net.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/lazy_net.cpp -------------------------------------------------------------------------------- /src/lazy_net_function.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/lazy_net_function.cpp -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "lazy_net.h" 2 | 3 | int main() 4 | { 5 | //data information 6 | string data_path = "data/"; 7 | int data_batch = 20; 8 | int data_channel = 3; 9 | int data_size = 28; 10 | 11 | //hyperparameter 12 | int max_iter = 1000; 13 | int disp_iter = 10; 14 | float weight_decay = 0.0005; 15 | float base_lr = 0.01; 16 | float momentum = 0.9; 17 | string lr_policy = "inv"; 18 | float power = 0.75; 19 | float gamma = 0.0001; 20 | int test_iter = 50; 21 | 22 | LazyNet lazy_net(data_path, data_batch, data_channel, 23 | data_size, max_iter, disp_iter, 24 | weight_decay, base_lr, momentum, 25 | lr_policy, power, gamma, test_iter); 26 | 27 | lazy_net.TrainNet(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /src/math_function.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/math_function.cpp -------------------------------------------------------------------------------- /src/maxpool_layer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/maxpool_layer.cpp -------------------------------------------------------------------------------- /src/relu_layer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/relu_layer.cpp -------------------------------------------------------------------------------- /src/softmax_layer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/softmax_layer.cpp -------------------------------------------------------------------------------- /src/softmax_loss_layer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samylee/LazyNet/fbb9c4c589affe6194856c352f025fa3ab30b415/src/softmax_loss_layer.cpp --------------------------------------------------------------------------------