├── 1.MNIST ├── convert_mnist_data.cpp ├── create_mnist.sh ├── get_mnist.sh ├── lenet.prototxt ├── lenet_adadelta_solver.prototxt ├── lenet_auto_solver.prototxt ├── lenet_consolidated_solver.prototxt ├── lenet_multistep_solver.prototxt ├── lenet_solver.prototxt ├── lenet_solver_adam.prototxt ├── lenet_solver_rmsprop.prototxt ├── lenet_train_test.prototxt ├── mnist_autoencoder.prototxt ├── mnist_autoencoder_solver.prototxt ├── mnist_autoencoder_solver_adadelta.prototxt ├── mnist_autoencoder_solver_adagrad.prototxt ├── mnist_autoencoder_solver_nesterov.prototxt ├── mnist_test_lmdb │ ├── data.mdb │ └── lock.mdb ├── mnist_train_lmdb │ ├── data.mdb │ └── lock.mdb ├── readme.md ├── train_lenet.sh ├── train_lenet_adam.sh ├── train_lenet_consolidated.sh ├── train_lenet_docker.sh ├── train_lenet_rmsprop.sh ├── train_mnist_autoencoder.sh ├── train_mnist_autoencoder_adadelta.sh ├── train_mnist_autoencoder_adagrad.sh └── train_mnist_autoencoder_nesterov.sh ├── 2.cifar10 ├── cifar10_full.prototxt ├── cifar10_full_sigmoid_solver.prototxt ├── cifar10_full_sigmoid_solver_bn.prototxt ├── cifar10_full_sigmoid_train_test.prototxt ├── cifar10_full_sigmoid_train_test_bn.prototxt ├── cifar10_full_solver.prototxt ├── cifar10_full_solver_lr1.prototxt ├── cifar10_full_solver_lr2.prototxt ├── cifar10_full_train_test.prototxt ├── cifar10_quick.prototxt ├── cifar10_quick_solver.prototxt ├── cifar10_quick_solver_lr1.prototxt ├── cifar10_quick_train_test.prototxt ├── convert_cifar_data.cpp ├── create_cifar10.sh ├── readme.md ├── train_full.sh ├── train_full_sigmoid.sh ├── train_full_sigmoid_bn.sh └── train_quick.sh ├── 3.Imagenet ├── bvlc_reference_caffenet │ ├── deploy.prototxt │ ├── readme.md │ ├── solver.prototxt │ └── train_val.prototxt ├── create_imagenet.sh ├── data │ └── get_ilsvrc_aux.sh ├── make_imagenet_mean.sh ├── readme.md ├── resume_training.sh └── train_caffenet.sh ├── Makefile_config_EX ├── Makefile.config_Mac ├── Makefile.config_Ubuntu16_CPU └── Makefile.config_Ubuntu16_GPU ├── README.md └── Tools └── autoGPUsetup.sh /1.MNIST/convert_mnist_data.cpp: -------------------------------------------------------------------------------- 1 | // This script converts the MNIST dataset to a lmdb (default) or 2 | // leveldb (--backend=leveldb) format used by caffe to load data. 3 | // Usage: 4 | // convert_mnist_data [FLAGS] input_image_file input_label_file 5 | // output_db_file 6 | // The MNIST dataset could be downloaded at 7 | // http://yann.lecun.com/exdb/mnist/ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #if defined(USE_LEVELDB) && defined(USE_LMDB) 14 | #include 15 | #include 16 | #include 17 | #endif 18 | 19 | #include 20 | #include 21 | 22 | #include // NOLINT(readability/streams) 23 | #include 24 | 25 | #include "boost/scoped_ptr.hpp" 26 | #include "caffe/proto/caffe.pb.h" 27 | #include "caffe/util/db.hpp" 28 | #include "caffe/util/format.hpp" 29 | 30 | #if defined(USE_LEVELDB) && defined(USE_LMDB) 31 | 32 | using namespace caffe; // NOLINT(build/namespaces) 33 | using boost::scoped_ptr; 34 | using std::string; 35 | 36 | DEFINE_string(backend, "lmdb", "The backend for storing the result"); 37 | 38 | uint32_t swap_endian(uint32_t val) { 39 | val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); 40 | return (val << 16) | (val >> 16); 41 | } 42 | 43 | void convert_dataset(const char* image_filename, const char* label_filename, 44 | const char* db_path, const string& db_backend) { 45 | // Open files 46 | std::ifstream image_file(image_filename, std::ios::in | std::ios::binary); 47 | std::ifstream label_file(label_filename, std::ios::in | std::ios::binary); 48 | CHECK(image_file) << "Unable to open file " << image_filename; 49 | CHECK(label_file) << "Unable to open file " << label_filename; 50 | // Read the magic and the meta data 51 | uint32_t magic; 52 | uint32_t num_items; 53 | uint32_t num_labels; 54 | uint32_t rows; 55 | uint32_t cols; 56 | 57 | image_file.read(reinterpret_cast(&magic), 4); 58 | magic = swap_endian(magic); 59 | CHECK_EQ(magic, 2051) << "Incorrect image file magic."; 60 | label_file.read(reinterpret_cast(&magic), 4); 61 | magic = swap_endian(magic); 62 | CHECK_EQ(magic, 2049) << "Incorrect label file magic."; 63 | image_file.read(reinterpret_cast(&num_items), 4); 64 | num_items = swap_endian(num_items); 65 | label_file.read(reinterpret_cast(&num_labels), 4); 66 | num_labels = swap_endian(num_labels); 67 | CHECK_EQ(num_items, num_labels); 68 | image_file.read(reinterpret_cast(&rows), 4); 69 | rows = swap_endian(rows); 70 | image_file.read(reinterpret_cast(&cols), 4); 71 | cols = swap_endian(cols); 72 | 73 | 74 | scoped_ptr db(db::GetDB(db_backend)); 75 | db->Open(db_path, db::NEW); 76 | scoped_ptr txn(db->NewTransaction()); 77 | 78 | // Storing to db 79 | char label; 80 | char* pixels = new char[rows * cols]; 81 | int count = 0; 82 | string value; 83 | 84 | Datum datum; 85 | datum.set_channels(1); 86 | datum.set_height(rows); 87 | datum.set_width(cols); 88 | LOG(INFO) << "A total of " << num_items << " items."; 89 | LOG(INFO) << "Rows: " << rows << " Cols: " << cols; 90 | for (int item_id = 0; item_id < num_items; ++item_id) { 91 | image_file.read(pixels, rows * cols); 92 | label_file.read(&label, 1); 93 | datum.set_data(pixels, rows*cols); 94 | datum.set_label(label); 95 | string key_str = caffe::format_int(item_id, 8); 96 | datum.SerializeToString(&value); 97 | 98 | txn->Put(key_str, value); 99 | 100 | if (++count % 1000 == 0) { 101 | txn->Commit(); 102 | } 103 | } 104 | // write the last batch 105 | if (count % 1000 != 0) { 106 | txn->Commit(); 107 | } 108 | LOG(INFO) << "Processed " << count << " files."; 109 | delete[] pixels; 110 | db->Close(); 111 | } 112 | 113 | int main(int argc, char** argv) { 114 | #ifndef GFLAGS_GFLAGS_H_ 115 | namespace gflags = google; 116 | #endif 117 | 118 | FLAGS_alsologtostderr = 1; 119 | 120 | gflags::SetUsageMessage("This script converts the MNIST dataset to\n" 121 | "the lmdb/leveldb format used by Caffe to load data.\n" 122 | "Usage:\n" 123 | " convert_mnist_data [FLAGS] input_image_file input_label_file " 124 | "output_db_file\n" 125 | "The MNIST dataset could be downloaded at\n" 126 | " http://yann.lecun.com/exdb/mnist/\n" 127 | "You should gunzip them after downloading," 128 | "or directly use data/mnist/get_mnist.sh\n"); 129 | gflags::ParseCommandLineFlags(&argc, &argv, true); 130 | 131 | const string& db_backend = FLAGS_backend; 132 | 133 | if (argc != 4) { 134 | gflags::ShowUsageWithFlagsRestrict(argv[0], 135 | "examples/mnist/convert_mnist_data"); 136 | } else { 137 | google::InitGoogleLogging(argv[0]); 138 | convert_dataset(argv[1], argv[2], argv[3], db_backend); 139 | } 140 | return 0; 141 | } 142 | #else 143 | int main(int argc, char** argv) { 144 | LOG(FATAL) << "This example requires LevelDB and LMDB; " << 145 | "compile with USE_LEVELDB and USE_LMDB."; 146 | } 147 | #endif // USE_LEVELDB and USE_LMDB 148 | -------------------------------------------------------------------------------- /1.MNIST/create_mnist.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # This script converts the mnist data into lmdb/leveldb format, 3 | # depending on the value assigned to $BACKEND. 4 | set -e 5 | 6 | EXAMPLE=examples/mnist 7 | DATA=dataset/mnist 8 | BUILD=build/examples/mnist 9 | 10 | BACKEND="lmdb" 11 | 12 | echo "Creating ${BACKEND}..." 13 | 14 | rm -rf $EXAMPLE/mnist_train_${BACKEND} 15 | rm -rf $EXAMPLE/mnist_test_${BACKEND} 16 | 17 | $BUILD/convert_mnist_data.bin $DATA/train-images-idx3-ubyte \ 18 | $DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND} 19 | $BUILD/convert_mnist_data.bin $DATA/t10k-images-idx3-ubyte \ 20 | $DATA/t10k-labels-idx1-ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND} 21 | 22 | echo "Done." 23 | -------------------------------------------------------------------------------- /1.MNIST/get_mnist.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # This scripts downloads the mnist data and unzips it. 3 | DIR=./dataset 4 | #DIR="$( cd "$(dirname "$0")" ; pwd -P )" 5 | cd "$DIR" 6 | 7 | echo "Downloading..." 8 | 9 | for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte 10 | do 11 | if [ ! -e $fname ]; then 12 | wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz 13 | gunzip ${fname}.gz 14 | fi 15 | done 16 | -------------------------------------------------------------------------------- /1.MNIST/lenet.prototxt: -------------------------------------------------------------------------------- 1 | name: "LeNet" 2 | layer { 3 | name: "data" 4 | type: "Input" 5 | top: "data" 6 | input_param { shape: { dim: 64 dim: 1 dim: 28 dim: 28 } } 7 | } 8 | layer { 9 | name: "conv1" 10 | type: "Convolution" 11 | bottom: "data" 12 | top: "conv1" 13 | param { 14 | lr_mult: 1 15 | } 16 | param { 17 | lr_mult: 2 18 | } 19 | convolution_param { 20 | num_output: 20 21 | kernel_size: 5 22 | stride: 1 23 | weight_filler { 24 | type: "xavier" 25 | } 26 | bias_filler { 27 | type: "constant" 28 | } 29 | } 30 | } 31 | layer { 32 | name: "pool1" 33 | type: "Pooling" 34 | bottom: "conv1" 35 | top: "pool1" 36 | pooling_param { 37 | pool: MAX 38 | kernel_size: 2 39 | stride: 2 40 | } 41 | } 42 | layer { 43 | name: "conv2" 44 | type: "Convolution" 45 | bottom: "pool1" 46 | top: "conv2" 47 | param { 48 | lr_mult: 1 49 | } 50 | param { 51 | lr_mult: 2 52 | } 53 | convolution_param { 54 | num_output: 50 55 | kernel_size: 5 56 | stride: 1 57 | weight_filler { 58 | type: "xavier" 59 | } 60 | bias_filler { 61 | type: "constant" 62 | } 63 | } 64 | } 65 | layer { 66 | name: "pool2" 67 | type: "Pooling" 68 | bottom: "conv2" 69 | top: "pool2" 70 | pooling_param { 71 | pool: MAX 72 | kernel_size: 2 73 | stride: 2 74 | } 75 | } 76 | layer { 77 | name: "ip1" 78 | type: "InnerProduct" 79 | bottom: "pool2" 80 | top: "ip1" 81 | param { 82 | lr_mult: 1 83 | } 84 | param { 85 | lr_mult: 2 86 | } 87 | inner_product_param { 88 | num_output: 500 89 | weight_filler { 90 | type: "xavier" 91 | } 92 | bias_filler { 93 | type: "constant" 94 | } 95 | } 96 | } 97 | layer { 98 | name: "relu1" 99 | type: "ReLU" 100 | bottom: "ip1" 101 | top: "ip1" 102 | } 103 | layer { 104 | name: "ip2" 105 | type: "InnerProduct" 106 | bottom: "ip1" 107 | top: "ip2" 108 | param { 109 | lr_mult: 1 110 | } 111 | param { 112 | lr_mult: 2 113 | } 114 | inner_product_param { 115 | num_output: 10 116 | weight_filler { 117 | type: "xavier" 118 | } 119 | bias_filler { 120 | type: "constant" 121 | } 122 | } 123 | } 124 | layer { 125 | name: "prob" 126 | type: "Softmax" 127 | bottom: "ip2" 128 | top: "prob" 129 | } 130 | -------------------------------------------------------------------------------- /1.MNIST/lenet_adadelta_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | net: "examples/mnist/lenet_train_test.prototxt" 3 | # test_iter specifies how many forward passes the test should carry out. 4 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 5 | # covering the full 10,000 testing images. 6 | test_iter: 100 7 | # Carry out testing every 500 training iterations. 8 | test_interval: 500 9 | # The base learning rate, momentum and the weight decay of the network. 10 | base_lr: 1.0 11 | lr_policy: "fixed" 12 | momentum: 0.95 13 | weight_decay: 0.0005 14 | # Display every 100 iterations 15 | display: 100 16 | # The maximum number of iterations 17 | max_iter: 10000 18 | # snapshot intermediate results 19 | snapshot: 5000 20 | snapshot_prefix: "examples/mnist/lenet_adadelta" 21 | # solver mode: CPU or GPU 22 | solver_mode: GPU 23 | type: "AdaDelta" 24 | delta: 1e-6 25 | -------------------------------------------------------------------------------- /1.MNIST/lenet_auto_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | train_net: "mnist/lenet_auto_train.prototxt" 3 | test_net: "mnist/lenet_auto_test.prototxt" 4 | # test_iter specifies how many forward passes the test should carry out. 5 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 6 | # covering the full 10,000 testing images. 7 | test_iter: 100 8 | # Carry out testing every 500 training iterations. 9 | test_interval: 500 10 | # The base learning rate, momentum and the weight decay of the network. 11 | base_lr: 0.01 12 | momentum: 0.9 13 | weight_decay: 0.0005 14 | # The learning rate policy 15 | lr_policy: "inv" 16 | gamma: 0.0001 17 | power: 0.75 18 | # Display every 100 iterations 19 | display: 100 20 | # The maximum number of iterations 21 | max_iter: 10000 22 | # snapshot intermediate results 23 | snapshot: 5000 24 | snapshot_prefix: "mnist/lenet" 25 | -------------------------------------------------------------------------------- /1.MNIST/lenet_consolidated_solver.prototxt: -------------------------------------------------------------------------------- 1 | # lenet_consolidated_solver.prototxt consolidates the lenet_solver, lenet_train, 2 | # and lenet_test prototxts into a single file. It also adds an additional test 3 | # net which runs on the training set, e.g., for the purpose of comparing 4 | # train/test accuracy (accuracy is computed only on the test set in the included 5 | # LeNet example). This is mainly included as an example of using these features 6 | # (specify NetParameters directly in the solver, specify multiple test nets) 7 | # if desired. 8 | # 9 | # Carry out testing every 500 training iterations. 10 | test_interval: 500 11 | # The base learning rate, momentum and the weight decay of the network. 12 | base_lr: 0.01 13 | momentum: 0.9 14 | weight_decay: 0.0005 15 | # The learning rate policy 16 | lr_policy: "inv" 17 | gamma: 0.0001 18 | power: 0.75 19 | # Display every 100 iterations 20 | display: 100 21 | # The maximum number of iterations 22 | max_iter: 10000 23 | # snapshot intermediate results 24 | snapshot: 5000 25 | snapshot_prefix: "examples/mnist/lenet" 26 | # Set a random_seed for repeatable results. 27 | # (For results that vary due to random initialization, comment out the below 28 | # line, or set to a negative integer -- e.g. "random_seed: -1") 29 | random_seed: 1701 30 | # solver mode: CPU or GPU 31 | solver_mode: GPU 32 | 33 | # We test on both the test and train set using "stages". The TEST DATA layers 34 | # each have a stage, either 'test-on-train-set' or 'test-on-test-set'. 35 | # test_iter specifies how many forward passes the test should carry out. 36 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 37 | # covering the full 10,000 testing images. 38 | test_iter: 100 39 | test_state: { stage: "test-on-test-set" } 40 | # The train set has 60K images, so we run 600 test iters (600 * 100 = 60K). 41 | test_iter: 600 42 | test_state: { stage: "test-on-train-set" } 43 | 44 | # The net protocol buffer definition 45 | net_param { 46 | name: "LeNet" 47 | layers { 48 | name: "mnist" 49 | type: DATA 50 | top: "data" 51 | top: "label" 52 | data_param { 53 | source: "examples/mnist/mnist_train_lmdb" 54 | backend: LMDB 55 | batch_size: 64 56 | } 57 | transform_param { 58 | scale: 0.00390625 59 | } 60 | include: { phase: TRAIN } 61 | } 62 | layers { 63 | name: "mnist" 64 | type: DATA 65 | top: "data" 66 | top: "label" 67 | data_param { 68 | source: "examples/mnist/mnist_test_lmdb" 69 | backend: LMDB 70 | batch_size: 100 71 | } 72 | transform_param { 73 | scale: 0.00390625 74 | } 75 | include: { 76 | phase: TEST 77 | stage: "test-on-test-set" 78 | } 79 | } 80 | layers { 81 | name: "mnist" 82 | type: DATA 83 | top: "data" 84 | top: "label" 85 | data_param { 86 | source: "examples/mnist/mnist_train_lmdb" 87 | backend: LMDB 88 | batch_size: 100 89 | } 90 | transform_param { 91 | scale: 0.00390625 92 | } 93 | include: { 94 | phase: TEST 95 | stage: "test-on-train-set" 96 | } 97 | } 98 | layers { 99 | name: "conv1" 100 | type: CONVOLUTION 101 | bottom: "data" 102 | top: "conv1" 103 | blobs_lr: 1 104 | blobs_lr: 2 105 | convolution_param { 106 | num_output: 20 107 | kernel_size: 5 108 | stride: 1 109 | weight_filler { 110 | type: "xavier" 111 | } 112 | bias_filler { 113 | type: "constant" 114 | } 115 | } 116 | } 117 | layers { 118 | name: "pool1" 119 | type: POOLING 120 | bottom: "conv1" 121 | top: "pool1" 122 | pooling_param { 123 | pool: MAX 124 | kernel_size: 2 125 | stride: 2 126 | } 127 | } 128 | layers { 129 | name: "conv2" 130 | type: CONVOLUTION 131 | bottom: "pool1" 132 | top: "conv2" 133 | blobs_lr: 1 134 | blobs_lr: 2 135 | convolution_param { 136 | num_output: 50 137 | kernel_size: 5 138 | stride: 1 139 | weight_filler { 140 | type: "xavier" 141 | } 142 | bias_filler { 143 | type: "constant" 144 | } 145 | } 146 | } 147 | layers { 148 | name: "pool2" 149 | type: POOLING 150 | bottom: "conv2" 151 | top: "pool2" 152 | pooling_param { 153 | pool: MAX 154 | kernel_size: 2 155 | stride: 2 156 | } 157 | } 158 | layers { 159 | name: "ip1" 160 | type: INNER_PRODUCT 161 | bottom: "pool2" 162 | top: "ip1" 163 | blobs_lr: 1 164 | blobs_lr: 2 165 | inner_product_param { 166 | num_output: 500 167 | weight_filler { 168 | type: "xavier" 169 | } 170 | bias_filler { 171 | type: "constant" 172 | } 173 | } 174 | } 175 | layers { 176 | name: "relu1" 177 | type: RELU 178 | bottom: "ip1" 179 | top: "ip1" 180 | } 181 | layers { 182 | name: "ip2" 183 | type: INNER_PRODUCT 184 | bottom: "ip1" 185 | top: "ip2" 186 | blobs_lr: 1 187 | blobs_lr: 2 188 | inner_product_param { 189 | num_output: 10 190 | weight_filler { 191 | type: "xavier" 192 | } 193 | bias_filler { 194 | type: "constant" 195 | } 196 | } 197 | } 198 | layers { 199 | name: "accuracy" 200 | type: ACCURACY 201 | bottom: "ip2" 202 | bottom: "label" 203 | top: "accuracy" 204 | } 205 | layers { 206 | name: "loss" 207 | type: SOFTMAX_LOSS 208 | bottom: "ip2" 209 | bottom: "label" 210 | top: "loss" 211 | } 212 | } 213 | 214 | # Expected results for first and last 500 iterations: 215 | # (with portions of log omitted for brevity) 216 | # 217 | # Iteration 0, Testing net (#0) 218 | # Test score #0: 0.067 219 | # Test score #1: 2.30256 220 | # Iteration 0, Testing net (#1) 221 | # Test score #0: 0.0670334 222 | # Test score #1: 2.30258 223 | # Iteration 100, lr = 0.00992565 224 | # Iteration 100, loss = 0.280585 225 | # Iteration 200, lr = 0.00985258 226 | # Iteration 200, loss = 0.345601 227 | # Iteration 300, lr = 0.00978075 228 | # Iteration 300, loss = 0.172217 229 | # Iteration 400, lr = 0.00971013 230 | # Iteration 400, loss = 0.261836 231 | # Iteration 500, lr = 0.00964069 232 | # Iteration 500, loss = 0.157803 233 | # Iteration 500, Testing net (#0) 234 | # Test score #0: 0.968 235 | # Test score #1: 0.0993772 236 | # Iteration 500, Testing net (#1) 237 | # Test score #0: 0.965883 238 | # Test score #1: 0.109374 239 | # 240 | # [...] 241 | # 242 | # Iteration 9500, Testing net (#0) 243 | # Test score #0: 0.9899 244 | # Test score #1: 0.0308299 245 | # Iteration 9500, Testing net (#1) 246 | # Test score #0: 0.996816 247 | # Test score #1: 0.0118238 248 | # Iteration 9600, lr = 0.00603682 249 | # Iteration 9600, loss = 0.0126215 250 | # Iteration 9700, lr = 0.00601382 251 | # Iteration 9700, loss = 0.00579304 252 | # Iteration 9800, lr = 0.00599102 253 | # Iteration 9800, loss = 0.00500633 254 | # Iteration 9900, lr = 0.00596843 255 | # Iteration 9900, loss = 0.00796607 256 | # Iteration 10000, lr = 0.00594604 257 | # Iteration 10000, loss = 0.00271736 258 | # Iteration 10000, Testing net (#0) 259 | # Test score #0: 0.9914 260 | # Test score #1: 0.0276671 261 | # Iteration 10000, Testing net (#1) 262 | # Test score #0: 0.997782 263 | # Test score #1: 0.00908085 264 | -------------------------------------------------------------------------------- /1.MNIST/lenet_multistep_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | net: "examples/mnist/lenet_train_test.prototxt" 3 | # test_iter specifies how many forward passes the test should carry out. 4 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 5 | # covering the full 10,000 testing images. 6 | test_iter: 100 7 | # Carry out testing every 500 training iterations. 8 | test_interval: 500 9 | # The base learning rate, momentum and the weight decay of the network. 10 | base_lr: 0.01 11 | momentum: 0.9 12 | weight_decay: 0.0005 13 | # The learning rate policy 14 | lr_policy: "multistep" 15 | gamma: 0.9 16 | stepvalue: 5000 17 | stepvalue: 7000 18 | stepvalue: 8000 19 | stepvalue: 9000 20 | stepvalue: 9500 21 | # Display every 100 iterations 22 | display: 100 23 | # The maximum number of iterations 24 | max_iter: 10000 25 | # snapshot intermediate results 26 | snapshot: 5000 27 | snapshot_prefix: "examples/mnist/lenet_multistep" 28 | # solver mode: CPU or GPU 29 | solver_mode: GPU 30 | -------------------------------------------------------------------------------- /1.MNIST/lenet_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | net: "examples/mnist/lenet_train_test.prototxt" 3 | # test_iter specifies how many forward passes the test should carry out. 4 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 5 | # covering the full 10,000 testing images. 6 | test_iter: 100 7 | # Carry out testing every 500 training iterations. 8 | test_interval: 500 9 | # The base learning rate, momentum and the weight decay of the network. 10 | base_lr: 0.01 11 | momentum: 0.9 12 | weight_decay: 0.0005 13 | # The learning rate policy 14 | lr_policy: "inv" 15 | gamma: 0.0001 16 | power: 0.75 17 | # Display every 100 iterations 18 | display: 100 19 | # The maximum number of iterations 20 | max_iter: 10000 21 | # snapshot intermediate results 22 | snapshot: 5000 23 | snapshot_prefix: "examples/mnist/lenet" 24 | # solver mode: CPU or GPU 25 | solver_mode: CPU 26 | -------------------------------------------------------------------------------- /1.MNIST/lenet_solver_adam.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | # this follows "ADAM: A METHOD FOR STOCHASTIC OPTIMIZATION" 3 | net: "examples/mnist/lenet_train_test.prototxt" 4 | # test_iter specifies how many forward passes the test should carry out. 5 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 6 | # covering the full 10,000 testing images. 7 | test_iter: 100 8 | # Carry out testing every 500 training iterations. 9 | test_interval: 500 10 | # All parameters are from the cited paper above 11 | base_lr: 0.001 12 | momentum: 0.9 13 | momentum2: 0.999 14 | # since Adam dynamically changes the learning rate, we set the base learning 15 | # rate to a fixed value 16 | lr_policy: "fixed" 17 | # Display every 100 iterations 18 | display: 100 19 | # The maximum number of iterations 20 | max_iter: 10000 21 | # snapshot intermediate results 22 | snapshot: 5000 23 | snapshot_prefix: "examples/mnist/lenet" 24 | # solver mode: CPU or GPU 25 | type: "Adam" 26 | solver_mode: GPU 27 | -------------------------------------------------------------------------------- /1.MNIST/lenet_solver_rmsprop.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | net: "examples/mnist/lenet_train_test.prototxt" 3 | # test_iter specifies how many forward passes the test should carry out. 4 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 5 | # covering the full 10,000 testing images. 6 | test_iter: 100 7 | # Carry out testing every 500 training iterations. 8 | test_interval: 500 9 | # The base learning rate, momentum and the weight decay of the network. 10 | base_lr: 0.01 11 | momentum: 0.0 12 | weight_decay: 0.0005 13 | # The learning rate policy 14 | lr_policy: "inv" 15 | gamma: 0.0001 16 | power: 0.75 17 | # Display every 100 iterations 18 | display: 100 19 | # The maximum number of iterations 20 | max_iter: 10000 21 | # snapshot intermediate results 22 | snapshot: 5000 23 | snapshot_prefix: "examples/mnist/lenet_rmsprop" 24 | # solver mode: CPU or GPU 25 | solver_mode: GPU 26 | type: "RMSProp" 27 | rms_decay: 0.98 28 | -------------------------------------------------------------------------------- /1.MNIST/lenet_train_test.prototxt: -------------------------------------------------------------------------------- 1 | name: "LeNet" 2 | layer { 3 | name: "mnist" 4 | type: "Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | transform_param { 11 | scale: 0.00390625 12 | } 13 | data_param { 14 | source: "examples/mnist/mnist_train_lmdb" 15 | batch_size: 64 16 | backend: LMDB 17 | } 18 | } 19 | layer { 20 | name: "mnist" 21 | type: "Data" 22 | top: "data" 23 | top: "label" 24 | include { 25 | phase: TEST 26 | } 27 | transform_param { 28 | scale: 0.00390625 29 | } 30 | data_param { 31 | source: "examples/mnist/mnist_test_lmdb" 32 | batch_size: 100 33 | backend: LMDB 34 | } 35 | } 36 | layer { 37 | name: "conv1" 38 | type: "Convolution" 39 | bottom: "data" 40 | top: "conv1" 41 | param { 42 | lr_mult: 1 43 | } 44 | param { 45 | lr_mult: 2 46 | } 47 | convolution_param { 48 | num_output: 20 49 | kernel_size: 5 50 | stride: 1 51 | weight_filler { 52 | type: "xavier" 53 | } 54 | bias_filler { 55 | type: "constant" 56 | } 57 | } 58 | } 59 | layer { 60 | name: "pool1" 61 | type: "Pooling" 62 | bottom: "conv1" 63 | top: "pool1" 64 | pooling_param { 65 | pool: MAX 66 | kernel_size: 2 67 | stride: 2 68 | } 69 | } 70 | layer { 71 | name: "conv2" 72 | type: "Convolution" 73 | bottom: "pool1" 74 | top: "conv2" 75 | param { 76 | lr_mult: 1 77 | } 78 | param { 79 | lr_mult: 2 80 | } 81 | convolution_param { 82 | num_output: 50 83 | kernel_size: 5 84 | stride: 1 85 | weight_filler { 86 | type: "xavier" 87 | } 88 | bias_filler { 89 | type: "constant" 90 | } 91 | } 92 | } 93 | layer { 94 | name: "pool2" 95 | type: "Pooling" 96 | bottom: "conv2" 97 | top: "pool2" 98 | pooling_param { 99 | pool: MAX 100 | kernel_size: 2 101 | stride: 2 102 | } 103 | } 104 | layer { 105 | name: "ip1" 106 | type: "InnerProduct" 107 | bottom: "pool2" 108 | top: "ip1" 109 | param { 110 | lr_mult: 1 111 | } 112 | param { 113 | lr_mult: 2 114 | } 115 | inner_product_param { 116 | num_output: 500 117 | weight_filler { 118 | type: "xavier" 119 | } 120 | bias_filler { 121 | type: "constant" 122 | } 123 | } 124 | } 125 | layer { 126 | name: "relu1" 127 | type: "ReLU" 128 | bottom: "ip1" 129 | top: "ip1" 130 | } 131 | layer { 132 | name: "ip2" 133 | type: "InnerProduct" 134 | bottom: "ip1" 135 | top: "ip2" 136 | param { 137 | lr_mult: 1 138 | } 139 | param { 140 | lr_mult: 2 141 | } 142 | inner_product_param { 143 | num_output: 10 144 | weight_filler { 145 | type: "xavier" 146 | } 147 | bias_filler { 148 | type: "constant" 149 | } 150 | } 151 | } 152 | layer { 153 | name: "accuracy" 154 | type: "Accuracy" 155 | bottom: "ip2" 156 | bottom: "label" 157 | top: "accuracy" 158 | include { 159 | phase: TEST 160 | } 161 | } 162 | layer { 163 | name: "loss" 164 | type: "SoftmaxWithLoss" 165 | bottom: "ip2" 166 | bottom: "label" 167 | top: "loss" 168 | } 169 | -------------------------------------------------------------------------------- /1.MNIST/mnist_autoencoder.prototxt: -------------------------------------------------------------------------------- 1 | name: "MNISTAutoencoder" 2 | layer { 3 | name: "data" 4 | type: "Data" 5 | top: "data" 6 | include { 7 | phase: TRAIN 8 | } 9 | transform_param { 10 | scale: 0.0039215684 11 | } 12 | data_param { 13 | source: "examples/mnist/mnist_train_lmdb" 14 | batch_size: 100 15 | backend: LMDB 16 | } 17 | } 18 | layer { 19 | name: "data" 20 | type: "Data" 21 | top: "data" 22 | include { 23 | phase: TEST 24 | stage: "test-on-train" 25 | } 26 | transform_param { 27 | scale: 0.0039215684 28 | } 29 | data_param { 30 | source: "examples/mnist/mnist_train_lmdb" 31 | batch_size: 100 32 | backend: LMDB 33 | } 34 | } 35 | layer { 36 | name: "data" 37 | type: "Data" 38 | top: "data" 39 | include { 40 | phase: TEST 41 | stage: "test-on-test" 42 | } 43 | transform_param { 44 | scale: 0.0039215684 45 | } 46 | data_param { 47 | source: "examples/mnist/mnist_test_lmdb" 48 | batch_size: 100 49 | backend: LMDB 50 | } 51 | } 52 | layer { 53 | name: "flatdata" 54 | type: "Flatten" 55 | bottom: "data" 56 | top: "flatdata" 57 | } 58 | layer { 59 | name: "encode1" 60 | type: "InnerProduct" 61 | bottom: "data" 62 | top: "encode1" 63 | param { 64 | lr_mult: 1 65 | decay_mult: 1 66 | } 67 | param { 68 | lr_mult: 1 69 | decay_mult: 0 70 | } 71 | inner_product_param { 72 | num_output: 1000 73 | weight_filler { 74 | type: "gaussian" 75 | std: 1 76 | sparse: 15 77 | } 78 | bias_filler { 79 | type: "constant" 80 | value: 0 81 | } 82 | } 83 | } 84 | layer { 85 | name: "encode1neuron" 86 | type: "Sigmoid" 87 | bottom: "encode1" 88 | top: "encode1neuron" 89 | } 90 | layer { 91 | name: "encode2" 92 | type: "InnerProduct" 93 | bottom: "encode1neuron" 94 | top: "encode2" 95 | param { 96 | lr_mult: 1 97 | decay_mult: 1 98 | } 99 | param { 100 | lr_mult: 1 101 | decay_mult: 0 102 | } 103 | inner_product_param { 104 | num_output: 500 105 | weight_filler { 106 | type: "gaussian" 107 | std: 1 108 | sparse: 15 109 | } 110 | bias_filler { 111 | type: "constant" 112 | value: 0 113 | } 114 | } 115 | } 116 | layer { 117 | name: "encode2neuron" 118 | type: "Sigmoid" 119 | bottom: "encode2" 120 | top: "encode2neuron" 121 | } 122 | layer { 123 | name: "encode3" 124 | type: "InnerProduct" 125 | bottom: "encode2neuron" 126 | top: "encode3" 127 | param { 128 | lr_mult: 1 129 | decay_mult: 1 130 | } 131 | param { 132 | lr_mult: 1 133 | decay_mult: 0 134 | } 135 | inner_product_param { 136 | num_output: 250 137 | weight_filler { 138 | type: "gaussian" 139 | std: 1 140 | sparse: 15 141 | } 142 | bias_filler { 143 | type: "constant" 144 | value: 0 145 | } 146 | } 147 | } 148 | layer { 149 | name: "encode3neuron" 150 | type: "Sigmoid" 151 | bottom: "encode3" 152 | top: "encode3neuron" 153 | } 154 | layer { 155 | name: "encode4" 156 | type: "InnerProduct" 157 | bottom: "encode3neuron" 158 | top: "encode4" 159 | param { 160 | lr_mult: 1 161 | decay_mult: 1 162 | } 163 | param { 164 | lr_mult: 1 165 | decay_mult: 0 166 | } 167 | inner_product_param { 168 | num_output: 30 169 | weight_filler { 170 | type: "gaussian" 171 | std: 1 172 | sparse: 15 173 | } 174 | bias_filler { 175 | type: "constant" 176 | value: 0 177 | } 178 | } 179 | } 180 | layer { 181 | name: "decode4" 182 | type: "InnerProduct" 183 | bottom: "encode4" 184 | top: "decode4" 185 | param { 186 | lr_mult: 1 187 | decay_mult: 1 188 | } 189 | param { 190 | lr_mult: 1 191 | decay_mult: 0 192 | } 193 | inner_product_param { 194 | num_output: 250 195 | weight_filler { 196 | type: "gaussian" 197 | std: 1 198 | sparse: 15 199 | } 200 | bias_filler { 201 | type: "constant" 202 | value: 0 203 | } 204 | } 205 | } 206 | layer { 207 | name: "decode4neuron" 208 | type: "Sigmoid" 209 | bottom: "decode4" 210 | top: "decode4neuron" 211 | } 212 | layer { 213 | name: "decode3" 214 | type: "InnerProduct" 215 | bottom: "decode4neuron" 216 | top: "decode3" 217 | param { 218 | lr_mult: 1 219 | decay_mult: 1 220 | } 221 | param { 222 | lr_mult: 1 223 | decay_mult: 0 224 | } 225 | inner_product_param { 226 | num_output: 500 227 | weight_filler { 228 | type: "gaussian" 229 | std: 1 230 | sparse: 15 231 | } 232 | bias_filler { 233 | type: "constant" 234 | value: 0 235 | } 236 | } 237 | } 238 | layer { 239 | name: "decode3neuron" 240 | type: "Sigmoid" 241 | bottom: "decode3" 242 | top: "decode3neuron" 243 | } 244 | layer { 245 | name: "decode2" 246 | type: "InnerProduct" 247 | bottom: "decode3neuron" 248 | top: "decode2" 249 | param { 250 | lr_mult: 1 251 | decay_mult: 1 252 | } 253 | param { 254 | lr_mult: 1 255 | decay_mult: 0 256 | } 257 | inner_product_param { 258 | num_output: 1000 259 | weight_filler { 260 | type: "gaussian" 261 | std: 1 262 | sparse: 15 263 | } 264 | bias_filler { 265 | type: "constant" 266 | value: 0 267 | } 268 | } 269 | } 270 | layer { 271 | name: "decode2neuron" 272 | type: "Sigmoid" 273 | bottom: "decode2" 274 | top: "decode2neuron" 275 | } 276 | layer { 277 | name: "decode1" 278 | type: "InnerProduct" 279 | bottom: "decode2neuron" 280 | top: "decode1" 281 | param { 282 | lr_mult: 1 283 | decay_mult: 1 284 | } 285 | param { 286 | lr_mult: 1 287 | decay_mult: 0 288 | } 289 | inner_product_param { 290 | num_output: 784 291 | weight_filler { 292 | type: "gaussian" 293 | std: 1 294 | sparse: 15 295 | } 296 | bias_filler { 297 | type: "constant" 298 | value: 0 299 | } 300 | } 301 | } 302 | layer { 303 | name: "loss" 304 | type: "SigmoidCrossEntropyLoss" 305 | bottom: "decode1" 306 | bottom: "flatdata" 307 | top: "cross_entropy_loss" 308 | loss_weight: 1 309 | } 310 | layer { 311 | name: "decode1neuron" 312 | type: "Sigmoid" 313 | bottom: "decode1" 314 | top: "decode1neuron" 315 | } 316 | layer { 317 | name: "loss" 318 | type: "EuclideanLoss" 319 | bottom: "decode1neuron" 320 | bottom: "flatdata" 321 | top: "l2_error" 322 | loss_weight: 0 323 | } 324 | -------------------------------------------------------------------------------- /1.MNIST/mnist_autoencoder_solver.prototxt: -------------------------------------------------------------------------------- 1 | net: "examples/mnist/mnist_autoencoder.prototxt" 2 | test_state: { stage: 'test-on-train' } 3 | test_iter: 500 4 | test_state: { stage: 'test-on-test' } 5 | test_iter: 100 6 | test_interval: 500 7 | test_compute_loss: true 8 | base_lr: 0.01 9 | lr_policy: "step" 10 | gamma: 0.1 11 | stepsize: 10000 12 | display: 100 13 | max_iter: 65000 14 | weight_decay: 0.0005 15 | snapshot: 10000 16 | snapshot_prefix: "examples/mnist/mnist_autoencoder" 17 | momentum: 0.9 18 | # solver mode: CPU or GPU 19 | solver_mode: GPU 20 | -------------------------------------------------------------------------------- /1.MNIST/mnist_autoencoder_solver_adadelta.prototxt: -------------------------------------------------------------------------------- 1 | net: "examples/mnist/mnist_autoencoder.prototxt" 2 | test_state: { stage: 'test-on-train' } 3 | test_iter: 500 4 | test_state: { stage: 'test-on-test' } 5 | test_iter: 100 6 | test_interval: 500 7 | test_compute_loss: true 8 | base_lr: 1.0 9 | lr_policy: "fixed" 10 | momentum: 0.95 11 | delta: 1e-8 12 | display: 100 13 | max_iter: 65000 14 | weight_decay: 0.0005 15 | snapshot: 10000 16 | snapshot_prefix: "examples/mnist/mnist_autoencoder_adadelta_train" 17 | # solver mode: CPU or GPU 18 | solver_mode: GPU 19 | type: "AdaDelta" 20 | -------------------------------------------------------------------------------- /1.MNIST/mnist_autoencoder_solver_adagrad.prototxt: -------------------------------------------------------------------------------- 1 | net: "examples/mnist/mnist_autoencoder.prototxt" 2 | test_state: { stage: 'test-on-train' } 3 | test_iter: 500 4 | test_state: { stage: 'test-on-test' } 5 | test_iter: 100 6 | test_interval: 500 7 | test_compute_loss: true 8 | base_lr: 0.01 9 | lr_policy: "fixed" 10 | display: 100 11 | max_iter: 65000 12 | weight_decay: 0.0005 13 | snapshot: 10000 14 | snapshot_prefix: "examples/mnist/mnist_autoencoder_adagrad_train" 15 | # solver mode: CPU or GPU 16 | solver_mode: GPU 17 | type: "AdaGrad" 18 | -------------------------------------------------------------------------------- /1.MNIST/mnist_autoencoder_solver_nesterov.prototxt: -------------------------------------------------------------------------------- 1 | net: "examples/mnist/mnist_autoencoder.prototxt" 2 | test_state: { stage: 'test-on-train' } 3 | test_iter: 500 4 | test_state: { stage: 'test-on-test' } 5 | test_iter: 100 6 | test_interval: 500 7 | test_compute_loss: true 8 | base_lr: 0.01 9 | lr_policy: "step" 10 | gamma: 0.1 11 | stepsize: 10000 12 | display: 100 13 | max_iter: 65000 14 | weight_decay: 0.0005 15 | snapshot: 10000 16 | snapshot_prefix: "examples/mnist/mnist_autoencoder_nesterov_train" 17 | momentum: 0.95 18 | # solver mode: CPU or GPU 19 | solver_mode: GPU 20 | type: "Nesterov" 21 | -------------------------------------------------------------------------------- /1.MNIST/mnist_test_lmdb/data.mdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hahnnz/Caffe_Tutorial/4955d6b4ba80bd543d2686cb2788e163b5059995/1.MNIST/mnist_test_lmdb/data.mdb -------------------------------------------------------------------------------- /1.MNIST/mnist_test_lmdb/lock.mdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hahnnz/Caffe_Tutorial/4955d6b4ba80bd543d2686cb2788e163b5059995/1.MNIST/mnist_test_lmdb/lock.mdb -------------------------------------------------------------------------------- /1.MNIST/mnist_train_lmdb/data.mdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hahnnz/Caffe_Tutorial/4955d6b4ba80bd543d2686cb2788e163b5059995/1.MNIST/mnist_train_lmdb/data.mdb -------------------------------------------------------------------------------- /1.MNIST/mnist_train_lmdb/lock.mdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hahnnz/Caffe_Tutorial/4955d6b4ba80bd543d2686cb2788e163b5059995/1.MNIST/mnist_train_lmdb/lock.mdb -------------------------------------------------------------------------------- /1.MNIST/readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | 제목: LeNet MNIST Tutorial 3 | 설명: 손으로 쓴 숫자 데이터셋을 활용하여 "Lenet"을 학습시켜보고 테스트를 해본다. 4 | 카테고리: Caffe_Tutorial 5 | include_in_docs: true 6 | priority: 1 7 | --- 8 | 9 | # Caffe를 사용하여 MNIST 데이터 셋을 활용해 Lenet 학습시키기 10 | 해당 튜토리얼을 진행하기 앞서 Caffe를 성공적으로 컴파일 해야합니다. 아니라면 [설치 페이지](https://github.com/Hahnnz/Caffe_Tutorial/wiki/0.-Caffe-%EC%84%A4%EC%B9%98-%EB%B0%A9%EB%B2%95)를 참고하여 설치 및 컴파일을 완료해주시기를 바랍니다. 11 | 이 튜토리얼에서는 Caffe가 튜토리얼 레퍼지토리에 존재해야합니다. 12 | 13 | ## 데이터셋 준비하기 14 | 15 | 먼저 MNIST 웹사이트에 있는 데이터셋을 다운로드를 하고 데이터 형식을 변환해주어야 합니다. 아래의 코드를 실행하면 간단히 이를 수행할 수 있습니다. 16 | 17 | cd $1.MNIST 18 | ./get_mnist.sh 19 | ./create_mnist.sh 20 | 21 | 만약 `wget`이나 `gunzip`이 설치되어있지 않다는 문제가 발생하면, 이 둘 각각을 설치해주어야만 합니다. 그래서 최종적으로 다음 두 Shell 파일들을 실행시킨후에는 `mnist_train_lmdb`과 `mnist_test_lmdb`이라는 두 개의 데이터셋이 생성된 것을 확인하실 수 있습니다.  22 | 23 | ## LeNet: MNIST 데이터 셋 분류 모델 24 | 25 | 실제로 우리가 이 학습 프로그램을 실행시켜보기전에, 무엇이 일어날지에 대해 설명드리겠습니다. 우리는 [LeNet](http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf)이라는 신경망을 사용할 것인데, 이 신경망은 숫자 분류하는 기능을 아주 탁월하게 수행하기로 유명합니다. 우린 기존에 구현된 LeNet과는 조금은 다른 버전을 사용할 것입니다. 기존에는 활성화 함수를 Sigmoid 함수로 구현하였지면 여기서 사용하는 것은 이를 대체해 현재 아주 유명한 Rectified Linear Unit (ReLU)를 사용하여 구현한 버전을 사용할 것입니다. 26 | 27 | LeNet의 디자인은 여전히 많이 사용되어지는 좀 더 큰 크기의 모델에 핵심이 되는 CNN을 포함하고 있습니다. 예를 들면 ImageNet에 속한 여러가지 모델들입니다. 일반적으로 convolution Layer 다음에 pooling layer를 붙이고, 또 그 뒤에 convolution layer, pooling layer를 붙이고, 그 뒤에는 전가산층(Fully Connected layers : 기존의 다층 퍼셉트론과 유사함)를 두 개 더 붙입니다. 이러한 구성은 다음의 파일에 구성되어 있습니다. 28 | 29 | 30 | `$Tutorial_ROOT/1.MNIST/lenet_train_test.prototxt`. 31 | 32 | ## MNIST 신경망 정의 33 | 34 | 해당 섹션에서는 손으로 쓴 숫자 MNIST 데이터셋을 분류하는 LeNet 모델을 구현한 `lenet_train_test.prototxt`의 모델 정의를 살펴볼 것입니다. 35 | [Google Protobuf](https://developers.google.com/protocol-buffers/docs/overview)에 어느정도 익숙하다고 가정을 하고 설명을 할 것이지만 모르더라도 충분히 따라올 수 있습니다. 그래도 가급적이면 `$CAFFE_ROOT/src/caffe/proto/caffe.proto`에 있는 Caffe에서 사용하는 protobuf를 한 번 읽어보시는 것을 추천합니다. 36 | 37 | 우린 `caffe::NetParameter` protobuf를(python에서는 `caffe.proto.caffe_pb2.NetParameter`) 작성할 것인데 먼저 신경망에게 이름을 지어주는 것부터 해서 시작한다. 38 | 39 | name: "LeNet" 40 | 41 | ### Data Layer 작성 42 | 43 | 현재 우리는 이전 위에서 데이터 셋을 다운로드 했는데, 신경망이 데이터 입력을 받을 수 있도록 다음과 같이 data layer를 작성해준다. 44 | 45 | layer { 46 | name: "mnist" 47 | type: "Data" 48 | transform_param { 49 | scale: 0.00390625 50 | } 51 | data_param { 52 | source: "mnist_train_lmdb" 53 | backend: LMDB 54 | batch_size: 64 55 | } 56 | top: "data" 57 | top: "label" 58 | } 59 | 60 | 특별하게도, 이 계층의 이름은 `mnist`, 타입은 `data`로 선언되고, 이 계층은 주어진 lmdb 데이터셋으로 부터 데이터를 읽어들인다. 우린 일괄 처리량을 한번에 64장 씩 처리하도록 batch_size를 64로 정의하였으며, 그리고 입력 크기가 픽셀 단위이기에 \[0,1\)의 범위로 설정을 해주어야 한다. 하지만 왜 하필 0.00390625인가? 이는 1을 256으로 나눈 숫자이기 때문이다. 그래서 최종적으로 이 계층은 두개의 blob 데이터 덩어리를 생성해낸다. 하난 `data` 이고, 다른 하나는 `label`이다. 61 | 62 | ### Convolution Layer 작성하기 63 | 64 | 이제 첫번째 합성곱 계층 (first convolution layer)를 정의해보자. 65 | 66 | layer { 67 | name: "conv1" 68 | type: "Convolution" 69 | param { lr_mult: 1 } 70 | param { lr_mult: 2 } 71 | convolution_param { 72 | num_output: 20 73 | kernel_size: 5 74 | stride: 1 75 | weight_filler { 76 | type: "xavier" 77 | } 78 | bias_filler { 79 | type: "constant" 80 | } 81 | } 82 | bottom: "data" 83 | top: "conv1" 84 | } 85 | 86 | 이 계층은 데이터 계층에서 제공하는 `data`이라는 데이터 덩어리를 받아 `conv1`계층을 수행합니다. convolution 커널 크기는 5이며 stride 1로 convolution 연산을 수행하여, 출력은 20개의 채널로 나갑니다. 87 | 88 | fillers는 weight와 bias 값을 임의로 초기화합니다. weight filler로 우리들은 `xavier` 알고리즘을 사용하는데, 이 알고리즘은 입력과 출력 뉴런들의 수에 기반하여서 초기화의 크기를 자동적으로 정해줍니다. bias filler로는 0의 값을 기본 값으로 하여 상수로 초기화 합니다. 89 | 90 | `lr_mult`는 계층 내에서 학습을 하는 파라미터들의 학습률을 조정해주는 역할을 수행합니다. 여기서 우리는 실행중에 Solver가 주는 학습률과 동일하게 weight learning rate를 설정해줍니다. 그리고 bias learning rate는 보통 두배 크게 설정해줍니다. 이는 보통 더 나은 수렴률을 보입니다. 91 | 92 | ### Pooling Layer 93 | 94 | 후. Pooling Layer는 좀 더 쉽게 정의할 수 있습니다. 95 | 96 | layer { 97 | name: "pool1" 98 | type: "Pooling" 99 | pooling_param { 100 | kernel_size: 2 101 | stride: 2 102 | pool: MAX 103 | } 104 | bottom: "conv1" 105 | top: "pool1" 106 |    } 107 | 108 | 이는 pool 커널 크기는 2로, stride 2로 (stride 2로 pooling region들이 서로 겹치지 않게 하기 위함이다.) 설정하여 Max pooling을 수행하는 것이다. 109 | 비슷하게, 두번째 Convolution과 Pooling layer를 작성할 수 있을 겁니다. 아직도 잘 모르시겠다면, `$TUTORIAL_ROOT/1.MNIST/lenet_train_test.prototxt`를 참고해주세요. 110 | 111 | 112 | ### Fully Connected Layer(전가산층) 작성하기 113 | 114 | 전가산층 작성하는 것도 간단합니다. 115 | 116 | layer { 117 | name: "ip1" 118 | type: "InnerProduct" 119 | param { lr_mult: 1 } 120 | param { lr_mult: 2 } 121 | inner_product_param { 122 | num_output: 500 123 | weight_filler { 124 | type: "xavier" 125 | } 126 | bias_filler { 127 | type: "constant" 128 | } 129 | } 130 | bottom: "pool2" 131 | top: "ip1" 132 | } 133 | 134 | (`InnerProduct` (내적) 계층으로써 Caffe에서 사용되는) 전가산 계층은 500개의 출력을 한다고 정의된다. 모든 다른 줄들은 친근해보이죠? 맞죠? 135 | 136 | ### ReLU Layer 작성하기 137 | ReLU Layer도 작성하기 쉽습니다. 138 | 139 | layer { 140 | name: "relu1" 141 | type: "ReLU" 142 | bottom: "ip1" 143 | top: "ip1" 144 | } 145 | 146 | ReLU는 원소단위(신경망 뉴런단위) 연산이기에, 우린 어느정도 메모리를 아끼기 위해서 *준비된* 연산을 할 수 있다. 이는 하위 (bottom = 입력)와 상위 (top = 출력)blob들에게 같은 이름을 부여해 설정이 가능하다. 당연한 것이지만, 다른 타입의 계층들에서는 blob이름이 같아서는 안됩니다! 147 | 148 | ReLU layer를 한 후에는, 다시 또 하나의 내적 계층 (innerproduct layer)을 작성합니다. 149 | 150 | layer { 151 | name: "ip2" 152 | type: "InnerProduct" 153 | param { lr_mult: 1 } 154 | param { lr_mult: 2 } 155 | inner_product_param { 156 | num_output: 10 157 | weight_filler { 158 | type: "xavier" 159 | } 160 | bias_filler { 161 | type: "constant" 162 | } 163 | } 164 | bottom: "ip1" 165 | top: "ip2" 166 | } 167 | 168 | ### Loss Layer 작성하기 169 | 170 | 마지막으로, Loss 계층을 작성해볼 것입니다! 171 | 172 | layer { 173 | name: "loss" 174 | type: "SoftmaxWithLoss" 175 | bottom: "ip2" 176 | bottom: "label" 177 | } 178 | 179 | `softmax_loss` 계층은 다항식의 로지스틱 손실 함수와 소프트맥스를 모두 구현한 것입니다. 이렇게 하면 수치적 안정성을 향상시키고 시간을 절약할 수 있습니다. 이 계층은 두개의 blob 데이터 덩어리를 입력으로 받는데 하나는 예측된 것이고 다른 하나는 맨 앞의 데이터 계층에서 제공하는 `label`를 받아옵니다. 여기서는 더이상의 출력을 해주지는 않습니다. 이 계층에서 하는 것은 손실 함수 값을 연산하는 것으로, 역전파(backpropagation) 과정을 수행할 때 사용하고, `ip2`에 대하여 그래디언트를 초기화 합니다. 180 | 181 | 182 | ### 추가 노트 : 계층 규칙 작성 183 | 184 | 계층을 정의 할 때 include라는 인자를 사용해, (학습 혹은 테스트)어떤 과정에서 해당 계층을 사용할 지도 명시해 줄 수 있습니다. 185 | 186 | layer { 187 | // ...layer definition... 188 | include: { phase: TRAIN } 189 | } 190 | 191 | 이것이 계층 규칙으로, 현재 신경망 상태에 기반해서, 신경망에 포함시킬지 여부를 정할수 있습니다. 192 | 모델 스키마와 규칙에 대해 더 많은 정보가 필요하다면 `$CAFFE_ROOT/src/caffe/proto/caffe.proto` 참고를 해주세요. 193 | 194 | 위와 같은 예시로, 이 계층은 오직 `TRAIN` 학습 단계에서만 포함될 것입니다. 195 | 만약 `TRAIN`를 `TEST`로 바꾼다면, 해당 계층은 테스트 단계에서만 포함될 것입니다. 196 | 계층 규칙이 없다면, 기본값으로 해당 계층은 모든 단계에서 수행될 것입니다. 197 | 그래서 `lenet_train_test.prototxt`에 두개의 `DATA` 계층이 정의되어 있습니다. (심지어 `batch_size`값도 다릅니다) 하나는 학습 단계를 위한 것이고, 다른하나는 테스트 단계를 위한 것입니다. 198 | 또한 `Accuracy`는 `TEST` 단계에만 포함되기 때문에 오직 테스트 단계에서만 `lenet_solver.prototxt`에 정의되어 있는 사항들에 따라, 모델 정확도는 매 100회마다 출력됩니다. 199 | 200 | ## MNIST Solver 정의하기 201 | 202 | `$TUTORIAL_ROOT/1.MNIST/lenet_solver.prototxt`에 있는 각 줄에 있는 설명 코멘트를 살펴봅시다. 203 | 204 |    # 학습/테스트 신경망 모델 파일 정의 205 | net: "examples/mnist/lenet_train_test.prototxt" 206 |    # test_iter는 테스트가 얼마나 순전파(forward) 과정을 진행할지 설정합니다. 207 |    # MNIST의 경우, 우리는 100회 테스트 반복에, 일괄처리량을 100개로 설정하였다. 208 |    # 모든 테스트 데이터 10,000 장 사용 209 |    test_iter: 100 210 |    # 매 학습 500회 진행후 테스트 수행 211 |    test_interval: 500 212 |    # 신경망의 기본 학습률, 모멘텀, weight decay 213 | base_lr: 0.01 214 | momentum: 0.9 215 | weight_decay: 0.0005 216 |    # 학습정책 217 |    lr_policy: "inv" 218 | gamma: 0.0001 219 | power: 0.75 220 |    # 매 100회 반복 때마다 출력합니다. 221 |    display: 100 222 |    # 최대 반복 횟수 223 | max_iter: 10000 224 |    # 매 5000번 반복마다 중간 Snapshot 저장 225 |    snapshot: 5000 226 | snapshot_prefix: "examples/mnist/lenet" 227 |    # CPU 혹은 GPU 설정 228 |    solver_mode: GPU 229 | 230 | 231 | ## 신경망 모델 학습 및 테스트 하기 232 | 233 | 신경망 모델 학습시키기는 여러분들이 신경망 정의 protobuf과 solver protobuf 파일들을 작성한 후에 간단하게 수행시켜 볼 수 있습니다. `train_lenet.sh`를 실행시키면 되므로 다음의 코드를 실행시켜봅시다. 234 | 235 | cd $TUTORIAL_ROOT 236 | ./1.MNIST/train_lenet.sh 237 | 238 | `train_lenet.sh` 는 간단한 스크립트입니다. 하지만 학습을 시키기 위한 메인 툴은 `caffe`내의 툴인 `train`과 solver protobuf 파일을 `train_lenet.sh`가 인자로 사용을 합니다. 239 | 240 | 코드를 실행시켜보면 여러분들은 다음과 같이 마구마구 날라오는 상당량의 메세지를 볼 것입니다. 241 | 242 | I1203 net.cpp:66] Creating Layer conv1 243 | I1203 net.cpp:76] conv1 <- data 244 | I1203 net.cpp:101] conv1 -> conv1 245 | I1203 net.cpp:116] Top shape: 20 24 24 246 | I1203 net.cpp:127] conv1 needs backward computation. 247 | 248 | 이러한 메세지들은 여러분들에게 디버깅할 때 도움이 될만한 각 계층들, 그리고 그 계층의 출력 shape와 연결들에 대한 자세한 설명을 알려줍니다. 초기화를 한 뒤에 학습이 시작될 것입니다. 249 | 250 | I1203 net.cpp:142] Network initialization done. 251 | I1203 solver.cpp:36] Solver scaffolding done. 252 | I1203 solver.cpp:44] Solving LeNet 253 | 254 | Solver 세팅에 기반해 우리는 각 100번 반복마다 학습 손실 함수를 출력할 것이고, 매 반복 500회마다 신경망을 테스트해볼 것입니다. 여러분들은 아마 다음과 같은 메세지를 확인할 수 있습니다. 255 | 256 | I1203 solver.cpp:204] Iteration 100, lr = 0.00992565 257 | I1203 solver.cpp:66] Iteration 100, loss = 0.26044 258 | ... 259 | I1203 solver.cpp:84] Testing net 260 | I1203 solver.cpp:111] Test score #0: 0.9785 261 | I1203 solver.cpp:111] Test score #1: 0.0606671 262 | 263 | 각각 반복마다, `lr`는 그 반복의 학습률을 의미하며, `loss`는 학습 함수중 하나입니다. 테스트 단계의 출력으로는 score 0은 정확도, score 1는 테스트 Loss 함수를 의미합니다. 264 | 265 | 그리고 어느 정도 기다려주면 학습이 완료될 것입니다! 266 | 267 | I1203 solver.cpp:84] Testing net 268 | I1203 solver.cpp:111] Test score #0: 0.9897 269 | I1203 solver.cpp:111] Test score #1: 0.0324599 270 | I1203 solver.cpp:126] Snapshotting to lenet_iter_10000 271 | I1203 solver.cpp:133] Snapshotting solver state to lenet_iter_10000.solverstate 272 | I1203 solver.cpp:78] Optimization Done. 273 | 274 | 최종 학습 모델은 binary protobuf file로 다음과 같이 저장될 것입니다. 275 | 276 | lenet_iter_10000 277 | 278 | 그리고 이를 활용해서, 여러분이 실제 어플리케이션의 데이터셋으로 학습시킨다면 여러분의 어플리케이션 속으로 여러분이 입력한 데이터로 학습된 모델로 저장될 것입니다. 279 | 280 | ### 음... GPU 학습은 어떻게 할 수 있죠? 281 | 대부분의 사람들이 Caffe를 컴파일 할 당시에 Makefile.config에서 CPU-ONLY로 컴파일을 수행해주었을 것입니다. 이를 Uncomment하여 다시 컴파일을 수행해준뒤에, Solver 파일에 들어가서 solver_mode를 CPU에서 GPU로 변경해주신 뒤에 학습을 시켜주면 됩니다. 이 튜토리얼 같은 경우에는 `lenet_solver.prototxt`의 다음과 같은 라인을 바꾸어 주시면 됩니다. 282 | 283 | # solver mode: CPU or GPU 284 |    solver_mode: GPU 285 | 286 | 이렇게 아주 간단하게 바꿀수 있답니다. 287 | 288 | MNIST는 규모가 작은 데이터셋입니다. 그래서 GPU로 학습시키는 것은 커뮤니케이션 오버헤드 덕택을 그렇게 많이 보지를 못합니다. 좀 더 규모가 크고 좀 더 복잡한 모델들, 예를 들면 ImageNet같은 모델들로 수행해본다면 연산 속도의 차이를 좀 더 확연하게 느끼실 수 있습니다.  289 | 290 | ### 어떻게 정해진 단계수에서 학습률을 감소시킬 수 있는 것이죠? 291 | lenet_multistep_solver.prototxt를 참고해주세요 292 | -------------------------------------------------------------------------------- /1.MNIST/train_lenet.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt $@ 5 | -------------------------------------------------------------------------------- /1.MNIST/train_lenet_adam.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ./build/tools/caffe train --solver=examples/mnist/lenet_solver_adam.prototxt $@ 5 | -------------------------------------------------------------------------------- /1.MNIST/train_lenet_consolidated.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ./build/tools/caffe train \ 5 | --solver=examples/mnist/lenet_consolidated_solver.prototxt $@ 6 | -------------------------------------------------------------------------------- /1.MNIST/train_lenet_docker.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | # The following example allows for the MNIST example (using LeNet) to be 4 | # trained using the caffe docker image instead of building from source. 5 | # 6 | # The GPU-enabled version of Caffe can be used, assuming that nvidia-docker 7 | # is installed, and the GPU-enabled Caffe image has been built. 8 | # Setting the GPU environment variable to 1 will enable the use of nvidia-docker. 9 | # e.g. 10 | # GPU=1 ./examples/mnist/train_lenet_docker.sh [ADDITIONAL_CAFFE_ARGS] 11 | # 12 | # With any arguments following the script being passed directly to caffe 13 | # when training the network. 14 | # 15 | # The steps that are performed by the script are as follows: 16 | # 1. The MNIST data set is downloaded 17 | # (see data/mnist/get_mnist.sh) 18 | # 2. An LMDB database is created from the downloaded data 19 | # (see examples/mnist/create_mnist.sh. 20 | # 3. A caffe network based on the LeNet solver is trained. 21 | # (see examples/mnist/lenet_solver.prototxt) 22 | # 23 | # For each of these, a step is executed to ensure that certain prerequisites 24 | # are available, after which a command that actually performs the work is 25 | # executed. 26 | # 27 | # In order to provide additional flexibility, the following shell (environment) 28 | # variables can be used to control the execution of each of the phases: 29 | # 30 | # DOWNLOAD_DATA: Enable (1) or disable (0) the downloading of the MNIST dataset 31 | # CREATE_LMDB: Enable (1) or disable (0) the creation of the LMDB database 32 | # TRAIN: Enable (1) or disable (0) the training of the LeNet networkd. 33 | # 34 | # As an example, assuming that the data set has been downloaded, and an LMDB 35 | # database created, the following command can be used to train the LeNet 36 | # network with GPU computing enabled. 37 | # 38 | # DOWNLOAD_DATA=0 CREATE_LMDB=0 GPU=1 ./examples/mnist/train_lenet_docker.sh 39 | # 40 | 41 | 42 | if [ x"$(uname -s)" != x"Linux" ] 43 | then 44 | echo "" 45 | echo "This script is designed to run on Linux." 46 | echo "There may be problems with the way Docker mounts host volumes on other" 47 | echo "systems which will cause the docker commands to fail." 48 | echo "" 49 | read -p "Press [ENTER] to continue..." key 50 | echo "" 51 | fi 52 | 53 | 54 | # Check if GPU mode has been enabled and set the docker executable accordingly 55 | if [ ${GPU:-0} -eq 1 ] 56 | then 57 | DOCKER_CMD=nvidia-docker 58 | IMAGE=caffe:gpu 59 | else 60 | DOCKER_CMD=docker 61 | IMAGE=caffe:cpu 62 | fi 63 | echo "Using $DOCKER_CMD to launch $IMAGE" 64 | 65 | # On non-Linux systems, the Docker host is typically a virtual machine. 66 | # This means that the user and group id's may be different. 67 | # On OS X, for example, the user and group are 1000 and 50, respectively. 68 | if [ x"$(uname -s)" != x"Linux" ] 69 | then 70 | CUID=1000 71 | CGID=50 72 | else 73 | CUID=$(id -u) 74 | CGID=$(id -g) 75 | fi 76 | 77 | # Define some helper variables to make the running of the actual docker 78 | # commands less verbose. 79 | # Note: 80 | # -u $CUID:$CGID runs the docker image as the current user to ensure 81 | # that the file permissions are compatible with the 82 | # host system. The variables CUID and CGID have been 83 | # set above depending on the host operating system. 84 | # --volume $(pwd):/workspace mounts the current directory as the docker volume 85 | # /workspace 86 | # --workdir /workspace Ensures that the docker container starts in the right 87 | # working directory 88 | DOCKER_OPTIONS="--rm -ti -u $CUID:$CGID --volume=$(pwd):/workspace --workdir=/workspace" 89 | DOCKER_RUN="$DOCKER_CMD run $DOCKER_OPTIONS $IMAGE" 90 | 91 | # Download the data 92 | if [ ${DOWNLOAD_DATA:-1} -eq 1 ] 93 | then 94 | $DOCKER_RUN bash -c "mkdir -p ./data/mnist; 95 | cp -ru \$CAFFE_ROOT/data/mnist/get_mnist.sh ./data/mnist/" 96 | $DOCKER_RUN ./data/mnist/get_mnist.sh 97 | fi 98 | 99 | # Create the LMDB database 100 | if [ ${CREATE_LMDB:-1} -eq 1 ] 101 | then 102 | $DOCKER_RUN bash -c "mkdir -p ./examples/mnist; 103 | cp -ru \$CAFFE_ROOT/examples/mnist/create_mnist.sh ./examples/mnist/; 104 | sed -i s#BUILD=build#BUILD=\$CAFFE_ROOT/build## ./examples/mnist/create_mnist.sh" 105 | $DOCKER_RUN ./examples/mnist/create_mnist.sh 106 | fi 107 | 108 | # Train the network 109 | if [ ${TRAIN:-1} -eq 1 ] 110 | then 111 | $DOCKER_RUN bash -c "cp \$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt ./examples/mnist/; 112 | cp \$CAFFE_ROOT/examples/mnist/lenet_train_test.prototxt ./examples/mnist/" 113 | # Ensure that the solver_mode is compatible with the desired GPU mode. 114 | if [ ${GPU:-0} -eq 0 ] 115 | then 116 | $DOCKER_RUN sed -i 's#solver_mode: GPU#solver_mode: CPU##' ./examples/mnist/lenet_solver.prototxt 117 | fi 118 | $DOCKER_RUN caffe train --solver=examples/mnist/lenet_solver.prototxt $* 119 | fi 120 | -------------------------------------------------------------------------------- /1.MNIST/train_lenet_rmsprop.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ./build/tools/caffe train \ 5 | --solver=examples/mnist/lenet_solver_rmsprop.prototxt $@ 6 | -------------------------------------------------------------------------------- /1.MNIST/train_mnist_autoencoder.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ./build/tools/caffe train \ 5 | --solver=examples/mnist/mnist_autoencoder_solver.prototxt $@ 6 | -------------------------------------------------------------------------------- /1.MNIST/train_mnist_autoencoder_adadelta.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | ./build/tools/caffe train \ 5 | --solver=examples/mnist/mnist_autoencoder_solver_adadelta.prototxt $@ 6 | -------------------------------------------------------------------------------- /1.MNIST/train_mnist_autoencoder_adagrad.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | ./build/tools/caffe train \ 5 | --solver=examples/mnist/mnist_autoencoder_solver_adagrad.prototxt $@ 6 | -------------------------------------------------------------------------------- /1.MNIST/train_mnist_autoencoder_nesterov.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | ./build/tools/caffe train \ 5 | --solver=examples/mnist/mnist_autoencoder_solver_nesterov.prototxt $@ 6 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full.prototxt: -------------------------------------------------------------------------------- 1 | name: "CIFAR10_full_deploy" 2 | # N.B. input image must be in CIFAR-10 format 3 | # as described at http://www.cs.toronto.edu/~kriz/cifar.html 4 | layer { 5 | name: "data" 6 | type: "Input" 7 | top: "data" 8 | input_param { shape: { dim: 1 dim: 3 dim: 32 dim: 32 } } 9 | } 10 | layer { 11 | name: "conv1" 12 | type: "Convolution" 13 | bottom: "data" 14 | top: "conv1" 15 | param { 16 | lr_mult: 1 17 | } 18 | param { 19 | lr_mult: 2 20 | } 21 | convolution_param { 22 | num_output: 32 23 | pad: 2 24 | kernel_size: 5 25 | stride: 1 26 | } 27 | } 28 | layer { 29 | name: "pool1" 30 | type: "Pooling" 31 | bottom: "conv1" 32 | top: "pool1" 33 | pooling_param { 34 | pool: MAX 35 | kernel_size: 3 36 | stride: 2 37 | } 38 | } 39 | layer { 40 | name: "relu1" 41 | type: "ReLU" 42 | bottom: "pool1" 43 | top: "pool1" 44 | } 45 | layer { 46 | name: "norm1" 47 | type: "LRN" 48 | bottom: "pool1" 49 | top: "norm1" 50 | lrn_param { 51 | local_size: 3 52 | alpha: 5e-05 53 | beta: 0.75 54 | norm_region: WITHIN_CHANNEL 55 | } 56 | } 57 | layer { 58 | name: "conv2" 59 | type: "Convolution" 60 | bottom: "norm1" 61 | top: "conv2" 62 | param { 63 | lr_mult: 1 64 | } 65 | param { 66 | lr_mult: 2 67 | } 68 | convolution_param { 69 | num_output: 32 70 | pad: 2 71 | kernel_size: 5 72 | stride: 1 73 | } 74 | } 75 | layer { 76 | name: "relu2" 77 | type: "ReLU" 78 | bottom: "conv2" 79 | top: "conv2" 80 | } 81 | layer { 82 | name: "pool2" 83 | type: "Pooling" 84 | bottom: "conv2" 85 | top: "pool2" 86 | pooling_param { 87 | pool: AVE 88 | kernel_size: 3 89 | stride: 2 90 | } 91 | } 92 | layer { 93 | name: "norm2" 94 | type: "LRN" 95 | bottom: "pool2" 96 | top: "norm2" 97 | lrn_param { 98 | local_size: 3 99 | alpha: 5e-05 100 | beta: 0.75 101 | norm_region: WITHIN_CHANNEL 102 | } 103 | } 104 | layer { 105 | name: "conv3" 106 | type: "Convolution" 107 | bottom: "norm2" 108 | top: "conv3" 109 | convolution_param { 110 | num_output: 64 111 | pad: 2 112 | kernel_size: 5 113 | stride: 1 114 | } 115 | } 116 | layer { 117 | name: "relu3" 118 | type: "ReLU" 119 | bottom: "conv3" 120 | top: "conv3" 121 | } 122 | layer { 123 | name: "pool3" 124 | type: "Pooling" 125 | bottom: "conv3" 126 | top: "pool3" 127 | pooling_param { 128 | pool: AVE 129 | kernel_size: 3 130 | stride: 2 131 | } 132 | } 133 | layer { 134 | name: "ip1" 135 | type: "InnerProduct" 136 | bottom: "pool3" 137 | top: "ip1" 138 | param { 139 | lr_mult: 1 140 | decay_mult: 250 141 | } 142 | param { 143 | lr_mult: 2 144 | decay_mult: 0 145 | } 146 | inner_product_param { 147 | num_output: 10 148 | } 149 | } 150 | layer { 151 | name: "prob" 152 | type: "Softmax" 153 | bottom: "ip1" 154 | top: "prob" 155 | } 156 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_sigmoid_solver.prototxt: -------------------------------------------------------------------------------- 1 | # reduce learning rate after 120 epochs (60000 iters) by factor 0f 10 2 | # then another factor of 10 after 10 more epochs (5000 iters) 3 | 4 | # The train/test net protocol buffer definition 5 | net: "examples/cifar10/cifar10_full_sigmoid_train_test.prototxt" 6 | # test_iter specifies how many forward passes the test should carry out. 7 | # In the case of CIFAR10, we have test batch size 100 and 100 test iterations, 8 | # covering the full 10,000 testing images. 9 | test_iter: 10 10 | # Carry out testing every 1000 training iterations. 11 | test_interval: 1000 12 | # The base learning rate, momentum and the weight decay of the network. 13 | base_lr: 0.001 14 | momentum: 0.9 15 | #weight_decay: 0.004 16 | # The learning rate policy 17 | lr_policy: "step" 18 | gamma: 1 19 | stepsize: 5000 20 | # Display every 100 iterations 21 | display: 100 22 | # The maximum number of iterations 23 | max_iter: 60000 24 | # snapshot intermediate results 25 | snapshot: 10000 26 | snapshot_prefix: "examples/cifar10_full_sigmoid" 27 | # solver mode: CPU or GPU 28 | solver_mode: GPU 29 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_sigmoid_solver_bn.prototxt: -------------------------------------------------------------------------------- 1 | # reduce learning rate after 120 epochs (60000 iters) by factor 0f 10 2 | # then another factor of 10 after 10 more epochs (5000 iters) 3 | 4 | # The train/test net protocol buffer definition 5 | net: "examples/cifar10/cifar10_full_sigmoid_train_test_bn.prototxt" 6 | # test_iter specifies how many forward passes the test should carry out. 7 | # In the case of CIFAR10, we have test batch size 100 and 100 test iterations, 8 | # covering the full 10,000 testing images. 9 | test_iter: 10 10 | # Carry out testing every 1000 training iterations. 11 | test_interval: 1000 12 | # The base learning rate, momentum and the weight decay of the network. 13 | base_lr: 0.001 14 | momentum: 0.9 15 | #weight_decay: 0.004 16 | # The learning rate policy 17 | lr_policy: "step" 18 | gamma: 1 19 | stepsize: 5000 20 | # Display every 100 iterations 21 | display: 100 22 | # The maximum number of iterations 23 | max_iter: 60000 24 | # snapshot intermediate results 25 | snapshot: 10000 26 | snapshot_prefix: "examples/cifar10_full_sigmoid_bn" 27 | # solver mode: CPU or GPU 28 | solver_mode: GPU 29 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_sigmoid_train_test.prototxt: -------------------------------------------------------------------------------- 1 | name: "CIFAR10_full" 2 | layer { 3 | name: "cifar" 4 | type: "Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | transform_param { 11 | mean_file: "examples/cifar10/mean.binaryproto" 12 | } 13 | data_param { 14 | source: "examples/cifar10/cifar10_train_lmdb" 15 | batch_size: 111 16 | backend: LMDB 17 | } 18 | } 19 | layer { 20 | name: "cifar" 21 | type: "Data" 22 | top: "data" 23 | top: "label" 24 | include { 25 | phase: TEST 26 | } 27 | transform_param { 28 | mean_file: "examples/cifar10/mean.binaryproto" 29 | } 30 | data_param { 31 | source: "examples/cifar10/cifar10_test_lmdb" 32 | batch_size: 1000 33 | backend: LMDB 34 | } 35 | } 36 | layer { 37 | name: "conv1" 38 | type: "Convolution" 39 | bottom: "data" 40 | top: "conv1" 41 | param { 42 | lr_mult: 1 43 | } 44 | param { 45 | lr_mult: 2 46 | } 47 | convolution_param { 48 | num_output: 32 49 | pad: 2 50 | kernel_size: 5 51 | stride: 1 52 | weight_filler { 53 | type: "gaussian" 54 | std: 0.0001 55 | } 56 | bias_filler { 57 | type: "constant" 58 | } 59 | } 60 | } 61 | layer { 62 | name: "pool1" 63 | type: "Pooling" 64 | bottom: "conv1" 65 | top: "pool1" 66 | pooling_param { 67 | pool: MAX 68 | kernel_size: 3 69 | stride: 2 70 | } 71 | } 72 | 73 | 74 | 75 | layer { 76 | name: "Sigmoid1" 77 | type: "Sigmoid" 78 | bottom: "pool1" 79 | top: "Sigmoid1" 80 | } 81 | 82 | layer { 83 | name: "conv2" 84 | type: "Convolution" 85 | bottom: "Sigmoid1" 86 | top: "conv2" 87 | param { 88 | lr_mult: 1 89 | } 90 | param { 91 | lr_mult: 2 92 | } 93 | convolution_param { 94 | num_output: 32 95 | pad: 2 96 | kernel_size: 5 97 | stride: 1 98 | weight_filler { 99 | type: "gaussian" 100 | std: 0.01 101 | } 102 | bias_filler { 103 | type: "constant" 104 | } 105 | } 106 | } 107 | 108 | 109 | layer { 110 | name: "Sigmoid2" 111 | type: "Sigmoid" 112 | bottom: "conv2" 113 | top: "Sigmoid2" 114 | } 115 | layer { 116 | name: "pool2" 117 | type: "Pooling" 118 | bottom: "Sigmoid2" 119 | top: "pool2" 120 | pooling_param { 121 | pool: AVE 122 | kernel_size: 3 123 | stride: 2 124 | } 125 | } 126 | layer { 127 | name: "conv3" 128 | type: "Convolution" 129 | bottom: "pool2" 130 | top: "conv3" 131 | convolution_param { 132 | num_output: 64 133 | pad: 2 134 | kernel_size: 5 135 | stride: 1 136 | weight_filler { 137 | type: "gaussian" 138 | std: 0.01 139 | } 140 | bias_filler { 141 | type: "constant" 142 | } 143 | } 144 | param { 145 | lr_mult: 1 146 | } 147 | param { 148 | lr_mult: 1 149 | } 150 | 151 | } 152 | 153 | layer { 154 | name: "Sigmoid3" 155 | type: "Sigmoid" 156 | bottom: "conv3" 157 | top: "Sigmoid3" 158 | } 159 | 160 | layer { 161 | name: "pool3" 162 | type: "Pooling" 163 | bottom: "Sigmoid3" 164 | top: "pool3" 165 | pooling_param { 166 | pool: AVE 167 | kernel_size: 3 168 | stride: 2 169 | } 170 | } 171 | 172 | layer { 173 | name: "ip1" 174 | type: "InnerProduct" 175 | bottom: "pool3" 176 | top: "ip1" 177 | param { 178 | lr_mult: 1 179 | decay_mult: 0 180 | } 181 | param { 182 | lr_mult: 2 183 | decay_mult: 0 184 | } 185 | inner_product_param { 186 | num_output: 10 187 | weight_filler { 188 | type: "gaussian" 189 | std: 0.01 190 | } 191 | bias_filler { 192 | type: "constant" 193 | } 194 | } 195 | } 196 | layer { 197 | name: "accuracy" 198 | type: "Accuracy" 199 | bottom: "ip1" 200 | bottom: "label" 201 | top: "accuracy" 202 | include { 203 | phase: TEST 204 | } 205 | } 206 | layer { 207 | name: "loss" 208 | type: "SoftmaxWithLoss" 209 | bottom: "ip1" 210 | bottom: "label" 211 | top: "loss" 212 | } 213 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_sigmoid_train_test_bn.prototxt: -------------------------------------------------------------------------------- 1 | name: "CIFAR10_full" 2 | layer { 3 | name: "cifar" 4 | type: "Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | transform_param { 11 | mean_file: "examples/cifar10/mean.binaryproto" 12 | } 13 | data_param { 14 | source: "examples/cifar10/cifar10_train_lmdb" 15 | batch_size: 100 16 | backend: LMDB 17 | } 18 | } 19 | layer { 20 | name: "cifar" 21 | type: "Data" 22 | top: "data" 23 | top: "label" 24 | include { 25 | phase: TEST 26 | } 27 | transform_param { 28 | mean_file: "examples/cifar10/mean.binaryproto" 29 | } 30 | data_param { 31 | source: "examples/cifar10/cifar10_test_lmdb" 32 | batch_size: 1000 33 | backend: LMDB 34 | } 35 | } 36 | layer { 37 | name: "conv1" 38 | type: "Convolution" 39 | bottom: "data" 40 | top: "conv1" 41 | param { 42 | lr_mult: 1 43 | } 44 | convolution_param { 45 | num_output: 32 46 | pad: 2 47 | kernel_size: 5 48 | stride: 1 49 | bias_term: false 50 | weight_filler { 51 | type: "gaussian" 52 | std: 0.0001 53 | } 54 | } 55 | } 56 | layer { 57 | name: "pool1" 58 | type: "Pooling" 59 | bottom: "conv1" 60 | top: "pool1" 61 | pooling_param { 62 | pool: MAX 63 | kernel_size: 3 64 | stride: 2 65 | } 66 | } 67 | 68 | layer { 69 | name: "bn1" 70 | type: "BatchNorm" 71 | bottom: "pool1" 72 | top: "bn1" 73 | param { 74 | lr_mult: 0 75 | } 76 | param { 77 | lr_mult: 0 78 | } 79 | param { 80 | lr_mult: 0 81 | } 82 | } 83 | 84 | layer { 85 | name: "Sigmoid1" 86 | type: "Sigmoid" 87 | bottom: "bn1" 88 | top: "Sigmoid1" 89 | } 90 | 91 | layer { 92 | name: "conv2" 93 | type: "Convolution" 94 | bottom: "Sigmoid1" 95 | top: "conv2" 96 | param { 97 | lr_mult: 1 98 | } 99 | convolution_param { 100 | num_output: 32 101 | pad: 2 102 | kernel_size: 5 103 | stride: 1 104 | bias_term: false 105 | weight_filler { 106 | type: "gaussian" 107 | std: 0.01 108 | } 109 | } 110 | } 111 | 112 | layer { 113 | name: "bn2" 114 | type: "BatchNorm" 115 | bottom: "conv2" 116 | top: "bn2" 117 | param { 118 | lr_mult: 0 119 | } 120 | param { 121 | lr_mult: 0 122 | } 123 | param { 124 | lr_mult: 0 125 | } 126 | } 127 | 128 | layer { 129 | name: "Sigmoid2" 130 | type: "Sigmoid" 131 | bottom: "bn2" 132 | top: "Sigmoid2" 133 | } 134 | layer { 135 | name: "pool2" 136 | type: "Pooling" 137 | bottom: "Sigmoid2" 138 | top: "pool2" 139 | pooling_param { 140 | pool: AVE 141 | kernel_size: 3 142 | stride: 2 143 | } 144 | } 145 | layer { 146 | name: "conv3" 147 | type: "Convolution" 148 | bottom: "pool2" 149 | top: "conv3" 150 | param { 151 | lr_mult: 1 152 | } 153 | convolution_param { 154 | num_output: 64 155 | pad: 2 156 | kernel_size: 5 157 | stride: 1 158 | bias_term: false 159 | weight_filler { 160 | type: "gaussian" 161 | std: 0.01 162 | } 163 | } 164 | } 165 | 166 | layer { 167 | name: "bn3" 168 | type: "BatchNorm" 169 | bottom: "conv3" 170 | top: "bn3" 171 | param { 172 | lr_mult: 0 173 | } 174 | param { 175 | lr_mult: 0 176 | } 177 | param { 178 | lr_mult: 0 179 | } 180 | } 181 | 182 | layer { 183 | name: "Sigmoid3" 184 | type: "Sigmoid" 185 | bottom: "bn3" 186 | top: "Sigmoid3" 187 | } 188 | layer { 189 | name: "pool3" 190 | type: "Pooling" 191 | bottom: "Sigmoid3" 192 | top: "pool3" 193 | pooling_param { 194 | pool: AVE 195 | kernel_size: 3 196 | stride: 2 197 | } 198 | } 199 | 200 | layer { 201 | name: "ip1" 202 | type: "InnerProduct" 203 | bottom: "pool3" 204 | top: "ip1" 205 | param { 206 | lr_mult: 1 207 | decay_mult: 1 208 | } 209 | param { 210 | lr_mult: 1 211 | decay_mult: 0 212 | } 213 | inner_product_param { 214 | num_output: 10 215 | weight_filler { 216 | type: "gaussian" 217 | std: 0.01 218 | } 219 | bias_filler { 220 | type: "constant" 221 | } 222 | } 223 | } 224 | layer { 225 | name: "accuracy" 226 | type: "Accuracy" 227 | bottom: "ip1" 228 | bottom: "label" 229 | top: "accuracy" 230 | include { 231 | phase: TEST 232 | } 233 | } 234 | layer { 235 | name: "loss" 236 | type: "SoftmaxWithLoss" 237 | bottom: "ip1" 238 | bottom: "label" 239 | top: "loss" 240 | } 241 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_solver.prototxt: -------------------------------------------------------------------------------- 1 | # reduce learning rate after 120 epochs (60000 iters) by factor 0f 10 2 | # then another factor of 10 after 10 more epochs (5000 iters) 3 | 4 | # The train/test net protocol buffer definition 5 | net: "examples/cifar10/cifar10_full_train_test.prototxt" 6 | # test_iter specifies how many forward passes the test should carry out. 7 | # In the case of CIFAR10, we have test batch size 100 and 100 test iterations, 8 | # covering the full 10,000 testing images. 9 | test_iter: 100 10 | # Carry out testing every 1000 training iterations. 11 | test_interval: 1000 12 | # The base learning rate, momentum and the weight decay of the network. 13 | base_lr: 0.001 14 | momentum: 0.9 15 | weight_decay: 0.004 16 | # The learning rate policy 17 | lr_policy: "fixed" 18 | # Display every 200 iterations 19 | display: 200 20 | # The maximum number of iterations 21 | max_iter: 60000 22 | # snapshot intermediate results 23 | snapshot: 10000 24 | snapshot_format: HDF5 25 | snapshot_prefix: "examples/cifar10/cifar10_full" 26 | # solver mode: CPU or GPU 27 | solver_mode: GPU 28 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_solver_lr1.prototxt: -------------------------------------------------------------------------------- 1 | # reduce learning rate after 120 epochs (60000 iters) by factor 0f 10 2 | # then another factor of 10 after 10 more epochs (5000 iters) 3 | 4 | # The train/test net protocol buffer definition 5 | net: "examples/cifar10/cifar10_full_train_test.prototxt" 6 | # test_iter specifies how many forward passes the test should carry out. 7 | # In the case of CIFAR10, we have test batch size 100 and 100 test iterations, 8 | # covering the full 10,000 testing images. 9 | test_iter: 100 10 | # Carry out testing every 1000 training iterations. 11 | test_interval: 1000 12 | # The base learning rate, momentum and the weight decay of the network. 13 | base_lr: 0.0001 14 | momentum: 0.9 15 | weight_decay: 0.004 16 | # The learning rate policy 17 | lr_policy: "fixed" 18 | # Display every 200 iterations 19 | display: 200 20 | # The maximum number of iterations 21 | max_iter: 65000 22 | # snapshot intermediate results 23 | snapshot: 5000 24 | snapshot_format: HDF5 25 | snapshot_prefix: "examples/cifar10/cifar10_full" 26 | # solver mode: CPU or GPU 27 | solver_mode: GPU 28 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_solver_lr2.prototxt: -------------------------------------------------------------------------------- 1 | # reduce learning rate after 120 epochs (60000 iters) by factor 0f 10 2 | # then another factor of 10 after 10 more epochs (5000 iters) 3 | 4 | # The train/test net protocol buffer definition 5 | net: "examples/cifar10/cifar10_full_train_test.prototxt" 6 | # test_iter specifies how many forward passes the test should carry out. 7 | # In the case of CIFAR10, we have test batch size 100 and 100 test iterations, 8 | # covering the full 10,000 testing images. 9 | test_iter: 100 10 | # Carry out testing every 1000 training iterations. 11 | test_interval: 1000 12 | # The base learning rate, momentum and the weight decay of the network. 13 | base_lr: 0.00001 14 | momentum: 0.9 15 | weight_decay: 0.004 16 | # The learning rate policy 17 | lr_policy: "fixed" 18 | # Display every 200 iterations 19 | display: 200 20 | # The maximum number of iterations 21 | max_iter: 70000 22 | # snapshot intermediate results 23 | snapshot: 5000 24 | snapshot_format: HDF5 25 | snapshot_prefix: "examples/cifar10/cifar10_full" 26 | # solver mode: CPU or GPU 27 | solver_mode: GPU 28 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_full_train_test.prototxt: -------------------------------------------------------------------------------- 1 | name: "CIFAR10_full" 2 | layer { 3 | name: "cifar" 4 | type: "Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | transform_param { 11 | mean_file: "examples/cifar10/mean.binaryproto" 12 | } 13 | data_param { 14 | source: "examples/cifar10/cifar10_train_lmdb" 15 | batch_size: 100 16 | backend: LMDB 17 | } 18 | } 19 | layer { 20 | name: "cifar" 21 | type: "Data" 22 | top: "data" 23 | top: "label" 24 | include { 25 | phase: TEST 26 | } 27 | transform_param { 28 | mean_file: "examples/cifar10/mean.binaryproto" 29 | } 30 | data_param { 31 | source: "examples/cifar10/cifar10_test_lmdb" 32 | batch_size: 100 33 | backend: LMDB 34 | } 35 | } 36 | layer { 37 | name: "conv1" 38 | type: "Convolution" 39 | bottom: "data" 40 | top: "conv1" 41 | param { 42 | lr_mult: 1 43 | } 44 | param { 45 | lr_mult: 2 46 | } 47 | convolution_param { 48 | num_output: 32 49 | pad: 2 50 | kernel_size: 5 51 | stride: 1 52 | weight_filler { 53 | type: "gaussian" 54 | std: 0.0001 55 | } 56 | bias_filler { 57 | type: "constant" 58 | } 59 | } 60 | } 61 | layer { 62 | name: "pool1" 63 | type: "Pooling" 64 | bottom: "conv1" 65 | top: "pool1" 66 | pooling_param { 67 | pool: MAX 68 | kernel_size: 3 69 | stride: 2 70 | } 71 | } 72 | layer { 73 | name: "relu1" 74 | type: "ReLU" 75 | bottom: "pool1" 76 | top: "pool1" 77 | } 78 | layer { 79 | name: "norm1" 80 | type: "LRN" 81 | bottom: "pool1" 82 | top: "norm1" 83 | lrn_param { 84 | local_size: 3 85 | alpha: 5e-05 86 | beta: 0.75 87 | norm_region: WITHIN_CHANNEL 88 | } 89 | } 90 | layer { 91 | name: "conv2" 92 | type: "Convolution" 93 | bottom: "norm1" 94 | top: "conv2" 95 | param { 96 | lr_mult: 1 97 | } 98 | param { 99 | lr_mult: 2 100 | } 101 | convolution_param { 102 | num_output: 32 103 | pad: 2 104 | kernel_size: 5 105 | stride: 1 106 | weight_filler { 107 | type: "gaussian" 108 | std: 0.01 109 | } 110 | bias_filler { 111 | type: "constant" 112 | } 113 | } 114 | } 115 | layer { 116 | name: "relu2" 117 | type: "ReLU" 118 | bottom: "conv2" 119 | top: "conv2" 120 | } 121 | layer { 122 | name: "pool2" 123 | type: "Pooling" 124 | bottom: "conv2" 125 | top: "pool2" 126 | pooling_param { 127 | pool: AVE 128 | kernel_size: 3 129 | stride: 2 130 | } 131 | } 132 | layer { 133 | name: "norm2" 134 | type: "LRN" 135 | bottom: "pool2" 136 | top: "norm2" 137 | lrn_param { 138 | local_size: 3 139 | alpha: 5e-05 140 | beta: 0.75 141 | norm_region: WITHIN_CHANNEL 142 | } 143 | } 144 | layer { 145 | name: "conv3" 146 | type: "Convolution" 147 | bottom: "norm2" 148 | top: "conv3" 149 | convolution_param { 150 | num_output: 64 151 | pad: 2 152 | kernel_size: 5 153 | stride: 1 154 | weight_filler { 155 | type: "gaussian" 156 | std: 0.01 157 | } 158 | bias_filler { 159 | type: "constant" 160 | } 161 | } 162 | } 163 | layer { 164 | name: "relu3" 165 | type: "ReLU" 166 | bottom: "conv3" 167 | top: "conv3" 168 | } 169 | layer { 170 | name: "pool3" 171 | type: "Pooling" 172 | bottom: "conv3" 173 | top: "pool3" 174 | pooling_param { 175 | pool: AVE 176 | kernel_size: 3 177 | stride: 2 178 | } 179 | } 180 | layer { 181 | name: "ip1" 182 | type: "InnerProduct" 183 | bottom: "pool3" 184 | top: "ip1" 185 | param { 186 | lr_mult: 1 187 | decay_mult: 250 188 | } 189 | param { 190 | lr_mult: 2 191 | decay_mult: 0 192 | } 193 | inner_product_param { 194 | num_output: 10 195 | weight_filler { 196 | type: "gaussian" 197 | std: 0.01 198 | } 199 | bias_filler { 200 | type: "constant" 201 | } 202 | } 203 | } 204 | layer { 205 | name: "accuracy" 206 | type: "Accuracy" 207 | bottom: "ip1" 208 | bottom: "label" 209 | top: "accuracy" 210 | include { 211 | phase: TEST 212 | } 213 | } 214 | layer { 215 | name: "loss" 216 | type: "SoftmaxWithLoss" 217 | bottom: "ip1" 218 | bottom: "label" 219 | top: "loss" 220 | } 221 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_quick.prototxt: -------------------------------------------------------------------------------- 1 | name: "CIFAR10_quick_test" 2 | layer { 3 | name: "data" 4 | type: "Input" 5 | top: "data" 6 | input_param { shape: { dim: 1 dim: 3 dim: 32 dim: 32 } } 7 | } 8 | layer { 9 | name: "conv1" 10 | type: "Convolution" 11 | bottom: "data" 12 | top: "conv1" 13 | param { 14 | lr_mult: 1 15 | } 16 | param { 17 | lr_mult: 2 18 | } 19 | convolution_param { 20 | num_output: 32 21 | pad: 2 22 | kernel_size: 5 23 | stride: 1 24 | } 25 | } 26 | layer { 27 | name: "pool1" 28 | type: "Pooling" 29 | bottom: "conv1" 30 | top: "pool1" 31 | pooling_param { 32 | pool: MAX 33 | kernel_size: 3 34 | stride: 2 35 | } 36 | } 37 | layer { 38 | name: "relu1" 39 | type: "ReLU" 40 | bottom: "pool1" 41 | top: "pool1" 42 | } 43 | layer { 44 | name: "conv2" 45 | type: "Convolution" 46 | bottom: "pool1" 47 | top: "conv2" 48 | param { 49 | lr_mult: 1 50 | } 51 | param { 52 | lr_mult: 2 53 | } 54 | convolution_param { 55 | num_output: 32 56 | pad: 2 57 | kernel_size: 5 58 | stride: 1 59 | } 60 | } 61 | layer { 62 | name: "relu2" 63 | type: "ReLU" 64 | bottom: "conv2" 65 | top: "conv2" 66 | } 67 | layer { 68 | name: "pool2" 69 | type: "Pooling" 70 | bottom: "conv2" 71 | top: "pool2" 72 | pooling_param { 73 | pool: AVE 74 | kernel_size: 3 75 | stride: 2 76 | } 77 | } 78 | layer { 79 | name: "conv3" 80 | type: "Convolution" 81 | bottom: "pool2" 82 | top: "conv3" 83 | param { 84 | lr_mult: 1 85 | } 86 | param { 87 | lr_mult: 2 88 | } 89 | convolution_param { 90 | num_output: 64 91 | pad: 2 92 | kernel_size: 5 93 | stride: 1 94 | } 95 | } 96 | layer { 97 | name: "relu3" 98 | type: "ReLU" 99 | bottom: "conv3" 100 | top: "conv3" 101 | } 102 | layer { 103 | name: "pool3" 104 | type: "Pooling" 105 | bottom: "conv3" 106 | top: "pool3" 107 | pooling_param { 108 | pool: AVE 109 | kernel_size: 3 110 | stride: 2 111 | } 112 | } 113 | layer { 114 | name: "ip1" 115 | type: "InnerProduct" 116 | bottom: "pool3" 117 | top: "ip1" 118 | param { 119 | lr_mult: 1 120 | } 121 | param { 122 | lr_mult: 2 123 | } 124 | inner_product_param { 125 | num_output: 64 126 | } 127 | } 128 | layer { 129 | name: "ip2" 130 | type: "InnerProduct" 131 | bottom: "ip1" 132 | top: "ip2" 133 | param { 134 | lr_mult: 1 135 | } 136 | param { 137 | lr_mult: 2 138 | } 139 | inner_product_param { 140 | num_output: 10 141 | } 142 | } 143 | layer { 144 | name: "prob" 145 | type: "Softmax" 146 | bottom: "ip2" 147 | top: "prob" 148 | } 149 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_quick_solver.prototxt: -------------------------------------------------------------------------------- 1 | # reduce the learning rate after 8 epochs (4000 iters) by a factor of 10 2 | 3 | # The train/test net protocol buffer definition 4 | net: "examples/cifar10/cifar10_quick_train_test.prototxt" 5 | # test_iter specifies how many forward passes the test should carry out. 6 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 7 | # covering the full 10,000 testing images. 8 | test_iter: 100 9 | # Carry out testing every 500 training iterations. 10 | test_interval: 500 11 | # The base learning rate, momentum and the weight decay of the network. 12 | base_lr: 0.001 13 | momentum: 0.9 14 | weight_decay: 0.004 15 | # The learning rate policy 16 | lr_policy: "fixed" 17 | # Display every 100 iterations 18 | display: 100 19 | # The maximum number of iterations 20 | max_iter: 4000 21 | # snapshot intermediate results 22 | snapshot: 4000 23 | snapshot_prefix: "examples/cifar10/cifar10_quick" 24 | # solver mode: CPU or GPU 25 | solver_mode: GPU 26 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_quick_solver_lr1.prototxt: -------------------------------------------------------------------------------- 1 | # reduce the learning rate after 8 epochs (4000 iters) by a factor of 10 2 | 3 | # The train/test net protocol buffer definition 4 | net: "examples/cifar10/cifar10_quick_train_test.prototxt" 5 | # test_iter specifies how many forward passes the test should carry out. 6 | # In the case of MNIST, we have test batch size 100 and 100 test iterations, 7 | # covering the full 10,000 testing images. 8 | test_iter: 100 9 | # Carry out testing every 500 training iterations. 10 | test_interval: 500 11 | # The base learning rate, momentum and the weight decay of the network. 12 | base_lr: 0.0001 13 | momentum: 0.9 14 | weight_decay: 0.004 15 | # The learning rate policy 16 | lr_policy: "fixed" 17 | # Display every 100 iterations 18 | display: 100 19 | # The maximum number of iterations 20 | max_iter: 5000 21 | # snapshot intermediate results 22 | snapshot: 5000 23 | snapshot_format: HDF5 24 | snapshot_prefix: "examples/cifar10/cifar10_quick" 25 | # solver mode: CPU or GPU 26 | solver_mode: GPU 27 | -------------------------------------------------------------------------------- /2.cifar10/cifar10_quick_train_test.prototxt: -------------------------------------------------------------------------------- 1 | name: "CIFAR10_quick" 2 | layer { 3 | name: "cifar" 4 | type: "Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | transform_param { 11 | mean_file: "examples/cifar10/mean.binaryproto" 12 | } 13 | data_param { 14 | source: "examples/cifar10/cifar10_train_lmdb" 15 | batch_size: 100 16 | backend: LMDB 17 | } 18 | } 19 | layer { 20 | name: "cifar" 21 | type: "Data" 22 | top: "data" 23 | top: "label" 24 | include { 25 | phase: TEST 26 | } 27 | transform_param { 28 | mean_file: "examples/cifar10/mean.binaryproto" 29 | } 30 | data_param { 31 | source: "examples/cifar10/cifar10_test_lmdb" 32 | batch_size: 100 33 | backend: LMDB 34 | } 35 | } 36 | layer { 37 | name: "conv1" 38 | type: "Convolution" 39 | bottom: "data" 40 | top: "conv1" 41 | param { 42 | lr_mult: 1 43 | } 44 | param { 45 | lr_mult: 2 46 | } 47 | convolution_param { 48 | num_output: 32 49 | pad: 2 50 | kernel_size: 5 51 | stride: 1 52 | weight_filler { 53 | type: "gaussian" 54 | std: 0.0001 55 | } 56 | bias_filler { 57 | type: "constant" 58 | } 59 | } 60 | } 61 | layer { 62 | name: "pool1" 63 | type: "Pooling" 64 | bottom: "conv1" 65 | top: "pool1" 66 | pooling_param { 67 | pool: MAX 68 | kernel_size: 3 69 | stride: 2 70 | } 71 | } 72 | layer { 73 | name: "relu1" 74 | type: "ReLU" 75 | bottom: "pool1" 76 | top: "pool1" 77 | } 78 | layer { 79 | name: "conv2" 80 | type: "Convolution" 81 | bottom: "pool1" 82 | top: "conv2" 83 | param { 84 | lr_mult: 1 85 | } 86 | param { 87 | lr_mult: 2 88 | } 89 | convolution_param { 90 | num_output: 32 91 | pad: 2 92 | kernel_size: 5 93 | stride: 1 94 | weight_filler { 95 | type: "gaussian" 96 | std: 0.01 97 | } 98 | bias_filler { 99 | type: "constant" 100 | } 101 | } 102 | } 103 | layer { 104 | name: "relu2" 105 | type: "ReLU" 106 | bottom: "conv2" 107 | top: "conv2" 108 | } 109 | layer { 110 | name: "pool2" 111 | type: "Pooling" 112 | bottom: "conv2" 113 | top: "pool2" 114 | pooling_param { 115 | pool: AVE 116 | kernel_size: 3 117 | stride: 2 118 | } 119 | } 120 | layer { 121 | name: "conv3" 122 | type: "Convolution" 123 | bottom: "pool2" 124 | top: "conv3" 125 | param { 126 | lr_mult: 1 127 | } 128 | param { 129 | lr_mult: 2 130 | } 131 | convolution_param { 132 | num_output: 64 133 | pad: 2 134 | kernel_size: 5 135 | stride: 1 136 | weight_filler { 137 | type: "gaussian" 138 | std: 0.01 139 | } 140 | bias_filler { 141 | type: "constant" 142 | } 143 | } 144 | } 145 | layer { 146 | name: "relu3" 147 | type: "ReLU" 148 | bottom: "conv3" 149 | top: "conv3" 150 | } 151 | layer { 152 | name: "pool3" 153 | type: "Pooling" 154 | bottom: "conv3" 155 | top: "pool3" 156 | pooling_param { 157 | pool: AVE 158 | kernel_size: 3 159 | stride: 2 160 | } 161 | } 162 | layer { 163 | name: "ip1" 164 | type: "InnerProduct" 165 | bottom: "pool3" 166 | top: "ip1" 167 | param { 168 | lr_mult: 1 169 | } 170 | param { 171 | lr_mult: 2 172 | } 173 | inner_product_param { 174 | num_output: 64 175 | weight_filler { 176 | type: "gaussian" 177 | std: 0.1 178 | } 179 | bias_filler { 180 | type: "constant" 181 | } 182 | } 183 | } 184 | layer { 185 | name: "ip2" 186 | type: "InnerProduct" 187 | bottom: "ip1" 188 | top: "ip2" 189 | param { 190 | lr_mult: 1 191 | } 192 | param { 193 | lr_mult: 2 194 | } 195 | inner_product_param { 196 | num_output: 10 197 | weight_filler { 198 | type: "gaussian" 199 | std: 0.1 200 | } 201 | bias_filler { 202 | type: "constant" 203 | } 204 | } 205 | } 206 | layer { 207 | name: "accuracy" 208 | type: "Accuracy" 209 | bottom: "ip2" 210 | bottom: "label" 211 | top: "accuracy" 212 | include { 213 | phase: TEST 214 | } 215 | } 216 | layer { 217 | name: "loss" 218 | type: "SoftmaxWithLoss" 219 | bottom: "ip2" 220 | bottom: "label" 221 | top: "loss" 222 | } 223 | -------------------------------------------------------------------------------- /2.cifar10/convert_cifar_data.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // This script converts the CIFAR dataset to the leveldb format used 3 | // by caffe to perform classification. 4 | // Usage: 5 | // convert_cifar_data input_folder output_db_file 6 | // The CIFAR dataset could be downloaded at 7 | // http://www.cs.toronto.edu/~kriz/cifar.html 8 | 9 | #include // NOLINT(readability/streams) 10 | #include 11 | 12 | #include "boost/scoped_ptr.hpp" 13 | #include "glog/logging.h" 14 | #include "google/protobuf/text_format.h" 15 | #include "stdint.h" 16 | 17 | #include "caffe/proto/caffe.pb.h" 18 | #include "caffe/util/db.hpp" 19 | #include "caffe/util/format.hpp" 20 | 21 | using caffe::Datum; 22 | using boost::scoped_ptr; 23 | using std::string; 24 | namespace db = caffe::db; 25 | 26 | const int kCIFARSize = 32; 27 | const int kCIFARImageNBytes = 3072; 28 | const int kCIFARBatchSize = 10000; 29 | const int kCIFARTrainBatches = 5; 30 | 31 | void read_image(std::ifstream* file, int* label, char* buffer) { 32 | char label_char; 33 | file->read(&label_char, 1); 34 | *label = label_char; 35 | file->read(buffer, kCIFARImageNBytes); 36 | return; 37 | } 38 | 39 | void convert_dataset(const string& input_folder, const string& output_folder, 40 | const string& db_type) { 41 | scoped_ptr train_db(db::GetDB(db_type)); 42 | train_db->Open(output_folder + "/cifar10_train_" + db_type, db::NEW); 43 | scoped_ptr txn(train_db->NewTransaction()); 44 | // Data buffer 45 | int label; 46 | char str_buffer[kCIFARImageNBytes]; 47 | Datum datum; 48 | datum.set_channels(3); 49 | datum.set_height(kCIFARSize); 50 | datum.set_width(kCIFARSize); 51 | 52 | LOG(INFO) << "Writing Training data"; 53 | for (int fileid = 0; fileid < kCIFARTrainBatches; ++fileid) { 54 | // Open files 55 | LOG(INFO) << "Training Batch " << fileid + 1; 56 | string batchFileName = input_folder + "/data_batch_" 57 | + caffe::format_int(fileid+1) + ".bin"; 58 | std::ifstream data_file(batchFileName.c_str(), 59 | std::ios::in | std::ios::binary); 60 | CHECK(data_file) << "Unable to open train file #" << fileid + 1; 61 | for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) { 62 | read_image(&data_file, &label, str_buffer); 63 | datum.set_label(label); 64 | datum.set_data(str_buffer, kCIFARImageNBytes); 65 | string out; 66 | CHECK(datum.SerializeToString(&out)); 67 | txn->Put(caffe::format_int(fileid * kCIFARBatchSize + itemid, 5), out); 68 | } 69 | } 70 | txn->Commit(); 71 | train_db->Close(); 72 | 73 | LOG(INFO) << "Writing Testing data"; 74 | scoped_ptr test_db(db::GetDB(db_type)); 75 | test_db->Open(output_folder + "/cifar10_test_" + db_type, db::NEW); 76 | txn.reset(test_db->NewTransaction()); 77 | // Open files 78 | std::ifstream data_file((input_folder + "/test_batch.bin").c_str(), 79 | std::ios::in | std::ios::binary); 80 | CHECK(data_file) << "Unable to open test file."; 81 | for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) { 82 | read_image(&data_file, &label, str_buffer); 83 | datum.set_label(label); 84 | datum.set_data(str_buffer, kCIFARImageNBytes); 85 | string out; 86 | CHECK(datum.SerializeToString(&out)); 87 | txn->Put(caffe::format_int(itemid, 5), out); 88 | } 89 | txn->Commit(); 90 | test_db->Close(); 91 | } 92 | 93 | int main(int argc, char** argv) { 94 | FLAGS_alsologtostderr = 1; 95 | 96 | if (argc != 4) { 97 | printf("This script converts the CIFAR dataset to the leveldb format used\n" 98 | "by caffe to perform classification.\n" 99 | "Usage:\n" 100 | " convert_cifar_data input_folder output_folder db_type\n" 101 | "Where the input folder should contain the binary batch files.\n" 102 | "The CIFAR dataset could be downloaded at\n" 103 | " http://www.cs.toronto.edu/~kriz/cifar.html\n" 104 | "You should gunzip them after downloading.\n"); 105 | } else { 106 | google::InitGoogleLogging(argv[0]); 107 | convert_dataset(string(argv[1]), string(argv[2]), string(argv[3])); 108 | } 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /2.cifar10/create_cifar10.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # This script converts the cifar data into leveldb format. 3 | set -e 4 | 5 | EXAMPLE=examples/cifar10 6 | DATA=data/cifar10 7 | DBTYPE=lmdb 8 | 9 | echo "Creating $DBTYPE..." 10 | 11 | rm -rf $EXAMPLE/cifar10_train_$DBTYPE $EXAMPLE/cifar10_test_$DBTYPE 12 | 13 | ./build/examples/cifar10/convert_cifar_data.bin $DATA $EXAMPLE $DBTYPE 14 | 15 | echo "Computing image mean..." 16 | 17 | ./build/tools/compute_image_mean -backend=$DBTYPE \ 18 | $EXAMPLE/cifar10_train_$DBTYPE $EXAMPLE/mean.binaryproto 19 | 20 | echo "Done." 21 | -------------------------------------------------------------------------------- /2.cifar10/readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | 제목: CIFAR-10 튜토리얼 3 | 설명: CIFAR-10 data로 Caffe를 사용해 학습 및 테스트를 해본다. 4 | 카테고리: 튜토리얼 5 | include_in_docs: true 6 | priority: 5 7 | --- 8 | 9 | Alex's CIFAR-10 tutorial, Caffe style 10 | ===================================== 11 | Alex Krizhevsky의 [cuda-convnet](https://code.google.com/p/cuda-convnet/)에 신경망 모델 정의와 파라미터들 그리고 CIFAR-10 데이터셋을 활용해 훌륭한 결과를 도출한 학습 과정에 대해 상세히 나와있다. 이 예시는 Caffe에서 결과를 재구성한 것입니다. 12 | 13 | 해당 튜토리얼을 진행하기 앞서 Caffe를 성공적으로 컴파일 해야합니다. 아니라면 [설치 페이지](https://github.com/Hahnnz/Caffe_Tutorial/wiki/0.-Caffe-%EC%84%A4%EC%B9%98-%EB%B0%A9%EB%B2%95)를 참고하여 설치 및 컴파일을 완료해주시기를 바랍니다. 14 | 이 튜토리얼에서는 Caffe가 튜토리얼 레퍼지토리에 존재해야합니다. 15 | 16 | 17 | 데이터셋 준비하기 18 | ------------------- 19 | 20 | 여러분은 먼저 [CIFAR-10 웹사이트](http://www.cs.toronto.edu/~kriz/cifar.html)에서 데이터를 다운 받고 형식을 변환해주어야합니다. 이는 다음과 같은 명령어를 입력해주어 파일을 실행시켜주면 됩니다. 21 | 22 | cd $CAFFE_TUTORIAL/2.cifar10 23 | ./get_cifar10.sh 24 | ./create_cifar10.sh 25 | 26 | 만약 `wget`이나 `gunzip`이 설치되어있지 않다는 문제가 발생하면, 이 둘 각각을 설치해주어야만 합니다. 그래서 최종적으로 다음 두 Shell 파일들을 실행시킨후에는 `./cifar10-leveldb`라는 데이터셋 파일과 `./mean.binaryproto`라는 이미지 평균 값 파일을 확인하실 수 있습니다.  27 | 28 | 29 | 신경망 모델 30 | --------- 31 | CIFAR-10 model은 특징 추출을 위한 convolution, Subsampling을 위한pooling, 비선형성을 주기 위한rectified linear unit (ReLU) , 그리고 선형 분류기를 사용한 local contrast normalization으로 구성된 CNN(Convolution Neural Network)입니다. 이 신경망 모델은 `./2.cifar10` 디렉토리 안의 `cifar10_quick_train_test.prototxt` 파일에 모델이 정의되어 있습니다. 32 | 33 | 34 | "빠른" 신경망 모델 학습 및 테스트하기 35 | -------------------------------------- 36 | 37 | 신경망 모델을 학습시키는 것은 이전에 튜토리얼에서 수행해보았던 것처럼 Solver protobuf파일과 신경망 정의 protobuf파일을 작성한 후에 학습을 진행할 수 있습니다. ([1.MNIST 튜토리얼](https://github.com/Hahnnz/Caffe_Tutorial/tree/master/1.MNIST)을 참고해 주세요!) `train_quick.sh`을 실행시키면 간단하게 학습을 수행시킬 수 있습니다. 38 | 39 | 40 |    cd $CAFFE_TUTORIAL/2.cifar10 41 |    ./train_quick.sh 42 | 43 | `train_quick.sh`는 간단한 스크립트니, 한번 들어가서 둘러보세요. 하지만 학습을 시키기 위한 메인 툴은 `caffe`내의 툴인 `train`과 `solver` protobuf 파일을 train_quick.sh가 인자로 사용을 합니다. 44 | 45 | 코드를 실행시켜보면 여러분들은 다음과 같이 마구마구 날라오는 상당량의 메세지를 볼 것입니다. 46 | 47 | I0317 21:52:48.945710 2008298256 net.cpp:74] Creating Layer conv1 48 | I0317 21:52:48.945716 2008298256 net.cpp:84] conv1 <- data 49 | I0317 21:52:48.945725 2008298256 net.cpp:110] conv1 -> conv1 50 | I0317 21:52:49.298691 2008298256 net.cpp:125] Top shape: 100 32 32 32 (3276800) 51 | I0317 21:52:49.298719 2008298256 net.cpp:151] conv1 needs backward computation. 52 | 53 | 이러한 메세지들은 여러분들에게 디버깅할 때 도움이 될만한 각 계층들, 그리고 그 계층의 출력 shape와 연결들에 대한 자세한 설명을 알려줍니다. 초기화를 한 뒤에 학습이 시작될 것입니다. 54 | 55 | I0317 21:52:49.309370 2008298256 net.cpp:166] Network initialization done. 56 | I0317 21:52:49.309376 2008298256 net.cpp:167] Memory required for Data 23790808 57 | I0317 21:52:49.309422 2008298256 solver.cpp:36] Solver scaffolding done. 58 | I0317 21:52:49.309447 2008298256 solver.cpp:47] Solving CIFAR10_quick_train 59 | 60 | Solver 세팅에 기반해 우리는 각 100번 반복마다 학습 손실 함수를 출력할 것이고, 매 반복 500회마다 신경망을 테스트해볼 것입니다. 여러분들은 아마 다음과 같은 메세지를 확인할 수 있습니다. 61 | 62 | I0317 21:53:12.179772 2008298256 solver.cpp:208] Iteration 100, lr = 0.001 63 | I0317 21:53:12.185698 2008298256 solver.cpp:65] Iteration 100, loss = 1.73643 64 | ... 65 | I0317 21:54:41.150030 2008298256 solver.cpp:87] Iteration 500, Testing net 66 | I0317 21:54:47.129461 2008298256 solver.cpp:114] Test score #0: 0.5504 67 | I0317 21:54:47.129500 2008298256 solver.cpp:114] Test score #1: 1.27805 68 | 69 | 70 | 각각 반복마다, `lr`는 그 반복의 학습률을 의미하며, `loss`는 학습 함수중 하나입니다. **테스트 단계의 출력으로는 score 0은 정확도, score 1는 테스트 Loss 함수를 의미**합니다. 71 | 72 | 그리고 어느 정도 기다려주면 학습이 완료될 것입니다! 73 | 74 | I0317 22:12:19.666914 2008298256 solver.cpp:87] Iteration 5000, Testing net 75 | I0317 22:12:25.580330 2008298256 solver.cpp:114] Test score #0: 0.7533 76 | I0317 22:12:25.580379 2008298256 solver.cpp:114] Test score #1: 0.739837 77 | I0317 22:12:25.587262 2008298256 solver.cpp:130] Snapshotting to cifar10_quick_iter_5000 78 | I0317 22:12:25.590215 2008298256 solver.cpp:137] Snapshotting solver state to cifar10_quick_iter_5000.solverstate 79 | I0317 22:12:25.592813 2008298256 solver.cpp:81] Optimization Done. 80 | 81 | 우리의 신경망 모델은 ~75%정도의 테스트 정확도를 달성했습니다. 신경망 모델 파라미터들은 binary protobuf형태로 CPU든 GPU든 언제든지 사용할 수 있는 82 | 83 | cifar10_quick_iter_5000 84 | 85 | 라는 이름으로 저장되어 있습니다. 새로운 데이터를 불러와서 테스트할 수 있는 `./2.cifar10/cifar10_quick.prototxt`라는 파일 디플로이먼트 모델을 참고해보세요. 86 | 87 | 왜 GPU에서 학습을 하는건가요? 88 | ------------------- 89 | 90 | CIFAR-10 데이터 세트는 여전히 그 세트의 크기가 작지만 GPU를 사용하여 학습을 하는데 매력을 느낄수 있을만큼의 충분한 데이터세트를 확보하고 있습니다. 91 | 92 | CPU와 GPU의 학습 속도를 비교하기 위해서는 `cifar*solver.prototxt`에 있는 한줄만 바꾸어주면 됩니다. 93 | 94 | # solver mode: CPU or GPU 95 | solver_mode: CPU 96 | 97 | 98 | 이렇게 설정하면 CPU를 사용할 수 있습니다. 99 | 100 | -------------------------------------------------------------------------------- /2.cifar10/train_full.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | TOOLS=./build/tools 5 | 6 | $TOOLS/caffe train \ 7 | --solver=examples/cifar10/cifar10_full_solver.prototxt $@ 8 | 9 | # reduce learning rate by factor of 10 10 | $TOOLS/caffe train \ 11 | --solver=examples/cifar10/cifar10_full_solver_lr1.prototxt \ 12 | --snapshot=examples/cifar10/cifar10_full_iter_60000.solverstate $@ 13 | 14 | # reduce learning rate by factor of 10 15 | $TOOLS/caffe train \ 16 | --solver=examples/cifar10/cifar10_full_solver_lr2.prototxt \ 17 | --snapshot=examples/cifar10/cifar10_full_iter_65000.solverstate $@ 18 | -------------------------------------------------------------------------------- /2.cifar10/train_full_sigmoid.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | TOOLS=./build/tools 5 | 6 | $TOOLS/caffe train \ 7 | --solver=examples/cifar10/cifar10_full_sigmoid_solver.prototxt $@ 8 | 9 | -------------------------------------------------------------------------------- /2.cifar10/train_full_sigmoid_bn.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | TOOLS=./build/tools 5 | 6 | $TOOLS/caffe train \ 7 | --solver=examples/cifar10/cifar10_full_sigmoid_solver_bn.prototxt $@ 8 | 9 | -------------------------------------------------------------------------------- /2.cifar10/train_quick.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | TOOLS=./build/tools 5 | 6 | $TOOLS/caffe train \ 7 | --solver=examples/cifar10/cifar10_quick_solver.prototxt $@ 8 | 9 | # reduce learning rate by factor of 10 after 8 epochs 10 | $TOOLS/caffe train \ 11 | --solver=examples/cifar10/cifar10_quick_solver_lr1.prototxt \ 12 | --snapshot=examples/cifar10/cifar10_quick_iter_4000.solverstate $@ 13 | -------------------------------------------------------------------------------- /3.Imagenet/bvlc_reference_caffenet/deploy.prototxt: -------------------------------------------------------------------------------- 1 | name: "CaffeNet" 2 | layer { 3 | name: "data" 4 | type: "Input" 5 | top: "data" 6 | input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } } 7 | } 8 | layer { 9 | name: "conv1" 10 | type: "Convolution" 11 | bottom: "data" 12 | top: "conv1" 13 | convolution_param { 14 | num_output: 96 15 | kernel_size: 11 16 | stride: 4 17 | } 18 | } 19 | layer { 20 | name: "relu1" 21 | type: "ReLU" 22 | bottom: "conv1" 23 | top: "conv1" 24 | } 25 | layer { 26 | name: "pool1" 27 | type: "Pooling" 28 | bottom: "conv1" 29 | top: "pool1" 30 | pooling_param { 31 | pool: MAX 32 | kernel_size: 3 33 | stride: 2 34 | } 35 | } 36 | layer { 37 | name: "norm1" 38 | type: "LRN" 39 | bottom: "pool1" 40 | top: "norm1" 41 | lrn_param { 42 | local_size: 5 43 | alpha: 0.0001 44 | beta: 0.75 45 | } 46 | } 47 | layer { 48 | name: "conv2" 49 | type: "Convolution" 50 | bottom: "norm1" 51 | top: "conv2" 52 | convolution_param { 53 | num_output: 256 54 | pad: 2 55 | kernel_size: 5 56 | group: 2 57 | } 58 | } 59 | layer { 60 | name: "relu2" 61 | type: "ReLU" 62 | bottom: "conv2" 63 | top: "conv2" 64 | } 65 | layer { 66 | name: "pool2" 67 | type: "Pooling" 68 | bottom: "conv2" 69 | top: "pool2" 70 | pooling_param { 71 | pool: MAX 72 | kernel_size: 3 73 | stride: 2 74 | } 75 | } 76 | layer { 77 | name: "norm2" 78 | type: "LRN" 79 | bottom: "pool2" 80 | top: "norm2" 81 | lrn_param { 82 | local_size: 5 83 | alpha: 0.0001 84 | beta: 0.75 85 | } 86 | } 87 | layer { 88 | name: "conv3" 89 | type: "Convolution" 90 | bottom: "norm2" 91 | top: "conv3" 92 | convolution_param { 93 | num_output: 384 94 | pad: 1 95 | kernel_size: 3 96 | } 97 | } 98 | layer { 99 | name: "relu3" 100 | type: "ReLU" 101 | bottom: "conv3" 102 | top: "conv3" 103 | } 104 | layer { 105 | name: "conv4" 106 | type: "Convolution" 107 | bottom: "conv3" 108 | top: "conv4" 109 | convolution_param { 110 | num_output: 384 111 | pad: 1 112 | kernel_size: 3 113 | group: 2 114 | } 115 | } 116 | layer { 117 | name: "relu4" 118 | type: "ReLU" 119 | bottom: "conv4" 120 | top: "conv4" 121 | } 122 | layer { 123 | name: "conv5" 124 | type: "Convolution" 125 | bottom: "conv4" 126 | top: "conv5" 127 | convolution_param { 128 | num_output: 256 129 | pad: 1 130 | kernel_size: 3 131 | group: 2 132 | } 133 | } 134 | layer { 135 | name: "relu5" 136 | type: "ReLU" 137 | bottom: "conv5" 138 | top: "conv5" 139 | } 140 | layer { 141 | name: "pool5" 142 | type: "Pooling" 143 | bottom: "conv5" 144 | top: "pool5" 145 | pooling_param { 146 | pool: MAX 147 | kernel_size: 3 148 | stride: 2 149 | } 150 | } 151 | layer { 152 | name: "fc6" 153 | type: "InnerProduct" 154 | bottom: "pool5" 155 | top: "fc6" 156 | inner_product_param { 157 | num_output: 4096 158 | } 159 | } 160 | layer { 161 | name: "relu6" 162 | type: "ReLU" 163 | bottom: "fc6" 164 | top: "fc6" 165 | } 166 | layer { 167 | name: "drop6" 168 | type: "Dropout" 169 | bottom: "fc6" 170 | top: "fc6" 171 | dropout_param { 172 | dropout_ratio: 0.5 173 | } 174 | } 175 | layer { 176 | name: "fc7" 177 | type: "InnerProduct" 178 | bottom: "fc6" 179 | top: "fc7" 180 | inner_product_param { 181 | num_output: 4096 182 | } 183 | } 184 | layer { 185 | name: "relu7" 186 | type: "ReLU" 187 | bottom: "fc7" 188 | top: "fc7" 189 | } 190 | layer { 191 | name: "drop7" 192 | type: "Dropout" 193 | bottom: "fc7" 194 | top: "fc7" 195 | dropout_param { 196 | dropout_ratio: 0.5 197 | } 198 | } 199 | layer { 200 | name: "fc8" 201 | type: "InnerProduct" 202 | bottom: "fc7" 203 | top: "fc8" 204 | inner_product_param { 205 | num_output: 1000 206 | } 207 | } 208 | layer { 209 | name: "prob" 210 | type: "Softmax" 211 | bottom: "fc8" 212 | top: "prob" 213 | } 214 | -------------------------------------------------------------------------------- /3.Imagenet/bvlc_reference_caffenet/readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: BAIR/BVLC CaffeNet Model 3 | caffemodel: bvlc_reference_caffenet.caffemodel 4 | caffemodel_url: http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel 5 | license: unrestricted 6 | sha1: 4c8d77deb20ea792f84eb5e6d0a11ca0a8660a46 7 | caffe_commit: 709dc15af4a06bebda027c1eb2b3f3e3375d5077 8 | --- 9 | 10 | This model is the result of following the Caffe [ImageNet model training instructions](http://caffe.berkeleyvision.org/gathered/examples/imagenet.html). 11 | It is a replication of the model described in the [AlexNet](http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks) publication with some differences: 12 | 13 | - not training with the relighting data-augmentation; 14 | - the order of pooling and normalization layers is switched (in CaffeNet, pooling is done before normalization). 15 | 16 | This model is snapshot of iteration 310,000. 17 | The best validation performance during training was iteration 313,000 with validation accuracy 57.412% and loss 1.82328. 18 | This model obtains a top-1 accuracy 57.4% and a top-5 accuracy 80.4% on the validation set, using just the center crop. 19 | (Using the average of 10 crops, (4 + 1 center) * 2 mirror, should obtain a bit higher accuracy still.) 20 | 21 | This model was trained by Jeff Donahue @jeffdonahue 22 | 23 | ## License 24 | 25 | This model is released for unrestricted use. 26 | -------------------------------------------------------------------------------- /3.Imagenet/bvlc_reference_caffenet/solver.prototxt: -------------------------------------------------------------------------------- 1 | net: "./bvlc_reference_caffenet/train_val.prototxt" 2 | test_iter: 1000 3 | test_interval: 1000 4 | base_lr: 0.01 5 | lr_policy: "step" 6 | gamma: 0.1 7 | stepsize: 100000 8 | display: 20 9 | max_iter: 450000 10 | momentum: 0.9 11 | weight_decay: 0.0005 12 | snapshot: 10000 13 | snapshot_prefix: "./bvlc_reference_caffenet/caffenet_train" 14 | solver_mode: CPU 15 | -------------------------------------------------------------------------------- /3.Imagenet/bvlc_reference_caffenet/train_val.prototxt: -------------------------------------------------------------------------------- 1 | name: "CaffeNet" 2 | layer { 3 | name: "data" 4 | type: "Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | transform_param { 11 | mirror: true 12 | crop_size: 227 13 | mean_file: "data/imagenet_mean.binaryproto" 14 | } 15 | # mean pixel / channel-wise mean instead of mean image 16 | # transform_param { 17 | # crop_size: 227 18 | # mean_value: 104 19 | # mean_value: 117 20 | # mean_value: 123 21 | # mirror: true 22 | # } 23 | data_param { 24 | source: "ilsvrc12_train_lmdb" 25 | batch_size: 256 26 | backend: LMDB 27 | } 28 | } 29 | layer { 30 | name: "data" 31 | type: "Data" 32 | top: "data" 33 | top: "label" 34 | include { 35 | phase: TEST 36 | } 37 | transform_param { 38 | mirror: false 39 | crop_size: 227 40 | mean_file: "data/imagenet_mean.binaryproto" 41 | } 42 | # mean pixel / channel-wise mean instead of mean image 43 | # transform_param { 44 | # crop_size: 227 45 | # mean_value: 104 46 | # mean_value: 117 47 | # mean_value: 123 48 | # mirror: false 49 | # } 50 | data_param { 51 | source: "ilsvrc12_val_lmdb" 52 | batch_size: 50 53 | backend: LMDB 54 | } 55 | } 56 | layer { 57 | name: "conv1" 58 | type: "Convolution" 59 | bottom: "data" 60 | top: "conv1" 61 | param { 62 | lr_mult: 1 63 | decay_mult: 1 64 | } 65 | param { 66 | lr_mult: 2 67 | decay_mult: 0 68 | } 69 | convolution_param { 70 | num_output: 96 71 | kernel_size: 11 72 | stride: 4 73 | weight_filler { 74 | type: "gaussian" 75 | std: 0.01 76 | } 77 | bias_filler { 78 | type: "constant" 79 | value: 0 80 | } 81 | } 82 | } 83 | layer { 84 | name: "relu1" 85 | type: "ReLU" 86 | bottom: "conv1" 87 | top: "conv1" 88 | } 89 | layer { 90 | name: "pool1" 91 | type: "Pooling" 92 | bottom: "conv1" 93 | top: "pool1" 94 | pooling_param { 95 | pool: MAX 96 | kernel_size: 3 97 | stride: 2 98 | } 99 | } 100 | layer { 101 | name: "norm1" 102 | type: "LRN" 103 | bottom: "pool1" 104 | top: "norm1" 105 | lrn_param { 106 | local_size: 5 107 | alpha: 0.0001 108 | beta: 0.75 109 | } 110 | } 111 | layer { 112 | name: "conv2" 113 | type: "Convolution" 114 | bottom: "norm1" 115 | top: "conv2" 116 | param { 117 | lr_mult: 1 118 | decay_mult: 1 119 | } 120 | param { 121 | lr_mult: 2 122 | decay_mult: 0 123 | } 124 | convolution_param { 125 | num_output: 256 126 | pad: 2 127 | kernel_size: 5 128 | group: 2 129 | weight_filler { 130 | type: "gaussian" 131 | std: 0.01 132 | } 133 | bias_filler { 134 | type: "constant" 135 | value: 1 136 | } 137 | } 138 | } 139 | layer { 140 | name: "relu2" 141 | type: "ReLU" 142 | bottom: "conv2" 143 | top: "conv2" 144 | } 145 | layer { 146 | name: "pool2" 147 | type: "Pooling" 148 | bottom: "conv2" 149 | top: "pool2" 150 | pooling_param { 151 | pool: MAX 152 | kernel_size: 3 153 | stride: 2 154 | } 155 | } 156 | layer { 157 | name: "norm2" 158 | type: "LRN" 159 | bottom: "pool2" 160 | top: "norm2" 161 | lrn_param { 162 | local_size: 5 163 | alpha: 0.0001 164 | beta: 0.75 165 | } 166 | } 167 | layer { 168 | name: "conv3" 169 | type: "Convolution" 170 | bottom: "norm2" 171 | top: "conv3" 172 | param { 173 | lr_mult: 1 174 | decay_mult: 1 175 | } 176 | param { 177 | lr_mult: 2 178 | decay_mult: 0 179 | } 180 | convolution_param { 181 | num_output: 384 182 | pad: 1 183 | kernel_size: 3 184 | weight_filler { 185 | type: "gaussian" 186 | std: 0.01 187 | } 188 | bias_filler { 189 | type: "constant" 190 | value: 0 191 | } 192 | } 193 | } 194 | layer { 195 | name: "relu3" 196 | type: "ReLU" 197 | bottom: "conv3" 198 | top: "conv3" 199 | } 200 | layer { 201 | name: "conv4" 202 | type: "Convolution" 203 | bottom: "conv3" 204 | top: "conv4" 205 | param { 206 | lr_mult: 1 207 | decay_mult: 1 208 | } 209 | param { 210 | lr_mult: 2 211 | decay_mult: 0 212 | } 213 | convolution_param { 214 | num_output: 384 215 | pad: 1 216 | kernel_size: 3 217 | group: 2 218 | weight_filler { 219 | type: "gaussian" 220 | std: 0.01 221 | } 222 | bias_filler { 223 | type: "constant" 224 | value: 1 225 | } 226 | } 227 | } 228 | layer { 229 | name: "relu4" 230 | type: "ReLU" 231 | bottom: "conv4" 232 | top: "conv4" 233 | } 234 | layer { 235 | name: "conv5" 236 | type: "Convolution" 237 | bottom: "conv4" 238 | top: "conv5" 239 | param { 240 | lr_mult: 1 241 | decay_mult: 1 242 | } 243 | param { 244 | lr_mult: 2 245 | decay_mult: 0 246 | } 247 | convolution_param { 248 | num_output: 256 249 | pad: 1 250 | kernel_size: 3 251 | group: 2 252 | weight_filler { 253 | type: "gaussian" 254 | std: 0.01 255 | } 256 | bias_filler { 257 | type: "constant" 258 | value: 1 259 | } 260 | } 261 | } 262 | layer { 263 | name: "relu5" 264 | type: "ReLU" 265 | bottom: "conv5" 266 | top: "conv5" 267 | } 268 | layer { 269 | name: "pool5" 270 | type: "Pooling" 271 | bottom: "conv5" 272 | top: "pool5" 273 | pooling_param { 274 | pool: MAX 275 | kernel_size: 3 276 | stride: 2 277 | } 278 | } 279 | layer { 280 | name: "fc6" 281 | type: "InnerProduct" 282 | bottom: "pool5" 283 | top: "fc6" 284 | param { 285 | lr_mult: 1 286 | decay_mult: 1 287 | } 288 | param { 289 | lr_mult: 2 290 | decay_mult: 0 291 | } 292 | inner_product_param { 293 | num_output: 4096 294 | weight_filler { 295 | type: "gaussian" 296 | std: 0.005 297 | } 298 | bias_filler { 299 | type: "constant" 300 | value: 1 301 | } 302 | } 303 | } 304 | layer { 305 | name: "relu6" 306 | type: "ReLU" 307 | bottom: "fc6" 308 | top: "fc6" 309 | } 310 | layer { 311 | name: "drop6" 312 | type: "Dropout" 313 | bottom: "fc6" 314 | top: "fc6" 315 | dropout_param { 316 | dropout_ratio: 0.5 317 | } 318 | } 319 | layer { 320 | name: "fc7" 321 | type: "InnerProduct" 322 | bottom: "fc6" 323 | top: "fc7" 324 | param { 325 | lr_mult: 1 326 | decay_mult: 1 327 | } 328 | param { 329 | lr_mult: 2 330 | decay_mult: 0 331 | } 332 | inner_product_param { 333 | num_output: 4096 334 | weight_filler { 335 | type: "gaussian" 336 | std: 0.005 337 | } 338 | bias_filler { 339 | type: "constant" 340 | value: 1 341 | } 342 | } 343 | } 344 | layer { 345 | name: "relu7" 346 | type: "ReLU" 347 | bottom: "fc7" 348 | top: "fc7" 349 | } 350 | layer { 351 | name: "drop7" 352 | type: "Dropout" 353 | bottom: "fc7" 354 | top: "fc7" 355 | dropout_param { 356 | dropout_ratio: 0.5 357 | } 358 | } 359 | layer { 360 | name: "fc8" 361 | type: "InnerProduct" 362 | bottom: "fc7" 363 | top: "fc8" 364 | param { 365 | lr_mult: 1 366 | decay_mult: 1 367 | } 368 | param { 369 | lr_mult: 2 370 | decay_mult: 0 371 | } 372 | inner_product_param { 373 | num_output: 1000 374 | weight_filler { 375 | type: "gaussian" 376 | std: 0.01 377 | } 378 | bias_filler { 379 | type: "constant" 380 | value: 0 381 | } 382 | } 383 | } 384 | layer { 385 | name: "accuracy" 386 | type: "Accuracy" 387 | bottom: "fc8" 388 | bottom: "label" 389 | top: "accuracy" 390 | include { 391 | phase: TEST 392 | } 393 | } 394 | layer { 395 | name: "loss" 396 | type: "SoftmaxWithLoss" 397 | bottom: "fc8" 398 | bottom: "label" 399 | top: "loss" 400 | } 401 | -------------------------------------------------------------------------------- /3.Imagenet/create_imagenet.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # Create the imagenet lmdb inputs 3 | # N.B. set the path to the imagenet train + val data dirs 4 | set -e 5 | 6 | EXAMPLE=. 7 | DATA=data 8 | TOOLS=../caffe/build/tools 9 | 10 | TRAIN_DATA_ROOT=./data/train/ 11 | VAL_DATA_ROOT=./data/val/ 12 | 13 | # Set RESIZE=true to resize the images to 256x256. Leave as false if images have 14 | # already been resized using another tool. 15 | RESIZE=false 16 | if $RESIZE; then 17 | RESIZE_HEIGHT=256 18 | RESIZE_WIDTH=256 19 | else 20 | RESIZE_HEIGHT=0 21 | RESIZE_WIDTH=0 22 | fi 23 | 24 | if [ ! -d "$TRAIN_DATA_ROOT" ]; then 25 | echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT" 26 | echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \ 27 | "where the ImageNet training data is stored." 28 | exit 1 29 | fi 30 | 31 | if [ ! -d "$VAL_DATA_ROOT" ]; then 32 | echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT" 33 | echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \ 34 | "where the ImageNet validation data is stored." 35 | exit 1 36 | fi 37 | 38 | echo "Creating train lmdb..." 39 | 40 | GLOG_logtostderr=1 $TOOLS/convert_imageset \ 41 | --resize_height=$RESIZE_HEIGHT \ 42 | --resize_width=$RESIZE_WIDTH \ 43 | --shuffle \ 44 | $TRAIN_DATA_ROOT \ 45 | $DATA/train.txt \ 46 | $EXAMPLE/ilsvrc12_train_lmdb 47 | 48 | echo "Creating val lmdb..." 49 | 50 | GLOG_logtostderr=1 $TOOLS/convert_imageset \ 51 | --resize_height=$RESIZE_HEIGHT \ 52 | --resize_width=$RESIZE_WIDTH \ 53 | --shuffle \ 54 | $VAL_DATA_ROOT \ 55 | $DATA/val.txt \ 56 | $EXAMPLE/ilsvrc12_val_lmdb 57 | 58 | echo "Done." 59 | -------------------------------------------------------------------------------- /3.Imagenet/data/get_ilsvrc_aux.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # 3 | # N.B. This does not download the ilsvrcC12 data set, as it is gargantuan. 4 | # This script downloads the imagenet example auxiliary files including: 5 | # - the ilsvrc12 image mean, binaryproto 6 | # - synset ids and words 7 | # - Python pickle-format data of ImageNet graph structure and relative infogain 8 | # - the training splits with labels 9 | 10 | DIR="$( cd "$(dirname "$0")" ; pwd -P )" 11 | cd "$DIR" 12 | 13 | echo "Downloading..." 14 | 15 | wget -c http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz 16 | 17 | echo "Unzipping..." 18 | 19 | tar -xf caffe_ilsvrc12.tar.gz && rm -f caffe_ilsvrc12.tar.gz 20 | 21 | echo "Done." 22 | -------------------------------------------------------------------------------- /3.Imagenet/make_imagenet_mean.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # Compute the mean image from the imagenet training lmdb 3 | # N.B. this is available in data/ilsvrc12 4 | 5 | EXAMPLE=. 6 | DATA=data 7 | TOOLS=../caffe/build/tools 8 | 9 | $TOOLS/compute_image_mean $EXAMPLE/ilsvrc12_train_lmdb \ 10 | $DATA/imagenet_mean.binaryproto 11 | 12 | echo "Done." 13 | -------------------------------------------------------------------------------- /3.Imagenet/readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | 제목: 이미지넷 튜토리얼 3 | 설명: Train and test "CaffeNet" on ImageNet data. 4 | category: example 5 | include_in_docs: true 6 | priority: 1 7 | --- 8 | 9 | # 이미지망 집중학습[(Brewing ImageNet)](http://caffe.berkeleyvision.org/gathered/examples/imagenet.html) 10 | 이 가이드는 여러분들이 소유한 데이터에 대한 여러분들만의 모델을 학습할 준비가 되었다는하에 진행합니다. 만약 이미지넷으로 학습된 네트워크(ImageNet-trained network)를 원한다면, 학습에는 많은 에너지를 소비하기때문에 우리는 CaffeNet 모델을 [model zoo]이 가이드는 여러분들이 소유한 데이터에 대한 여러분들만의 모델을 학습할 준비가 되었다는하에 진행합니다. 만약 이미지넷으로 학습된 네트워크(ImageNet-trained network)를 원한다면, 학습에는 많은 에너지를 소비하기때문에 우리는 CaffeNet 모델을 [model zoo](https://github.com/Hahnnz/Caffe_Tutorial/wiki/Caffe-Documentation-:-Caffe-Model-Zoo-(Kor))에 있는 미리 학습된 데이터로 학습을 시키었습니다. 11 | )에 있는 미리 학습된 데이터로 학습을 시키었습니다. 12 | ## 데이터준비(Data Preparation) 13 | 이 가이드는 모든 과정을 명시하며 모든 명령어가 Caffe Tutorial root directory에서 실행된다는 가정하에 진행합니다. 14 | “ImageNet”에의해 우리가 여기 ILSVRC12 challenge를 말하지만, 약간의 디스크 공간과, 더 적은 학습시간으로 당신은 쉽게 ImageNet의 전체 또한 학습시킬수 있다. 15 | 16 | 여러분이 이미지넷 학습데이터와 유효데이터를 다운로드 해놓았다고 가정하며, 이들은 당신의 디스크상에 다음과 같이 저장됩니다: 17 | ``` 18 | /path/to/imagenet/train/n01440764/n01440764_10026.JPEG 19 | /path/to/imagenet/val/ILSVRC2012_val_00000001.JPEG 20 | ``` 21 | 우선 학습을 위해 몇개의 예비 데이터를 준비해놓을 필요가 있습니다. 이 데이터는 다음 파일을 실행시켜 다운로드할 수 있습니다. 22 | ``` 23 | ./data/ilsvrc12/get_ilsvrc_aux.sh 24 | ``` 25 | 학습입력과 validation입력은 모든 파일들과 그들의 라벨들을 리스트한 글로적힌 train.txt와 val.txt에 적혀있습니다. 우리는 ILSVRC devkit와는 달리 라벨에대해 인덱스를 매겨놓는 색다른 방법을 사용할 것임을 알아두어야합니다. 우리는 synset이름들을 그들의 아스키법칙에 따라 분류하고, 그러고나서 이를 0부터 999까지 라벨을 붙입니다. synset/name 맵핑에 대해서는 synset_words.txt을 참고해주세요. 26 | 27 | 여러분들은은 아마 미리 256x256로 이미지의 크기를 재설정하길 원하실겁니다. 디폴트값 덕분에, 우리는 딱히 이를 할 필요가 없는데, 이는 군집화 환경에서, mapreduce를 사용해서 병렬 방식에서 이미지의 크기를 조정할 수 있기 때문입니다. 예들들어 Yangqing은 그의 경량의 [mincepie](https://github.com/Yangqing/mincepie) 패키지를 사용했습니다. 만약 더 간단하게 하는 것을 좋아하신다면, 여러분들은 다음과 같은 shell 명령어를 사용하여 진행하실 수 있습니다. 28 | ``` 29 | for name in /path/to/imagenet/val/*.JPEG; do 30 | convert -resize 256x256\! $name $name 31 | done 32 | ``` 33 | examples/imagenet/create_imagenet.sh을 실행시켜보세요! 필요하다면 학습디렉토리와 Validation 디렉토리 경로를 지정하고, 미리 이미지의 크기를 수정해두지 않았다면, 256x256로 모든 이미지의 크기를 재설정하기위해 “RESIZE=true”를 설정하세요. 지금, examples/imagenet/create_imagenet.sh로 leveldbs를 간단히 만들어보실 수 있을겁니다. examples/imagenet/ilsvrc12_train_leveldb와 examples/imagenet/ilsvrc12_val_leveldb를 실행해야 leveldb파일을 얻을 수 있습니다. 34 | 35 | ## 이미지 평균 연산하기(Compute Image Mean) 36 | 모델은 여러분들에게 각각 이미지로부터 이미지 평균을 달라고 요구할 것이고, 그래서 우리들은 이 평균을 계산해야만합니다. tools/compute_image_mean.cpp가 이를 수행해줄겁니다. 이는 또한 어떻게 다중요소들, 예를들면 프로토콜 버퍼(protocol buffer)나 leveldbs, 그리고 로깅(logging)와 같은 것들을 다루는 방법과 여러분들자신을 친숙하게 해줄 좋은 예입니다. 어쨌거나, 평균연산은 다음과 같이 수행할 수 있습니다: 37 | ``` 38 | ./examples/imagenet/make_imagenet_mean.sh 39 | ``` 40 | 이는 data/ilsvrc12/imagenet_mean.binaryproto를 만들어낼 것입니다. 41 | ## 모델정의(Model definition) 42 | 우리는 Krizhevsky, Sutskever, 그리고 Hinton가 처음 [NIPS 2012 paper](http://papers.nips.cc/book/advances-in-neural-information-processing-systems-25-2012)에 제시한 접근방법에 대하여 참조구현을 살펴볼 것입니다. 43 | 44 | 네트워크 정의(models/bvlc_reference_caffenet/train_val.prototxt)는 in Krizhevsky 와 그의 동료들이 만든 것을 따라 볼 것입니다. 만약 여러분들이 이 가이드에서 제시한 파일 경로와 다른 경로를 사용하신다면, .prototxt 파일들에서 적절한 경로를 조절할 필요가 있음을 주의하세요. 45 | 46 | 만약 여러분들이 유심히 models/bvlc_reference_caffenet/train_val.prototxt를 보았다면, phase: TRAIN이나 혹은 phase: TEST 둘 중 하나를 명시한 include 세션들 보았을 것입니다. 이러한 세션들은 한 파일에서 우리에게 둘이 가깝게 연관된 네트워크들(학습용으로 사용되는 네트워크와 실험용으로 사용되는 네트워크)을 정의할 수 있게 해줍니다. 이러한 두 네트워크들은 include { phase: TRAIN }이나 include { phase: TEST }로 적힌 것들을 제외하고 모든 계층들을 공유하여 동일하게 작동합니다. 47 | 48 | * 입력계층 차이점(Input layer differences) 49 | 50 | 학습 네트워크의 데이터 입력 계층은 examples/imagenet/ilsvrc12_train_leveldb로부터 해당하는 데이터를 서술하며 랜덤으로 입력이미지를 미러링합니다. 테스트 네트워크의 데이터 계층은 examples/imagenet/ilsvrc12_val_leveldb에서 데이터를 가져오지만 랜덤 미러링은 수행하지 않습니다. 51 | 52 | * 출력계층 차이점(Output layer differences) 53 | 54 | 두 네트워크 모두 softmax_loss 계층을 출력하며, 이는 학습에서 손실 함수를 연산하고 backpropagation을 초기화하는데 사용됩니다. 반면에 Validation에서는 이 손실은 간단히 보고만 합니다. 테스트 네트워크는 또한 두번째 출력 계층, 정확도를 지니고 있고, 이는 실험 세트에 대한 정확도를 보고하는데만 사용됩니다. 학습과정에서, 실험 네트워크는 때로는 예를들며 설명할 것이고 Test score #0: xxx 이나 Test score #1: xxx와 같은 줄을 생성하면서 테스트 세트에 대하여 실험할 것입니다. 이러한 경우, 스코어 0은 정확도이며(이 정확도는 학습되지 않은 네트워크에 대하여 1/1000 = 0.001 주변에서 시작합니다), 그리고 스코어 1은 손실입니다(이 손실은 학습되지않은 네트워크에 대하여 약 7 주변에서 시작합니다). 55 | 56 | 우리는 또한 Solver를 실행하기 위한 프로토콜 버퍼를 구현해야하죠. 계획을 세워봅시다: 57 | * 256의 일회처리량으로 실행할 것이고, 전체 450,000만번의 반복 (약 90회의 에코)을 실행 58 | * 매 1000번 반복마다 우리는 validation 데이터에 대하여 학습된 네트워크를 저장 59 | * 초기 학습율을 0.01로 설정하고, 이를 매 100,000만번 (약 20회의 에코)에서 감소 60 | * 매 20회 반복마다 정보들 출력 61 | * 네트워크는 모멘텀 0.9 그리고 0.0005의 가중치 감소로 학습 62 | * 매 10,000번 반복마다, 현재 상태의 스냅샷을 저장 63 | 64 | 좋아보이죠? 사실 이 계획은 models/bvlc_reference_caffenet/solver.prototxt.에 구현되어있습니다. 65 | ## 이미지망 학습하기(Training ImageNet) 66 | 준비되었나요? 시작해봅시다! 67 | ``` 68 | ./build/tools/caffe train --solver=models/bvlc_reference_caffenet/solver.prototxt 69 | ``` 70 | 71 | 학습을 즐겨봅시다! 72 | 73 | K40 머신상에서는, 매 20회 반복마다 (K20머신상에서는 36초가 걸리는데 비해) 실행하는데 약 26.5초가 걸리며, 매우 효과적으로 정방향-역방향 과정 모두에 대하여 이미지당 약 5.2 ms가 걸립니다. 약 2 ms가 순방향연산에 사용되고, 나머지는 역방향연산에서 사용됩니다. 만약 이 연산시간을 해부하는데 흥미가있다면, 다음을 실행시켜보세요. 74 | ``` 75 | ./build/tools/caffe time --model=models/bvlc_reference_caffenet/train_val.prototxt 76 | ``` 77 | 78 | ## 학습 다시 시작하기?(Resume Training?) 79 | 우리는 모두 파워가 나갈 때를 경험해봤거나 배틀필드를 즐김으로써 거의 우리자신을 가치있게 보람있지않은 것 같이 느꼈다(누구 퀘이크를 아직도 기억하는 이가 있나요?). 우리가 학습중에 중간 결과를 스냅샷으로 저장하기 때문에, 우리는 스냅샷 횟수에서 부터 학습을 다시 시작할 수 있습니다. 이는 다음과 같이 간단히 수행할 수 있습니다. 80 | ``` 81 | ./build/tools/caffe train --solver=models/bvlc_reference_caffenet/solver.prototxt --snapshot=models/bvlc_reference_caffenet/caffenet_train_iter_10000.solverstate 82 | ``` 83 | 스크립트에서 caffenet_train_iter_10000.solverstate는 정확한 Solver 상태를 복구하는 모든 필수적인 정보를 저장하는 Solver 상태 스냅샷입니다. 84 | ## 작별인사(Parting Words) 85 | 여러분들이 이 튜토리얼을 좋아하기를 희망해요! 네트워크의 구조를 바꾸면서 그리고/혹은 새로운 데이터와 업무를 보내려는 네트워크내의 다양한 파라미터들을 잘 조율하면서, ILSVRC 2012 challenge이래로 많은 연구자들은 더욱 진전되었습니다. 간단히 적은 다양한 prototxt 파일들에의해 Caffe가 당신을 다양한 네트워크 선택을 좀 더 쉽게 탐색해보세요. - 흥미롭지 않은가요? 86 | 87 | 그리고 여러분들이 네트워크를 이제 막 훈련시켰기 때문에, [classifying ImageNet](http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb)를 참고해서 Python 인터페이스로는 어떻게 사용할 수 있는지 확인해보세요! 88 | -------------------------------------------------------------------------------- /3.Imagenet/resume_training.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ../caffe/build/tools/caffe train \ 5 | --solver=./bvlc_reference_caffenet/solver.prototxt \ 6 | --snapshot=./bvlc_reference_caffenet/caffenet_train_10000.solverstate.h5 \ 7 | $@ 8 | -------------------------------------------------------------------------------- /3.Imagenet/train_caffenet.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | ../caffe/build/tools/caffe train \ 5 | --solver=./bvlc_reference_caffenet/solver.prototxt $@ 6 | -------------------------------------------------------------------------------- /Makefile_config_EX/Makefile.config_Mac: -------------------------------------------------------------------------------- 1 | ## Refer to http://caffe.berkeleyvision.org/installation.html 2 | # Contributions simplifying and improving our build system are welcome! 3 | 4 | # cuDNN acceleration switch (uncomment to build with cuDNN). 5 | # USE_CUDNN := 1 6 | 7 | # CPU-only switch (uncomment to build without GPU support). 8 | CPU_ONLY := 1 9 | 10 | # uncomment to disable IO dependencies and corresponding data layers 11 | # USE_OPENCV := 0 12 | # USE_LEVELDB := 0 13 | # USE_LMDB := 0 14 | 15 | # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) 16 | # You should not set this flag if you will be reading LMDBs with any 17 | # possibility of simultaneous read and write 18 | # ALLOW_LMDB_NOLOCK := 1 19 | 20 | # Uncomment if you're using OpenCV 3 21 | # OPENCV_VERSION := 3 22 | 23 | # To customize your choice of compiler, uncomment and set the following. 24 | # N.B. the default for Linux is g++ and the default for OSX is clang++ 25 | # CUSTOM_CXX := g++ 26 | 27 | # CUDA directory contains bin/ and lib/ directories that we need. 28 | # CUDA_DIR := /usr/local/cuda 29 | # On Ubuntu 14.04, if cuda tools are installed via 30 | # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: 31 | # CUDA_DIR := /usr 32 | 33 | # CUDA architecture setting: going with all of them. 34 | # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility. 35 | # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility. 36 | # CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ 37 | -gencode arch=compute_20,code=sm_21 \ 38 | -gencode arch=compute_30,code=sm_30 \ 39 | -gencode arch=compute_35,code=sm_35 \ 40 | -gencode arch=compute_50,code=sm_50 \ 41 | -gencode arch=compute_52,code=sm_52 \ 42 | -gencode arch=compute_60,code=sm_60 \ 43 | -gencode arch=compute_61,code=sm_61 \ 44 | -gencode arch=compute_61,code=compute_61 45 | 46 | # BLAS choice: 47 | # atlas for ATLAS (default) 48 | # mkl for MKL 49 | # open for OpenBlas 50 | BLAS := atlas 51 | # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. 52 | # Leave commented to accept the defaults for your choice of BLAS 53 | # (which should work)! 54 | # BLAS_INCLUDE := /path/to/your/blas 55 | # BLAS_LIB := /path/to/your/blas 56 | 57 | # Homebrew puts openblas in a directory that is not on the standard search path 58 | BLAS_INCLUDE := $(shell brew --prefix openblas)/include 59 | BLAS_LIB := $(shell brew --prefix openblas)/lib 60 | 61 | # This is required only if you will compile the matlab interface. 62 | # MATLAB directory should contain the mex binary in /bin. 63 | # MATLAB_DIR := /usr/local 64 | MATLAB_DIR := /Applications/MATLAB_R2017b.app 65 | 66 | # NOTE: this is required only if you will compile the python interface. 67 | # We need to be able to find Python.h and numpy/arrayobject.h. 68 | PYTHON_INCLUDE := /usr/include/python2.7 \ 69 | /usr/lib/python2.7/dist-packages/numpy/core/include 70 | # Anaconda Python distribution is quite popular. Include path: 71 | # Verify anaconda location, sometimes it's in root. 72 | # ANACONDA_HOME := $(HOME)/anaconda 73 | # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ 74 | # $(ANACONDA_HOME)/include/python2.7 \ 75 | # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include 76 | 77 | # Uncomment to use Python 3 (default is Python 2) 78 | # PYTHON_LIBRARIES := boost_python3 python3.5m 79 | # PYTHON_INCLUDE := /usr/include/python3.5m \ 80 | # /usr/lib/python3.5/dist-packages/numpy/core/include 81 | 82 | # We need to be able to find libpythonX.X.so or .dylib. 83 | PYTHON_LIB := /usr/lib 84 | # PYTHON_LIB := $(ANACONDA_HOME)/lib 85 | 86 | # Homebrew installs numpy in a non standard path (keg only) 87 | PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include 88 | PYTHON_LIB += $(shell brew --prefix numpy)/lib 89 | 90 | # Uncomment to support layers written in Python (will link against Python libs) 91 | WITH_PYTHON_LAYER := 1 92 | 93 | # Whatever else you find you need goes here. 94 | INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 95 | LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 96 | 97 | # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies 98 | INCLUDE_DIRS += $(shell brew --prefix)/include 99 | LIBRARY_DIRS += $(shell brew --prefix)/lib 100 | 101 | # NCCL acceleration switch (uncomment to build with NCCL) 102 | # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0) 103 | # USE_NCCL := 1 104 | 105 | # Uncomment to use `pkg-config` to specify OpenCV library paths. 106 | # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) 107 | # USE_PKG_CONFIG := 1 108 | 109 | # N.B. both build and distribute dirs are cleared on `make clean` 110 | BUILD_DIR := build 111 | DISTRIBUTE_DIR := distribute 112 | 113 | # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 114 | # DEBUG := 1 115 | 116 | # The ID of the GPU that 'make runtest' will use to run unit tests. 117 | TEST_GPUID := 0 118 | 119 | # enable pretty build (comment to see full commands) 120 | Q ?= @ 121 | -------------------------------------------------------------------------------- /Makefile_config_EX/Makefile.config_Ubuntu16_CPU: -------------------------------------------------------------------------------- 1 | ## Refer to http://caffe.berkeleyvision.org/installation.html 2 | # Contributions simplifying and improving our build system are welcome! 3 | 4 | # cuDNN acceleration switch (uncomment to build with cuDNN). 5 | # USE_CUDNN := 1 6 | 7 | # CPU-only switch (uncomment to build without GPU support). 8 | CPU_ONLY := 1 9 | 10 | # uncomment to disable IO dependencies and corresponding data layers 11 | # USE_OPENCV := 1 12 | # USE_LEVELDB := 0 13 | # USE_LMDB := 1 14 | 15 | # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) 16 | # You should not set this flag if you will be reading LMDBs with any 17 | # possibility of simultaneous read and write 18 | # ALLOW_LMDB_NOLOCK := 1 19 | 20 | # Uncomment if you're using OpenCV 3 21 | # OPENCV_VERSION := 3 22 | 23 | # To customize your choice of compiler, uncomment and set the following. 24 | # N.B. the default for Linux is g++ and the default for OSX is clang++ 25 | CUSTOM_CXX := g++ 26 | 27 | # CUDA directory contains bin/ and lib/ directories that we need. 28 | CUDA_DIR := /usr/local/cuda 29 | # On Ubuntu 14.04, if cuda tools are installed via 30 | # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: 31 | # CUDA_DIR := /usr 32 | 33 | # CUDA architecture setting: going with all of them. 34 | # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility. 35 | # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility. 36 | CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ 37 | -gencode arch=compute_20,code=sm_21 \ 38 | -gencode arch=compute_30,code=sm_30 \ 39 | -gencode arch=compute_35,code=sm_35 \ 40 | -gencode arch=compute_50,code=sm_50 \ 41 | -gencode arch=compute_52,code=sm_52 \ 42 | -gencode arch=compute_60,code=sm_60 \ 43 | -gencode arch=compute_61,code=sm_61 \ 44 | -gencode arch=compute_61,code=compute_61 45 | 46 | # BLAS choice: 47 | # atlas for ATLAS (default) 48 | # mkl for MKL 49 | # open for OpenBlas 50 | BLAS := atlas 51 | # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. 52 | # Leave commented to accept the defaults for your choice of BLAS 53 | # (which should work)! 54 | # BLAS_INCLUDE := /path/to/your/blas 55 | # BLAS_LIB := /path/to/your/blas 56 | 57 | # Homebrew puts openblas in a directory that is not on the standard search path 58 | # BLAS_INCLUDE := $(shell brew --prefix openblas)/include 59 | # BLAS_LIB := $(shell brew --prefix openblas)/lib 60 | 61 | # This is required only if you will compile the matlab interface. 62 | # MATLAB directory should contain the mex binary in /bin. 63 | # MATLAB_DIR := /usr/local 64 | # MATLAB_DIR := /Applications/MATLAB_R2012b.app 65 | 66 | # NOTE: this is required only if you will compile the python interface. 67 | # We need to be able to find Python.h and numpy/arrayobject.h. 68 | PYTHON_INCLUDE := /usr/include/python2.7 \ 69 | /usr/lib/python2.7/dist-packages/numpy/core/include 70 | # Anaconda Python distribution is quite popular. Include path: 71 | # Verify anaconda location, sometimes it's in root. 72 | # ANACONDA_HOME := $(HOME)/anaconda 73 | # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ 74 | # $(ANACONDA_HOME)/include/python2.7 \ 75 | # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include 76 | 77 | # Uncomment to use Python 3 (default is Python 2) 78 | # PYTHON_LIBRARIES := boost_python3 python3.5m 79 | # PYTHON_INCLUDE := /usr/include/python3.5m \ 80 | # /usr/lib/python3.5/dist-packages/numpy/core/include 81 | 82 | # We need to be able to find libpythonX.X.so or .dylib. 83 | PYTHON_LIB := /usr/lib 84 | # PYTHON_LIB := $(ANACONDA_HOME)/lib 85 | 86 | # Homebrew installs numpy in a non standard path (keg only) 87 | # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include 88 | # PYTHON_LIB += $(shell brew --prefix numpy)/lib 89 | 90 | # Uncomment to support layers written in Python (will link against Python libs) 91 | # WITH_PYTHON_LAYER := 1 92 | 93 | # Whatever else you find you need goes here. 94 | INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial 95 | LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial 96 | 97 | # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies 98 | # INCLUDE_DIRS += $(shell brew --prefix)/include 99 | # LIBRARY_DIRS += $(shell brew --prefix)/lib 100 | 101 | # NCCL acceleration switch (uncomment to build with NCCL) 102 | # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0) 103 | # USE_NCCL := 1 104 | 105 | # Uncomment to use `pkg-config` to specify OpenCV library paths. 106 | # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) 107 | # USE_PKG_CONFIG := 1 108 | 109 | # N.B. both build and distribute dirs are cleared on `make clean` 110 | BUILD_DIR := build 111 | DISTRIBUTE_DIR := distribute 112 | 113 | # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 114 | # DEBUG := 1 115 | 116 | # The ID of the GPU that 'make runtest' will use to run unit tests. 117 | TEST_GPUID := 0 118 | 119 | # enable pretty build (comment to see full commands) 120 | Q ?= @ 121 | -------------------------------------------------------------------------------- /Makefile_config_EX/Makefile.config_Ubuntu16_GPU: -------------------------------------------------------------------------------- 1 | ## Refer to http://caffe.berkeleyvision.org/installation.html 2 | # Contributions simplifying and improving our build system are welcome! 3 | 4 | # cuDNN acceleration switch (uncomment to build with cuDNN). 5 | USE_CUDNN := 1 6 | 7 | # CPU-only switch (uncomment to build without GPU support). 8 | # CPU_ONLY := 1 9 | 10 | # uncomment to disable IO dependencies and corresponding data layers 11 | # USE_OPENCV := 0 12 | # USE_LEVELDB := 0 13 | # USE_LMDB := 0 14 | 15 | # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) 16 | # You should not set this flag if you will be reading LMDBs with any 17 | # possibility of simultaneous read and write 18 | # ALLOW_LMDB_NOLOCK := 1 19 | 20 | # Uncomment if you're using OpenCV 3 21 | # OPENCV_VERSION := 3 22 | 23 | # To customize your choice of compiler, uncomment and set the following. 24 | # N.B. the default for Linux is g++ and the default for OSX is clang++ 25 | CUSTOM_CXX := g++ 26 | 27 | # CUDA directory contains bin/ and lib/ directories that we need. 28 | CUDA_DIR := /usr/local/cuda 29 | # On Ubuntu 14.04, if cuda tools are installed via 30 | # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: 31 | # CUDA_DIR := /usr 32 | 33 | # CUDA architecture setting: going with all of them. 34 | # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility. 35 | # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility. 36 | CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ 37 | -gencode arch=compute_20,code=sm_21 \ 38 | -gencode arch=compute_30,code=sm_30 \ 39 | -gencode arch=compute_35,code=sm_35 \ 40 | -gencode arch=compute_50,code=sm_50 \ 41 | -gencode arch=compute_52,code=sm_52 \ 42 | -gencode arch=compute_60,code=sm_60 \ 43 | -gencode arch=compute_61,code=sm_61 \ 44 | -gencode arch=compute_61,code=compute_61 45 | 46 | # BLAS choice: 47 | # atlas for ATLAS (default) 48 | # mkl for MKL 49 | # open for OpenBlas 50 | BLAS := atlas 51 | # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. 52 | # Leave commented to accept the defaults for your choice of BLAS 53 | # (which should work)! 54 | # BLAS_INCLUDE := /path/to/your/blas 55 | # BLAS_LIB := /path/to/your/blas 56 | 57 | # Homebrew puts openblas in a directory that is not on the standard search path 58 | # BLAS_INCLUDE := $(shell brew --prefix openblas)/include 59 | # BLAS_LIB := $(shell brew --prefix openblas)/lib 60 | 61 | # This is required only if you will compile the matlab interface. 62 | # MATLAB directory should contain the mex binary in /bin. 63 | # MATLAB_DIR := /usr/local 64 | # MATLAB_DIR := /Applications/MATLAB_R2012b.app 65 | 66 | # NOTE: this is required only if you will compile the python interface. 67 | # We need to be able to find Python.h and numpy/arrayobject.h. 68 | PYTHON_INCLUDE := /usr/include/python2.7 \ 69 | /usr/lib/python2.7/dist-packages/numpy/core/include 70 | # Anaconda Python distribution is quite popular. Include path: 71 | # Verify anaconda location, sometimes it's in root. 72 | # ANACONDA_HOME := $(HOME)/anaconda 73 | # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ 74 | # $(ANACONDA_HOME)/include/python2.7 \ 75 | # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include 76 | 77 | # Uncomment to use Python 3 (default is Python 2) 78 | # PYTHON_LIBRARIES := boost_python3 python3.5m 79 | # PYTHON_INCLUDE := /usr/include/python3.5m \ 80 | # /usr/lib/python3.5/dist-packages/numpy/core/include 81 | 82 | # We need to be able to find libpythonX.X.so or .dylib. 83 | PYTHON_LIB := /usr/lib 84 | # PYTHON_LIB := $(ANACONDA_HOME)/lib 85 | 86 | # Homebrew installs numpy in a non standard path (keg only) 87 | # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include 88 | # PYTHON_LIB += $(shell brew --prefix numpy)/lib 89 | 90 | # Uncomment to support layers written in Python (will link against Python libs) 91 | # WITH_PYTHON_LAYER := 1 92 | 93 | # Whatever else you find you need goes here. 94 | INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial 95 | LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial 96 | 97 | # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies 98 | # INCLUDE_DIRS += $(shell brew --prefix)/include 99 | # LIBRARY_DIRS += $(shell brew --prefix)/lib 100 | 101 | # NCCL acceleration switch (uncomment to build with NCCL) 102 | # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0) 103 | # USE_NCCL := 1 104 | 105 | # Uncomment to use `pkg-config` to specify OpenCV library paths. 106 | # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) 107 | # USE_PKG_CONFIG := 1 108 | 109 | # N.B. both build and distribute dirs are cleared on `make clean` 110 | BUILD_DIR := build 111 | DISTRIBUTE_DIR := distribute 112 | 113 | # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 114 | # DEBUG := 1 115 | 116 | # The ID of the GPU that 'make runtest' will use to run unit tests. 117 | TEST_GPUID := 0 118 | 119 | # enable pretty build (comment to see full commands) 120 | Q ?= @ 121 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Caffe : Convolutional Architecture for Fast Feature Embedding 2 | 3 | Caffe는 다양한 딥러닝 프레임워크들 중 하나로 버클리 인공지능 연구소 (Berkeley AI Research([BAIR](http://bair.berkeley.edu)))/ 버클리 비전 & 학습 센터 (The Berkeley Vision and Learning Center (BVLC)), 그리고 Caffe 커뮤니티의 수많은 기여자들에의해 개발된 것입니다. 4 | 5 | 좀 더 자세한 사항들을 확인하고자 한다면 [Caffe 공식 사이트](http://caffe.berkeleyvision.org)를 확인해주세요.  6 | 7 | [해당 공식 사이트(이전 버전)](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Documentation-:-Caffe-Tutorial-(Kor)) 번역 또한 같이 첨부하였습니다. 8 | 9 | ## [Caffe 튜토리얼](https://github.com/Hahnnz/Caffe_Tutorial/wiki) 10 | 11 | Caffe는 딥러닝 프레임워크로 이 튜토리얼은 Caffe의 원리, 구성 그리고 사용법에 대해서 설명할 것입니다. 12 | 13 | 이 튜토리얼은 프레임워크 튜토리얼로써 작성한 것이며, 딥러닝의 전체 과정과 맥락 그리고 한계에 대해서는 다루지는 않습니다. 14 | 15 | 이 튜토리얼이 여러분들에게 유용한 설명이 되기를 바라며 기계학습의 배경지식과 신경망에 대한 이해가 이 튜토리얼을 이해하는데에 도움이 될 것입니다. 16 | 17 | **이 튜토리얼은 [Caffe 공식 튜토리얼](http://caffe.berkeleyvision.org/tutorial/)을 기반으로 하여 작성하였습니다.** 18 | 19 | ### 이론 20 | 여러분들은 해당 Caffe 튜토리얼을 진행하실 때마다 다음과 같은 점들을 이해하실 수 있을 것입니다. 21 | 22 | 1. 표현 : 모델과 최적화는 일반적 코딩 작업과는 달리 일반적문장으로 이루어진 평문을 작성하듯이 선언을 할 수 있습니다. 23 | 24 | 2. 속도 : 산업과 연구분야 쪽에서 수많은 데이터를 처리하고 이에 대한 최신의 모델에 있어 처리속도는 중요합니다. 25 | 26 | 3. 모듈성 : 새로운 업무(계층의 새로운 역할)와 환경(구동환경)은 유연성과 확장성을 필요로 합니다. 27 | 28 | 4. 공개성 : 과학적이고 응용된 진보기술들은 공동의 코드와 참조 모델들에 대한 재생산성이 좋아야합니다. 29 | 30 | 5. 공동성 : 학문적 연구, 초기 프로토타입 그리고 산업분야의 응용품들 모두가 합동 연구회와 BSD-2프로젝트의 분야로 강하게 공유하고 있습니다. 31 | 32 | ### Caffe 튜토리얼 33 | 그리고 다음과 같은 항목을 이 튜토리얼에서 알려주고자 합니다. 34 | 35 | 1. [Nets, layers, Bolbs](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-1.Blobs,-Layers,-and-Nets-(Kor)) : ‘Caffe’ 모델의 분석 36 | 2. [Forward / Backward](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-2.Forward-and-Backward-(kor)) : 계층화로 구성된 모델의 필수적인 연산 37 | 3. [Loss](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-3.Loss-(Kor)) : 학습되어야할 업무(계층)를 손실로 정의합니다. 38 | 4. [Solver] (https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-4.Solver-(Kor)) : Solver는 모델 최적화를 수행해줍니다. 39 | 5. [Layer Catalogue](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-5.Layer-Catalogue-(Kor)) : 계층은 최신모델에 대한 계층을 포함하는 ‘Caffe’ 카탈로그인 모델링과 연산의 기본단위 40 | 6. [Interfaces](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-6.Interface-(Kor)) : 커맨드 라인, Python, Matlab Caffe를 사용. 41 | 7. [Data](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-7.Data-(Kor)) : 모델 입력에 대하여 어떻게 caffe화를 할 것인가 42 | 8. [Caffeinated Convolution](https://github.com/ys7yoo/BrainCaffe/wiki/Caffe-Tutorial-:-8.Caffeinated-Convolution-(Kor)) : 어떻게 Caffe가 컨볼루션을 계산할까 (심화내용) 43 | 44 | ### 심층학습 (Deeper Learning) 45 | 우리가 다루는 튜토리얼을 수행하는 심층학습에 대한 이해에 도움이 될 만한 참고사항이 온라인상에 많이 공개되어 있습니다. 46 | 이는 입문사항과 발전된 요소, 배경지식과 역사 그리고 기술을 다룹니다. 47 | CVPR’14에서 [시각에 대한 심층학습(Deep Learning)에 대한 튜토리얼](https://sites.google.com/site/deeplearningcvpr2014/)은 연구자들이 참고하기 좋은 튜토리얼입니다. 48 | 여러분들이 Caffe 튜토리얼로부터 실제 기반과 작동, 프레임워크를 알게된다면, CVPR’14 tutorial에서 향상된 연구방향과 49 | 핵심적인 아이디어를 탐구할 수 있을 것입니다. 50 | 이 입문서는 Michael Nielsen에 의한 [신경망과 심층학습](http://neuralnetworksanddeeplearning.com/index.html/)을 무료 온라인 드래프트로 제공합니다. 51 | 실제로 신경망과 ‘backpropagation’이 작동하는 방법을 다루는 챕터들이 이 분야가 처음이라면 도움이 될 것입니다. 52 | 이러한 학문적 튜토리얼은 기계학습과 영상분야의 연구자들을 위한 심층학습에 대해 다룹니다. 53 | 코드와 회로상에서 신경망에 대한 해석으로, Andrej Karpathy (Stanford)가 작성한 54 | [프로그래머의 관점으로 이해하는 신경망들](http://karpathy.github.io/neuralnets/)를 참고해보세요! 55 | 56 | ## 커스텀 배포판 57 | Caffe는 Intel에서 다중 노드를 지원해주고 CPU(특히 제논 프로세서 (HSW, BDW, SKX, Xeon Phi))에 좀 더 최적화된 Caffe를 만들어 배포하고 있으며 Window에서도 사용이 가능한 Caffe 등등을 배포하고 있습니다. 58 | 59 | 60 | - [Intel Caffe](https://github.com/BVLC/caffe/tree/intel) (Optimized for CPU and support for multi-node), in particular Xeon processors (HSW, BDW, SKX, Xeon Phi). 61 | - [OpenCL Caffe](https://github.com/BVLC/caffe/tree/opencl) e.g. for AMD or Intel devices. 62 | - [Windows Caffe](https://github.com/BVLC/caffe/tree/windows) 63 | 64 | ## 라이센스 및 인용 65 | 66 | Caffe는 [BSD 2-Clause 라이센스](https://github.com/BVLC/caffe/blob/master/LICENSE)하에 배포됩니다. 67 | 68 | BAIR/BVLC가 제공하는 신경망 모델들은 자유롭게 사용이 가능하도록 배포되었습니다. 69 | 70 | Caffe가 여러분의 연구에 도움이 되었다면 여러분들의 출판물에 Caffe를 인용 자료로써 넣어주세요! 71 | 72 | @article{jia2014caffe, 73 | Author = {Jia, Yangqing and Shelhamer, Evan and Donahue, Jeff and Karayev, Sergey and Long, Jonathan and Girshick, Ross and Guadarrama, Sergio and Darrell, Trevor}, 74 | Journal = {arXiv preprint arXiv:1408.5093}, 75 | Title = {Caffe: Convolutional Architecture for Fast Feature Embedding}, 76 | Year = {2014} 77 | } 78 | -------------------------------------------------------------------------------- /Tools/autoGPUsetup.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | 3 | sudo apt-get -y update && sudo apt-get -y upgrade 4 | sudo apt-get install -y build-essential cmake git pkg-config 5 | sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler 6 | sudo apt-get install -y libatlas-base-dev 7 | sudo apt-get install -y --no-install-recommends libboost-all-dev 8 | sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev 9 | sudo apt-get install -y python-pip 10 | sudo apt-get install -y python-dev 11 | sudo apt-get install -y python-numpy python-scipy 12 | sudo apt-get install -y libopencv-dev 13 | 14 | #GPU-cuda 15 | sudo apt-get update && sudo apt-get install wget -y --no-install-recommends 16 | wget "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb" 17 | sudo dpkg -i cuda-repo-ubuntu1604_8.0.61-1_amd64.deb 18 | sudo apt-get update 19 | sudo apt-get install cuda 20 | 21 | #GPU-cudnn 22 | CUDNN_URL="http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/cudnn-8.0-linux-x64-v5.1.tgz" 23 | wget ${CUDNN_URL} 24 | sudo tar -xzf cudnn-8.0-linux-x64-v5.1.tgz -C /usr/local 25 | rm cudnn-8.0-linux-x64-v5.1.tgz && sudo ldconfig 26 | 27 | --------------------------------------------------------------------------------