├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── caffe ├── .Doxyfile ├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── CONTRIBUTING.md ├── CONTRIBUTORS.md ├── INSTALL.md ├── LICENSE ├── Makefile ├── Makefile.config.example ├── README.md ├── caffe.cloc ├── cmake │ ├── ConfigGen.cmake │ ├── Cuda.cmake │ ├── Dependencies.cmake │ ├── External │ │ ├── gflags.cmake │ │ └── glog.cmake │ ├── Misc.cmake │ ├── Modules │ │ ├── FindAtlas.cmake │ │ ├── FindGFlags.cmake │ │ ├── FindGlog.cmake │ │ ├── FindLAPACK.cmake │ │ ├── FindLMDB.cmake │ │ ├── FindLevelDB.cmake │ │ ├── FindMKL.cmake │ │ ├── FindMatlabMex.cmake │ │ ├── FindNumPy.cmake │ │ ├── FindOpenBLAS.cmake │ │ ├── FindSnappy.cmake │ │ └── FindvecLib.cmake │ ├── ProtoBuf.cmake │ ├── Summary.cmake │ ├── Targets.cmake │ ├── Templates │ │ ├── CaffeConfig.cmake.in │ │ ├── CaffeConfigVersion.cmake.in │ │ └── caffe_config.h.in │ ├── Utils.cmake │ └── lint.cmake ├── docs │ ├── CMakeLists.txt │ ├── CNAME │ ├── README.md │ ├── _config.yml │ ├── _layouts │ │ └── default.html │ ├── development.md │ ├── images │ │ ├── GitHub-Mark-64px.png │ │ └── caffeine-icon.png │ ├── index.md │ ├── install_apt.md │ ├── install_osx.md │ ├── install_yum.md │ ├── installation.md │ ├── model_zoo.md │ ├── performance_hardware.md │ ├── stylesheets │ │ ├── pygment_trac.css │ │ ├── reset.css │ │ └── styles.css │ └── tutorial │ │ ├── convolution.md │ │ ├── data.md │ │ ├── fig │ │ ├── .gitignore │ │ ├── backward.jpg │ │ ├── forward.jpg │ │ ├── forward_backward.png │ │ └── layer.jpg │ │ ├── forward_backward.md │ │ ├── index.md │ │ ├── interfaces.md │ │ ├── layers.md │ │ ├── loss.md │ │ ├── net_layer_blob.md │ │ └── solver.md ├── examples │ ├── 00-classification.ipynb │ ├── 01-learning-lenet.ipynb │ ├── 02-brewing-logreg.ipynb │ ├── 03-fine-tuning.ipynb │ ├── CMakeLists.txt │ ├── cifar10 │ │ ├── cifar10_full.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_quick.sh │ ├── cpp_classification │ │ ├── classification.cpp │ │ └── readme.md │ ├── detection.ipynb │ ├── feature_extraction │ │ ├── imagenet_val.prototxt │ │ └── readme.md │ ├── finetune_flickr_style │ │ ├── assemble_data.py │ │ ├── flickr_style.csv.gz │ │ ├── readme.md │ │ └── style_names.txt │ ├── finetune_pascal_detection │ │ ├── pascal_finetune_solver.prototxt │ │ └── pascal_finetune_trainval_test.prototxt │ ├── hdf5_classification │ │ ├── nonlinear_auto_test.prototxt │ │ ├── nonlinear_auto_train.prototxt │ │ ├── nonlinear_solver.prototxt │ │ ├── nonlinear_train_val.prototxt │ │ ├── solver.prototxt │ │ └── train_val.prototxt │ ├── imagenet │ │ ├── create_imagenet.sh │ │ ├── make_imagenet_mean.sh │ │ ├── readme.md │ │ ├── resume_training.sh │ │ └── train_caffenet.sh │ ├── images │ │ ├── cat.jpg │ │ ├── cat_gray.jpg │ │ └── fish-bike.jpg │ ├── mnist │ │ ├── convert_mnist_data.cpp │ │ ├── create_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_stepearly_solver.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 │ │ ├── readme.md │ │ ├── train_lenet.sh │ │ ├── train_lenet_adam.sh │ │ ├── train_lenet_consolidated.sh │ │ ├── train_lenet_rmsprop.sh │ │ ├── train_mnist_autoencoder.sh │ │ ├── train_mnist_autoencoder_adadelta.sh │ │ ├── train_mnist_autoencoder_adagrad.sh │ │ └── train_mnist_autoencoder_nesterov.sh │ ├── net_surgery.ipynb │ ├── net_surgery │ │ ├── bvlc_caffenet_full_conv.prototxt │ │ └── conv.prototxt │ ├── pycaffe │ │ ├── caffenet.py │ │ ├── layers │ │ │ └── pyloss.py │ │ └── linreg.prototxt │ ├── siamese │ │ ├── convert_mnist_siamese_data.cpp │ │ ├── create_mnist_siamese.sh │ │ ├── mnist_siamese.ipynb │ │ ├── mnist_siamese.prototxt │ │ ├── mnist_siamese_solver.prototxt │ │ ├── mnist_siamese_train_test.prototxt │ │ ├── readme.md │ │ └── train_mnist_siamese.sh │ └── web_demo │ │ ├── app.py │ │ ├── exifutil.py │ │ ├── readme.md │ │ ├── requirements.txt │ │ └── templates │ │ └── index.html ├── include │ └── caffe │ │ ├── blob.hpp │ │ ├── caffe.hpp │ │ ├── common.hpp │ │ ├── common_layers.hpp │ │ ├── data_layers.hpp │ │ ├── data_reader.hpp │ │ ├── data_transformer.hpp │ │ ├── fast_rcnn_layers.hpp │ │ ├── filler.hpp │ │ ├── internal_thread.hpp │ │ ├── layer.hpp │ │ ├── layer_factory.hpp │ │ ├── loss_layers.hpp │ │ ├── net.hpp │ │ ├── neuron_layers.hpp │ │ ├── parallel.hpp │ │ ├── python_layer.hpp │ │ ├── solver.hpp │ │ ├── syncedmem.hpp │ │ ├── test │ │ ├── test_caffe_main.hpp │ │ └── test_gradient_check_util.hpp │ │ ├── util │ │ ├── benchmark.hpp │ │ ├── blocking_queue.hpp │ │ ├── coords.hpp │ │ ├── cudnn.hpp │ │ ├── db.hpp │ │ ├── db_leveldb.hpp │ │ ├── db_lmdb.hpp │ │ ├── device_alternate.hpp │ │ ├── gpu_util.cuh │ │ ├── hdf5.hpp │ │ ├── im2col.hpp │ │ ├── insert_splits.hpp │ │ ├── io.hpp │ │ ├── math_functions.hpp │ │ ├── mkl_alternate.hpp │ │ ├── rng.hpp │ │ ├── signal_handler.h │ │ └── upgrade_proto.hpp │ │ └── vision_layers.hpp ├── matlab │ ├── +caffe │ │ ├── +test │ │ │ ├── test_net.m │ │ │ └── test_solver.m │ │ ├── Blob.m │ │ ├── Layer.m │ │ ├── Net.m │ │ ├── Solver.m │ │ ├── get_net.m │ │ ├── get_solver.m │ │ ├── imagenet │ │ │ └── ilsvrc_2012_mean.mat │ │ ├── io.m │ │ ├── private │ │ │ ├── CHECK.m │ │ │ ├── CHECK_FILE_EXIST.m │ │ │ ├── caffe_.cpp │ │ │ └── is_valid_handle.m │ │ ├── reset_all.m │ │ ├── run_tests.m │ │ ├── set_device.m │ │ ├── set_mode_cpu.m │ │ └── set_mode_gpu.m │ ├── CMakeLists.txt │ ├── demo │ │ └── classification_demo.m │ └── hdf5creation │ │ ├── .gitignore │ │ ├── demo.m │ │ └── store2hdf5.m ├── python │ ├── CMakeLists.txt │ ├── caffe │ │ ├── __init__.py │ │ ├── _caffe.cpp │ │ ├── classifier.py │ │ ├── detector.py │ │ ├── draw.py │ │ ├── imagenet │ │ │ └── ilsvrc_2012_mean.npy │ │ ├── io.py │ │ ├── net_spec.py │ │ ├── pycaffe.py │ │ └── test │ │ │ ├── test_layer_type_list.py │ │ │ ├── test_net.py │ │ │ ├── test_net_spec.py │ │ │ ├── test_python_layer.py │ │ │ ├── test_python_layer_with_param_str.py │ │ │ └── test_solver.py │ ├── classify.py │ ├── detect.py │ ├── draw_net.py │ ├── requirements.txt │ └── summarize.py ├── scripts │ ├── build_docs.sh │ ├── copy_notebook.py │ ├── cpp_lint.py │ ├── deploy_docs.sh │ ├── download_model_binary.py │ ├── download_model_from_gist.sh │ ├── gather_examples.sh │ ├── travis │ │ ├── travis_build_and_test.sh │ │ ├── travis_install.sh │ │ └── travis_setup_makefile_config.sh │ └── upload_model_to_gist.sh ├── src │ ├── caffe │ │ ├── CMakeLists.txt │ │ ├── blob.cpp │ │ ├── common.cpp │ │ ├── data_reader.cpp │ │ ├── data_transformer.cpp │ │ ├── internal_thread.cpp │ │ ├── layer.cpp │ │ ├── layer_factory.cpp │ │ ├── layers │ │ │ ├── absval_layer.cpp │ │ │ ├── absval_layer.cu │ │ │ ├── accuracy_layer.cpp │ │ │ ├── argmax_layer.cpp │ │ │ ├── base_conv_layer.cpp │ │ │ ├── base_data_layer.cpp │ │ │ ├── base_data_layer.cu │ │ │ ├── batch_norm_layer.cpp │ │ │ ├── batch_norm_layer.cu │ │ │ ├── bnll_layer.cpp │ │ │ ├── bnll_layer.cu │ │ │ ├── concat_layer.cpp │ │ │ ├── concat_layer.cu │ │ │ ├── contrastive_loss_layer.cpp │ │ │ ├── contrastive_loss_layer.cu │ │ │ ├── conv_layer.cpp │ │ │ ├── conv_layer.cu │ │ │ ├── crop_layer.cpp │ │ │ ├── crop_layer.cu │ │ │ ├── cudnn_conv_layer.cpp │ │ │ ├── cudnn_conv_layer.cu │ │ │ ├── cudnn_pooling_layer.cpp │ │ │ ├── cudnn_pooling_layer.cu │ │ │ ├── cudnn_relu_layer.cpp │ │ │ ├── cudnn_relu_layer.cu │ │ │ ├── cudnn_sigmoid_layer.cpp │ │ │ ├── cudnn_sigmoid_layer.cu │ │ │ ├── cudnn_softmax_layer.cpp │ │ │ ├── cudnn_softmax_layer.cu │ │ │ ├── cudnn_tanh_layer.cpp │ │ │ ├── cudnn_tanh_layer.cu │ │ │ ├── data_layer.cpp │ │ │ ├── deconv_layer.cpp │ │ │ ├── deconv_layer.cu │ │ │ ├── dropout_layer.cpp │ │ │ ├── dropout_layer.cu │ │ │ ├── dummy_data_layer.cpp │ │ │ ├── eltwise_layer.cpp │ │ │ ├── eltwise_layer.cu │ │ │ ├── embed_layer.cpp │ │ │ ├── embed_layer.cu │ │ │ ├── euclidean_loss_layer.cpp │ │ │ ├── euclidean_loss_layer.cu │ │ │ ├── exp_layer.cpp │ │ │ ├── exp_layer.cu │ │ │ ├── filter_layer.cpp │ │ │ ├── filter_layer.cu │ │ │ ├── flatten_layer.cpp │ │ │ ├── hdf5_data_layer.cpp │ │ │ ├── hdf5_data_layer.cu │ │ │ ├── hdf5_output_layer.cpp │ │ │ ├── hdf5_output_layer.cu │ │ │ ├── hinge_loss_layer.cpp │ │ │ ├── im2col_layer.cpp │ │ │ ├── im2col_layer.cu │ │ │ ├── image_data_layer.cpp │ │ │ ├── infogain_loss_layer.cpp │ │ │ ├── inner_product_layer.cpp │ │ │ ├── inner_product_layer.cu │ │ │ ├── log_layer.cpp │ │ │ ├── log_layer.cu │ │ │ ├── loss_layer.cpp │ │ │ ├── lrn_layer.cpp │ │ │ ├── lrn_layer.cu │ │ │ ├── lstm_layer.cpp │ │ │ ├── lstm_layer.cu │ │ │ ├── memory_data_layer.cpp │ │ │ ├── multinomial_logistic_loss_layer.cpp │ │ │ ├── mvn_layer.cpp │ │ │ ├── mvn_layer.cu │ │ │ ├── neuron_layer.cpp │ │ │ ├── pooling_layer.cpp │ │ │ ├── pooling_layer.cu │ │ │ ├── power_layer.cpp │ │ │ ├── power_layer.cu │ │ │ ├── prelu_layer.cpp │ │ │ ├── prelu_layer.cu │ │ │ ├── reduction_layer.cpp │ │ │ ├── reduction_layer.cu │ │ │ ├── relu_layer.cpp │ │ │ ├── relu_layer.cu │ │ │ ├── reshape_layer.cpp │ │ │ ├── reverse_layer.cpp │ │ │ ├── reverse_layer.cu │ │ │ ├── roi_pooling_layer.cpp │ │ │ ├── roi_pooling_layer.cu │ │ │ ├── sigmoid_cross_entropy_loss_layer.cpp │ │ │ ├── sigmoid_cross_entropy_loss_layer.cu │ │ │ ├── sigmoid_layer.cpp │ │ │ ├── sigmoid_layer.cu │ │ │ ├── silence_layer.cpp │ │ │ ├── silence_layer.cu │ │ │ ├── slice_layer.cpp │ │ │ ├── slice_layer.cu │ │ │ ├── smooth_L1_loss_layer.cpp │ │ │ ├── smooth_L1_loss_layer.cu │ │ │ ├── softmax_layer.cpp │ │ │ ├── softmax_layer.cu │ │ │ ├── softmax_loss_layer.cpp │ │ │ ├── softmax_loss_layer.cu │ │ │ ├── sparse_im2col_layer.cu │ │ │ ├── split_layer.cpp │ │ │ ├── split_layer.cu │ │ │ ├── spp_layer.cpp │ │ │ ├── tanh_layer.cpp │ │ │ ├── tanh_layer.cu │ │ │ ├── threshold_layer.cpp │ │ │ ├── threshold_layer.cu │ │ │ ├── tile_layer.cpp │ │ │ ├── tile_layer.cu │ │ │ ├── transpose_layer.cpp │ │ │ ├── transpose_layer.cu │ │ │ └── window_data_layer.cpp │ │ ├── net.cpp │ │ ├── parallel.cpp │ │ ├── proto │ │ │ └── caffe.proto │ │ ├── solver.cpp │ │ ├── syncedmem.cpp │ │ ├── test │ │ │ ├── CMakeLists.txt │ │ │ ├── test_accuracy_layer.cpp │ │ │ ├── test_argmax_layer.cpp │ │ │ ├── test_batch_norm_layer.cpp │ │ │ ├── test_benchmark.cpp │ │ │ ├── test_blob.cpp │ │ │ ├── test_caffe_main.cpp │ │ │ ├── test_common.cpp │ │ │ ├── test_concat_layer.cpp │ │ │ ├── test_contrastive_loss_layer.cpp │ │ │ ├── test_convolution_layer.cpp │ │ │ ├── test_data │ │ │ │ ├── generate_sample_data.py │ │ │ │ ├── sample_data.h5 │ │ │ │ ├── sample_data_2_gzip.h5 │ │ │ │ ├── sample_data_list.txt │ │ │ │ ├── solver_data.h5 │ │ │ │ └── solver_data_list.txt │ │ │ ├── test_data_layer.cpp │ │ │ ├── test_data_transformer.cpp │ │ │ ├── test_db.cpp │ │ │ ├── test_deconvolution_layer.cpp │ │ │ ├── test_dummy_data_layer.cpp │ │ │ ├── test_eltwise_layer.cpp │ │ │ ├── test_embed_layer.cpp │ │ │ ├── test_euclidean_loss_layer.cpp │ │ │ ├── test_filler.cpp │ │ │ ├── test_filter_layer.cpp │ │ │ ├── test_flatten_layer.cpp │ │ │ ├── test_gradient_based_solver.cpp │ │ │ ├── test_hdf5_output_layer.cpp │ │ │ ├── test_hdf5data_layer.cpp │ │ │ ├── test_hinge_loss_layer.cpp │ │ │ ├── test_im2col_kernel.cu │ │ │ ├── test_im2col_layer.cpp │ │ │ ├── test_image_data_layer.cpp │ │ │ ├── test_infogain_loss_layer.cpp │ │ │ ├── test_inner_product_layer.cpp │ │ │ ├── test_internal_thread.cpp │ │ │ ├── test_io.cpp │ │ │ ├── test_layer_factory.cpp │ │ │ ├── test_lrn_layer.cpp │ │ │ ├── test_lstm_layer.cpp │ │ │ ├── test_math_functions.cpp │ │ │ ├── test_maxpool_dropout_layers.cpp │ │ │ ├── test_memory_data_layer.cpp │ │ │ ├── test_multinomial_logistic_loss_layer.cpp │ │ │ ├── test_mvn_layer.cpp │ │ │ ├── test_net.cpp │ │ │ ├── test_neuron_layer.cpp │ │ │ ├── test_platform.cpp │ │ │ ├── test_pooling_layer.cpp │ │ │ ├── test_power_layer.cpp │ │ │ ├── test_protobuf.cpp │ │ │ ├── test_random_number_generator.cpp │ │ │ ├── test_reduction_layer.cpp │ │ │ ├── test_reshape_layer.cpp │ │ │ ├── test_reverse_layer.cpp │ │ │ ├── test_roi_pooling_layer.cpp │ │ │ ├── test_sigmoid_cross_entropy_loss_layer.cpp │ │ │ ├── test_slice_layer.cpp │ │ │ ├── test_smooth_L1_loss_layer.cpp │ │ │ ├── test_softmax_layer.cpp │ │ │ ├── test_softmax_with_loss_layer.cpp │ │ │ ├── test_solver.cpp │ │ │ ├── test_split_layer.cpp │ │ │ ├── test_spp_layer.cpp │ │ │ ├── test_stochastic_pooling.cpp │ │ │ ├── test_syncedmem.cpp │ │ │ ├── test_tanh_layer.cpp │ │ │ ├── test_threshold_layer.cpp │ │ │ ├── test_tile_layer.cpp │ │ │ ├── test_transpose_layer.cpp │ │ │ ├── test_upgrade_proto.cpp │ │ │ └── test_util_blas.cpp │ │ └── util │ │ │ ├── benchmark.cpp │ │ │ ├── blocking_queue.cpp │ │ │ ├── cudnn.cpp │ │ │ ├── db.cpp │ │ │ ├── db_leveldb.cpp │ │ │ ├── db_lmdb.cpp │ │ │ ├── hdf5.cpp │ │ │ ├── im2col.cpp │ │ │ ├── im2col.cu │ │ │ ├── insert_splits.cpp │ │ │ ├── io.cpp │ │ │ ├── math_functions.cpp │ │ │ ├── math_functions.cu │ │ │ ├── signal_handler.cpp │ │ │ └── upgrade_proto.cpp │ └── gtest │ │ ├── CMakeLists.txt │ │ ├── gtest-all.cpp │ │ ├── gtest.h │ │ └── gtest_main.cc └── tools │ ├── CMakeLists.txt │ ├── caffe.cpp │ ├── compute_image_mean.cpp │ ├── convert_imageset.cpp │ ├── device_query.cpp │ ├── extra │ ├── extract_seconds.py │ ├── launch_resize_and_crop_images.sh │ ├── parse_log.py │ ├── parse_log.sh │ ├── plot_log.gnuplot.example │ ├── plot_log.py │ ├── plot_training_log.py.example │ ├── resize_and_crop_images.py │ └── summarize.py │ ├── extract_features.cpp │ ├── finetune_net.cpp │ ├── net_speed_benchmark.cpp │ ├── test_net.cpp │ ├── train_net.cpp │ ├── upgrade_net_proto_binary.cpp │ └── upgrade_net_proto_text.cpp ├── demo_images ├── img_1.jpg ├── img_2.jpg └── img_3.jpg ├── models └── deploy.prototxt ├── src ├── anchor.py ├── detectors.py ├── layers │ ├── __init__.py │ └── text_proposal_layer.py ├── other.py ├── text_proposal_connector.py ├── text_proposal_graph_builder.py └── utils │ ├── __init__.py │ ├── cpu_nms.pyx │ └── timer.py └── tools ├── cfg.py └── demo.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.caffemodel 3 | .idea 4 | *.so 5 | results 6 | *.xml 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Shenzhen Institutes of Advanced Technology, Chinese Academy of Sciences 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | ARE THIRD PARTY CODES ARE LICENSED TO YOU UNDER THEIR ORIGINAL LICENSE TERMS. 24 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | cython src/utils/cpu_nms.pyx 3 | gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \ 4 | -I/usr/include/python2.7 -o src/utils/cpu_nms.so src/utils/cpu_nms.c 5 | rm -rf src/utils/cpu_nms.c 6 | -------------------------------------------------------------------------------- /caffe/.gitignore: -------------------------------------------------------------------------------- 1 | ## General 2 | 3 | # Compiled Object files 4 | *.slo 5 | *.lo 6 | *.o 7 | *.cuo 8 | 9 | # Compiled Dynamic libraries 10 | *.so 11 | *.dylib 12 | 13 | # Compiled Static libraries 14 | *.lai 15 | *.la 16 | *.a 17 | 18 | # Compiled protocol buffers 19 | *.pb.h 20 | *.pb.cc 21 | *_pb2.py 22 | 23 | # Compiled python 24 | *.pyc 25 | 26 | # Compiled MATLAB 27 | *.mex* 28 | 29 | # IPython notebook checkpoints 30 | .ipynb_checkpoints 31 | 32 | # Editor temporaries 33 | *.swp 34 | *~ 35 | 36 | # Sublime Text settings 37 | *.sublime-workspace 38 | *.sublime-project 39 | 40 | # Eclipse Project settings 41 | *.*project 42 | .settings 43 | 44 | # QtCreator files 45 | *.user 46 | 47 | # PyCharm files 48 | .idea 49 | 50 | # OSX dir files 51 | .DS_Store 52 | 53 | ## Caffe 54 | 55 | # User's build configuration 56 | Makefile.config 57 | 58 | # Data and models are either 59 | # 1. reference, and not casually committed 60 | # 2. custom, and live on their own unless they're deliberated contributed 61 | data/* 62 | models/* 63 | *.caffemodel 64 | *.caffemodel.h5 65 | *.solverstate 66 | *.solverstate.h5 67 | *.binaryproto 68 | *leveldb 69 | *lmdb 70 | 71 | # build, distribute, and bins (+ python proto bindings) 72 | build 73 | .build_debug/* 74 | .build_release/* 75 | distribute/* 76 | *.testbin 77 | *.bin 78 | python/caffe/proto/ 79 | cmake_build 80 | .cmake_build 81 | 82 | # Generated documentation 83 | docs/_site 84 | docs/gathered 85 | _site 86 | doxygen 87 | docs/dev 88 | 89 | # LevelDB files 90 | *.sst 91 | *.ldb 92 | LOCK 93 | LOG* 94 | CURRENT 95 | MANIFEST-* 96 | -------------------------------------------------------------------------------- /caffe/.travis.yml: -------------------------------------------------------------------------------- 1 | # Use a build matrix to do two builds in parallel: 2 | # one using CMake, and one using make. 3 | env: 4 | matrix: 5 | - WITH_CUDA=false WITH_CMAKE=false 6 | - WITH_CUDA=false WITH_CMAKE=true 7 | - WITH_CUDA=true WITH_CMAKE=false 8 | - WITH_CUDA=true WITH_CMAKE=true 9 | - WITH_CUDA=false WITH_CMAKE=true PYTHON_VERSION=3 10 | 11 | language: cpp 12 | 13 | # Cache Ubuntu apt packages. 14 | cache: 15 | apt: true 16 | directories: 17 | - /home/travis/miniconda 18 | - /home/travis/miniconda2 19 | - /home/travis/miniconda3 20 | 21 | compiler: gcc 22 | 23 | before_install: 24 | - export NUM_THREADS=4 25 | - export SCRIPTS=./scripts/travis 26 | - export CONDA_DIR="/home/travis/miniconda$PYTHON_VERSION" 27 | 28 | install: 29 | - sudo -E $SCRIPTS/travis_install.sh 30 | 31 | before_script: 32 | - export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/cuda/lib64:$CONDA_DIR/lib 33 | - export PATH=$CONDA_DIR/bin:$PATH 34 | - if ! $WITH_CMAKE; then $SCRIPTS/travis_setup_makefile_config.sh; fi 35 | 36 | script: $SCRIPTS/travis_build_and_test.sh 37 | 38 | notifications: 39 | # Emails are sent to the committer's git-configured email address by default, 40 | # but only if they have access to the repository. To enable Travis on your 41 | # public fork of Caffe, just go to travis-ci.org and flip the switch on for 42 | # your Caffe fork. To configure your git email address, use: 43 | # git config --global user.email me@example.com 44 | email: 45 | on_success: always 46 | on_failure: always 47 | 48 | # IRC notifications disabled by default. 49 | # Uncomment next 5 lines to send notifications to chat.freenode.net#caffe 50 | # irc: 51 | # channels: 52 | # - "chat.freenode.net#caffe" 53 | # template: 54 | # - "%{repository}/%{branch} (%{commit} - %{author}): %{message}" 55 | -------------------------------------------------------------------------------- /caffe/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Issues 4 | 5 | Specific Caffe design and development issues, bugs, and feature requests are maintained by GitHub Issues. 6 | 7 | _Please do not post usage, installation, or modeling questions, or other requests for help to Issues._ 8 | Use the [caffe-users list](https://groups.google.com/forum/#!forum/caffe-users) instead. This helps developers maintain a clear, uncluttered, and efficient view of the state of Caffe. 9 | 10 | When reporting a bug, it's most helpful to provide the following information, where applicable: 11 | 12 | * What steps reproduce the bug? 13 | * Can you reproduce the bug using the latest [master](https://github.com/BVLC/caffe/tree/master), compiled with the `DEBUG` make option? 14 | * What hardware and operating system/distribution are you running? 15 | * If the bug is a crash, provide the backtrace (usually printed by Caffe; always obtainable with `gdb`). 16 | 17 | Try to give your issue a title that is succinct and specific. The devs will rename issues as needed to keep track of them. 18 | 19 | ## Pull Requests 20 | 21 | Caffe welcomes all contributions. 22 | 23 | See the [contributing guide](http://caffe.berkeleyvision.org/development.html) for details. 24 | 25 | Briefly: read commit by commit, a PR should tell a clean, compelling story of _one_ improvement to Caffe. In particular: 26 | 27 | * A PR should do one clear thing that obviously improves Caffe, and nothing more. Making many smaller PRs is better than making one large PR; review effort is superlinear in the amount of code involved. 28 | * Similarly, each commit should be a small, atomic change representing one step in development. PRs should be made of many commits where appropriate. 29 | * Please do rewrite PR history to be clean rather than chronological. Within-PR bugfixes, style cleanups, reversions, etc. should be squashed and should not appear in merged PR history. 30 | * Anything nonobvious from the code should be explained in comments, commit messages, or the PR description, as appropriate. 31 | -------------------------------------------------------------------------------- /caffe/CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Contributors 2 | 3 | Caffe is developed by a core set of BVLC members and the open-source community. 4 | 5 | We thank all of our [contributors](https://github.com/BVLC/caffe/graphs/contributors)! 6 | 7 | **For the detailed history of contributions** of a given file, try 8 | 9 | git blame file 10 | 11 | to see line-by-line credits and 12 | 13 | git log --follow file 14 | 15 | to see the change log even across renames and rewrites. 16 | 17 | Please refer to the [acknowledgements](http://caffe.berkeleyvision.org/#acknowledgements) on the Caffe site for further details. 18 | 19 | **Copyright** is held by the original contributor according to the versioning history; see LICENSE. 20 | -------------------------------------------------------------------------------- /caffe/INSTALL.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | See http://caffe.berkeleyvision.org/installation.html for the latest 4 | installation instructions. 5 | 6 | Check the issue tracker in case you need help: 7 | https://github.com/BVLC/caffe/issues 8 | -------------------------------------------------------------------------------- /caffe/README.md: -------------------------------------------------------------------------------- 1 | # Caffe 2 | 3 | Caffe is a deep learning framework made with expression, speed, and modularity in mind. 4 | It is developed by the Berkeley Vision and Learning Center ([BVLC](http://bvlc.eecs.berkeley.edu)) and community contributors. 5 | 6 | Check out the [project site](http://caffe.berkeleyvision.org) for all the details like 7 | 8 | - [DIY Deep Learning for Vision with Caffe](https://docs.google.com/presentation/d/1UeKXVgRvvxg9OUdh_UiC5G71UMscNPlvArsWER41PsU/edit#slide=id.p) 9 | - [Tutorial Documentation](http://caffe.berkeleyvision.org/tutorial/) 10 | - [BVLC reference models](http://caffe.berkeleyvision.org/model_zoo.html) and the [community model zoo](https://github.com/BVLC/caffe/wiki/Model-Zoo) 11 | - [Installation instructions](http://caffe.berkeleyvision.org/installation.html) 12 | 13 | and step-by-step examples. 14 | 15 | [![Join the chat at https://gitter.im/BVLC/caffe](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/BVLC/caffe?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 16 | 17 | Please join the [caffe-users group](https://groups.google.com/forum/#!forum/caffe-users) or [gitter chat](https://gitter.im/BVLC/caffe) to ask questions and talk about methods and models. 18 | Framework development discussions and thorough bug reports are collected on [Issues](https://github.com/BVLC/caffe/issues). 19 | 20 | Happy brewing! 21 | 22 | ## License and Citation 23 | 24 | Caffe is released under the [BSD 2-Clause license](https://github.com/BVLC/caffe/blob/master/LICENSE). 25 | The BVLC reference models are released for unrestricted use. 26 | 27 | Please cite Caffe in your publications if it helps your research: 28 | 29 | @article{jia2014caffe, 30 | Author = {Jia, Yangqing and Shelhamer, Evan and Donahue, Jeff and Karayev, Sergey and Long, Jonathan and Girshick, Ross and Guadarrama, Sergio and Darrell, Trevor}, 31 | Journal = {arXiv preprint arXiv:1408.5093}, 32 | Title = {Caffe: Convolutional Architecture for Fast Feature Embedding}, 33 | Year = {2014} 34 | } 35 | -------------------------------------------------------------------------------- /caffe/caffe.cloc: -------------------------------------------------------------------------------- 1 | Bourne Shell 2 | filter remove_matches ^\s*# 3 | filter remove_inline #.*$ 4 | extension sh 5 | script_exe sh 6 | C 7 | filter remove_matches ^\s*// 8 | filter call_regexp_common C 9 | filter remove_inline //.*$ 10 | extension c 11 | extension ec 12 | extension pgc 13 | C++ 14 | filter remove_matches ^\s*// 15 | filter remove_inline //.*$ 16 | filter call_regexp_common C 17 | extension C 18 | extension cc 19 | extension cpp 20 | extension cxx 21 | extension pcc 22 | C/C++ Header 23 | filter remove_matches ^\s*// 24 | filter call_regexp_common C 25 | filter remove_inline //.*$ 26 | extension H 27 | extension h 28 | extension hh 29 | extension hpp 30 | CUDA 31 | filter remove_matches ^\s*// 32 | filter remove_inline //.*$ 33 | filter call_regexp_common C 34 | extension cu 35 | Python 36 | filter remove_matches ^\s*# 37 | filter docstring_to_C 38 | filter call_regexp_common C 39 | filter remove_inline #.*$ 40 | extension py 41 | make 42 | filter remove_matches ^\s*# 43 | filter remove_inline #.*$ 44 | extension Gnumakefile 45 | extension Makefile 46 | extension am 47 | extension gnumakefile 48 | extension makefile 49 | filename Gnumakefile 50 | filename Makefile 51 | filename gnumakefile 52 | filename makefile 53 | script_exe make 54 | -------------------------------------------------------------------------------- /caffe/cmake/External/glog.cmake: -------------------------------------------------------------------------------- 1 | # glog depends on gflags 2 | include("cmake/External/gflags.cmake") 3 | 4 | if (NOT __GLOG_INCLUDED) 5 | set(__GLOG_INCLUDED TRUE) 6 | 7 | # try the system-wide glog first 8 | find_package(Glog) 9 | if (GLOG_FOUND) 10 | set(GLOG_EXTERNAL FALSE) 11 | else() 12 | # fetch and build glog from github 13 | 14 | # build directory 15 | set(glog_PREFIX ${CMAKE_BINARY_DIR}/external/glog-prefix) 16 | # install directory 17 | set(glog_INSTALL ${CMAKE_BINARY_DIR}/external/glog-install) 18 | 19 | # we build glog statically, but want to link it into the caffe shared library 20 | # this requires position-independent code 21 | if (UNIX) 22 | set(GLOG_EXTRA_COMPILER_FLAGS "-fPIC") 23 | endif() 24 | 25 | set(GLOG_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${GLOG_EXTRA_COMPILER_FLAGS}) 26 | set(GLOG_C_FLAGS ${CMAKE_C_FLAGS} ${GLOG_EXTRA_COMPILER_FLAGS}) 27 | 28 | # depend on gflags if we're also building it 29 | if (GFLAGS_EXTERNAL) 30 | set(GLOG_DEPENDS gflags) 31 | endif() 32 | 33 | ExternalProject_Add(glog 34 | DEPENDS ${GLOG_DEPENDS} 35 | PREFIX ${glog_PREFIX} 36 | GIT_REPOSITORY "https://github.com/google/glog" 37 | GIT_TAG "v0.3.4" 38 | UPDATE_COMMAND "" 39 | INSTALL_DIR ${gflags_INSTALL} 40 | CONFIGURE_COMMAND env "CFLAGS=${GLOG_C_FLAGS}" "CXXFLAGS=${GLOG_CXX_FLAGS}" ${glog_PREFIX}/src/glog/configure --prefix=${glog_INSTALL} --enable-shared=no --enable-static=yes --with-gflags=${GFLAGS_LIBRARY_DIRS}/.. 41 | LOG_DOWNLOAD 1 42 | LOG_CONFIGURE 1 43 | LOG_INSTALL 1 44 | ) 45 | 46 | set(GLOG_FOUND TRUE) 47 | set(GLOG_INCLUDE_DIRS ${glog_INSTALL}/include) 48 | set(GLOG_LIBRARIES ${GFLAGS_LIBRARIES} ${glog_INSTALL}/lib/libglog.a) 49 | set(GLOG_LIBRARY_DIRS ${glog_INSTALL}/lib) 50 | set(GLOG_EXTERNAL TRUE) 51 | 52 | list(APPEND external_project_dependencies glog) 53 | endif() 54 | 55 | endif() 56 | 57 | -------------------------------------------------------------------------------- /caffe/cmake/Misc.cmake: -------------------------------------------------------------------------------- 1 | # ---[ Configuration types 2 | set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Possible configurations" FORCE) 3 | mark_as_advanced(CMAKE_CONFIGURATION_TYPES) 4 | 5 | if(DEFINED CMAKE_BUILD_TYPE) 6 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES}) 7 | endif() 8 | 9 | # --[ If user doesn't specify build type then assume release 10 | if("${CMAKE_BUILD_TYPE}" STREQUAL "") 11 | set(CMAKE_BUILD_TYPE Release) 12 | endif() 13 | 14 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 15 | set(CMAKE_COMPILER_IS_CLANGXX TRUE) 16 | endif() 17 | 18 | # ---[ Solution folders 19 | caffe_option(USE_PROJECT_FOLDERS "IDE Solution folders" (MSVC_IDE OR CMAKE_GENERATOR MATCHES Xcode) ) 20 | 21 | if(USE_PROJECT_FOLDERS) 22 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) 23 | set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMakeTargets") 24 | endif() 25 | 26 | # ---[ Install options 27 | if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 28 | set(CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/install" CACHE PATH "Default install path" FORCE) 29 | endif() 30 | 31 | # ---[ RPATH settings 32 | set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOLEAN "Use link paths for shared library rpath") 33 | set(CMAKE_MACOSX_RPATH TRUE) 34 | 35 | list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES ${CMAKE_INSTALL_PREFIX}/lib __is_systtem_dir) 36 | if(${__is_systtem_dir} STREQUAL -1) 37 | set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) 38 | endif() 39 | 40 | # ---[ Funny target 41 | if(UNIX OR APPLE) 42 | add_custom_target(symlink_to_build COMMAND "ln" "-sf" "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/build" 43 | COMMENT "Adding symlink: /build -> ${PROJECT_BINARY_DIR}" ) 44 | endif() 45 | 46 | # ---[ Set debug postfix 47 | set(Caffe_DEBUG_POSTFIX "-d") 48 | 49 | set(CAffe_POSTFIX "") 50 | if(CMAKE_BUILD_TYPE MATCHES "Debug") 51 | set(CAffe_POSTFIX ${Caffe_DEBUG_POSTFIX}) 52 | endif() 53 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindAtlas.cmake: -------------------------------------------------------------------------------- 1 | # Find the Atlas (and Lapack) libraries 2 | # 3 | # The following variables are optionally searched for defaults 4 | # Atlas_ROOT_DIR: Base directory where all Atlas components are found 5 | # 6 | # The following are set after configuration is done: 7 | # Atlas_FOUND 8 | # Atlas_INCLUDE_DIRS 9 | # Atlas_LIBRARIES 10 | # Atlas_LIBRARYRARY_DIRS 11 | 12 | set(Atlas_INCLUDE_SEARCH_PATHS 13 | /usr/include/atlas 14 | /usr/include/atlas-base 15 | $ENV{Atlas_ROOT_DIR} 16 | $ENV{Atlas_ROOT_DIR}/include 17 | ) 18 | 19 | set(Atlas_LIB_SEARCH_PATHS 20 | /usr/lib/atlas 21 | /usr/lib/atlas-base 22 | $ENV{Atlas_ROOT_DIR} 23 | $ENV{Atlas_ROOT_DIR}/lib 24 | ) 25 | 26 | find_path(Atlas_CBLAS_INCLUDE_DIR NAMES cblas.h PATHS ${Atlas_INCLUDE_SEARCH_PATHS}) 27 | find_path(Atlas_CLAPACK_INCLUDE_DIR NAMES clapack.h PATHS ${Atlas_INCLUDE_SEARCH_PATHS}) 28 | 29 | find_library(Atlas_CBLAS_LIBRARY NAMES ptcblas_r ptcblas cblas_r cblas PATHS ${Atlas_LIB_SEARCH_PATHS}) 30 | find_library(Atlas_BLAS_LIBRARY NAMES atlas_r atlas PATHS ${Atlas_LIB_SEARCH_PATHS}) 31 | find_library(Atlas_LAPACK_LIBRARY NAMES alapack_r alapack lapack_atlas PATHS ${Atlas_LIB_SEARCH_PATHS}) 32 | 33 | set(LOOKED_FOR 34 | Atlas_CBLAS_INCLUDE_DIR 35 | Atlas_CLAPACK_INCLUDE_DIR 36 | 37 | Atlas_CBLAS_LIBRARY 38 | Atlas_BLAS_LIBRARY 39 | Atlas_LAPACK_LIBRARY 40 | ) 41 | 42 | include(FindPackageHandleStandardArgs) 43 | find_package_handle_standard_args(Atlas DEFAULT_MSG ${LOOKED_FOR}) 44 | 45 | if(ATLAS_FOUND) 46 | set(Atlas_INCLUDE_DIR ${Atlas_CBLAS_INCLUDE_DIR} ${Atlas_CLAPACK_INCLUDE_DIR}) 47 | set(Atlas_LIBRARIES ${Atlas_LAPACK_LIBRARY} ${Atlas_CBLAS_LIBRARY} ${Atlas_BLAS_LIBRARY}) 48 | mark_as_advanced(${LOOKED_FOR}) 49 | 50 | message(STATUS "Found Atlas (include: ${Atlas_CBLAS_INCLUDE_DIR}, library: ${Atlas_BLAS_LIBRARY})") 51 | endif(ATLAS_FOUND) 52 | 53 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindGFlags.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find GFLAGS 2 | # 3 | # The following variables are optionally searched for defaults 4 | # GFLAGS_ROOT_DIR: Base directory where all GFLAGS components are found 5 | # 6 | # The following are set after configuration is done: 7 | # GFLAGS_FOUND 8 | # GFLAGS_INCLUDE_DIRS 9 | # GFLAGS_LIBRARIES 10 | # GFLAGS_LIBRARYRARY_DIRS 11 | 12 | include(FindPackageHandleStandardArgs) 13 | 14 | set(GFLAGS_ROOT_DIR "" CACHE PATH "Folder contains Gflags") 15 | 16 | # We are testing only a couple of files in the include directories 17 | if(WIN32) 18 | find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h 19 | PATHS ${GFLAGS_ROOT_DIR}/src/windows) 20 | else() 21 | find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h 22 | PATHS ${GFLAGS_ROOT_DIR}) 23 | endif() 24 | 25 | if(MSVC) 26 | find_library(GFLAGS_LIBRARY_RELEASE 27 | NAMES libgflags 28 | PATHS ${GFLAGS_ROOT_DIR} 29 | PATH_SUFFIXES Release) 30 | 31 | find_library(GFLAGS_LIBRARY_DEBUG 32 | NAMES libgflags-debug 33 | PATHS ${GFLAGS_ROOT_DIR} 34 | PATH_SUFFIXES Debug) 35 | 36 | set(GFLAGS_LIBRARY optimized ${GFLAGS_LIBRARY_RELEASE} debug ${GFLAGS_LIBRARY_DEBUG}) 37 | else() 38 | find_library(GFLAGS_LIBRARY gflags) 39 | endif() 40 | 41 | find_package_handle_standard_args(GFlags DEFAULT_MSG GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY) 42 | 43 | 44 | if(GFLAGS_FOUND) 45 | set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR}) 46 | set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY}) 47 | message(STATUS "Found gflags (include: ${GFLAGS_INCLUDE_DIR}, library: ${GFLAGS_LIBRARY})") 48 | mark_as_advanced(GFLAGS_LIBRARY_DEBUG GFLAGS_LIBRARY_RELEASE 49 | GFLAGS_LIBRARY GFLAGS_INCLUDE_DIR GFLAGS_ROOT_DIR) 50 | endif() 51 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindGlog.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find Glog 2 | # 3 | # The following variables are optionally searched for defaults 4 | # GLOG_ROOT_DIR: Base directory where all GLOG components are found 5 | # 6 | # The following are set after configuration is done: 7 | # GLOG_FOUND 8 | # GLOG_INCLUDE_DIRS 9 | # GLOG_LIBRARIES 10 | # GLOG_LIBRARYRARY_DIRS 11 | 12 | include(FindPackageHandleStandardArgs) 13 | 14 | set(GLOG_ROOT_DIR "" CACHE PATH "Folder contains Google glog") 15 | 16 | if(WIN32) 17 | find_path(GLOG_INCLUDE_DIR glog/logging.h 18 | PATHS ${GLOG_ROOT_DIR}/src/windows) 19 | else() 20 | find_path(GLOG_INCLUDE_DIR glog/logging.h 21 | PATHS ${GLOG_ROOT_DIR}) 22 | endif() 23 | 24 | if(MSVC) 25 | find_library(GLOG_LIBRARY_RELEASE libglog_static 26 | PATHS ${GLOG_ROOT_DIR} 27 | PATH_SUFFIXES Release) 28 | 29 | find_library(GLOG_LIBRARY_DEBUG libglog_static 30 | PATHS ${GLOG_ROOT_DIR} 31 | PATH_SUFFIXES Debug) 32 | 33 | set(GLOG_LIBRARY optimized ${GLOG_LIBRARY_RELEASE} debug ${GLOG_LIBRARY_DEBUG}) 34 | else() 35 | find_library(GLOG_LIBRARY glog 36 | PATHS ${GLOG_ROOT_DIR} 37 | PATH_SUFFIXES lib lib64) 38 | endif() 39 | 40 | find_package_handle_standard_args(Glog DEFAULT_MSG GLOG_INCLUDE_DIR GLOG_LIBRARY) 41 | 42 | if(GLOG_FOUND) 43 | set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR}) 44 | set(GLOG_LIBRARIES ${GLOG_LIBRARY}) 45 | message(STATUS "Found glog (include: ${GLOG_INCLUDE_DIR}, library: ${GLOG_LIBRARY})") 46 | mark_as_advanced(GLOG_ROOT_DIR GLOG_LIBRARY_RELEASE GLOG_LIBRARY_DEBUG 47 | GLOG_LIBRARY GLOG_INCLUDE_DIR) 48 | endif() 49 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindLMDB.cmake: -------------------------------------------------------------------------------- 1 | # Try to find the LMBD libraries and headers 2 | # LMDB_FOUND - system has LMDB lib 3 | # LMDB_INCLUDE_DIR - the LMDB include directory 4 | # LMDB_LIBRARIES - Libraries needed to use LMDB 5 | 6 | # FindCWD based on FindGMP by: 7 | # Copyright (c) 2006, Laurent Montel, 8 | # 9 | # Redistribution and use is allowed according to the terms of the BSD license. 10 | 11 | # Adapted from FindCWD by: 12 | # Copyright 2013 Conrad Steenberg 13 | # Aug 31, 2013 14 | 15 | find_path(LMDB_INCLUDE_DIR NAMES lmdb.h PATHS "$ENV{LMDB_DIR}/include") 16 | find_library(LMDB_LIBRARIES NAMES lmdb PATHS "$ENV{LMDB_DIR}/lib" ) 17 | 18 | include(FindPackageHandleStandardArgs) 19 | find_package_handle_standard_args(LMDB DEFAULT_MSG LMDB_INCLUDE_DIR LMDB_LIBRARIES) 20 | 21 | if(LMDB_FOUND) 22 | message(STATUS "Found lmdb (include: ${LMDB_INCLUDE_DIR}, library: ${LMDB_LIBRARIES})") 23 | mark_as_advanced(LMDB_INCLUDE_DIR LMDB_LIBRARIES) 24 | 25 | caffe_parse_header(${LMDB_INCLUDE_DIR}/lmdb.h 26 | LMDB_VERSION_LINES MDB_VERSION_MAJOR MDB_VERSION_MINOR MDB_VERSION_PATCH) 27 | set(LMDB_VERSION "${MDB_VERSION_MAJOR}.${MDB_VERSION_MINOR}.${MDB_VERSION_PATCH}") 28 | endif() 29 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindLevelDB.cmake: -------------------------------------------------------------------------------- 1 | # - Find LevelDB 2 | # 3 | # LevelDB_INCLUDES - List of LevelDB includes 4 | # LevelDB_LIBRARIES - List of libraries when using LevelDB. 5 | # LevelDB_FOUND - True if LevelDB found. 6 | 7 | # Look for the header file. 8 | find_path(LevelDB_INCLUDE NAMES leveldb/db.h 9 | PATHS $ENV{LEVELDB_ROOT}/include /opt/local/include /usr/local/include /usr/include 10 | DOC "Path in which the file leveldb/db.h is located." ) 11 | 12 | # Look for the library. 13 | find_library(LevelDB_LIBRARY NAMES leveldb 14 | PATHS /usr/lib $ENV{LEVELDB_ROOT}/lib 15 | DOC "Path to leveldb library." ) 16 | 17 | include(FindPackageHandleStandardArgs) 18 | find_package_handle_standard_args(LevelDB DEFAULT_MSG LevelDB_INCLUDE LevelDB_LIBRARY) 19 | 20 | if(LEVELDB_FOUND) 21 | message(STATUS "Found LevelDB (include: ${LevelDB_INCLUDE}, library: ${LevelDB_LIBRARY})") 22 | set(LevelDB_INCLUDES ${LevelDB_INCLUDE}) 23 | set(LevelDB_LIBRARIES ${LevelDB_LIBRARY}) 24 | mark_as_advanced(LevelDB_INCLUDE LevelDB_LIBRARY) 25 | 26 | if(EXISTS "${LevelDB_INCLUDE}/leveldb/db.h") 27 | file(STRINGS "${LevelDB_INCLUDE}/leveldb/db.h" __version_lines 28 | REGEX "static const int k[^V]+Version[ \t]+=[ \t]+[0-9]+;") 29 | 30 | foreach(__line ${__version_lines}) 31 | if(__line MATCHES "[^k]+kMajorVersion[ \t]+=[ \t]+([0-9]+);") 32 | set(LEVELDB_VERSION_MAJOR ${CMAKE_MATCH_1}) 33 | elseif(__line MATCHES "[^k]+kMinorVersion[ \t]+=[ \t]+([0-9]+);") 34 | set(LEVELDB_VERSION_MINOR ${CMAKE_MATCH_1}) 35 | endif() 36 | endforeach() 37 | 38 | if(LEVELDB_VERSION_MAJOR AND LEVELDB_VERSION_MINOR) 39 | set(LEVELDB_VERSION "${LEVELDB_VERSION_MAJOR}.${LEVELDB_VERSION_MINOR}") 40 | endif() 41 | 42 | caffe_clear_vars(__line __version_lines) 43 | endif() 44 | endif() 45 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindMatlabMex.cmake: -------------------------------------------------------------------------------- 1 | # This module looks for MatlabMex compiler 2 | # Defines variables: 3 | # Matlab_DIR - Matlab root dir 4 | # Matlab_mex - path to mex compiler 5 | # Matlab_mexext - path to mexext 6 | 7 | if(MSVC) 8 | foreach(__ver "9.30" "7.14" "7.11" "7.10" "7.9" "7.8" "7.7") 9 | get_filename_component(__matlab_root "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\${__ver};MATLABROOT]" ABSOLUTE) 10 | if(__matlab_root) 11 | break() 12 | endif() 13 | endforeach() 14 | endif() 15 | 16 | if(APPLE) 17 | foreach(__ver "R2014b" "R2014a" "R2013b" "R2013a" "R2012b" "R2012a" "R2011b" "R2011a" "R2010b" "R2010a") 18 | if(EXISTS /Applications/MATLAB_${__ver}.app) 19 | set(__matlab_root /Applications/MATLAB_${__ver}.app) 20 | break() 21 | endif() 22 | endforeach() 23 | endif() 24 | 25 | if(UNIX) 26 | execute_process(COMMAND which matlab OUTPUT_STRIP_TRAILING_WHITESPACE 27 | OUTPUT_VARIABLE __out RESULT_VARIABLE __res) 28 | 29 | if(__res MATCHES 0) # Suppress `readlink` warning if `which` returned nothing 30 | execute_process(COMMAND which matlab COMMAND xargs readlink 31 | COMMAND xargs dirname COMMAND xargs dirname COMMAND xargs echo -n 32 | OUTPUT_VARIABLE __matlab_root OUTPUT_STRIP_TRAILING_WHITESPACE) 33 | endif() 34 | endif() 35 | 36 | 37 | find_path(Matlab_DIR NAMES bin/mex bin/mexext PATHS ${__matlab_root} 38 | DOC "Matlab directory" NO_DEFAULT_PATH) 39 | 40 | find_program(Matlab_mex NAMES mex mex.bat HINTS ${Matlab_DIR} PATH_SUFFIXES bin NO_DEFAULT_PATH) 41 | find_program(Matlab_mexext NAMES mexext mexext.bat HINTS ${Matlab_DIR} PATH_SUFFIXES bin NO_DEFAULT_PATH) 42 | 43 | include(FindPackageHandleStandardArgs) 44 | find_package_handle_standard_args(MatlabMex DEFAULT_MSG Matlab_mex Matlab_mexext) 45 | 46 | if(MATLABMEX_FOUND) 47 | mark_as_advanced(Matlab_mex Matlab_mexext) 48 | endif() 49 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindOpenBLAS.cmake: -------------------------------------------------------------------------------- 1 | 2 | 3 | SET(Open_BLAS_INCLUDE_SEARCH_PATHS 4 | /usr/include 5 | /usr/include/openblas-base 6 | /usr/local/include 7 | /usr/local/include/openblas-base 8 | /opt/OpenBLAS/include 9 | $ENV{OpenBLAS_HOME} 10 | $ENV{OpenBLAS_HOME}/include 11 | ) 12 | 13 | SET(Open_BLAS_LIB_SEARCH_PATHS 14 | /lib/ 15 | /lib/openblas-base 16 | /lib64/ 17 | /usr/lib 18 | /usr/lib/openblas-base 19 | /usr/lib64 20 | /usr/local/lib 21 | /usr/local/lib64 22 | /opt/OpenBLAS/lib 23 | $ENV{OpenBLAS}cd 24 | $ENV{OpenBLAS}/lib 25 | $ENV{OpenBLAS_HOME} 26 | $ENV{OpenBLAS_HOME}/lib 27 | ) 28 | 29 | FIND_PATH(OpenBLAS_INCLUDE_DIR NAMES cblas.h PATHS ${Open_BLAS_INCLUDE_SEARCH_PATHS}) 30 | FIND_LIBRARY(OpenBLAS_LIB NAMES openblas PATHS ${Open_BLAS_LIB_SEARCH_PATHS}) 31 | 32 | SET(OpenBLAS_FOUND ON) 33 | 34 | # Check include files 35 | IF(NOT OpenBLAS_INCLUDE_DIR) 36 | SET(OpenBLAS_FOUND OFF) 37 | MESSAGE(STATUS "Could not find OpenBLAS include. Turning OpenBLAS_FOUND off") 38 | ENDIF() 39 | 40 | # Check libraries 41 | IF(NOT OpenBLAS_LIB) 42 | SET(OpenBLAS_FOUND OFF) 43 | MESSAGE(STATUS "Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off") 44 | ENDIF() 45 | 46 | IF (OpenBLAS_FOUND) 47 | IF (NOT OpenBLAS_FIND_QUIETLY) 48 | MESSAGE(STATUS "Found OpenBLAS libraries: ${OpenBLAS_LIB}") 49 | MESSAGE(STATUS "Found OpenBLAS include: ${OpenBLAS_INCLUDE_DIR}") 50 | ENDIF (NOT OpenBLAS_FIND_QUIETLY) 51 | ELSE (OpenBLAS_FOUND) 52 | IF (OpenBLAS_FIND_REQUIRED) 53 | MESSAGE(FATAL_ERROR "Could not find OpenBLAS") 54 | ENDIF (OpenBLAS_FIND_REQUIRED) 55 | ENDIF (OpenBLAS_FOUND) 56 | 57 | MARK_AS_ADVANCED( 58 | OpenBLAS_INCLUDE_DIR 59 | OpenBLAS_LIB 60 | OpenBLAS 61 | ) 62 | 63 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindSnappy.cmake: -------------------------------------------------------------------------------- 1 | # Find the Snappy libraries 2 | # 3 | # The following variables are optionally searched for defaults 4 | # Snappy_ROOT_DIR: Base directory where all Snappy components are found 5 | # 6 | # The following are set after configuration is done: 7 | # SNAPPY_FOUND 8 | # Snappy_INCLUDE_DIR 9 | # Snappy_LIBRARIES 10 | 11 | find_path(Snappy_INCLUDE_DIR NAMES snappy.h 12 | PATHS ${SNAPPY_ROOT_DIR} ${SNAPPY_ROOT_DIR}/include) 13 | 14 | find_library(Snappy_LIBRARIES NAMES snappy 15 | PATHS ${SNAPPY_ROOT_DIR} ${SNAPPY_ROOT_DIR}/lib) 16 | 17 | include(FindPackageHandleStandardArgs) 18 | find_package_handle_standard_args(Snappy DEFAULT_MSG Snappy_INCLUDE_DIR Snappy_LIBRARIES) 19 | 20 | if(SNAPPY_FOUND) 21 | message(STATUS "Found Snappy (include: ${Snappy_INCLUDE_DIR}, library: ${Snappy_LIBRARIES})") 22 | mark_as_advanced(Snappy_INCLUDE_DIR Snappy_LIBRARIES) 23 | 24 | caffe_parse_header(${Snappy_INCLUDE_DIR}/snappy-stubs-public.h 25 | SNAPPY_VERION_LINES SNAPPY_MAJOR SNAPPY_MINOR SNAPPY_PATCHLEVEL) 26 | set(Snappy_VERSION "${SNAPPY_MAJOR}.${SNAPPY_MINOR}.${SNAPPY_PATCHLEVEL}") 27 | endif() 28 | 29 | -------------------------------------------------------------------------------- /caffe/cmake/Modules/FindvecLib.cmake: -------------------------------------------------------------------------------- 1 | # Find the vecLib libraries as part of Accelerate.framework or as standalon framework 2 | # 3 | # The following are set after configuration is done: 4 | # VECLIB_FOUND 5 | # vecLib_INCLUDE_DIR 6 | # vecLib_LINKER_LIBS 7 | 8 | 9 | if(NOT APPLE) 10 | return() 11 | endif() 12 | 13 | set(__veclib_include_suffix "Frameworks/vecLib.framework/Versions/Current/Headers") 14 | 15 | find_path(vecLib_INCLUDE_DIR vecLib.h 16 | DOC "vecLib include directory" 17 | PATHS /System/Library/${__veclib_include_suffix} 18 | /System/Library/Frameworks/Accelerate.framework/Versions/Current/${__veclib_include_suffix} 19 | /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/) 20 | 21 | include(FindPackageHandleStandardArgs) 22 | find_package_handle_standard_args(vecLib DEFAULT_MSG vecLib_INCLUDE_DIR) 23 | 24 | if(VECLIB_FOUND) 25 | if(vecLib_INCLUDE_DIR MATCHES "^/System/Library/Frameworks/vecLib.framework.*") 26 | set(vecLib_LINKER_LIBS -lcblas "-framework vecLib") 27 | message(STATUS "Found standalone vecLib.framework") 28 | else() 29 | set(vecLib_LINKER_LIBS -lcblas "-framework Accelerate") 30 | message(STATUS "Found vecLib as part of Accelerate.framework") 31 | endif() 32 | 33 | mark_as_advanced(vecLib_INCLUDE_DIR) 34 | endif() 35 | -------------------------------------------------------------------------------- /caffe/cmake/Templates/CaffeConfig.cmake.in: -------------------------------------------------------------------------------- 1 | # Config file for the Caffe package. 2 | # 3 | # Note: 4 | # Caffe and this config file depends on opencv, 5 | # so put `find_package(OpenCV)` before searching Caffe 6 | # via `find_package(Caffe)`. All other lib/includes 7 | # dependencies are hard coded in the file 8 | # 9 | # After successful configuration the following variables 10 | # will be defined: 11 | # 12 | # Caffe_INCLUDE_DIRS - Caffe include directories 13 | # Caffe_LIBRARIES - libraries to link against 14 | # Caffe_DEFINITIONS - a list of definitions to pass to compiler 15 | # 16 | # Caffe_HAVE_CUDA - signals about CUDA support 17 | # Caffe_HAVE_CUDNN - signals about cuDNN support 18 | 19 | 20 | # OpenCV dependency 21 | 22 | if(NOT OpenCV_FOUND) 23 | set(Caffe_OpenCV_CONFIG_PATH "@OpenCV_CONFIG_PATH@") 24 | if(Caffe_OpenCV_CONFIG_PATH) 25 | get_filename_component(Caffe_OpenCV_CONFIG_PATH ${Caffe_OpenCV_CONFIG_PATH} ABSOLUTE) 26 | 27 | if(EXISTS ${Caffe_OpenCV_CONFIG_PATH} AND NOT TARGET opencv_core) 28 | message(STATUS "Caffe: using OpenCV config from ${Caffe_OpenCV_CONFIG_PATH}") 29 | include(${Caffe_OpenCV_CONFIG_PATH}/OpenCVModules.cmake) 30 | endif() 31 | 32 | else() 33 | find_package(OpenCV REQUIRED) 34 | endif() 35 | unset(Caffe_OpenCV_CONFIG_PATH) 36 | endif() 37 | 38 | # Compute paths 39 | get_filename_component(Caffe_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) 40 | set(Caffe_INCLUDE_DIRS "@Caffe_INCLUDE_DIRS@") 41 | 42 | @Caffe_INSTALL_INCLUDE_DIR_APPEND_COMMAND@ 43 | 44 | # Our library dependencies 45 | if(NOT TARGET caffe AND NOT caffe_BINARY_DIR) 46 | include("${Caffe_CMAKE_DIR}/CaffeTargets.cmake") 47 | endif() 48 | 49 | # List of IMPORTED libs created by CaffeTargets.cmake 50 | set(Caffe_LIBRARIES caffe) 51 | 52 | # Definitions 53 | set(Caffe_DEFINITIONS "@Caffe_DEFINITIONS@") 54 | 55 | # Cuda support variables 56 | set(Caffe_CPU_ONLY @CPU_ONLY@) 57 | set(Caffe_HAVE_CUDA @HAVE_CUDA@) 58 | set(Caffe_HAVE_CUDNN @HAVE_CUDNN@) 59 | -------------------------------------------------------------------------------- /caffe/cmake/Templates/CaffeConfigVersion.cmake.in: -------------------------------------------------------------------------------- 1 | set(PACKAGE_VERSION "@Caffe_VERSION@") 2 | 3 | # Check whether the requested PACKAGE_FIND_VERSION is compatible 4 | if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") 5 | set(PACKAGE_VERSION_COMPATIBLE FALSE) 6 | else() 7 | set(PACKAGE_VERSION_COMPATIBLE TRUE) 8 | if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") 9 | set(PACKAGE_VERSION_EXACT TRUE) 10 | endif() 11 | endif() 12 | -------------------------------------------------------------------------------- /caffe/cmake/Templates/caffe_config.h.in: -------------------------------------------------------------------------------- 1 | /* Sources directory */ 2 | #define SOURCE_FOLDER "${PROJECT_SOURCE_DIR}" 3 | 4 | /* Binaries directory */ 5 | #define BINARY_FOLDER "${PROJECT_BINARY_DIR}" 6 | 7 | /* NVIDA Cuda */ 8 | #cmakedefine HAVE_CUDA 9 | 10 | /* NVIDA cuDNN */ 11 | #cmakedefine HAVE_CUDNN 12 | #cmakedefine USE_CUDNN 13 | 14 | /* NVIDA cuDNN */ 15 | #cmakedefine CPU_ONLY 16 | 17 | /* Test device */ 18 | #define CUDA_TEST_DEVICE ${CUDA_TEST_DEVICE} 19 | 20 | /* Temporary (TODO: remove) */ 21 | #if 1 22 | #define CMAKE_SOURCE_DIR SOURCE_FOLDER "/src/" 23 | #define EXAMPLES_SOURCE_DIR BINARY_FOLDER "/examples/" 24 | #define CMAKE_EXT ".gen.cmake" 25 | #else 26 | #define CMAKE_SOURCE_DIR "src/" 27 | #define EXAMPLES_SOURCE_DIR "examples/" 28 | #define CMAKE_EXT "" 29 | #endif 30 | 31 | /* Matlab */ 32 | #cmakedefine HAVE_MATLAB 33 | -------------------------------------------------------------------------------- /caffe/cmake/lint.cmake: -------------------------------------------------------------------------------- 1 | 2 | set(CMAKE_SOURCE_DIR ..) 3 | set(LINT_COMMAND ${CMAKE_SOURCE_DIR}/scripts/cpp_lint.py) 4 | set(SRC_FILE_EXTENSIONS h hpp hu c cpp cu cc) 5 | set(EXCLUDE_FILE_EXTENSTIONS pb.h pb.cc) 6 | set(LINT_DIRS include src/caffe examples tools python matlab) 7 | 8 | cmake_policy(SET CMP0009 NEW) # suppress cmake warning 9 | 10 | # find all files of interest 11 | foreach(ext ${SRC_FILE_EXTENSIONS}) 12 | foreach(dir ${LINT_DIRS}) 13 | file(GLOB_RECURSE FOUND_FILES ${CMAKE_SOURCE_DIR}/${dir}/*.${ext}) 14 | set(LINT_SOURCES ${LINT_SOURCES} ${FOUND_FILES}) 15 | endforeach() 16 | endforeach() 17 | 18 | # find all files that should be excluded 19 | foreach(ext ${EXCLUDE_FILE_EXTENSTIONS}) 20 | file(GLOB_RECURSE FOUND_FILES ${CMAKE_SOURCE_DIR}/*.${ext}) 21 | set(EXCLUDED_FILES ${EXCLUDED_FILES} ${FOUND_FILES}) 22 | endforeach() 23 | 24 | # exclude generated pb files 25 | list(REMOVE_ITEM LINT_SOURCES ${EXCLUDED_FILES}) 26 | 27 | execute_process( 28 | COMMAND ${LINT_COMMAND} ${LINT_SOURCES} 29 | ERROR_VARIABLE LINT_OUTPUT 30 | ERROR_STRIP_TRAILING_WHITESPACE 31 | ) 32 | 33 | string(REPLACE "\n" ";" LINT_OUTPUT ${LINT_OUTPUT}) 34 | 35 | list(GET LINT_OUTPUT -1 LINT_RESULT) 36 | list(REMOVE_AT LINT_OUTPUT -1) 37 | string(REPLACE " " ";" LINT_RESULT ${LINT_RESULT}) 38 | list(GET LINT_RESULT -1 NUM_ERRORS) 39 | if(NUM_ERRORS GREATER 0) 40 | foreach(msg ${LINT_OUTPUT}) 41 | string(FIND ${msg} "Done" result) 42 | if(result LESS 0) 43 | message(STATUS ${msg}) 44 | endif() 45 | endforeach() 46 | message(FATAL_ERROR "Lint found ${NUM_ERRORS} errors!") 47 | else() 48 | message(STATUS "Lint did not find any errors!") 49 | endif() 50 | 51 | -------------------------------------------------------------------------------- /caffe/docs/CNAME: -------------------------------------------------------------------------------- 1 | caffe.berkeleyvision.org 2 | -------------------------------------------------------------------------------- /caffe/docs/README.md: -------------------------------------------------------------------------------- 1 | # Caffe Documentation 2 | 3 | To generate the documentation, run `$CAFFE_ROOT/scripts/build_docs.sh`. 4 | 5 | To push your changes to the documentation to the gh-pages branch of your or the BVLC repo, run `$CAFFE_ROOT/scripts/deploy_docs.sh `. 6 | -------------------------------------------------------------------------------- /caffe/docs/_config.yml: -------------------------------------------------------------------------------- 1 | defaults: 2 | - 3 | scope: 4 | path: "" # an empty string here means all files in the project 5 | values: 6 | layout: "default" 7 | 8 | -------------------------------------------------------------------------------- /caffe/docs/images/GitHub-Mark-64px.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/images/GitHub-Mark-64px.png -------------------------------------------------------------------------------- /caffe/docs/images/caffeine-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/images/caffeine-icon.png -------------------------------------------------------------------------------- /caffe/docs/install_apt.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Installation: Ubuntu 3 | --- 4 | 5 | # Ubuntu Installation 6 | 7 | **General dependencies** 8 | 9 | sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler 10 | sudo apt-get install --no-install-recommends libboost-all-dev 11 | 12 | **CUDA**: Install via the NVIDIA package instead of `apt-get` to be certain of the library and driver versions. 13 | Install the library and latest driver separately; the driver bundled with the library is usually out-of-date. 14 | This can be skipped for CPU-only installation. 15 | 16 | **BLAS**: install ATLAS by `sudo apt-get install libatlas-base-dev` or install OpenBLAS or MKL for better CPU performance. 17 | 18 | **Python** (optional): if you use the default Python you will need to `sudo apt-get install` the `python-dev` package to have the Python headers for building the pycaffe interface. 19 | 20 | **Remaining dependencies, 14.04** 21 | 22 | Everything is packaged in 14.04. 23 | 24 | sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev 25 | 26 | **Remaining dependencies, 12.04** 27 | 28 | These dependencies need manual installation in 12.04. 29 | 30 | # glog 31 | wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz 32 | tar zxvf glog-0.3.3.tar.gz 33 | cd glog-0.3.3 34 | ./configure 35 | make && make install 36 | # gflags 37 | wget https://github.com/schuhschuh/gflags/archive/master.zip 38 | unzip master.zip 39 | cd gflags-master 40 | mkdir build && cd build 41 | export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1 42 | make && make install 43 | # lmdb 44 | git clone https://github.com/LMDB/lmdb 45 | cd lmdb/libraries/liblmdb 46 | make && make install 47 | 48 | Note that glog does not compile with the most recent gflags version (2.1), so before that is resolved you will need to build with glog first. 49 | 50 | Continue with [compilation](installation.html#compilation). 51 | -------------------------------------------------------------------------------- /caffe/docs/install_yum.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Installation: RHEL / Fedora / CentOS 3 | --- 4 | 5 | # RHEL / Fedora / CentOS Installation 6 | 7 | **General dependencies** 8 | 9 | sudo yum install protobuf-devel leveldb-devel snappy-devel opencv-devel boost-devel hdf5-devel 10 | 11 | **Remaining dependencies, recent OS** 12 | 13 | sudo yum install gflags-devel glog-devel lmdb-devel 14 | 15 | **Remaining dependencies, if not found** 16 | 17 | # glog 18 | wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz 19 | tar zxvf glog-0.3.3.tar.gz 20 | cd glog-0.3.3 21 | ./configure 22 | make && make install 23 | # gflags 24 | wget https://github.com/schuhschuh/gflags/archive/master.zip 25 | unzip master.zip 26 | cd gflags-master 27 | mkdir build && cd build 28 | export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1 29 | make && make install 30 | # lmdb 31 | git clone https://github.com/LMDB/lmdb 32 | cd lmdb/libraries/liblmdb 33 | make && make install 34 | 35 | Note that glog does not compile with the most recent gflags version (2.1), so before that is resolved you will need to build with glog first. 36 | 37 | **CUDA**: Install via the NVIDIA package instead of `yum` to be certain of the library and driver versions. 38 | Install the library and latest driver separately; the driver bundled with the library is usually out-of-date. 39 | + CentOS/RHEL/Fedora: 40 | 41 | **BLAS**: install ATLAS by `sudo yum install atlas-devel` or install OpenBLAS or MKL for better CPU performance. For the Makefile build, uncomment and set `BLAS_LIB` accordingly as ATLAS is usually installed under `/usr/lib[64]/atlas`). 42 | 43 | **Python** (optional): if you use the default Python you will need to `sudo yum install` the `python-devel` package to have the Python headers for building the pycaffe wrapper. 44 | 45 | Continue with [compilation](installation.html#compilation). 46 | -------------------------------------------------------------------------------- /caffe/docs/stylesheets/reset.css: -------------------------------------------------------------------------------- 1 | /* MeyerWeb Reset */ 2 | 3 | html, body, div, span, applet, object, iframe, 4 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 5 | a, abbr, acronym, address, big, cite, code, 6 | del, dfn, em, img, ins, kbd, q, s, samp, 7 | small, strike, strong, sub, sup, tt, var, 8 | b, u, i, center, 9 | dl, dt, dd, ol, ul, li, 10 | fieldset, form, label, legend, 11 | table, caption, tbody, tfoot, thead, tr, th, td, 12 | article, aside, canvas, details, embed, 13 | figure, figcaption, footer, header, hgroup, 14 | menu, nav, output, ruby, section, summary, 15 | time, mark, audio, video { 16 | margin: 0; 17 | padding: 0; 18 | border: 0; 19 | font: inherit; 20 | vertical-align: baseline; 21 | } 22 | -------------------------------------------------------------------------------- /caffe/docs/tutorial/convolution.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Convolution 3 | --- 4 | # Caffeinated Convolution 5 | 6 | The Caffe strategy for convolution is to reduce the problem to matrix-matrix multiplication. 7 | This linear algebra computation is highly-tuned in BLAS libraries and efficiently computed on GPU devices. 8 | 9 | For more details read Yangqing's [Convolution in Caffe: a memo](https://github.com/Yangqing/caffe/wiki/Convolution-in-Caffe:-a-memo). 10 | 11 | As it turns out, this same reduction was independently explored in the context of conv. nets by 12 | 13 | > K. Chellapilla, S. Puri, P. Simard, et al. High performance convolutional neural networks for document processing. In Tenth International Workshop on Frontiers in Handwriting Recognition, 2006. 14 | -------------------------------------------------------------------------------- /caffe/docs/tutorial/fig/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/tutorial/fig/.gitignore -------------------------------------------------------------------------------- /caffe/docs/tutorial/fig/backward.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/tutorial/fig/backward.jpg -------------------------------------------------------------------------------- /caffe/docs/tutorial/fig/forward.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/tutorial/fig/forward.jpg -------------------------------------------------------------------------------- /caffe/docs/tutorial/fig/forward_backward.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/tutorial/fig/forward_backward.png -------------------------------------------------------------------------------- /caffe/docs/tutorial/fig/layer.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/docs/tutorial/fig/layer.jpg -------------------------------------------------------------------------------- /caffe/examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file(GLOB_RECURSE examples_srcs "${PROJECT_SOURCE_DIR}/examples/*.cpp") 2 | 3 | foreach(source_file ${examples_srcs}) 4 | # get file name 5 | get_filename_component(name ${source_file} NAME_WE) 6 | 7 | # get folder name 8 | get_filename_component(path ${source_file} PATH) 9 | get_filename_component(folder ${path} NAME_WE) 10 | 11 | add_executable(${name} ${source_file}) 12 | target_link_libraries(${name} ${Caffe_LINK}) 13 | caffe_default_properties(${name}) 14 | 15 | # set back RUNTIME_OUTPUT_DIRECTORY 16 | set_target_properties(${name} PROPERTIES 17 | RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/examples/${folder}") 18 | 19 | caffe_set_solution_folder(${name} examples) 20 | 21 | # install 22 | install(TARGETS ${name} DESTINATION bin) 23 | 24 | if(UNIX OR APPLE) 25 | # Funny command to make tutorials work 26 | # TODO: remove in future as soon as naming is standartaized everywhere 27 | set(__outname ${PROJECT_BINARY_DIR}/examples/${folder}/${name}${CAffe_POSTFIX}) 28 | add_custom_command(TARGET ${name} POST_BUILD 29 | COMMAND ln -sf "${__outname}" "${__outname}.bin") 30 | endif() 31 | endforeach() 32 | -------------------------------------------------------------------------------- /caffe/examples/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_prefix: "examples/cifar10/cifar10_full" 25 | # solver mode: CPU or GPU 26 | solver_mode: GPU 27 | -------------------------------------------------------------------------------- /caffe/examples/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_prefix: "examples/cifar10/cifar10_full" 25 | # solver mode: CPU or GPU 26 | solver_mode: GPU 27 | -------------------------------------------------------------------------------- /caffe/examples/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_prefix: "examples/cifar10/cifar10_full" 25 | # solver mode: CPU or GPU 26 | solver_mode: GPU 27 | -------------------------------------------------------------------------------- /caffe/examples/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 | -------------------------------------------------------------------------------- /caffe/examples/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_prefix: "examples/cifar10/cifar10_quick" 24 | # solver mode: CPU or GPU 25 | solver_mode: GPU 26 | -------------------------------------------------------------------------------- /caffe/examples/cifar10/create_cifar10.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # This script converts the cifar data into leveldb format. 3 | 4 | EXAMPLE=examples/cifar10 5 | DATA=data/cifar10 6 | DBTYPE=lmdb 7 | 8 | echo "Creating $DBTYPE..." 9 | 10 | rm -rf $EXAMPLE/cifar10_train_$DBTYPE $EXAMPLE/cifar10_test_$DBTYPE 11 | 12 | ./build/examples/cifar10/convert_cifar_data.bin $DATA $EXAMPLE $DBTYPE 13 | 14 | echo "Computing image mean..." 15 | 16 | ./build/tools/compute_image_mean -backend=$DBTYPE \ 17 | $EXAMPLE/cifar10_train_$DBTYPE $EXAMPLE/mean.binaryproto 18 | 19 | echo "Done." 20 | -------------------------------------------------------------------------------- /caffe/examples/cifar10/train_full.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | TOOLS=./build/tools 4 | 5 | $TOOLS/caffe train \ 6 | --solver=examples/cifar10/cifar10_full_solver.prototxt 7 | 8 | # reduce learning rate by factor of 10 9 | $TOOLS/caffe train \ 10 | --solver=examples/cifar10/cifar10_full_solver_lr1.prototxt \ 11 | --snapshot=examples/cifar10/cifar10_full_iter_60000.solverstate.h5 12 | 13 | # reduce learning rate by factor of 10 14 | $TOOLS/caffe train \ 15 | --solver=examples/cifar10/cifar10_full_solver_lr2.prototxt \ 16 | --snapshot=examples/cifar10/cifar10_full_iter_65000.solverstate.h5 17 | -------------------------------------------------------------------------------- /caffe/examples/cifar10/train_quick.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | TOOLS=./build/tools 4 | 5 | $TOOLS/caffe train \ 6 | --solver=examples/cifar10/cifar10_quick_solver.prototxt 7 | 8 | # reduce learning rate by factor of 10 after 8 epochs 9 | $TOOLS/caffe train \ 10 | --solver=examples/cifar10/cifar10_quick_solver_lr1.prototxt \ 11 | --snapshot=examples/cifar10/cifar10_quick_iter_4000.solverstate.h5 12 | -------------------------------------------------------------------------------- /caffe/examples/finetune_flickr_style/flickr_style.csv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/examples/finetune_flickr_style/flickr_style.csv.gz -------------------------------------------------------------------------------- /caffe/examples/finetune_flickr_style/style_names.txt: -------------------------------------------------------------------------------- 1 | Detailed 2 | Pastel 3 | Melancholy 4 | Noir 5 | HDR 6 | Vintage 7 | Long Exposure 8 | Horror 9 | Sunny 10 | Bright 11 | Hazy 12 | Bokeh 13 | Serene 14 | Texture 15 | Ethereal 16 | Macro 17 | Depth of Field 18 | Geometric Composition 19 | Minimal 20 | Romantic 21 | -------------------------------------------------------------------------------- /caffe/examples/finetune_pascal_detection/pascal_finetune_solver.prototxt: -------------------------------------------------------------------------------- 1 | net: "examples/finetune_pascal_detection/pascal_finetune_trainval_test.prototxt" 2 | test_iter: 100 3 | test_interval: 1000 4 | base_lr: 0.001 5 | lr_policy: "step" 6 | gamma: 0.1 7 | stepsize: 20000 8 | display: 20 9 | max_iter: 100000 10 | momentum: 0.9 11 | weight_decay: 0.0005 12 | snapshot: 10000 13 | snapshot_prefix: "examples/finetune_pascal_detection/pascal_det_finetune" 14 | -------------------------------------------------------------------------------- /caffe/examples/hdf5_classification/nonlinear_auto_test.prototxt: -------------------------------------------------------------------------------- 1 | layer { 2 | name: "data" 3 | type: "HDF5Data" 4 | top: "data" 5 | top: "label" 6 | hdf5_data_param { 7 | source: "examples/hdf5_classification/data/test.txt" 8 | batch_size: 10 9 | } 10 | } 11 | layer { 12 | name: "ip1" 13 | type: "InnerProduct" 14 | bottom: "data" 15 | top: "ip1" 16 | inner_product_param { 17 | num_output: 40 18 | weight_filler { 19 | type: "xavier" 20 | } 21 | } 22 | } 23 | layer { 24 | name: "relu1" 25 | type: "ReLU" 26 | bottom: "ip1" 27 | top: "ip1" 28 | } 29 | layer { 30 | name: "ip2" 31 | type: "InnerProduct" 32 | bottom: "ip1" 33 | top: "ip2" 34 | inner_product_param { 35 | num_output: 2 36 | weight_filler { 37 | type: "xavier" 38 | } 39 | } 40 | } 41 | layer { 42 | name: "accuracy" 43 | type: "Accuracy" 44 | bottom: "ip2" 45 | bottom: "label" 46 | top: "accuracy" 47 | } 48 | layer { 49 | name: "loss" 50 | type: "SoftmaxWithLoss" 51 | bottom: "ip2" 52 | bottom: "label" 53 | top: "loss" 54 | } 55 | -------------------------------------------------------------------------------- /caffe/examples/hdf5_classification/nonlinear_auto_train.prototxt: -------------------------------------------------------------------------------- 1 | layer { 2 | name: "data" 3 | type: "HDF5Data" 4 | top: "data" 5 | top: "label" 6 | hdf5_data_param { 7 | source: "examples/hdf5_classification/data/train.txt" 8 | batch_size: 10 9 | } 10 | } 11 | layer { 12 | name: "ip1" 13 | type: "InnerProduct" 14 | bottom: "data" 15 | top: "ip1" 16 | inner_product_param { 17 | num_output: 40 18 | weight_filler { 19 | type: "xavier" 20 | } 21 | } 22 | } 23 | layer { 24 | name: "relu1" 25 | type: "ReLU" 26 | bottom: "ip1" 27 | top: "ip1" 28 | } 29 | layer { 30 | name: "ip2" 31 | type: "InnerProduct" 32 | bottom: "ip1" 33 | top: "ip2" 34 | inner_product_param { 35 | num_output: 2 36 | weight_filler { 37 | type: "xavier" 38 | } 39 | } 40 | } 41 | layer { 42 | name: "accuracy" 43 | type: "Accuracy" 44 | bottom: "ip2" 45 | bottom: "label" 46 | top: "accuracy" 47 | } 48 | layer { 49 | name: "loss" 50 | type: "SoftmaxWithLoss" 51 | bottom: "ip2" 52 | bottom: "label" 53 | top: "loss" 54 | } 55 | -------------------------------------------------------------------------------- /caffe/examples/hdf5_classification/nonlinear_solver.prototxt: -------------------------------------------------------------------------------- 1 | train_net: "examples/hdf5_classification/nonlinear_auto_train.prototxt" 2 | test_net: "examples/hdf5_classification/nonlinear_auto_test.prototxt" 3 | test_iter: 250 4 | test_interval: 1000 5 | base_lr: 0.01 6 | lr_policy: "step" 7 | gamma: 0.1 8 | stepsize: 5000 9 | display: 1000 10 | max_iter: 10000 11 | momentum: 0.9 12 | weight_decay: 0.0005 13 | snapshot: 10000 14 | snapshot_prefix: "examples/hdf5_classification/data/train" 15 | solver_mode: CPU 16 | -------------------------------------------------------------------------------- /caffe/examples/hdf5_classification/nonlinear_train_val.prototxt: -------------------------------------------------------------------------------- 1 | name: "LogisticRegressionNet" 2 | layer { 3 | name: "data" 4 | type: "HDF5Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | hdf5_data_param { 11 | source: "examples/hdf5_classification/data/train.txt" 12 | batch_size: 10 13 | } 14 | } 15 | layer { 16 | name: "data" 17 | type: "HDF5Data" 18 | top: "data" 19 | top: "label" 20 | include { 21 | phase: TEST 22 | } 23 | hdf5_data_param { 24 | source: "examples/hdf5_classification/data/test.txt" 25 | batch_size: 10 26 | } 27 | } 28 | layer { 29 | name: "fc1" 30 | type: "InnerProduct" 31 | bottom: "data" 32 | top: "fc1" 33 | param { 34 | lr_mult: 1 35 | decay_mult: 1 36 | } 37 | param { 38 | lr_mult: 2 39 | decay_mult: 0 40 | } 41 | inner_product_param { 42 | num_output: 40 43 | weight_filler { 44 | type: "xavier" 45 | } 46 | bias_filler { 47 | type: "constant" 48 | value: 0 49 | } 50 | } 51 | } 52 | layer { 53 | name: "relu1" 54 | type: "ReLU" 55 | bottom: "fc1" 56 | top: "fc1" 57 | } 58 | layer { 59 | name: "fc2" 60 | type: "InnerProduct" 61 | bottom: "fc1" 62 | top: "fc2" 63 | param { 64 | lr_mult: 1 65 | decay_mult: 1 66 | } 67 | param { 68 | lr_mult: 2 69 | decay_mult: 0 70 | } 71 | inner_product_param { 72 | num_output: 2 73 | weight_filler { 74 | type: "xavier" 75 | } 76 | bias_filler { 77 | type: "constant" 78 | value: 0 79 | } 80 | } 81 | } 82 | layer { 83 | name: "loss" 84 | type: "SoftmaxWithLoss" 85 | bottom: "fc2" 86 | bottom: "label" 87 | top: "loss" 88 | } 89 | layer { 90 | name: "accuracy" 91 | type: "Accuracy" 92 | bottom: "fc2" 93 | bottom: "label" 94 | top: "accuracy" 95 | include { 96 | phase: TEST 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /caffe/examples/hdf5_classification/solver.prototxt: -------------------------------------------------------------------------------- 1 | train_net: "examples/hdf5_classification/logreg_auto_train.prototxt" 2 | test_net: "examples/hdf5_classification/logreg_auto_test.prototxt" 3 | test_iter: 250 4 | test_interval: 1000 5 | base_lr: 0.01 6 | lr_policy: "step" 7 | gamma: 0.1 8 | stepsize: 5000 9 | display: 1000 10 | max_iter: 10000 11 | momentum: 0.9 12 | weight_decay: 0.0005 13 | snapshot: 10000 14 | snapshot_prefix: "examples/hdf5_classification/data/train" 15 | solver_mode: CPU 16 | -------------------------------------------------------------------------------- /caffe/examples/hdf5_classification/train_val.prototxt: -------------------------------------------------------------------------------- 1 | name: "LogisticRegressionNet" 2 | layer { 3 | name: "data" 4 | type: "HDF5Data" 5 | top: "data" 6 | top: "label" 7 | include { 8 | phase: TRAIN 9 | } 10 | hdf5_data_param { 11 | source: "examples/hdf5_classification/data/train.txt" 12 | batch_size: 10 13 | } 14 | } 15 | layer { 16 | name: "data" 17 | type: "HDF5Data" 18 | top: "data" 19 | top: "label" 20 | include { 21 | phase: TEST 22 | } 23 | hdf5_data_param { 24 | source: "examples/hdf5_classification/data/test.txt" 25 | batch_size: 10 26 | } 27 | } 28 | layer { 29 | name: "fc1" 30 | type: "InnerProduct" 31 | bottom: "data" 32 | top: "fc1" 33 | param { 34 | lr_mult: 1 35 | decay_mult: 1 36 | } 37 | param { 38 | lr_mult: 2 39 | decay_mult: 0 40 | } 41 | inner_product_param { 42 | num_output: 2 43 | weight_filler { 44 | type: "xavier" 45 | } 46 | bias_filler { 47 | type: "constant" 48 | value: 0 49 | } 50 | } 51 | } 52 | layer { 53 | name: "loss" 54 | type: "SoftmaxWithLoss" 55 | bottom: "fc1" 56 | bottom: "label" 57 | top: "loss" 58 | } 59 | layer { 60 | name: "accuracy" 61 | type: "Accuracy" 62 | bottom: "fc1" 63 | bottom: "label" 64 | top: "accuracy" 65 | include { 66 | phase: TEST 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /caffe/examples/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 | 5 | EXAMPLE=examples/imagenet 6 | DATA=data/ilsvrc12 7 | TOOLS=build/tools 8 | 9 | TRAIN_DATA_ROOT=/path/to/imagenet/train/ 10 | VAL_DATA_ROOT=/path/to/imagenet/val/ 11 | 12 | # Set RESIZE=true to resize the images to 256x256. Leave as false if images have 13 | # already been resized using another tool. 14 | RESIZE=false 15 | if $RESIZE; then 16 | RESIZE_HEIGHT=256 17 | RESIZE_WIDTH=256 18 | else 19 | RESIZE_HEIGHT=0 20 | RESIZE_WIDTH=0 21 | fi 22 | 23 | if [ ! -d "$TRAIN_DATA_ROOT" ]; then 24 | echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT" 25 | echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \ 26 | "where the ImageNet training data is stored." 27 | exit 1 28 | fi 29 | 30 | if [ ! -d "$VAL_DATA_ROOT" ]; then 31 | echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT" 32 | echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \ 33 | "where the ImageNet validation data is stored." 34 | exit 1 35 | fi 36 | 37 | echo "Creating train lmdb..." 38 | 39 | GLOG_logtostderr=1 $TOOLS/convert_imageset \ 40 | --resize_height=$RESIZE_HEIGHT \ 41 | --resize_width=$RESIZE_WIDTH \ 42 | --shuffle \ 43 | $TRAIN_DATA_ROOT \ 44 | $DATA/train.txt \ 45 | $EXAMPLE/ilsvrc12_train_lmdb 46 | 47 | echo "Creating val lmdb..." 48 | 49 | GLOG_logtostderr=1 $TOOLS/convert_imageset \ 50 | --resize_height=$RESIZE_HEIGHT \ 51 | --resize_width=$RESIZE_WIDTH \ 52 | --shuffle \ 53 | $VAL_DATA_ROOT \ 54 | $DATA/val.txt \ 55 | $EXAMPLE/ilsvrc12_val_lmdb 56 | 57 | echo "Done." 58 | -------------------------------------------------------------------------------- /caffe/examples/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=examples/imagenet 6 | DATA=data/ilsvrc12 7 | TOOLS=build/tools 8 | 9 | $TOOLS/compute_image_mean $EXAMPLE/ilsvrc12_train_lmdb \ 10 | $DATA/imagenet_mean.binaryproto 11 | 12 | echo "Done." 13 | -------------------------------------------------------------------------------- /caffe/examples/imagenet/resume_training.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train \ 4 | --solver=models/bvlc_reference_caffenet/solver.prototxt \ 5 | --snapshot=models/bvlc_reference_caffenet/caffenet_train_10000.solverstate.h5 6 | -------------------------------------------------------------------------------- /caffe/examples/imagenet/train_caffenet.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train \ 4 | --solver=models/bvlc_reference_caffenet/solver.prototxt 5 | -------------------------------------------------------------------------------- /caffe/examples/images/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/examples/images/cat.jpg -------------------------------------------------------------------------------- /caffe/examples/images/cat_gray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/examples/images/cat_gray.jpg -------------------------------------------------------------------------------- /caffe/examples/images/fish-bike.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/examples/images/fish-bike.jpg -------------------------------------------------------------------------------- /caffe/examples/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 | 5 | EXAMPLE=examples/mnist 6 | DATA=data/mnist 7 | BUILD=build/examples/mnist 8 | 9 | BACKEND="lmdb" 10 | 11 | echo "Creating ${BACKEND}..." 12 | 13 | rm -rf $EXAMPLE/mnist_train_${BACKEND} 14 | rm -rf $EXAMPLE/mnist_test_${BACKEND} 15 | 16 | $BUILD/convert_mnist_data.bin $DATA/train-images-idx3-ubyte \ 17 | $DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND} 18 | $BUILD/convert_mnist_data.bin $DATA/t10k-images-idx3-ubyte \ 19 | $DATA/t10k-labels-idx1-ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND} 20 | 21 | echo "Done." 22 | -------------------------------------------------------------------------------- /caffe/examples/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 | solver_type: ADADELTA 24 | delta: 1e-6 25 | -------------------------------------------------------------------------------- /caffe/examples/mnist/lenet_auto_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | train_net: "examples/mnist/lenet_auto_train.prototxt" 3 | test_net: "examples/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: "examples/mnist/lenet" 25 | -------------------------------------------------------------------------------- /caffe/examples/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 | -------------------------------------------------------------------------------- /caffe/examples/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: GPU 26 | -------------------------------------------------------------------------------- /caffe/examples/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 | solver_type: ADAM 26 | solver_mode: GPU 27 | -------------------------------------------------------------------------------- /caffe/examples/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 | solver_type: RMSPROP 27 | rms_decay: 0.98 28 | -------------------------------------------------------------------------------- /caffe/examples/mnist/lenet_stepearly_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The training protocol buffer definition 2 | train_net: "lenet_train.prototxt" 3 | # The testing protocol buffer definition 4 | test_net: "lenet_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.01 13 | momentum: 0.9 14 | weight_decay: 0.0005 15 | # The learning rate policy 16 | lr_policy: "stepearly" 17 | gamma: 0.9 18 | stepearly: 1 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: "lenet" 26 | # solver mode: 0 for CPU and 1 for GPU 27 | solver_mode: 1 28 | device_id: 1 29 | -------------------------------------------------------------------------------- /caffe/examples/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 | -------------------------------------------------------------------------------- /caffe/examples/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 | solver_type: ADADELTA 20 | -------------------------------------------------------------------------------- /caffe/examples/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 | solver_type: ADAGRAD 18 | -------------------------------------------------------------------------------- /caffe/examples/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 | solver_type: NESTEROV 21 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_lenet.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt -gpu 0,1,3,4 4 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_lenet_adam.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train --solver=examples/mnist/lenet_solver_adam.prototxt 4 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_lenet_consolidated.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train \ 4 | --solver=examples/mnist/lenet_consolidated_solver.prototxt 5 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_lenet_rmsprop.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train --solver=examples/mnist/lenet_solver_rmsprop.prototxt 4 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_mnist_autoencoder.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ./build/tools/caffe train \ 4 | --solver=examples/mnist/mnist_autoencoder_solver.prototxt 5 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_mnist_autoencoder_adadelta.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./build/tools/caffe train \ 4 | --solver=examples/mnist/mnist_autoencoder_solver_adadelta.prototxt 5 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_mnist_autoencoder_adagrad.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./build/tools/caffe train \ 4 | --solver=examples/mnist/mnist_autoencoder_solver_adagrad.prototxt 5 | -------------------------------------------------------------------------------- /caffe/examples/mnist/train_mnist_autoencoder_nesterov.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./build/tools/caffe train \ 4 | --solver=examples/mnist/mnist_autoencoder_solver_nesterov.prototxt 5 | -------------------------------------------------------------------------------- /caffe/examples/net_surgery/conv.prototxt: -------------------------------------------------------------------------------- 1 | # Simple single-layer network to showcase editing model parameters. 2 | name: "convolution" 3 | input: "data" 4 | input_shape { 5 | dim: 1 6 | dim: 1 7 | dim: 100 8 | dim: 100 9 | } 10 | layer { 11 | name: "conv" 12 | type: "Convolution" 13 | bottom: "data" 14 | top: "conv" 15 | convolution_param { 16 | num_output: 3 17 | kernel_size: 5 18 | stride: 1 19 | weight_filler { 20 | type: "gaussian" 21 | std: 0.01 22 | } 23 | bias_filler { 24 | type: "constant" 25 | value: 0 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /caffe/examples/pycaffe/layers/pyloss.py: -------------------------------------------------------------------------------- 1 | import caffe 2 | import numpy as np 3 | 4 | 5 | class EuclideanLossLayer(caffe.Layer): 6 | """ 7 | Compute the Euclidean Loss in the same manner as the C++ EuclideanLossLayer 8 | to demonstrate the class interface for developing layers in Python. 9 | """ 10 | 11 | def setup(self, bottom, top): 12 | # check input pair 13 | if len(bottom) != 2: 14 | raise Exception("Need two inputs to compute distance.") 15 | 16 | def reshape(self, bottom, top): 17 | # check input dimensions match 18 | if bottom[0].count != bottom[1].count: 19 | raise Exception("Inputs must have the same dimension.") 20 | # difference is shape of inputs 21 | self.diff = np.zeros_like(bottom[0].data, dtype=np.float32) 22 | # loss output is scalar 23 | top[0].reshape(1) 24 | 25 | def forward(self, bottom, top): 26 | self.diff[...] = bottom[0].data - bottom[1].data 27 | top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2. 28 | 29 | def backward(self, top, propagate_down, bottom): 30 | for i in range(2): 31 | if not propagate_down[i]: 32 | continue 33 | if i == 0: 34 | sign = 1 35 | else: 36 | sign = -1 37 | bottom[i].diff[...] = sign * self.diff / bottom[i].num 38 | -------------------------------------------------------------------------------- /caffe/examples/pycaffe/linreg.prototxt: -------------------------------------------------------------------------------- 1 | name: 'LinearRegressionExample' 2 | # define a simple network for linear regression on dummy data 3 | # that computes the loss by a PythonLayer. 4 | layer { 5 | type: 'DummyData' 6 | name: 'x' 7 | top: 'x' 8 | dummy_data_param { 9 | shape: { dim: 10 dim: 3 dim: 2 } 10 | data_filler: { type: 'gaussian' } 11 | } 12 | } 13 | layer { 14 | type: 'DummyData' 15 | name: 'y' 16 | top: 'y' 17 | dummy_data_param { 18 | shape: { dim: 10 dim: 3 dim: 2 } 19 | data_filler: { type: 'gaussian' } 20 | } 21 | } 22 | # include InnerProduct layers for parameters 23 | # so the net will need backward 24 | layer { 25 | type: 'InnerProduct' 26 | name: 'ipx' 27 | top: 'ipx' 28 | bottom: 'x' 29 | inner_product_param { 30 | num_output: 10 31 | weight_filler { type: 'xavier' } 32 | } 33 | } 34 | layer { 35 | type: 'InnerProduct' 36 | name: 'ipy' 37 | top: 'ipy' 38 | bottom: 'y' 39 | inner_product_param { 40 | num_output: 10 41 | weight_filler { type: 'xavier' } 42 | } 43 | } 44 | layer { 45 | type: 'Python' 46 | name: 'loss' 47 | top: 'loss' 48 | bottom: 'ipx' 49 | bottom: 'ipy' 50 | python_param { 51 | # the module name -- usually the filename -- that needs to be in $PYTHONPATH 52 | module: 'pyloss' 53 | # the layer name -- the class name in the module 54 | layer: 'EuclideanLossLayer' 55 | } 56 | # set loss weight so Caffe knows this is a loss layer. 57 | # since PythonLayer inherits directly from Layer, this isn't automatically 58 | # known to Caffe 59 | loss_weight: 1 60 | } 61 | -------------------------------------------------------------------------------- /caffe/examples/siamese/create_mnist_siamese.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # This script converts the mnist data into leveldb format. 3 | 4 | EXAMPLES=./build/examples/siamese 5 | DATA=./data/mnist 6 | 7 | echo "Creating leveldb..." 8 | 9 | rm -rf ./examples/siamese/mnist_siamese_train_leveldb 10 | rm -rf ./examples/siamese/mnist_siamese_test_leveldb 11 | 12 | $EXAMPLES/convert_mnist_siamese_data.bin \ 13 | $DATA/train-images-idx3-ubyte \ 14 | $DATA/train-labels-idx1-ubyte \ 15 | ./examples/siamese/mnist_siamese_train_leveldb 16 | $EXAMPLES/convert_mnist_siamese_data.bin \ 17 | $DATA/t10k-images-idx3-ubyte \ 18 | $DATA/t10k-labels-idx1-ubyte \ 19 | ./examples/siamese/mnist_siamese_test_leveldb 20 | 21 | echo "Done." 22 | -------------------------------------------------------------------------------- /caffe/examples/siamese/mnist_siamese_solver.prototxt: -------------------------------------------------------------------------------- 1 | # The train/test net protocol buffer definition 2 | net: "examples/siamese/mnist_siamese_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.0000 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: 50000 21 | # snapshot intermediate results 22 | snapshot: 5000 23 | snapshot_prefix: "examples/siamese/mnist_siamese" 24 | # solver mode: CPU or GPU 25 | solver_mode: GPU 26 | -------------------------------------------------------------------------------- /caffe/examples/siamese/train_mnist_siamese.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | TOOLS=./build/tools 4 | 5 | $TOOLS/caffe train --solver=examples/siamese/mnist_siamese_solver.prototxt 6 | -------------------------------------------------------------------------------- /caffe/examples/web_demo/exifutil.py: -------------------------------------------------------------------------------- 1 | """ 2 | This script handles the skimage exif problem. 3 | """ 4 | 5 | from PIL import Image 6 | import numpy as np 7 | 8 | ORIENTATIONS = { # used in apply_orientation 9 | 2: (Image.FLIP_LEFT_RIGHT,), 10 | 3: (Image.ROTATE_180,), 11 | 4: (Image.FLIP_TOP_BOTTOM,), 12 | 5: (Image.FLIP_LEFT_RIGHT, Image.ROTATE_90), 13 | 6: (Image.ROTATE_270,), 14 | 7: (Image.FLIP_LEFT_RIGHT, Image.ROTATE_270), 15 | 8: (Image.ROTATE_90,) 16 | } 17 | 18 | 19 | def open_oriented_im(im_path): 20 | im = Image.open(im_path) 21 | if hasattr(im, '_getexif'): 22 | exif = im._getexif() 23 | if exif is not None and 274 in exif: 24 | orientation = exif[274] 25 | im = apply_orientation(im, orientation) 26 | img = np.asarray(im).astype(np.float32) / 255. 27 | if img.ndim == 2: 28 | img = img[:, :, np.newaxis] 29 | img = np.tile(img, (1, 1, 3)) 30 | elif img.shape[2] == 4: 31 | img = img[:, :, :3] 32 | return img 33 | 34 | 35 | def apply_orientation(im, orientation): 36 | if orientation in ORIENTATIONS: 37 | for method in ORIENTATIONS[orientation]: 38 | im = im.transpose(method) 39 | return im 40 | -------------------------------------------------------------------------------- /caffe/examples/web_demo/readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Web demo 3 | description: Image classification demo running as a Flask web server. 4 | category: example 5 | include_in_docs: true 6 | priority: 10 7 | --- 8 | 9 | # Web Demo 10 | 11 | ## Requirements 12 | 13 | The demo server requires Python with some dependencies. 14 | To make sure you have the dependencies, please run `pip install -r examples/web_demo/requirements.txt`, and also make sure that you've compiled the Python Caffe interface and that it is on your `PYTHONPATH` (see [installation instructions](/installation.html)). 15 | 16 | Make sure that you have obtained the Reference CaffeNet Model and the ImageNet Auxiliary Data: 17 | 18 | ./scripts/download_model_binary.py models/bvlc_reference_caffenet 19 | ./data/ilsvrc12/get_ilsvrc_aux.sh 20 | 21 | NOTE: if you run into trouble, try re-downloading the auxiliary files. 22 | 23 | ## Run 24 | 25 | Running `python examples/web_demo/app.py` will bring up the demo server, accessible at `http://0.0.0.0:5000`. 26 | You can enable debug mode of the web server, or switch to a different port: 27 | 28 | % python examples/web_demo/app.py -h 29 | Usage: app.py [options] 30 | 31 | Options: 32 | -h, --help show this help message and exit 33 | -d, --debug enable debug mode 34 | -p PORT, --port=PORT which port to serve content on 35 | 36 | ## How are the "maximally accurate" results generated? 37 | 38 | In a nutshell: ImageNet predictions are made at the leaf nodes, but the organization of the project allows leaf nodes to be united via more general parent nodes, with 'entity' at the very top. 39 | To give "maximally accurate" results, we "back off" from maximally specific predictions to maintain a high accuracy. 40 | The `bet_file` that is loaded in the demo provides the graph structure and names of all relevant ImageNet nodes as well as measures of information gain between them. 41 | Please see the "Hedging your bets" paper from [CVPR 2012](http://www.image-net.org/projects/hedging/) for further information. 42 | -------------------------------------------------------------------------------- /caffe/examples/web_demo/requirements.txt: -------------------------------------------------------------------------------- 1 | werkzeug 2 | flask 3 | tornado 4 | numpy 5 | pandas 6 | pillow 7 | -------------------------------------------------------------------------------- /caffe/include/caffe/caffe.hpp: -------------------------------------------------------------------------------- 1 | // caffe.hpp is the header file that you need to include in your code. It wraps 2 | // all the internal caffe header files into one for simpler inclusion. 3 | 4 | #ifndef CAFFE_CAFFE_HPP_ 5 | #define CAFFE_CAFFE_HPP_ 6 | 7 | #include "caffe/blob.hpp" 8 | #include "caffe/common.hpp" 9 | #include "caffe/filler.hpp" 10 | #include "caffe/layer.hpp" 11 | #include "caffe/layer_factory.hpp" 12 | #include "caffe/net.hpp" 13 | #include "caffe/parallel.hpp" 14 | #include "caffe/proto/caffe.pb.h" 15 | #include "caffe/solver.hpp" 16 | #include "caffe/util/benchmark.hpp" 17 | #include "caffe/util/io.hpp" 18 | #include "caffe/vision_layers.hpp" 19 | 20 | #endif // CAFFE_CAFFE_HPP_ 21 | -------------------------------------------------------------------------------- /caffe/include/caffe/internal_thread.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_INTERNAL_THREAD_HPP_ 2 | #define CAFFE_INTERNAL_THREAD_HPP_ 3 | 4 | #include "caffe/common.hpp" 5 | 6 | /** 7 | Forward declare boost::thread instead of including boost/thread.hpp 8 | to avoid a boost/NVCC issues (#1009, #1010) on OSX. 9 | */ 10 | namespace boost { class thread; } 11 | 12 | namespace caffe { 13 | 14 | /** 15 | * Virtual class encapsulate boost::thread for use in base class 16 | * The child class will acquire the ability to run a single thread, 17 | * by reimplementing the virtual function InternalThreadEntry. 18 | */ 19 | class InternalThread { 20 | public: 21 | InternalThread() : thread_() {} 22 | virtual ~InternalThread(); 23 | 24 | /** 25 | * Caffe's thread local state will be initialized using the current 26 | * thread values, e.g. device id, solver index etc. The random seed 27 | * is initialized using caffe_rng_rand. 28 | */ 29 | void StartInternalThread(); 30 | 31 | /** Will not return until the internal thread has exited. */ 32 | void StopInternalThread(); 33 | 34 | bool is_started() const; 35 | 36 | protected: 37 | /* Implement this method in your subclass 38 | with the code you want your thread to run. */ 39 | virtual void InternalThreadEntry() {} 40 | 41 | /* Should be tested when running loops to exit when requested. */ 42 | bool must_stop(); 43 | 44 | private: 45 | void entry(int device, Caffe::Brew mode, int rand_seed, int solver_count, 46 | bool root_solver); 47 | 48 | shared_ptr thread_; 49 | }; 50 | 51 | } // namespace caffe 52 | 53 | #endif // CAFFE_INTERNAL_THREAD_HPP_ 54 | -------------------------------------------------------------------------------- /caffe/include/caffe/python_layer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_PYTHON_LAYER_HPP_ 2 | #define CAFFE_PYTHON_LAYER_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | #include "caffe/layer.hpp" 8 | 9 | namespace bp = boost::python; 10 | 11 | namespace caffe { 12 | 13 | template 14 | class PythonLayer : public Layer { 15 | public: 16 | PythonLayer(PyObject* self, const LayerParameter& param) 17 | : Layer(param), self_(bp::handle<>(bp::borrowed(self))) { } 18 | 19 | virtual void LayerSetUp(const vector*>& bottom, 20 | const vector*>& top) { 21 | self_.attr("param_str_") = bp::str( 22 | this->layer_param_.python_param().param_str()); 23 | self_.attr("setup")(bottom, top); 24 | } 25 | virtual void Reshape(const vector*>& bottom, 26 | const vector*>& top) { 27 | self_.attr("reshape")(bottom, top); 28 | } 29 | 30 | virtual inline bool ShareInParallel() const { 31 | return this->layer_param_.python_param().share_in_parallel(); 32 | } 33 | 34 | virtual inline const char* type() const { return "Python"; } 35 | 36 | protected: 37 | virtual void Forward_cpu(const vector*>& bottom, 38 | const vector*>& top) { 39 | self_.attr("forward")(bottom, top); 40 | } 41 | virtual void Backward_cpu(const vector*>& top, 42 | const vector& propagate_down, const vector*>& bottom) { 43 | self_.attr("backward")(top, propagate_down, bottom); 44 | } 45 | 46 | private: 47 | bp::object self_; 48 | }; 49 | 50 | } // namespace caffe 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/benchmark.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_UTIL_BENCHMARK_H_ 2 | #define CAFFE_UTIL_BENCHMARK_H_ 3 | 4 | #include 5 | 6 | #include "caffe/util/device_alternate.hpp" 7 | 8 | namespace caffe { 9 | 10 | class Timer { 11 | public: 12 | Timer(); 13 | virtual ~Timer(); 14 | virtual void Start(); 15 | virtual void Stop(); 16 | virtual float MilliSeconds(); 17 | virtual float MicroSeconds(); 18 | virtual float Seconds(); 19 | 20 | inline bool initted() { return initted_; } 21 | inline bool running() { return running_; } 22 | inline bool has_run_at_least_once() { return has_run_at_least_once_; } 23 | 24 | protected: 25 | void Init(); 26 | 27 | bool initted_; 28 | bool running_; 29 | bool has_run_at_least_once_; 30 | #ifndef CPU_ONLY 31 | cudaEvent_t start_gpu_; 32 | cudaEvent_t stop_gpu_; 33 | #endif 34 | boost::posix_time::ptime start_cpu_; 35 | boost::posix_time::ptime stop_cpu_; 36 | float elapsed_milliseconds_; 37 | float elapsed_microseconds_; 38 | }; 39 | 40 | class CPUTimer : public Timer { 41 | public: 42 | explicit CPUTimer(); 43 | virtual ~CPUTimer() {} 44 | virtual void Start(); 45 | virtual void Stop(); 46 | virtual float MilliSeconds(); 47 | virtual float MicroSeconds(); 48 | }; 49 | 50 | } // namespace caffe 51 | 52 | #endif // CAFFE_UTIL_BENCHMARK_H_ 53 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/blocking_queue.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_UTIL_BLOCKING_QUEUE_HPP_ 2 | #define CAFFE_UTIL_BLOCKING_QUEUE_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | #include "caffe/common.hpp" 8 | 9 | namespace caffe { 10 | 11 | template 12 | class BlockingQueue { 13 | public: 14 | explicit BlockingQueue(); 15 | 16 | void push(const T& t); 17 | 18 | bool try_pop(T* t); 19 | 20 | // This logs a message if the threads needs to be blocked 21 | // useful for detecting e.g. when data feeding is too slow 22 | T pop(const string& log_on_wait = ""); 23 | 24 | bool try_peek(T* t); 25 | 26 | // Return element without removing it 27 | T peek(); 28 | 29 | size_t size() const; 30 | 31 | protected: 32 | /** 33 | Move synchronization fields out instead of including boost/thread.hpp 34 | to avoid a boost/NVCC issues (#1009, #1010) on OSX. Also fails on 35 | Linux CUDA 7.0.18. 36 | */ 37 | class sync; 38 | 39 | std::queue queue_; 40 | shared_ptr sync_; 41 | 42 | DISABLE_COPY_AND_ASSIGN(BlockingQueue); 43 | }; 44 | 45 | } // namespace caffe 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/db.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_UTIL_DB_HPP 2 | #define CAFFE_UTIL_DB_HPP 3 | 4 | #include 5 | 6 | #include "caffe/common.hpp" 7 | #include "caffe/proto/caffe.pb.h" 8 | 9 | namespace caffe { namespace db { 10 | 11 | enum Mode { READ, WRITE, NEW }; 12 | 13 | class Cursor { 14 | public: 15 | Cursor() { } 16 | virtual ~Cursor() { } 17 | virtual void SeekToFirst() = 0; 18 | virtual void Next() = 0; 19 | virtual string key() = 0; 20 | virtual string value() = 0; 21 | virtual bool valid() = 0; 22 | 23 | DISABLE_COPY_AND_ASSIGN(Cursor); 24 | }; 25 | 26 | class Transaction { 27 | public: 28 | Transaction() { } 29 | virtual ~Transaction() { } 30 | virtual void Put(const string& key, const string& value) = 0; 31 | virtual void Commit() = 0; 32 | 33 | DISABLE_COPY_AND_ASSIGN(Transaction); 34 | }; 35 | 36 | class DB { 37 | public: 38 | DB() { } 39 | virtual ~DB() { } 40 | virtual void Open(const string& source, Mode mode) = 0; 41 | virtual void Close() = 0; 42 | virtual Cursor* NewCursor() = 0; 43 | virtual Transaction* NewTransaction() = 0; 44 | 45 | DISABLE_COPY_AND_ASSIGN(DB); 46 | }; 47 | 48 | DB* GetDB(DataParameter::DB backend); 49 | DB* GetDB(const string& backend); 50 | 51 | } // namespace db 52 | } // namespace caffe 53 | 54 | #endif // CAFFE_UTIL_DB_HPP 55 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/gpu_util.cuh: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_UTIL_GPU_UTIL_H_ 2 | #define CAFFE_UTIL_GPU_UTIL_H_ 3 | 4 | namespace caffe { 5 | 6 | template 7 | inline __device__ Dtype caffe_gpu_atomic_add(const Dtype val, Dtype* address); 8 | 9 | template <> 10 | inline __device__ 11 | float caffe_gpu_atomic_add(const float val, float* address) { 12 | return atomicAdd(address, val); 13 | } 14 | 15 | // double atomicAdd implementation taken from: 16 | // http://docs.nvidia.com/cuda/cuda-c-programming-guide/#axzz3PVCpVsEG 17 | template <> 18 | inline __device__ 19 | double caffe_gpu_atomic_add(const double val, double* address) { 20 | unsigned long long int* address_as_ull = // NOLINT(runtime/int) 21 | // NOLINT_NEXT_LINE(runtime/int) 22 | reinterpret_cast(address); 23 | unsigned long long int old = *address_as_ull; // NOLINT(runtime/int) 24 | unsigned long long int assumed; // NOLINT(runtime/int) 25 | do { 26 | assumed = old; 27 | old = atomicCAS(address_as_ull, assumed, 28 | __double_as_longlong(val + __longlong_as_double(assumed))); 29 | } while (assumed != old); 30 | return __longlong_as_double(old); 31 | } 32 | 33 | } // namespace caffe 34 | 35 | #endif // CAFFE_UTIL_GPU_UTIL_H_ 36 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/hdf5.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_UTIL_HDF5_H_ 2 | #define CAFFE_UTIL_HDF5_H_ 3 | 4 | #include 5 | 6 | #include "hdf5.h" 7 | #include "hdf5_hl.h" 8 | 9 | #include "caffe/blob.hpp" 10 | 11 | namespace caffe { 12 | 13 | template 14 | void hdf5_load_nd_dataset_helper( 15 | hid_t file_id, const char* dataset_name_, int min_dim, int max_dim, 16 | Blob* blob); 17 | 18 | template 19 | void hdf5_load_nd_dataset( 20 | hid_t file_id, const char* dataset_name_, int min_dim, int max_dim, 21 | Blob* blob); 22 | 23 | template 24 | void hdf5_save_nd_dataset( 25 | const hid_t file_id, const string& dataset_name, const Blob& blob, 26 | bool write_diff = false); 27 | 28 | int hdf5_load_int(hid_t loc_id, const string& dataset_name); 29 | void hdf5_save_int(hid_t loc_id, const string& dataset_name, int i); 30 | string hdf5_load_string(hid_t loc_id, const string& dataset_name); 31 | void hdf5_save_string(hid_t loc_id, const string& dataset_name, 32 | const string& s); 33 | 34 | int hdf5_get_num_links(hid_t loc_id); 35 | string hdf5_get_name_by_idx(hid_t loc_id, int idx); 36 | 37 | } // namespace caffe 38 | 39 | #endif // CAFFE_UTIL_HDF5_H_ 40 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/im2col.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _CAFFE_UTIL_IM2COL_HPP_ 2 | #define _CAFFE_UTIL_IM2COL_HPP_ 3 | 4 | namespace caffe { 5 | 6 | template 7 | void im2col_cpu(const Dtype* data_im, const int channels, 8 | const int height, const int width, const int kernel_h, const int kernel_w, 9 | const int pad_h, const int pad_w, const int stride_h, 10 | const int stride_w, Dtype* data_col); 11 | 12 | template 13 | void col2im_cpu(const Dtype* data_col, const int channels, 14 | const int height, const int width, const int patch_h, const int patch_w, 15 | const int pad_h, const int pad_w, const int stride_h, 16 | const int stride_w, Dtype* data_im); 17 | 18 | template 19 | void im2col_gpu(const Dtype* data_im, const int channels, 20 | const int height, const int width, const int kernel_h, const int kernel_w, 21 | const int pad_h, const int pad_w, const int stride_h, 22 | const int stride_w, Dtype* data_col); 23 | 24 | template 25 | void col2im_gpu(const Dtype* data_col, const int channels, 26 | const int height, const int width, const int patch_h, const int patch_w, 27 | const int pad_h, const int pad_w, const int stride_h, 28 | const int stride_w, Dtype* data_im); 29 | 30 | } // namespace caffe 31 | 32 | #endif // CAFFE_UTIL_IM2COL_HPP_ 33 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/insert_splits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _CAFFE_UTIL_INSERT_SPLITS_HPP_ 2 | #define _CAFFE_UTIL_INSERT_SPLITS_HPP_ 3 | 4 | #include 5 | 6 | #include "caffe/proto/caffe.pb.h" 7 | 8 | namespace caffe { 9 | 10 | // Copy NetParameters with SplitLayers added to replace any shared bottom 11 | // blobs with unique bottom blobs provided by the SplitLayer. 12 | void InsertSplits(const NetParameter& param, NetParameter* param_split); 13 | 14 | void ConfigureSplitLayer(const string& layer_name, const string& blob_name, 15 | const int blob_idx, const int split_count, const float loss_weight, 16 | LayerParameter* split_layer_param); 17 | 18 | string SplitLayerName(const string& layer_name, const string& blob_name, 19 | const int blob_idx); 20 | 21 | string SplitBlobName(const string& layer_name, const string& blob_name, 22 | const int blob_idx, const int split_idx); 23 | 24 | } // namespace caffe 25 | 26 | #endif // CAFFE_UTIL_INSERT_SPLITS_HPP_ 27 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/rng.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_RNG_CPP_HPP_ 2 | #define CAFFE_RNG_CPP_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | #include "boost/random/mersenne_twister.hpp" 8 | #include "boost/random/uniform_int.hpp" 9 | 10 | #include "caffe/common.hpp" 11 | 12 | namespace caffe { 13 | 14 | typedef boost::mt19937 rng_t; 15 | 16 | inline rng_t* caffe_rng() { 17 | return static_cast(Caffe::rng_stream().generator()); 18 | } 19 | 20 | // Fisher–Yates algorithm 21 | template 22 | inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end, 23 | RandomGenerator* gen) { 24 | typedef typename std::iterator_traits::difference_type 25 | difference_type; 26 | typedef typename boost::uniform_int dist_type; 27 | 28 | difference_type length = std::distance(begin, end); 29 | if (length <= 0) return; 30 | 31 | for (difference_type i = length - 1; i > 0; --i) { 32 | dist_type dist(0, i); 33 | std::iter_swap(begin + i, begin + dist(*gen)); 34 | } 35 | } 36 | 37 | template 38 | inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end) { 39 | shuffle(begin, end, caffe_rng()); 40 | } 41 | } // namespace caffe 42 | 43 | #endif // CAFFE_RNG_HPP_ 44 | -------------------------------------------------------------------------------- /caffe/include/caffe/util/signal_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_CAFFE_UTIL_SIGNAL_HANDLER_H_ 2 | #define INCLUDE_CAFFE_UTIL_SIGNAL_HANDLER_H_ 3 | 4 | #include "caffe/proto/caffe.pb.h" 5 | #include "caffe/solver.hpp" 6 | 7 | namespace caffe { 8 | 9 | class SignalHandler { 10 | public: 11 | // Contructor. Specify what action to take when a signal is received. 12 | SignalHandler(SolverAction::Enum SIGINT_action, 13 | SolverAction::Enum SIGHUP_action); 14 | ~SignalHandler(); 15 | ActionCallback GetActionFunction(); 16 | private: 17 | SolverAction::Enum CheckForSignals() const; 18 | SolverAction::Enum SIGINT_action_; 19 | SolverAction::Enum SIGHUP_action_; 20 | }; 21 | 22 | } // namespace caffe 23 | 24 | #endif // INCLUDE_CAFFE_UTIL_SIGNAL_HANDLER_H_ 25 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/+test/test_solver.m: -------------------------------------------------------------------------------- 1 | classdef test_solver < matlab.unittest.TestCase 2 | 3 | properties 4 | num_output 5 | solver 6 | end 7 | 8 | methods 9 | function self = test_solver() 10 | self.num_output = 13; 11 | model_file = caffe.test.test_net.simple_net_file(self.num_output); 12 | solver_file = tempname(); 13 | 14 | fid = fopen(solver_file, 'w'); 15 | fprintf(fid, [ ... 16 | 'net: "' model_file '"\n' ... 17 | 'test_iter: 10 test_interval: 10 base_lr: 0.01 momentum: 0.9\n' ... 18 | 'weight_decay: 0.0005 lr_policy: "inv" gamma: 0.0001 power: 0.75\n' ... 19 | 'display: 100 max_iter: 100 snapshot_after_train: false\n' ]); 20 | fclose(fid); 21 | 22 | self.solver = caffe.Solver(solver_file); 23 | % also make sure get_solver runs 24 | caffe.get_solver(solver_file); 25 | caffe.set_mode_cpu(); 26 | % fill in valid labels 27 | self.solver.net.blobs('label').set_data(randi( ... 28 | self.num_output - 1, self.solver.net.blobs('label').shape)); 29 | self.solver.test_nets(1).blobs('label').set_data(randi( ... 30 | self.num_output - 1, self.solver.test_nets(1).blobs('label').shape)); 31 | 32 | delete(solver_file); 33 | delete(model_file); 34 | end 35 | end 36 | methods (Test) 37 | function test_solve(self) 38 | self.verifyEqual(self.solver.iter(), 0) 39 | self.solver.step(30); 40 | self.verifyEqual(self.solver.iter(), 30) 41 | self.solver.solve() 42 | self.verifyEqual(self.solver.iter(), 100) 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/Layer.m: -------------------------------------------------------------------------------- 1 | classdef Layer < handle 2 | % Wrapper class of caffe::Layer in matlab 3 | 4 | properties (Access = private) 5 | hLayer_self 6 | attributes 7 | % attributes fields: 8 | % hBlob_blobs 9 | end 10 | properties (SetAccess = private) 11 | params 12 | end 13 | 14 | methods 15 | function self = Layer(hLayer_layer) 16 | CHECK(is_valid_handle(hLayer_layer), 'invalid Layer handle'); 17 | 18 | % setup self handle and attributes 19 | self.hLayer_self = hLayer_layer; 20 | self.attributes = caffe_('layer_get_attr', self.hLayer_self); 21 | 22 | % setup weights 23 | self.params = caffe.Blob.empty(); 24 | for n = 1:length(self.attributes.hBlob_blobs) 25 | self.params(n) = caffe.Blob(self.attributes.hBlob_blobs(n)); 26 | end 27 | end 28 | function layer_type = type(self) 29 | layer_type = caffe_('layer_get_type', self.hLayer_self); 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/Solver.m: -------------------------------------------------------------------------------- 1 | classdef Solver < handle 2 | % Wrapper class of caffe::SGDSolver in matlab 3 | 4 | properties (Access = private) 5 | hSolver_self 6 | attributes 7 | % attribute fields 8 | % hNet_net 9 | % hNet_test_nets 10 | end 11 | properties (SetAccess = private) 12 | net 13 | test_nets 14 | end 15 | 16 | methods 17 | function self = Solver(varargin) 18 | % decide whether to construct a solver from solver_file or handle 19 | if ~(nargin == 1 && isstruct(varargin{1})) 20 | % construct a solver from solver_file 21 | self = caffe.get_solver(varargin{:}); 22 | return 23 | end 24 | % construct a solver from handle 25 | hSolver_solver = varargin{1}; 26 | CHECK(is_valid_handle(hSolver_solver), 'invalid Solver handle'); 27 | 28 | % setup self handle and attributes 29 | self.hSolver_self = hSolver_solver; 30 | self.attributes = caffe_('solver_get_attr', self.hSolver_self); 31 | 32 | % setup net and test_nets 33 | self.net = caffe.Net(self.attributes.hNet_net); 34 | self.test_nets = caffe.Net.empty(); 35 | for n = 1:length(self.attributes.hNet_test_nets) 36 | self.test_nets(n) = caffe.Net(self.attributes.hNet_test_nets(n)); 37 | end 38 | end 39 | function iter = iter(self) 40 | iter = caffe_('solver_get_iter', self.hSolver_self); 41 | end 42 | function restore(self, snapshot_filename) 43 | CHECK(ischar(snapshot_filename), 'snapshot_filename must be a string'); 44 | CHECK_FILE_EXIST(snapshot_filename); 45 | caffe_('solver_restore', self.hSolver_self, snapshot_filename); 46 | end 47 | function solve(self) 48 | caffe_('solver_solve', self.hSolver_self); 49 | end 50 | function step(self, iters) 51 | CHECK(isscalar(iters) && iters > 0, 'iters must be positive integer'); 52 | iters = double(iters); 53 | caffe_('solver_step', self.hSolver_self, iters); 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/get_net.m: -------------------------------------------------------------------------------- 1 | function net = get_net(varargin) 2 | % net = get_net(model_file, phase_name) or 3 | % net = get_net(model_file, weights_file, phase_name) 4 | % Construct a net from model_file, and load weights from weights_file 5 | % phase_name can only be 'train' or 'test' 6 | 7 | CHECK(nargin == 2 || nargin == 3, ['usage: ' ... 8 | 'net = get_net(model_file, phase_name) or ' ... 9 | 'net = get_net(model_file, weights_file, phase_name)']); 10 | if nargin == 3 11 | model_file = varargin{1}; 12 | weights_file = varargin{2}; 13 | phase_name = varargin{3}; 14 | elseif nargin == 2 15 | model_file = varargin{1}; 16 | phase_name = varargin{2}; 17 | end 18 | 19 | CHECK(ischar(model_file), 'model_file must be a string'); 20 | CHECK(ischar(phase_name), 'phase_name must be a string'); 21 | CHECK_FILE_EXIST(model_file); 22 | CHECK(strcmp(phase_name, 'train') || strcmp(phase_name, 'test'), ... 23 | sprintf('phase_name can only be %strain%s or %stest%s', ... 24 | char(39), char(39), char(39), char(39))); 25 | 26 | % construct caffe net from model_file 27 | hNet = caffe_('get_net', model_file, phase_name); 28 | net = caffe.Net(hNet); 29 | 30 | % load weights from weights_file 31 | if nargin == 3 32 | CHECK(ischar(weights_file), 'weights_file must be a string'); 33 | CHECK_FILE_EXIST(weights_file); 34 | net.copy_from(weights_file); 35 | end 36 | 37 | end 38 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/get_solver.m: -------------------------------------------------------------------------------- 1 | function solver = get_solver(solver_file) 2 | % solver = get_solver(solver_file) 3 | % Construct a Solver object from solver_file 4 | 5 | CHECK(ischar(solver_file), 'solver_file must be a string'); 6 | CHECK_FILE_EXIST(solver_file); 7 | pSolver = caffe_('get_solver', solver_file); 8 | solver = caffe.Solver(pSolver); 9 | 10 | end 11 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat -------------------------------------------------------------------------------- /caffe/matlab/+caffe/io.m: -------------------------------------------------------------------------------- 1 | classdef io 2 | % a class for input and output functions 3 | 4 | methods (Static) 5 | function im_data = load_image(im_file) 6 | % im_data = load_image(im_file) 7 | % load an image from disk into Caffe-supported data format 8 | % switch channels from RGB to BGR, make width the fastest dimension 9 | % and convert to single 10 | % returns im_data in W x H x C. For colored images, C = 3 in BGR 11 | % channels, and for grayscale images, C = 1 12 | CHECK(ischar(im_file), 'im_file must be a string'); 13 | CHECK_FILE_EXIST(im_file); 14 | im_data = imread(im_file); 15 | % permute channels from RGB to BGR for colored images 16 | if size(im_data, 3) == 3 17 | im_data = im_data(:, :, [3, 2, 1]); 18 | end 19 | % flip width and height to make width the fastest dimension 20 | im_data = permute(im_data, [2, 1, 3]); 21 | % convert from uint8 to single 22 | im_data = single(im_data); 23 | end 24 | function mean_data = read_mean(mean_proto_file) 25 | % mean_data = read_mean(mean_proto_file) 26 | % read image mean data from binaryproto file 27 | % returns mean_data in W x H x C with BGR channels 28 | CHECK(ischar(mean_proto_file), 'mean_proto_file must be a string'); 29 | CHECK_FILE_EXIST(mean_proto_file); 30 | mean_data = caffe_('read_mean', mean_proto_file); 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/private/CHECK.m: -------------------------------------------------------------------------------- 1 | function CHECK(expr, error_msg) 2 | 3 | if ~expr 4 | error(error_msg); 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/private/CHECK_FILE_EXIST.m: -------------------------------------------------------------------------------- 1 | function CHECK_FILE_EXIST(filename) 2 | 3 | if exist(filename, 'file') == 0 4 | error('%s does not exist', filename); 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/private/is_valid_handle.m: -------------------------------------------------------------------------------- 1 | function valid = is_valid_handle(hObj) 2 | % valid = is_valid_handle(hObj) or is_valid_handle('get_new_init_key') 3 | % Check if a handle is valid (has the right data type and init_key matches) 4 | % Use is_valid_handle('get_new_init_key') to get new init_key from C++; 5 | 6 | % a handle is a struct array with the following fields 7 | % (uint64) ptr : the pointer to the C++ object 8 | % (double) init_key : caffe initialization key 9 | 10 | persistent init_key; 11 | if isempty(init_key) 12 | init_key = caffe_('get_init_key'); 13 | end 14 | 15 | % is_valid_handle('get_new_init_key') to get new init_key from C++; 16 | if ischar(hObj) && strcmp(hObj, 'get_new_init_key') 17 | init_key = caffe_('get_init_key'); 18 | return 19 | else 20 | % check whether data types are correct and init_key matches 21 | valid = isstruct(hObj) ... 22 | && isscalar(hObj.ptr) && isa(hObj.ptr, 'uint64') ... 23 | && isscalar(hObj.init_key) && isa(hObj.init_key, 'double') ... 24 | && hObj.init_key == init_key; 25 | end 26 | 27 | end 28 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/reset_all.m: -------------------------------------------------------------------------------- 1 | function reset_all() 2 | % reset_all() 3 | % clear all solvers and stand-alone nets and reset Caffe to initial status 4 | 5 | caffe_('reset'); 6 | is_valid_handle('get_new_init_key'); 7 | 8 | end 9 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/run_tests.m: -------------------------------------------------------------------------------- 1 | function results = run_tests() 2 | % results = run_tests() 3 | % run all tests in this caffe matlab wrapper package 4 | 5 | % use CPU for testing 6 | caffe.set_mode_cpu(); 7 | 8 | % reset caffe before testing 9 | caffe.reset_all(); 10 | 11 | % put all test cases here 12 | results = [... 13 | run(caffe.test.test_net) ... 14 | run(caffe.test.test_solver) ]; 15 | 16 | % reset caffe after testing 17 | caffe.reset_all(); 18 | 19 | end 20 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/set_device.m: -------------------------------------------------------------------------------- 1 | function set_device(device_id) 2 | % set_device(device_id) 3 | % set Caffe's GPU device ID 4 | 5 | CHECK(isscalar(device_id) && device_id >= 0, ... 6 | 'device_id must be non-negative integer'); 7 | device_id = double(device_id); 8 | 9 | caffe_('set_device', device_id); 10 | 11 | end 12 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/set_mode_cpu.m: -------------------------------------------------------------------------------- 1 | function set_mode_cpu() 2 | % set_mode_cpu() 3 | % set Caffe to CPU mode 4 | 5 | caffe_('set_mode_cpu'); 6 | 7 | end 8 | -------------------------------------------------------------------------------- /caffe/matlab/+caffe/set_mode_gpu.m: -------------------------------------------------------------------------------- 1 | function set_mode_gpu() 2 | % set_mode_gpu() 3 | % set Caffe to GPU mode 4 | 5 | caffe_('set_mode_gpu'); 6 | 7 | end 8 | -------------------------------------------------------------------------------- /caffe/matlab/hdf5creation/.gitignore: -------------------------------------------------------------------------------- 1 | *.h5 2 | list.txt 3 | -------------------------------------------------------------------------------- /caffe/python/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if(NOT HAVE_PYTHON) 2 | message(STATUS "Python interface is disabled or not all required dependecies found. Building without it...") 3 | return() 4 | endif() 5 | 6 | include_directories(${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} ${Boost_INCLUDE_DIRS}) 7 | file(GLOB_RECURSE python_srcs ${PROJECT_SOURCE_DIR}/python/*.cpp) 8 | 9 | add_library(pycaffe SHARED ${python_srcs}) 10 | target_link_libraries(pycaffe ${Caffe_LINK} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) 11 | set_target_properties(pycaffe PROPERTIES PREFIX "" OUTPUT_NAME "_caffe") 12 | caffe_default_properties(pycaffe) 13 | 14 | if(UNIX OR APPLE) 15 | set(__linkname "${PROJECT_SOURCE_DIR}/python/caffe/_caffe.so") 16 | add_custom_command(TARGET pycaffe POST_BUILD 17 | COMMAND ln -sf $ "${__linkname}" 18 | COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/python/caffe/proto 19 | COMMAND touch ${PROJECT_SOURCE_DIR}/python/caffe/proto/__init__.py 20 | COMMAND cp ${proto_gen_folder}/*.py ${PROJECT_SOURCE_DIR}/python/caffe/proto/ 21 | COMMENT "Creating symlink ${__linkname} -> ${PROJECT_BINARY_DIR}/lib/_caffe${CAffe_POSTFIX}.so") 22 | endif() 23 | 24 | # ---[ Install 25 | file(GLOB files1 *.py requirements.txt) 26 | install(FILES ${files1} DESTINATION python) 27 | 28 | file(GLOB files2 caffe/*.py) 29 | install(FILES ${files2} DESTINATION python/caffe) 30 | install(TARGETS pycaffe DESTINATION python/caffe) 31 | install(DIRECTORY caffe/imagenet caffe/proto caffe/test DESTINATION python/caffe) 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /caffe/python/caffe/__init__.py: -------------------------------------------------------------------------------- 1 | from .pycaffe import Net, SGDSolver 2 | from ._caffe import set_mode_cpu, set_mode_gpu, set_device, Layer, get_solver, layer_type_list, set_random_seed 3 | from .proto.caffe_pb2 import TRAIN, TEST 4 | from .classifier import Classifier 5 | from .detector import Detector 6 | from . import io 7 | from .net_spec import layers, params, NetSpec, to_proto 8 | -------------------------------------------------------------------------------- /caffe/python/caffe/imagenet/ilsvrc_2012_mean.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/python/caffe/imagenet/ilsvrc_2012_mean.npy -------------------------------------------------------------------------------- /caffe/python/caffe/test/test_layer_type_list.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import caffe 4 | 5 | class TestLayerTypeList(unittest.TestCase): 6 | 7 | def test_standard_types(self): 8 | for type_name in ['Data', 'Convolution', 'InnerProduct']: 9 | self.assertIn(type_name, caffe.layer_type_list(), 10 | '%s not in layer_type_list()' % type_name) 11 | -------------------------------------------------------------------------------- /caffe/python/caffe/test/test_solver.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import tempfile 3 | import os 4 | import numpy as np 5 | import six 6 | 7 | import caffe 8 | from test_net import simple_net_file 9 | 10 | 11 | class TestSolver(unittest.TestCase): 12 | def setUp(self): 13 | self.num_output = 13 14 | net_f = simple_net_file(self.num_output) 15 | f = tempfile.NamedTemporaryFile(mode='w+', delete=False) 16 | f.write("""net: '""" + net_f + """' 17 | test_iter: 10 test_interval: 10 base_lr: 0.01 momentum: 0.9 18 | weight_decay: 0.0005 lr_policy: 'inv' gamma: 0.0001 power: 0.75 19 | display: 100 max_iter: 100 snapshot_after_train: false""") 20 | f.close() 21 | self.solver = caffe.SGDSolver(f.name) 22 | # also make sure get_solver runs 23 | caffe.get_solver(f.name) 24 | caffe.set_mode_cpu() 25 | # fill in valid labels 26 | self.solver.net.blobs['label'].data[...] = \ 27 | np.random.randint(self.num_output, 28 | size=self.solver.net.blobs['label'].data.shape) 29 | self.solver.test_nets[0].blobs['label'].data[...] = \ 30 | np.random.randint(self.num_output, 31 | size=self.solver.test_nets[0].blobs['label'].data.shape) 32 | os.remove(f.name) 33 | os.remove(net_f) 34 | 35 | def test_solve(self): 36 | self.assertEqual(self.solver.iter, 0) 37 | self.solver.solve() 38 | self.assertEqual(self.solver.iter, 100) 39 | 40 | def test_net_memory(self): 41 | """Check that nets survive after the solver is destroyed.""" 42 | 43 | nets = [self.solver.net] + list(self.solver.test_nets) 44 | self.assertEqual(len(nets), 2) 45 | del self.solver 46 | 47 | total = 0 48 | for net in nets: 49 | for ps in six.itervalues(net.params): 50 | for p in ps: 51 | total += p.data.sum() + p.diff.sum() 52 | for bl in six.itervalues(net.blobs): 53 | total += bl.data.sum() + bl.diff.sum() 54 | -------------------------------------------------------------------------------- /caffe/python/draw_net.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | Draw a graph of the net architecture. 4 | """ 5 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter 6 | from google.protobuf import text_format 7 | 8 | import caffe 9 | import caffe.draw 10 | from caffe.proto import caffe_pb2 11 | 12 | 13 | def parse_args(): 14 | """Parse input arguments 15 | """ 16 | 17 | parser = ArgumentParser(description=__doc__, 18 | formatter_class=ArgumentDefaultsHelpFormatter) 19 | 20 | parser.add_argument('input_net_proto_file', 21 | help='Input network prototxt file') 22 | parser.add_argument('output_image_file', 23 | help='Output image file') 24 | parser.add_argument('--rankdir', 25 | help=('One of TB (top-bottom, i.e., vertical), ' 26 | 'RL (right-left, i.e., horizontal), or another ' 27 | 'valid dot option; see ' 28 | 'http://www.graphviz.org/doc/info/' 29 | 'attrs.html#k:rankdir'), 30 | default='LR') 31 | 32 | args = parser.parse_args() 33 | return args 34 | 35 | 36 | def main(): 37 | args = parse_args() 38 | net = caffe_pb2.NetParameter() 39 | text_format.Merge(open(args.input_net_proto_file).read(), net) 40 | print('Drawing net to %s' % args.output_image_file) 41 | caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir) 42 | 43 | 44 | if __name__ == '__main__': 45 | main() 46 | -------------------------------------------------------------------------------- /caffe/python/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython>=0.19.2 2 | numpy>=1.7.1 3 | scipy>=0.13.2 4 | scikit-image>=0.9.3 5 | matplotlib>=1.3.1 6 | ipython>=3.0.0 7 | h5py>=2.2.0 8 | leveldb>=0.191 9 | networkx>=1.8.1 10 | nose>=1.3.0 11 | pandas>=0.12.0 12 | python-dateutil>=1.4,<2 13 | protobuf>=2.5.0 14 | python-gflags>=2.0 15 | pyyaml>=3.10 16 | Pillow>=2.3.0 17 | six>=1.1.0 -------------------------------------------------------------------------------- /caffe/scripts/build_docs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Build documentation for display in web browser. 3 | 4 | PORT=${1:-4000} 5 | 6 | echo "usage: build_docs.sh [port]" 7 | 8 | # Find the docs dir, no matter where the script is called 9 | ROOT_DIR="$( cd "$(dirname "$0")"/.. ; pwd -P )" 10 | cd $ROOT_DIR 11 | 12 | # Gather docs. 13 | scripts/gather_examples.sh 14 | 15 | # Generate developer docs. 16 | make docs 17 | 18 | # Display docs using web server. 19 | cd docs 20 | jekyll serve -w -s . -d _site --port=$PORT 21 | -------------------------------------------------------------------------------- /caffe/scripts/copy_notebook.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | Takes as arguments: 4 | 1. the path to a JSON file (such as an IPython notebook). 5 | 2. the path to output file 6 | 7 | If 'metadata' dict in the JSON file contains 'include_in_docs': true, 8 | then copies the file to output file, appending the 'metadata' property 9 | as YAML front-matter, adding the field 'category' with value 'notebook'. 10 | """ 11 | import os 12 | import sys 13 | import json 14 | 15 | filename = sys.argv[1] 16 | output_filename = sys.argv[2] 17 | content = json.load(open(filename)) 18 | 19 | if 'include_in_docs' in content['metadata'] and content['metadata']['include_in_docs']: 20 | yaml_frontmatter = ['---'] 21 | for key, val in content['metadata'].iteritems(): 22 | if key == 'example_name': 23 | key = 'title' 24 | if val == '': 25 | val = os.path.basename(filename) 26 | yaml_frontmatter.append('{}: {}'.format(key, val)) 27 | yaml_frontmatter += ['category: notebook'] 28 | yaml_frontmatter += ['original_path: ' + filename] 29 | 30 | with open(output_filename, 'w') as fo: 31 | fo.write('\n'.join(yaml_frontmatter + ['---']) + '\n') 32 | fo.write(open(filename).read()) 33 | -------------------------------------------------------------------------------- /caffe/scripts/deploy_docs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Publish documentation to the gh-pages site. 3 | 4 | # The remote for pushing the docs (defaults to origin). 5 | # This is where you will submit the PR to BVLC:gh-pages from. 6 | REMOTE=${1:-origin} 7 | 8 | echo "Generating docs and pushing to $REMOTE:gh-pages..." 9 | echo "To build and view docs when not on master, simply do 'jekyll serve -s docs'." 10 | echo 11 | 12 | REMOTE_URL=`git config --get remote.${REMOTE}.url` 13 | BRANCH=`git rev-parse --abbrev-ref HEAD` 14 | MSG=`git log --oneline -1` 15 | 16 | if [[ $BRANCH = 'master' ]]; then 17 | # Find the docs dir, no matter where the script is called 18 | DIR="$( cd "$(dirname "$0")" ; pwd -P )" 19 | DOCS_SITE_DIR=$DIR/../docs/_site 20 | 21 | # Make sure that docs/_site tracks remote:gh-pages. 22 | # If not, then we make a new repo and check out just that branch. 23 | mkdir -p $DOCS_SITE_DIR 24 | cd $DOCS_SITE_DIR 25 | SITE_REMOTE_URL=`git config --get remote.${REMOTE}.url` 26 | SITE_BRANCH=`git rev-parse --abbrev-ref HEAD` 27 | 28 | echo $SITE_REMOTE_URL 29 | echo $SITE_BRANCH 30 | echo `pwd` 31 | 32 | if [[ ( $SITE_REMOTE_URL = $REMOTE_URL ) && ( $SITE_BRANCH = 'gh-pages' ) ]]; then 33 | echo "Confirmed that docs/_site has same remote as main repo, and is on gh-pages." 34 | else 35 | echo "Checking out $REMOTE:gh-pages into docs/_site (will take a little time)." 36 | git init . 37 | git remote add -t gh-pages -f $REMOTE $REMOTE_URL 38 | git checkout gh-pages 39 | fi 40 | 41 | echo "Building the site into docs/_site, and committing the changes." 42 | jekyll build -s .. -d . 43 | git add --all . 44 | git commit -m "$MSG" 45 | git push $REMOTE gh-pages 46 | 47 | echo "All done!" 48 | cd ../.. 49 | else echo "You must run this deployment script from the 'master' branch." 50 | fi 51 | -------------------------------------------------------------------------------- /caffe/scripts/download_model_from_gist.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | GIST=$1 4 | DIRNAME=${2:-./models} 5 | 6 | if [ -z $GIST ]; then 7 | echo "usage: download_model_from_gist.sh " 8 | exit 9 | fi 10 | 11 | GIST_DIR=$(echo $GIST | tr '/' '-') 12 | MODEL_DIR="$DIRNAME/$GIST_DIR" 13 | 14 | if [ -d $MODEL_DIR ]; then 15 | echo "$MODEL_DIR already exists! Please make sure you're not overwriting anything important!" 16 | exit 17 | fi 18 | 19 | echo "Downloading Caffe model info to $MODEL_DIR ..." 20 | mkdir -p $MODEL_DIR 21 | wget https://gist.github.com/$GIST/download -O $MODEL_DIR/gist.zip 22 | unzip -j $MODEL_DIR/gist.zip -d $MODEL_DIR 23 | rm $MODEL_DIR/gist.zip 24 | echo "Done" 25 | -------------------------------------------------------------------------------- /caffe/scripts/gather_examples.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Assemble documentation for the project into one directory via symbolic links. 3 | 4 | # Find the docs dir, no matter where the script is called 5 | ROOT_DIR="$( cd "$(dirname "$0")"/.. ; pwd -P )" 6 | cd $ROOT_DIR 7 | 8 | # Gather docs from examples/**/readme.md 9 | GATHERED_DIR=docs/gathered 10 | rm -r $GATHERED_DIR 11 | mkdir $GATHERED_DIR 12 | for README_FILENAME in $(find examples -iname "readme.md"); do 13 | # Only use file if it is to be included in docs. 14 | if grep -Fxq "include_in_docs: true" $README_FILENAME; then 15 | # Make link to readme.md in docs/gathered/. 16 | # Since everything is called readme.md, rename it by its dirname. 17 | README_DIRNAME=`dirname $README_FILENAME` 18 | DOCS_FILENAME=$GATHERED_DIR/$README_DIRNAME.md 19 | mkdir -p `dirname $DOCS_FILENAME` 20 | ln -s $ROOT_DIR/$README_FILENAME $DOCS_FILENAME 21 | fi 22 | done 23 | 24 | # Gather docs from examples/*.ipynb and add YAML front-matter. 25 | for NOTEBOOK_FILENAME in $(find examples -depth -iname "*.ipynb"); do 26 | DOCS_FILENAME=$GATHERED_DIR/$NOTEBOOK_FILENAME 27 | mkdir -p `dirname $DOCS_FILENAME` 28 | python scripts/copy_notebook.py $NOTEBOOK_FILENAME $DOCS_FILENAME 29 | done 30 | -------------------------------------------------------------------------------- /caffe/scripts/travis/travis_build_and_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Script called by Travis to do a CPU-only build of and test Caffe. 3 | 4 | set -e 5 | MAKE="make --jobs=$NUM_THREADS --keep-going" 6 | 7 | if $WITH_CMAKE; then 8 | mkdir build 9 | cd build 10 | CPU_ONLY=" -DCPU_ONLY=ON" 11 | if ! $WITH_CUDA; then 12 | CPU_ONLY=" -DCPU_ONLY=OFF" 13 | fi 14 | PYTHON_ARGS="" 15 | if [ "$PYTHON_VERSION" = "3" ]; then 16 | PYTHON_ARGS="$PYTHON_ARGS -Dpython_version=3 -DBOOST_LIBRARYDIR=$CONDA_DIR/lib/" 17 | fi 18 | cmake -DBUILD_python=ON -DCMAKE_BUILD_TYPE=Release $CPU_ONLY $PYTHON_ARGS -DCMAKE_INCLUDE_PATH="$CONDA_DIR/include/" -DCMAKE_LIBRARY_PATH="$CONDA_DIR/lib/" .. 19 | $MAKE 20 | $MAKE pytest 21 | if ! $WITH_CUDA; then 22 | $MAKE runtest 23 | $MAKE lint 24 | fi 25 | $MAKE clean 26 | cd - 27 | else 28 | if ! $WITH_CUDA; then 29 | export CPU_ONLY=1 30 | fi 31 | $MAKE all test pycaffe warn lint || true 32 | if ! $WITH_CUDA; then 33 | $MAKE runtest 34 | fi 35 | $MAKE all 36 | $MAKE test 37 | $MAKE pycaffe 38 | $MAKE pytest 39 | $MAKE warn 40 | if ! $WITH_CUDA; then 41 | $MAKE lint 42 | fi 43 | fi 44 | -------------------------------------------------------------------------------- /caffe/scripts/travis/travis_setup_makefile_config.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | mv Makefile.config.example Makefile.config 6 | 7 | if $WITH_CUDA; then 8 | # Only generate compute_50. 9 | GENCODE="-gencode arch=compute_50,code=sm_50" 10 | GENCODE="$GENCODE -gencode arch=compute_50,code=compute_50" 11 | echo "CUDA_ARCH := $GENCODE" >> Makefile.config 12 | fi 13 | 14 | cat << 'EOF' >> Makefile.config 15 | # Travis' nvcc doesn't like newer boost versions 16 | NVCCFLAGS := -Xcudafe --diag_suppress=cc_clobber_ignored -Xcudafe --diag_suppress=useless_using_declaration -Xcudafe --diag_suppress=set_but_not_used 17 | ANACONDA_HOME := $(CONDA_DIR) 18 | PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ 19 | $(ANACONDA_HOME)/include/python2.7 \ 20 | $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include 21 | PYTHON_LIB := $(ANACONDA_HOME)/lib 22 | INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 23 | LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 24 | WITH_PYTHON_LAYER := 1 25 | EOF 26 | -------------------------------------------------------------------------------- /caffe/scripts/upload_model_to_gist.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Check for valid directory 4 | DIRNAME=$1 5 | if [ ! -f $DIRNAME/readme.md ]; then 6 | echo "usage: upload_model_to_gist.sh " 7 | echo " /readme.md must exist" 8 | fi 9 | cd $DIRNAME 10 | FILES=`find . -maxdepth 1 -type f ! -name "*.caffemodel*" | xargs echo` 11 | 12 | # Check for gist tool. 13 | gist -v >/dev/null 2>&1 || { echo >&2 "I require 'gist' but it's not installed. Do 'gem install gist'."; exit 1; } 14 | 15 | NAME=`sed -n 's/^name:[[:space:]]*//p' readme.md` 16 | if [ -z "$NAME" ]; then 17 | echo " /readme.md must contain name field in the front-matter." 18 | fi 19 | 20 | GIST=`sed -n 's/^gist_id:[[:space:]]*//p' readme.md` 21 | if [ -z "$GIST" ]; then 22 | echo "Uploading new Gist" 23 | gist -p -d "$NAME" $FILES 24 | else 25 | echo "Updating existing Gist, id $GIST" 26 | gist -u $GIST -d "$NAME" $FILES 27 | fi 28 | 29 | RESULT=$? 30 | if [ $RESULT -eq 0 ]; then 31 | echo "You've uploaded your model!" 32 | echo "Don't forget to add the gist_id field to your /readme.md now!" 33 | echo "Run the command again after you do that, to make sure the Gist id propagates." 34 | echo "" 35 | echo "And do share your model over at https://github.com/BVLC/caffe/wiki/Model-Zoo" 36 | else 37 | echo "Something went wrong!" 38 | fi 39 | -------------------------------------------------------------------------------- /caffe/src/caffe/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # generate protobuf sources 2 | file(GLOB proto_files proto/*.proto) 3 | caffe_protobuf_generate_cpp_py(${proto_gen_folder} proto_srcs proto_hdrs proto_python ${proto_files}) 4 | 5 | # include python files either to force generation 6 | add_library(proto STATIC ${proto_hdrs} ${proto_srcs} ${proto_python}) 7 | set(Caffe_LINKER_LIBS proto ${Caffe_LINKER_LIBS}) # note, crucial to prepend! 8 | caffe_default_properties(proto) 9 | 10 | # --[ Caffe library 11 | 12 | # creates 'test_srcs', 'srcs', 'test_cuda', 'cuda' lists 13 | caffe_pickup_caffe_sources(${PROJECT_SOURCE_DIR}) 14 | 15 | if(HAVE_CUDA) 16 | caffe_cuda_compile(cuda_objs ${cuda}) 17 | list(APPEND srcs ${cuda_objs} ${cuda}) 18 | endif() 19 | 20 | add_library(caffe ${srcs}) 21 | target_link_libraries(caffe proto ${Caffe_LINKER_LIBS}) 22 | caffe_default_properties(caffe) 23 | 24 | # ---[ Tests 25 | add_subdirectory(test) 26 | 27 | # ---[ Install 28 | install(DIRECTORY ${Caffe_INCLUDE_DIR}/caffe DESTINATION include) 29 | install(FILES ${proto_hdrs} DESTINATION include/caffe/proto) 30 | install(TARGETS caffe proto EXPORT CaffeTargets DESTINATION lib) 31 | 32 | file(WRITE ${PROJECT_BINARY_DIR}/__init__.py) 33 | list(APPEND proto_python ${PROJECT_BINARY_DIR}/__init__.py) 34 | install(PROGRAMS ${proto_python} DESTINATION python/caffe/proto) 35 | 36 | 37 | -------------------------------------------------------------------------------- /caffe/src/caffe/internal_thread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "caffe/internal_thread.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | 7 | namespace caffe { 8 | 9 | InternalThread::~InternalThread() { 10 | StopInternalThread(); 11 | } 12 | 13 | bool InternalThread::is_started() const { 14 | return thread_ && thread_->joinable(); 15 | } 16 | 17 | bool InternalThread::must_stop() { 18 | return thread_ && thread_->interruption_requested(); 19 | } 20 | 21 | void InternalThread::StartInternalThread() { 22 | CHECK(!is_started()) << "Threads should persist and not be restarted."; 23 | 24 | int device = 0; 25 | #ifndef CPU_ONLY 26 | CUDA_CHECK(cudaGetDevice(&device)); 27 | #endif 28 | Caffe::Brew mode = Caffe::mode(); 29 | int rand_seed = caffe_rng_rand(); 30 | int solver_count = Caffe::solver_count(); 31 | bool root_solver = Caffe::root_solver(); 32 | 33 | try { 34 | thread_.reset(new boost::thread(&InternalThread::entry, this, device, mode, 35 | rand_seed, solver_count, root_solver)); 36 | } catch (std::exception& e) { 37 | LOG(FATAL) << "Thread exception: " << e.what(); 38 | } 39 | } 40 | 41 | void InternalThread::entry(int device, Caffe::Brew mode, int rand_seed, 42 | int solver_count, bool root_solver) { 43 | #ifndef CPU_ONLY 44 | CUDA_CHECK(cudaSetDevice(device)); 45 | #endif 46 | Caffe::set_mode(mode); 47 | Caffe::set_random_seed(rand_seed); 48 | Caffe::set_solver_count(solver_count); 49 | Caffe::set_root_solver(root_solver); 50 | 51 | InternalThreadEntry(); 52 | } 53 | 54 | void InternalThread::StopInternalThread() { 55 | if (is_started()) { 56 | thread_->interrupt(); 57 | try { 58 | thread_->join(); 59 | } catch (boost::thread_interrupted&) { 60 | } catch (std::exception& e) { 61 | LOG(FATAL) << "Thread exception: " << e.what(); 62 | } 63 | } 64 | } 65 | 66 | } // namespace caffe 67 | -------------------------------------------------------------------------------- /caffe/src/caffe/layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "caffe/layer.hpp" 3 | 4 | namespace caffe { 5 | 6 | template 7 | void Layer::InitMutex() { 8 | forward_mutex_.reset(new boost::mutex()); 9 | } 10 | 11 | template 12 | void Layer::Lock() { 13 | if (IsShared()) { 14 | forward_mutex_->lock(); 15 | } 16 | } 17 | 18 | template 19 | void Layer::Unlock() { 20 | if (IsShared()) { 21 | forward_mutex_->unlock(); 22 | } 23 | } 24 | 25 | INSTANTIATE_CLASS(Layer); 26 | 27 | } // namespace caffe 28 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/absval_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/neuron_layers.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void AbsValLayer::LayerSetUp(const vector*>& bottom, 11 | const vector*>& top) { 12 | NeuronLayer::LayerSetUp(bottom, top); 13 | CHECK_NE(top[0], bottom[0]) << this->type() << " Layer does not " 14 | "allow in-place computation."; 15 | } 16 | 17 | template 18 | void AbsValLayer::Forward_cpu( 19 | const vector*>& bottom, const vector*>& top) { 20 | const int count = top[0]->count(); 21 | Dtype* top_data = top[0]->mutable_cpu_data(); 22 | caffe_abs(count, bottom[0]->cpu_data(), top_data); 23 | } 24 | 25 | template 26 | void AbsValLayer::Backward_cpu(const vector*>& top, 27 | const vector& propagate_down, const vector*>& bottom) { 28 | const int count = top[0]->count(); 29 | const Dtype* top_diff = top[0]->cpu_diff(); 30 | if (propagate_down[0]) { 31 | const Dtype* bottom_data = bottom[0]->cpu_data(); 32 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 33 | caffe_cpu_sign(count, bottom_data, bottom_diff); 34 | caffe_mul(count, bottom_diff, top_diff, bottom_diff); 35 | } 36 | } 37 | 38 | #ifdef CPU_ONLY 39 | STUB_GPU(AbsValLayer); 40 | #endif 41 | 42 | INSTANTIATE_CLASS(AbsValLayer); 43 | REGISTER_LAYER_CLASS(AbsVal); 44 | 45 | } // namespace caffe 46 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/absval_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/util/math_functions.hpp" 5 | #include "caffe/vision_layers.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void AbsValLayer::Forward_gpu( 11 | const vector*>& bottom, const vector*>& top) { 12 | const int count = top[0]->count(); 13 | Dtype* top_data = top[0]->mutable_gpu_data(); 14 | caffe_gpu_abs(count, bottom[0]->gpu_data(), top_data); 15 | } 16 | 17 | template 18 | void AbsValLayer::Backward_gpu(const vector*>& top, 19 | const vector& propagate_down, const vector*>& bottom) { 20 | const int count = top[0]->count(); 21 | const Dtype* top_diff = top[0]->gpu_diff(); 22 | if (propagate_down[0]) { 23 | const Dtype* bottom_data = bottom[0]->gpu_data(); 24 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 25 | caffe_gpu_sign(count, bottom_data, bottom_diff); 26 | caffe_gpu_mul(count, bottom_diff, top_diff, bottom_diff); 27 | } 28 | } 29 | 30 | INSTANTIATE_LAYER_GPU_FUNCS(AbsValLayer); 31 | 32 | 33 | } // namespace caffe 34 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/base_data_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/data_layers.hpp" 4 | 5 | namespace caffe { 6 | 7 | template 8 | void BasePrefetchingDataLayer::Forward_gpu( 9 | const vector*>& bottom, const vector*>& top) { 10 | Batch* batch = prefetch_full_.pop("Data layer prefetch queue empty"); 11 | // Reshape to loaded data. 12 | top[0]->ReshapeLike(batch->data_); 13 | // Copy the data 14 | caffe_copy(batch->data_.count(), batch->data_.gpu_data(), 15 | top[0]->mutable_gpu_data()); 16 | if (this->output_labels_) { 17 | // Reshape to loaded labels. 18 | top[1]->ReshapeLike(batch->label_); 19 | // Copy the labels. 20 | caffe_copy(batch->label_.count(), batch->label_.gpu_data(), 21 | top[1]->mutable_gpu_data()); 22 | } 23 | 24 | prefetch_free_.push(batch); 25 | } 26 | 27 | INSTANTIATE_LAYER_GPU_FORWARD(BasePrefetchingDataLayer); 28 | 29 | } // namespace caffe 30 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/bnll_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "caffe/layer.hpp" 5 | #include "caffe/vision_layers.hpp" 6 | 7 | namespace caffe { 8 | 9 | const float kBNLL_THRESHOLD = 50.; 10 | 11 | template 12 | void BNLLLayer::Forward_cpu(const vector*>& bottom, 13 | const vector*>& top) { 14 | const Dtype* bottom_data = bottom[0]->cpu_data(); 15 | Dtype* top_data = top[0]->mutable_cpu_data(); 16 | const int count = bottom[0]->count(); 17 | for (int i = 0; i < count; ++i) { 18 | top_data[i] = bottom_data[i] > 0 ? 19 | bottom_data[i] + log(1. + exp(-bottom_data[i])) : 20 | log(1. + exp(bottom_data[i])); 21 | } 22 | } 23 | 24 | template 25 | void BNLLLayer::Backward_cpu(const vector*>& top, 26 | const vector& propagate_down, 27 | const vector*>& bottom) { 28 | if (propagate_down[0]) { 29 | const Dtype* bottom_data = bottom[0]->cpu_data(); 30 | const Dtype* top_diff = top[0]->cpu_diff(); 31 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 32 | const int count = bottom[0]->count(); 33 | Dtype expval; 34 | for (int i = 0; i < count; ++i) { 35 | expval = exp(std::min(bottom_data[i], Dtype(kBNLL_THRESHOLD))); 36 | bottom_diff[i] = top_diff[i] * expval / (expval + 1.); 37 | } 38 | } 39 | } 40 | 41 | #ifdef CPU_ONLY 42 | STUB_GPU(BNLLLayer); 43 | #endif 44 | 45 | INSTANTIATE_CLASS(BNLLLayer); 46 | REGISTER_LAYER_CLASS(BNLL); 47 | 48 | } // namespace caffe 49 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_pooling_layer.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | 4 | #include "caffe/filler.hpp" 5 | #include "caffe/layer.hpp" 6 | #include "caffe/util/im2col.hpp" 7 | #include "caffe/util/math_functions.hpp" 8 | #include "caffe/vision_layers.hpp" 9 | 10 | namespace caffe { 11 | 12 | template 13 | void CuDNNPoolingLayer::LayerSetUp(const vector*>& bottom, 14 | const vector*>& top) { 15 | PoolingLayer::LayerSetUp(bottom, top); 16 | CUDNN_CHECK(cudnnCreate(&handle_)); 17 | cudnn::createTensor4dDesc(&bottom_desc_); 18 | cudnn::createTensor4dDesc(&top_desc_); 19 | cudnn::createPoolingDesc(&pooling_desc_, 20 | this->layer_param_.pooling_param().pool(), &mode_, 21 | this->kernel_h_, this->kernel_w_, this->pad_h_, this->pad_w_, 22 | this->stride_h_, this->stride_w_); 23 | handles_setup_ = true; 24 | } 25 | 26 | template 27 | void CuDNNPoolingLayer::Reshape(const vector*>& bottom, 28 | const vector*>& top) { 29 | PoolingLayer::Reshape(bottom, top); 30 | cudnn::setTensor4dDesc(&bottom_desc_, bottom[0]->num(), 31 | this->channels_, this->height_, this->width_); 32 | cudnn::setTensor4dDesc(&top_desc_, bottom[0]->num(), 33 | this->channels_, this->pooled_height_, this->pooled_width_); 34 | } 35 | 36 | template 37 | CuDNNPoolingLayer::~CuDNNPoolingLayer() { 38 | // Check that handles have been setup before destroying. 39 | if (!handles_setup_) { return; } 40 | 41 | cudnnDestroyTensorDescriptor(bottom_desc_); 42 | cudnnDestroyTensorDescriptor(top_desc_); 43 | cudnnDestroyPoolingDescriptor(pooling_desc_); 44 | cudnnDestroy(handle_); 45 | } 46 | 47 | INSTANTIATE_CLASS(CuDNNPoolingLayer); 48 | 49 | } // namespace caffe 50 | #endif 51 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_pooling_layer.cu: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | 4 | #include "caffe/filler.hpp" 5 | #include "caffe/layer.hpp" 6 | #include "caffe/util/im2col.hpp" 7 | #include "caffe/util/math_functions.hpp" 8 | #include "caffe/vision_layers.hpp" 9 | 10 | namespace caffe { 11 | 12 | template 13 | void CuDNNPoolingLayer::Forward_gpu(const vector*>& bottom, 14 | const vector*>& top) { 15 | const Dtype* bottom_data = bottom[0]->gpu_data(); 16 | Dtype* top_data = top[0]->mutable_gpu_data(); 17 | CUDNN_CHECK(cudnnPoolingForward(handle_, pooling_desc_, 18 | cudnn::dataType::one, 19 | bottom_desc_, bottom_data, 20 | cudnn::dataType::zero, 21 | top_desc_, top_data)); 22 | } 23 | 24 | template 25 | void CuDNNPoolingLayer::Backward_gpu(const vector*>& top, 26 | const vector& propagate_down, const vector*>& bottom) { 27 | if (!propagate_down[0]) { 28 | return; 29 | } 30 | const Dtype* top_diff = top[0]->gpu_diff(); 31 | const Dtype* top_data = top[0]->gpu_data(); 32 | const Dtype* bottom_data = bottom[0]->gpu_data(); 33 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 34 | CUDNN_CHECK(cudnnPoolingBackward(handle_, pooling_desc_, 35 | cudnn::dataType::one, 36 | top_desc_, top_data, top_desc_, top_diff, 37 | bottom_desc_, bottom_data, 38 | cudnn::dataType::zero, 39 | bottom_desc_, bottom_diff)); 40 | } 41 | 42 | INSTANTIATE_LAYER_GPU_FUNCS(CuDNNPoolingLayer); 43 | 44 | } // namespace caffe 45 | #endif 46 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_relu_layer.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void CuDNNReLULayer::LayerSetUp(const vector*>& bottom, 12 | const vector*>& top) { 13 | ReLULayer::LayerSetUp(bottom, top); 14 | // initialize cuDNN 15 | CUDNN_CHECK(cudnnCreate(&handle_)); 16 | cudnn::createTensor4dDesc(&bottom_desc_); 17 | cudnn::createTensor4dDesc(&top_desc_); 18 | handles_setup_ = true; 19 | } 20 | 21 | template 22 | void CuDNNReLULayer::Reshape(const vector*>& bottom, 23 | const vector*>& top) { 24 | ReLULayer::Reshape(bottom, top); 25 | const int N = bottom[0]->num(); 26 | const int K = bottom[0]->channels(); 27 | const int H = bottom[0]->height(); 28 | const int W = bottom[0]->width(); 29 | cudnn::setTensor4dDesc(&bottom_desc_, N, K, H, W); 30 | cudnn::setTensor4dDesc(&top_desc_, N, K, H, W); 31 | } 32 | 33 | template 34 | CuDNNReLULayer::~CuDNNReLULayer() { 35 | // Check that handles have been setup before destroying. 36 | if (!handles_setup_) { return; } 37 | 38 | cudnnDestroyTensorDescriptor(this->bottom_desc_); 39 | cudnnDestroyTensorDescriptor(this->top_desc_); 40 | cudnnDestroy(this->handle_); 41 | } 42 | 43 | INSTANTIATE_CLASS(CuDNNReLULayer); 44 | 45 | } // namespace caffe 46 | #endif 47 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_relu_layer.cu: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void CuDNNReLULayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | // Fallback to standard Caffe for leaky ReLU. 14 | if (ReLULayer::layer_param_.relu_param().negative_slope() != 0) { 15 | return ReLULayer::Forward_gpu(bottom, top); 16 | } 17 | 18 | const Dtype* bottom_data = bottom[0]->gpu_data(); 19 | Dtype* top_data = top[0]->mutable_gpu_data(); 20 | CUDNN_CHECK(cudnnActivationForward(this->handle_, 21 | CUDNN_ACTIVATION_RELU, 22 | cudnn::dataType::one, 23 | this->bottom_desc_, bottom_data, 24 | cudnn::dataType::zero, 25 | this->top_desc_, top_data)); 26 | } 27 | 28 | template 29 | void CuDNNReLULayer::Backward_gpu(const vector*>& top, 30 | const vector& propagate_down, 31 | const vector*>& bottom) { 32 | if (!propagate_down[0]) { 33 | return; 34 | } 35 | 36 | // Fallback to standard Caffe for leaky ReLU. 37 | if (ReLULayer::layer_param_.relu_param().negative_slope() != 0) { 38 | return ReLULayer::Backward_gpu(top, propagate_down, bottom); 39 | } 40 | 41 | const Dtype* top_data = top[0]->gpu_data(); 42 | const Dtype* top_diff = top[0]->gpu_diff(); 43 | const Dtype* bottom_data = bottom[0]->gpu_data(); 44 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 45 | CUDNN_CHECK(cudnnActivationBackward(this->handle_, 46 | CUDNN_ACTIVATION_RELU, 47 | cudnn::dataType::one, 48 | this->top_desc_, top_data, this->top_desc_, top_diff, 49 | this->bottom_desc_, bottom_data, 50 | cudnn::dataType::zero, 51 | this->bottom_desc_, bottom_diff)); 52 | } 53 | 54 | INSTANTIATE_LAYER_GPU_FUNCS(CuDNNReLULayer); 55 | 56 | } // namespace caffe 57 | #endif 58 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_sigmoid_layer.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void CuDNNSigmoidLayer::LayerSetUp(const vector*>& bottom, 12 | const vector*>& top) { 13 | SigmoidLayer::LayerSetUp(bottom, top); 14 | // initialize cuDNN 15 | CUDNN_CHECK(cudnnCreate(&handle_)); 16 | cudnn::createTensor4dDesc(&bottom_desc_); 17 | cudnn::createTensor4dDesc(&top_desc_); 18 | handles_setup_ = true; 19 | } 20 | 21 | template 22 | void CuDNNSigmoidLayer::Reshape(const vector*>& bottom, 23 | const vector*>& top) { 24 | SigmoidLayer::Reshape(bottom, top); 25 | const int N = bottom[0]->num(); 26 | const int K = bottom[0]->channels(); 27 | const int H = bottom[0]->height(); 28 | const int W = bottom[0]->width(); 29 | cudnn::setTensor4dDesc(&bottom_desc_, N, K, H, W); 30 | cudnn::setTensor4dDesc(&top_desc_, N, K, H, W); 31 | } 32 | 33 | template 34 | CuDNNSigmoidLayer::~CuDNNSigmoidLayer() { 35 | // Check that handles have been setup before destroying. 36 | if (!handles_setup_) { return; } 37 | 38 | cudnnDestroyTensorDescriptor(this->bottom_desc_); 39 | cudnnDestroyTensorDescriptor(this->top_desc_); 40 | cudnnDestroy(this->handle_); 41 | } 42 | 43 | INSTANTIATE_CLASS(CuDNNSigmoidLayer); 44 | 45 | } // namespace caffe 46 | #endif 47 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_sigmoid_layer.cu: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void CuDNNSigmoidLayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | const Dtype* bottom_data = bottom[0]->gpu_data(); 14 | Dtype* top_data = top[0]->mutable_gpu_data(); 15 | CUDNN_CHECK(cudnnActivationForward(this->handle_, 16 | CUDNN_ACTIVATION_SIGMOID, 17 | cudnn::dataType::one, 18 | this->bottom_desc_, bottom_data, 19 | cudnn::dataType::zero, 20 | this->top_desc_, top_data)); 21 | } 22 | 23 | template 24 | void CuDNNSigmoidLayer::Backward_gpu(const vector*>& top, 25 | const vector& propagate_down, 26 | const vector*>& bottom) { 27 | if (!propagate_down[0]) { 28 | return; 29 | } 30 | 31 | const Dtype* top_data = top[0]->gpu_data(); 32 | const Dtype* top_diff = top[0]->gpu_diff(); 33 | const Dtype* bottom_data = bottom[0]->gpu_data(); 34 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 35 | CUDNN_CHECK(cudnnActivationBackward(this->handle_, 36 | CUDNN_ACTIVATION_SIGMOID, 37 | cudnn::dataType::one, 38 | this->top_desc_, top_data, this->top_desc_, top_diff, 39 | this->bottom_desc_, bottom_data, 40 | cudnn::dataType::zero, 41 | this->bottom_desc_, bottom_diff)); 42 | } 43 | 44 | INSTANTIATE_LAYER_GPU_FUNCS(CuDNNSigmoidLayer); 45 | 46 | } // namespace caffe 47 | #endif 48 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_softmax_layer.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | #include 5 | 6 | #include "thrust/device_vector.h" 7 | 8 | #include "caffe/layer.hpp" 9 | #include "caffe/util/math_functions.hpp" 10 | #include "caffe/vision_layers.hpp" 11 | 12 | namespace caffe { 13 | 14 | template 15 | void CuDNNSoftmaxLayer::LayerSetUp(const vector*>& bottom, 16 | const vector*>& top) { 17 | SoftmaxLayer::LayerSetUp(bottom, top); 18 | // Initialize CUDNN. 19 | CUDNN_CHECK(cudnnCreate(&handle_)); 20 | cudnn::createTensor4dDesc(&bottom_desc_); 21 | cudnn::createTensor4dDesc(&top_desc_); 22 | handles_setup_ = true; 23 | } 24 | 25 | template 26 | void CuDNNSoftmaxLayer::Reshape(const vector*>& bottom, 27 | const vector*>& top) { 28 | SoftmaxLayer::Reshape(bottom, top); 29 | int N = this->outer_num_; 30 | int K = bottom[0]->shape(this->softmax_axis_); 31 | int H = this->inner_num_; 32 | int W = 1; 33 | cudnn::setTensor4dDesc(&bottom_desc_, N, K, H, W); 34 | cudnn::setTensor4dDesc(&top_desc_, N, K, H, W); 35 | } 36 | 37 | template 38 | CuDNNSoftmaxLayer::~CuDNNSoftmaxLayer() { 39 | // Check that handles have been setup before destroying. 40 | if (!handles_setup_) { return; } 41 | 42 | cudnnDestroyTensorDescriptor(bottom_desc_); 43 | cudnnDestroyTensorDescriptor(top_desc_); 44 | cudnnDestroy(handle_); 45 | } 46 | 47 | INSTANTIATE_CLASS(CuDNNSoftmaxLayer); 48 | 49 | } // namespace caffe 50 | #endif 51 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_softmax_layer.cu: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | #include 5 | 6 | #include "thrust/device_vector.h" 7 | 8 | #include "caffe/layer.hpp" 9 | #include "caffe/util/math_functions.hpp" 10 | #include "caffe/vision_layers.hpp" 11 | 12 | namespace caffe { 13 | 14 | template 15 | void CuDNNSoftmaxLayer::Forward_gpu(const vector*>& bottom, 16 | const vector*>& top) { 17 | const Dtype* bottom_data = bottom[0]->gpu_data(); 18 | Dtype* top_data = top[0]->mutable_gpu_data(); 19 | CUDNN_CHECK(cudnnSoftmaxForward(handle_, CUDNN_SOFTMAX_ACCURATE, 20 | CUDNN_SOFTMAX_MODE_CHANNEL, 21 | cudnn::dataType::one, 22 | bottom_desc_, bottom_data, 23 | cudnn::dataType::zero, 24 | top_desc_, top_data)); 25 | } 26 | 27 | template 28 | void CuDNNSoftmaxLayer::Backward_gpu(const vector*>& top, 29 | const vector& propagate_down, const vector*>& bottom) { 30 | if (propagate_down[0]) { 31 | const Dtype* top_data = top[0]->gpu_data(); 32 | const Dtype* top_diff = top[0]->gpu_diff(); 33 | const Dtype* bottom_data = bottom[0]->gpu_data(); 34 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 35 | 36 | CUDNN_CHECK(cudnnSoftmaxBackward(handle_, CUDNN_SOFTMAX_ACCURATE, 37 | CUDNN_SOFTMAX_MODE_CHANNEL, 38 | cudnn::dataType::one, 39 | top_desc_, top_data, top_desc_, top_diff, 40 | cudnn::dataType::zero, 41 | bottom_desc_, bottom_diff)); 42 | } 43 | } 44 | 45 | INSTANTIATE_LAYER_GPU_FUNCS(CuDNNSoftmaxLayer); 46 | 47 | } // namespace caffe 48 | #endif 49 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_tanh_layer.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void CuDNNTanHLayer::LayerSetUp(const vector*>& bottom, 12 | const vector*>& top) { 13 | TanHLayer::LayerSetUp(bottom, top); 14 | // initialize cuDNN 15 | CUDNN_CHECK(cudnnCreate(&handle_)); 16 | cudnn::createTensor4dDesc(&bottom_desc_); 17 | cudnn::createTensor4dDesc(&top_desc_); 18 | handles_setup_ = true; 19 | } 20 | 21 | template 22 | void CuDNNTanHLayer::Reshape(const vector*>& bottom, 23 | const vector*>& top) { 24 | TanHLayer::Reshape(bottom, top); 25 | const int N = bottom[0]->num(); 26 | const int K = bottom[0]->channels(); 27 | const int H = bottom[0]->height(); 28 | const int W = bottom[0]->width(); 29 | cudnn::setTensor4dDesc(&bottom_desc_, N, K, H, W); 30 | cudnn::setTensor4dDesc(&top_desc_, N, K, H, W); 31 | } 32 | 33 | template 34 | CuDNNTanHLayer::~CuDNNTanHLayer() { 35 | // Check that handles have been setup before destroying. 36 | if (!handles_setup_) { return; } 37 | 38 | cudnnDestroyTensorDescriptor(this->bottom_desc_); 39 | cudnnDestroyTensorDescriptor(this->top_desc_); 40 | cudnnDestroy(this->handle_); 41 | } 42 | 43 | INSTANTIATE_CLASS(CuDNNTanHLayer); 44 | 45 | } // namespace caffe 46 | #endif 47 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/cudnn_tanh_layer.cu: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void CuDNNTanHLayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | const Dtype* bottom_data = bottom[0]->gpu_data(); 14 | Dtype* top_data = top[0]->mutable_gpu_data(); 15 | CUDNN_CHECK(cudnnActivationForward(this->handle_, 16 | CUDNN_ACTIVATION_TANH, 17 | cudnn::dataType::one, 18 | this->bottom_desc_, bottom_data, 19 | cudnn::dataType::zero, 20 | this->top_desc_, top_data)); 21 | } 22 | 23 | template 24 | void CuDNNTanHLayer::Backward_gpu(const vector*>& top, 25 | const vector& propagate_down, 26 | const vector*>& bottom) { 27 | if (!propagate_down[0]) { 28 | return; 29 | } 30 | 31 | const Dtype* top_data = top[0]->gpu_data(); 32 | const Dtype* top_diff = top[0]->gpu_diff(); 33 | const Dtype* bottom_data = bottom[0]->gpu_data(); 34 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 35 | 36 | CUDNN_CHECK(cudnnActivationBackward(this->handle_, 37 | CUDNN_ACTIVATION_TANH, 38 | cudnn::dataType::one, 39 | this->top_desc_, top_data, this->top_desc_, top_diff, 40 | this->bottom_desc_, bottom_data, 41 | cudnn::dataType::zero, 42 | this->bottom_desc_, bottom_diff)); 43 | } 44 | 45 | INSTANTIATE_LAYER_GPU_FUNCS(CuDNNTanHLayer); 46 | 47 | } // namespace caffe 48 | #endif 49 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/euclidean_loss_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/util/io.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void EuclideanLossLayer::Reshape( 12 | const vector*>& bottom, const vector*>& top) { 13 | LossLayer::Reshape(bottom, top); 14 | CHECK_EQ(bottom[0]->count(1), bottom[1]->count(1)) 15 | << "Inputs must have the same dimension."; 16 | diff_.ReshapeLike(*bottom[0]); 17 | } 18 | 19 | template 20 | void EuclideanLossLayer::Forward_cpu(const vector*>& bottom, 21 | const vector*>& top) { 22 | int count = bottom[0]->count(); 23 | caffe_sub( 24 | count, 25 | bottom[0]->cpu_data(), 26 | bottom[1]->cpu_data(), 27 | diff_.mutable_cpu_data()); 28 | Dtype dot = caffe_cpu_dot(count, diff_.cpu_data(), diff_.cpu_data()); 29 | Dtype loss = dot / bottom[0]->num() / Dtype(2); 30 | top[0]->mutable_cpu_data()[0] = loss; 31 | } 32 | 33 | template 34 | void EuclideanLossLayer::Backward_cpu(const vector*>& top, 35 | const vector& propagate_down, const vector*>& bottom) { 36 | for (int i = 0; i < 2; ++i) { 37 | if (propagate_down[i]) { 38 | const Dtype sign = (i == 0) ? 1 : -1; 39 | const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num(); 40 | caffe_cpu_axpby( 41 | bottom[i]->count(), // count 42 | alpha, // alpha 43 | diff_.cpu_data(), // a 44 | Dtype(0), // beta 45 | bottom[i]->mutable_cpu_diff()); // b 46 | } 47 | } 48 | } 49 | 50 | #ifdef CPU_ONLY 51 | STUB_GPU(EuclideanLossLayer); 52 | #endif 53 | 54 | INSTANTIATE_CLASS(EuclideanLossLayer); 55 | REGISTER_LAYER_CLASS(EuclideanLoss); 56 | 57 | } // namespace caffe 58 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/euclidean_loss_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/util/io.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void EuclideanLossLayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | int count = bottom[0]->count(); 14 | caffe_gpu_sub( 15 | count, 16 | bottom[0]->gpu_data(), 17 | bottom[1]->gpu_data(), 18 | diff_.mutable_gpu_data()); 19 | Dtype dot; 20 | caffe_gpu_dot(count, diff_.gpu_data(), diff_.gpu_data(), &dot); 21 | Dtype loss = dot / bottom[0]->num() / Dtype(2); 22 | top[0]->mutable_cpu_data()[0] = loss; 23 | } 24 | 25 | template 26 | void EuclideanLossLayer::Backward_gpu(const vector*>& top, 27 | const vector& propagate_down, const vector*>& bottom) { 28 | for (int i = 0; i < 2; ++i) { 29 | if (propagate_down[i]) { 30 | const Dtype sign = (i == 0) ? 1 : -1; 31 | const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num(); 32 | caffe_gpu_axpby( 33 | bottom[i]->count(), // count 34 | alpha, // alpha 35 | diff_.gpu_data(), // a 36 | Dtype(0), // beta 37 | bottom[i]->mutable_gpu_diff()); // b 38 | } 39 | } 40 | } 41 | 42 | INSTANTIATE_LAYER_GPU_FUNCS(EuclideanLossLayer); 43 | 44 | } // namespace caffe 45 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/exp_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "caffe/layer.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void ExpLayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | const int count = bottom[0]->count(); 14 | const Dtype* bottom_data = bottom[0]->gpu_data(); 15 | Dtype* top_data = top[0]->mutable_gpu_data(); 16 | if (inner_scale_ == Dtype(1)) { 17 | caffe_gpu_exp(count, bottom_data, top_data); 18 | } else { 19 | caffe_gpu_scale(count, inner_scale_, bottom_data, top_data); 20 | caffe_gpu_exp(count, top_data, top_data); 21 | } 22 | if (outer_scale_ != Dtype(1)) { 23 | caffe_gpu_scal(count, outer_scale_, top_data); 24 | } 25 | } 26 | 27 | template 28 | void ExpLayer::Backward_gpu(const vector*>& top, 29 | const vector& propagate_down, const vector*>& bottom) { 30 | if (!propagate_down[0]) { return; } 31 | const int count = bottom[0]->count(); 32 | const Dtype* top_data = top[0]->gpu_data(); 33 | const Dtype* top_diff = top[0]->gpu_diff(); 34 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 35 | caffe_gpu_mul(count, top_data, top_diff, bottom_diff); 36 | if (inner_scale_ != Dtype(1)) { 37 | caffe_gpu_scal(count, inner_scale_, bottom_diff); 38 | } 39 | } 40 | 41 | INSTANTIATE_LAYER_GPU_FUNCS(ExpLayer); 42 | 43 | 44 | } // namespace caffe 45 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/flatten_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/util/math_functions.hpp" 5 | #include "caffe/vision_layers.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void FlattenLayer::Reshape(const vector*>& bottom, 11 | const vector*>& top) { 12 | const int start_axis = bottom[0]->CanonicalAxisIndex( 13 | this->layer_param_.flatten_param().axis()); 14 | const int end_axis = bottom[0]->CanonicalAxisIndex( 15 | this->layer_param_.flatten_param().end_axis()); 16 | vector top_shape; 17 | for (int i = 0; i < start_axis; ++i) { 18 | top_shape.push_back(bottom[0]->shape(i)); 19 | } 20 | const int flattened_dim = bottom[0]->count(start_axis, end_axis + 1); 21 | top_shape.push_back(flattened_dim); 22 | for (int i = end_axis + 1; i < bottom[0]->num_axes(); ++i) { 23 | top_shape.push_back(bottom[0]->shape(i)); 24 | } 25 | top[0]->Reshape(top_shape); 26 | CHECK_EQ(top[0]->count(), bottom[0]->count()); 27 | } 28 | 29 | template 30 | void FlattenLayer::Forward_cpu(const vector*>& bottom, 31 | const vector*>& top) { 32 | top[0]->ShareData(*bottom[0]); 33 | } 34 | 35 | template 36 | void FlattenLayer::Backward_cpu(const vector*>& top, 37 | const vector& propagate_down, const vector*>& bottom) { 38 | bottom[0]->ShareDiff(*top[0]); 39 | } 40 | 41 | INSTANTIATE_CLASS(FlattenLayer); 42 | REGISTER_LAYER_CLASS(Flatten); 43 | 44 | } // namespace caffe 45 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/hdf5_data_layer.cu: -------------------------------------------------------------------------------- 1 | /* 2 | TODO: 3 | - only load parts of the file, in accordance with a prototxt param "max_mem" 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include "hdf5.h" 11 | #include "hdf5_hl.h" 12 | 13 | #include "caffe/data_layers.hpp" 14 | #include "caffe/layer.hpp" 15 | #include "caffe/util/io.hpp" 16 | 17 | namespace caffe { 18 | 19 | template 20 | void HDF5DataLayer::Forward_gpu(const vector*>& bottom, 21 | const vector*>& top) { 22 | const int batch_size = this->layer_param_.hdf5_data_param().batch_size(); 23 | for (int i = 0; i < batch_size; ++i, ++current_row_) { 24 | if (current_row_ == hdf_blobs_[0]->shape(0)) { 25 | if (num_files_ > 1) { 26 | current_file_ += 1; 27 | if (current_file_ == num_files_) { 28 | current_file_ = 0; 29 | if (this->layer_param_.hdf5_data_param().shuffle()) { 30 | std::random_shuffle(file_permutation_.begin(), 31 | file_permutation_.end()); 32 | } 33 | DLOG(INFO) << "Looping around to first file."; 34 | } 35 | LoadHDF5FileData( 36 | hdf_filenames_[file_permutation_[current_file_]].c_str()); 37 | } 38 | current_row_ = 0; 39 | if (this->layer_param_.hdf5_data_param().shuffle()) 40 | std::random_shuffle(data_permutation_.begin(), data_permutation_.end()); 41 | } 42 | for (int j = 0; j < this->layer_param_.top_size(); ++j) { 43 | int data_dim = top[j]->count() / top[j]->shape(0); 44 | caffe_copy(data_dim, 45 | &hdf_blobs_[j]->cpu_data()[data_permutation_[current_row_] 46 | * data_dim], &top[j]->mutable_gpu_data()[i * data_dim]); 47 | } 48 | } 49 | } 50 | 51 | INSTANTIATE_LAYER_GPU_FUNCS(HDF5DataLayer); 52 | 53 | } // namespace caffe 54 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/hdf5_output_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "hdf5.h" 4 | #include "hdf5_hl.h" 5 | 6 | #include "caffe/blob.hpp" 7 | #include "caffe/common.hpp" 8 | #include "caffe/layer.hpp" 9 | #include "caffe/vision_layers.hpp" 10 | 11 | namespace caffe { 12 | 13 | template 14 | void HDF5OutputLayer::Forward_gpu(const vector*>& bottom, 15 | const vector*>& top) { 16 | CHECK_GE(bottom.size(), 2); 17 | CHECK_EQ(bottom[0]->num(), bottom[1]->num()); 18 | data_blob_.Reshape(bottom[0]->num(), bottom[0]->channels(), 19 | bottom[0]->height(), bottom[0]->width()); 20 | label_blob_.Reshape(bottom[1]->num(), bottom[1]->channels(), 21 | bottom[1]->height(), bottom[1]->width()); 22 | const int data_datum_dim = bottom[0]->count() / bottom[0]->num(); 23 | const int label_datum_dim = bottom[1]->count() / bottom[1]->num(); 24 | 25 | for (int i = 0; i < bottom[0]->num(); ++i) { 26 | caffe_copy(data_datum_dim, &bottom[0]->gpu_data()[i * data_datum_dim], 27 | &data_blob_.mutable_cpu_data()[i * data_datum_dim]); 28 | caffe_copy(label_datum_dim, &bottom[1]->gpu_data()[i * label_datum_dim], 29 | &label_blob_.mutable_cpu_data()[i * label_datum_dim]); 30 | } 31 | SaveBlobs(); 32 | } 33 | 34 | template 35 | void HDF5OutputLayer::Backward_gpu(const vector*>& top, 36 | const vector& propagate_down, const vector*>& bottom) { 37 | return; 38 | } 39 | 40 | INSTANTIATE_LAYER_GPU_FUNCS(HDF5OutputLayer); 41 | 42 | } // namespace caffe 43 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/im2col_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/common.hpp" 4 | #include "caffe/layer.hpp" 5 | #include "caffe/util/im2col.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | void Im2colLayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | const Dtype* bottom_data = bottom[0]->gpu_data(); 14 | Dtype* top_data = top[0]->mutable_gpu_data(); 15 | for (int n = 0; n < bottom[0]->num(); ++n) { 16 | im2col_gpu(bottom_data + bottom[0]->offset(n), channels_, height_, 17 | width_, kernel_h_, kernel_w_, pad_h_, pad_w_, 18 | stride_h_, stride_w_, top_data + top[0]->offset(n)); 19 | } 20 | } 21 | 22 | template 23 | void Im2colLayer::Backward_gpu(const vector*>& top, 24 | const vector& propagate_down, const vector*>& bottom) { 25 | const Dtype* top_diff = top[0]->gpu_diff(); 26 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 27 | for (int n = 0; n < top[0]->num(); ++n) { 28 | col2im_gpu(top_diff + top[0]->offset(n), channels_, height_, width_, 29 | kernel_h_, kernel_w_, pad_h_, pad_w_, 30 | stride_h_, stride_w_, bottom_diff + bottom[0]->offset(n)); 31 | } 32 | } 33 | 34 | 35 | INSTANTIATE_LAYER_GPU_FUNCS(Im2colLayer); 36 | 37 | } // namespace caffe 38 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/loss_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "caffe/layer.hpp" 7 | #include "caffe/util/io.hpp" 8 | #include "caffe/util/math_functions.hpp" 9 | #include "caffe/vision_layers.hpp" 10 | 11 | namespace caffe { 12 | 13 | template 14 | void LossLayer::LayerSetUp( 15 | const vector*>& bottom, const vector*>& top) { 16 | // LossLayers have a non-zero (1) loss by default. 17 | if (this->layer_param_.loss_weight_size() == 0) { 18 | this->layer_param_.add_loss_weight(Dtype(1)); 19 | } 20 | } 21 | 22 | template 23 | void LossLayer::Reshape( 24 | const vector*>& bottom, const vector*>& top) { 25 | CHECK_EQ(bottom[0]->num(), bottom[1]->num()) 26 | << "The data and label should have the same number."; 27 | vector loss_shape(0); // Loss layers output a scalar; 0 axes. 28 | top[0]->Reshape(loss_shape); 29 | } 30 | 31 | INSTANTIATE_CLASS(LossLayer); 32 | 33 | } // namespace caffe 34 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/neuron_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/vision_layers.hpp" 5 | 6 | namespace caffe { 7 | 8 | template 9 | void NeuronLayer::Reshape(const vector*>& bottom, 10 | const vector*>& top) { 11 | top[0]->ReshapeLike(*bottom[0]); 12 | } 13 | 14 | INSTANTIATE_CLASS(NeuronLayer); 15 | 16 | } // namespace caffe 17 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/relu_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "caffe/layer.hpp" 5 | #include "caffe/vision_layers.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void ReLULayer::Forward_cpu(const vector*>& bottom, 11 | const vector*>& top) { 12 | const Dtype* bottom_data = bottom[0]->cpu_data(); 13 | Dtype* top_data = top[0]->mutable_cpu_data(); 14 | const int count = bottom[0]->count(); 15 | Dtype negative_slope = this->layer_param_.relu_param().negative_slope(); 16 | for (int i = 0; i < count; ++i) { 17 | top_data[i] = std::max(bottom_data[i], Dtype(0)) 18 | + negative_slope * std::min(bottom_data[i], Dtype(0)); 19 | } 20 | } 21 | 22 | template 23 | void ReLULayer::Backward_cpu(const vector*>& top, 24 | const vector& propagate_down, 25 | const vector*>& bottom) { 26 | if (propagate_down[0]) { 27 | const Dtype* bottom_data = bottom[0]->cpu_data(); 28 | const Dtype* top_diff = top[0]->cpu_diff(); 29 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 30 | const int count = bottom[0]->count(); 31 | Dtype negative_slope = this->layer_param_.relu_param().negative_slope(); 32 | for (int i = 0; i < count; ++i) { 33 | bottom_diff[i] = top_diff[i] * ((bottom_data[i] > 0) 34 | + negative_slope * (bottom_data[i] <= 0)); 35 | } 36 | } 37 | } 38 | 39 | 40 | #ifdef CPU_ONLY 41 | STUB_GPU(ReLULayer); 42 | #endif 43 | 44 | INSTANTIATE_CLASS(ReLULayer); 45 | 46 | } // namespace caffe 47 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/reverse_layer.cu: -------------------------------------------------------------------------------- 1 | #include "caffe/common_layers.hpp" 2 | 3 | namespace caffe { 4 | 5 | template 6 | __global__ void reverse_gpu(const int nthreads, const Dtype* from_data, Dtype* to_data, 7 | const int* counts, const int axis_count, const int axis) { 8 | CUDA_KERNEL_LOOP(index, nthreads) { 9 | int ind=(index/counts[axis])%axis_count; 10 | int to_index=counts[axis]*(axis_count-2*ind-1)+index; 11 | *(to_data+to_index)=*(from_data+index); 12 | } 13 | } 14 | 15 | template 16 | void ReverseLayer::Forward_gpu(const vector*>& bottom, 17 | const vector*>& top) { 18 | const int nthreads=bottom[0]->count(); 19 | reverse_gpu // NOLINT_NEXT_LINE(whitespace/operators) 20 | <<>>( 21 | nthreads, bottom[0]->gpu_data(), top[0]->mutable_gpu_data(), 22 | bottom_counts_.gpu_data(), bottom[0]->shape(axis_), axis_); 23 | } 24 | 25 | template 26 | void ReverseLayer::Backward_gpu(const vector*>& top, 27 | const vector& propagate_down, const vector*>& bottom) { 28 | if (!propagate_down[0]) { 29 | return; 30 | } 31 | const int nthreads=bottom[0]->count(); 32 | reverse_gpu // NOLINT_NEXT_LINE(whitespace/operators) 33 | <<>>( 34 | nthreads, top[0]->gpu_diff(), bottom[0]->mutable_gpu_diff(), 35 | bottom_counts_.gpu_data(), bottom[0]->shape(axis_), axis_); 36 | } 37 | 38 | INSTANTIATE_LAYER_GPU_FUNCS(ReverseLayer); 39 | 40 | } // namespace caffe 41 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/util/math_functions.hpp" 7 | #include "caffe/vision_layers.hpp" 8 | 9 | namespace caffe { 10 | 11 | template 12 | void SigmoidCrossEntropyLossLayer::Backward_gpu( 13 | const vector*>& top, const vector& propagate_down, 14 | const vector*>& bottom) { 15 | if (propagate_down[1]) { 16 | LOG(FATAL) << this->type() 17 | << " Layer cannot backpropagate to label inputs."; 18 | } 19 | if (propagate_down[0]) { 20 | // First, compute the diff 21 | const int count = bottom[0]->count(); 22 | const int num = bottom[0]->num(); 23 | const Dtype* sigmoid_output_data = sigmoid_output_->gpu_data(); 24 | const Dtype* target = bottom[1]->gpu_data(); 25 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 26 | caffe_copy(count, sigmoid_output_data, bottom_diff); 27 | caffe_gpu_axpy(count, Dtype(-1), target, bottom_diff); 28 | // Scale down gradient 29 | const Dtype loss_weight = top[0]->cpu_diff()[0]; 30 | caffe_gpu_scal(count, loss_weight / num, bottom_diff); 31 | } 32 | } 33 | 34 | INSTANTIATE_LAYER_GPU_BACKWARD(SigmoidCrossEntropyLossLayer); 35 | 36 | 37 | } // namespace caffe 38 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/sigmoid_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "caffe/layer.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | 10 | template 11 | inline Dtype sigmoid(Dtype x) { 12 | return 1. / (1. + exp(-x)); 13 | } 14 | 15 | template 16 | void SigmoidLayer::Forward_cpu(const vector*>& bottom, 17 | const vector*>& top) { 18 | const Dtype* bottom_data = bottom[0]->cpu_data(); 19 | Dtype* top_data = top[0]->mutable_cpu_data(); 20 | const int count = bottom[0]->count(); 21 | for (int i = 0; i < count; ++i) { 22 | top_data[i] = sigmoid(bottom_data[i]); 23 | } 24 | } 25 | 26 | template 27 | void SigmoidLayer::Backward_cpu(const vector*>& top, 28 | const vector& propagate_down, 29 | const vector*>& bottom) { 30 | if (propagate_down[0]) { 31 | const Dtype* top_data = top[0]->cpu_data(); 32 | const Dtype* top_diff = top[0]->cpu_diff(); 33 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 34 | const int count = bottom[0]->count(); 35 | for (int i = 0; i < count; ++i) { 36 | const Dtype sigmoid_x = top_data[i]; 37 | bottom_diff[i] = top_diff[i] * sigmoid_x * (1. - sigmoid_x); 38 | } 39 | } 40 | } 41 | 42 | #ifdef CPU_ONLY 43 | STUB_GPU(SigmoidLayer); 44 | #endif 45 | 46 | INSTANTIATE_CLASS(SigmoidLayer); 47 | 48 | 49 | } // namespace caffe 50 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/silence_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/common_layers.hpp" 4 | #include "caffe/layer.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void SilenceLayer::Backward_cpu(const vector*>& top, 11 | const vector& propagate_down, const vector*>& bottom) { 12 | for (int i = 0; i < bottom.size(); ++i) { 13 | if (propagate_down[i]) { 14 | caffe_set(bottom[i]->count(), Dtype(0), 15 | bottom[i]->mutable_cpu_data()); 16 | } 17 | } 18 | } 19 | 20 | #ifdef CPU_ONLY 21 | STUB_GPU(SilenceLayer); 22 | #endif 23 | 24 | INSTANTIATE_CLASS(SilenceLayer); 25 | REGISTER_LAYER_CLASS(Silence); 26 | 27 | } // namespace caffe 28 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/silence_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/common_layers.hpp" 4 | #include "caffe/layer.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void SilenceLayer::Forward_gpu(const vector*>& bottom, 11 | const vector*>& top) { 12 | // Do nothing. 13 | } 14 | 15 | template 16 | void SilenceLayer::Backward_gpu(const vector*>& top, 17 | const vector& propagate_down, const vector*>& bottom) { 18 | for (int i = 0; i < bottom.size(); ++i) { 19 | if (propagate_down[i]) { 20 | caffe_gpu_set(bottom[i]->count(), Dtype(0), 21 | bottom[i]->mutable_gpu_data()); 22 | } 23 | } 24 | } 25 | 26 | INSTANTIATE_LAYER_GPU_FUNCS(SilenceLayer); 27 | 28 | } // namespace caffe 29 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/sparse_im2col_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/common.hpp" 4 | #include "caffe/layer.hpp" 5 | #include "caffe/util/im2col.hpp" 6 | #include "caffe/vision_layers.hpp" 7 | 8 | namespace caffe { 9 | /* 10 | template 11 | void SparseIm2colLayer::Forward_gpu(const vector*>& bottom, 12 | const vector*>& top) { 13 | const Dtype* bottom_data = bottom[0]->gpu_data(); 14 | Dtype* top_data = top[0]->mutable_gpu_data(); 15 | for (int n = 0; n < bottom[0]->num(); ++n) { 16 | sparse_im2col_gpu(bottom_data + bottom[0]->offset(n), channels_, height_, 17 | width_, kernel_h_, kernel_w_, pad_h_, pad_w_, 18 | stride_h_, stride_w_, top_data + top[0]->offset(n)); 19 | } 20 | } 21 | 22 | template 23 | void SparseIm2colLayer::Backward_gpu(const vector*>& top, 24 | const vector& propagate_down, const vector*>& bottom) { 25 | const Dtype* top_diff = top[0]->cpu_diff(); 26 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 27 | for (int n = 0; n < top[0]->num(); ++n) { 28 | sparse_col2im_cpu(top_diff + top[0]->offset(n), channels_, height_, width_, 29 | kernel_h_, kernel_w_, pad_h_, pad_w_, 30 | stride_h_, stride_w_, bottom_diff + bottom[0]->offset(n)); 31 | } 32 | } 33 | 34 | INSTANTIATE_LAYER_GPU_FUNCS(SparseIm2colLayer); 35 | */ 36 | } // namespace caffe 37 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/split_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/util/math_functions.hpp" 5 | #include "caffe/vision_layers.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | void SplitLayer::Forward_gpu(const vector*>& bottom, 11 | const vector*>& top) { 12 | for (int i = 0; i < top.size(); ++i) { 13 | top[i]->ShareData(*bottom[0]); 14 | } 15 | } 16 | 17 | template 18 | void SplitLayer::Backward_gpu(const vector*>& top, 19 | const vector& propagate_down, const vector*>& bottom) { 20 | if (!propagate_down[0]) { return; } 21 | if (top.size() == 1) { 22 | caffe_copy(count_, top[0]->gpu_diff(), bottom[0]->mutable_gpu_diff()); 23 | return; 24 | } 25 | caffe_gpu_add(count_, top[0]->gpu_diff(), top[1]->gpu_diff(), 26 | bottom[0]->mutable_gpu_diff()); 27 | // Add remaining top blob diffs. 28 | for (int i = 2; i < top.size(); ++i) { 29 | const Dtype* top_diff = top[i]->gpu_diff(); 30 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 31 | caffe_gpu_axpy(count_, Dtype(1.), top_diff, bottom_diff); 32 | } 33 | } 34 | 35 | 36 | INSTANTIATE_LAYER_GPU_FUNCS(SplitLayer); 37 | 38 | } // namespace caffe 39 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/tanh_layer.cpp: -------------------------------------------------------------------------------- 1 | // TanH neuron activation function layer. 2 | // Adapted from ReLU layer code written by Yangqing Jia 3 | 4 | #include 5 | #include 6 | 7 | #include "caffe/layer.hpp" 8 | #include "caffe/vision_layers.hpp" 9 | 10 | namespace caffe { 11 | 12 | template 13 | void TanHLayer::Forward_cpu(const vector*>& bottom, 14 | const vector*>& top) { 15 | const Dtype* bottom_data = bottom[0]->cpu_data(); 16 | Dtype* top_data = top[0]->mutable_cpu_data(); 17 | const int count = bottom[0]->count(); 18 | for (int i = 0; i < count; ++i) { 19 | top_data[i] = tanh(bottom_data[i]); 20 | } 21 | } 22 | 23 | template 24 | void TanHLayer::Backward_cpu(const vector*>& top, 25 | const vector& propagate_down, 26 | const vector*>& bottom) { 27 | if (propagate_down[0]) { 28 | const Dtype* top_data = top[0]->cpu_data(); 29 | const Dtype* top_diff = top[0]->cpu_diff(); 30 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 31 | const int count = bottom[0]->count(); 32 | Dtype tanhx; 33 | for (int i = 0; i < count; ++i) { 34 | tanhx = top_data[i]; 35 | bottom_diff[i] = top_diff[i] * (1 - tanhx * tanhx); 36 | } 37 | } 38 | } 39 | 40 | #ifdef CPU_ONLY 41 | STUB_GPU(TanHLayer); 42 | #endif 43 | 44 | INSTANTIATE_CLASS(TanHLayer); 45 | 46 | } // namespace caffe 47 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/tanh_layer.cu: -------------------------------------------------------------------------------- 1 | // TanH neuron activation function layer. 2 | // Adapted from ReLU layer code written by Yangqing Jia 3 | 4 | #include 5 | #include 6 | 7 | #include "caffe/layer.hpp" 8 | #include "caffe/vision_layers.hpp" 9 | 10 | namespace caffe { 11 | 12 | template 13 | __global__ void TanHForward(const int n, const Dtype* in, Dtype* out) { 14 | CUDA_KERNEL_LOOP(index, n) { 15 | out[index] = tanh(in[index]); 16 | } 17 | } 18 | 19 | template 20 | void TanHLayer::Forward_gpu(const vector*>& bottom, 21 | const vector*>& top) { 22 | const Dtype* bottom_data = bottom[0]->gpu_data(); 23 | Dtype* top_data = top[0]->mutable_gpu_data(); 24 | const int count = bottom[0]->count(); 25 | // NOLINT_NEXT_LINE(whitespace/operators) 26 | TanHForward<<>>( 27 | count, bottom_data, top_data); 28 | CUDA_POST_KERNEL_CHECK; 29 | } 30 | 31 | template 32 | __global__ void TanHBackward(const int n, const Dtype* in_diff, 33 | const Dtype* out_data, Dtype* out_diff) { 34 | CUDA_KERNEL_LOOP(index, n) { 35 | Dtype tanhx = out_data[index]; 36 | out_diff[index] = in_diff[index] * (1 - tanhx * tanhx); 37 | } 38 | } 39 | 40 | template 41 | void TanHLayer::Backward_gpu(const vector*>& top, 42 | const vector& propagate_down, 43 | const vector*>& bottom) { 44 | if (propagate_down[0]) { 45 | const Dtype* top_data = top[0]->gpu_data(); 46 | const Dtype* top_diff = top[0]->gpu_diff(); 47 | Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); 48 | const int count = bottom[0]->count(); 49 | // NOLINT_NEXT_LINE(whitespace/operators) 50 | TanHBackward<<>>( 51 | count, top_diff, top_data, bottom_diff); 52 | CUDA_POST_KERNEL_CHECK; 53 | } 54 | } 55 | 56 | INSTANTIATE_LAYER_GPU_FUNCS(TanHLayer); 57 | 58 | 59 | } // namespace caffe 60 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/threshold_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "caffe/layer.hpp" 4 | #include "caffe/vision_layers.hpp" 5 | 6 | 7 | namespace caffe { 8 | 9 | template 10 | void ThresholdLayer::LayerSetUp(const vector*>& bottom, 11 | const vector*>& top) { 12 | NeuronLayer::LayerSetUp(bottom, top); 13 | threshold_ = this->layer_param_.threshold_param().threshold(); 14 | } 15 | 16 | template 17 | void ThresholdLayer::Forward_cpu(const vector*>& bottom, 18 | const vector*>& top) { 19 | const Dtype* bottom_data = bottom[0]->cpu_data(); 20 | Dtype* top_data = top[0]->mutable_cpu_data(); 21 | const int count = bottom[0]->count(); 22 | for (int i = 0; i < count; ++i) { 23 | top_data[i] = (bottom_data[i] > threshold_) ? Dtype(1) : Dtype(0); 24 | } 25 | } 26 | 27 | #ifdef CPU_ONLY 28 | STUB_GPU_FORWARD(ThresholdLayer, Forward); 29 | #endif 30 | 31 | INSTANTIATE_CLASS(ThresholdLayer); 32 | REGISTER_LAYER_CLASS(Threshold); 33 | 34 | } // namespace caffe 35 | -------------------------------------------------------------------------------- /caffe/src/caffe/layers/threshold_layer.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "caffe/layer.hpp" 5 | #include "caffe/vision_layers.hpp" 6 | 7 | namespace caffe { 8 | 9 | template 10 | __global__ void ThresholdForward(const int n, const Dtype threshold, 11 | const Dtype* in, Dtype* out) { 12 | CUDA_KERNEL_LOOP(index, n) { 13 | out[index] = in[index] > threshold ? 1 : 0; 14 | } 15 | } 16 | 17 | template 18 | void ThresholdLayer::Forward_gpu(const vector*>& bottom, 19 | const vector*>& top) { 20 | const Dtype* bottom_data = bottom[0]->gpu_data(); 21 | Dtype* top_data = top[0]->mutable_gpu_data(); 22 | const int count = bottom[0]->count(); 23 | // NOLINT_NEXT_LINE(whitespace/operators) 24 | ThresholdForward<<>>( 25 | count, threshold_, bottom_data, top_data); 26 | CUDA_POST_KERNEL_CHECK; 27 | } 28 | 29 | 30 | INSTANTIATE_LAYER_GPU_FORWARD(ThresholdLayer); 31 | 32 | 33 | } // namespace caffe 34 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The option allows to include in build only selected test files and exclude all others 2 | # Usage example: 3 | # cmake -DBUILD_only_tests="common,net,blob,im2col_kernel" 4 | set(BUILD_only_tests "" CACHE STRING "Blank or comma-separated list of test files to build without 'test_' prefix and extention") 5 | caffe_leave_only_selected_tests(test_srcs ${BUILD_only_tests}) 6 | caffe_leave_only_selected_tests(test_cuda ${BUILD_only_tests}) 7 | 8 | # For 'make runtest' target we don't need to embed test data paths to 9 | # source files, because test target is executed in source directory 10 | # That's why the lines below are commented. TODO: remove them 11 | 12 | # definition needed to include CMake generated files 13 | #add_definitions(-DCMAKE_BUILD) 14 | 15 | # generates test_data/sample_data_list.txt.gen.cmake 16 | #caffe_configure_testdatafile(test_data/sample_data_list.txt) 17 | 18 | set(the_target test.testbin) 19 | set(test_args --gtest_shuffle) 20 | 21 | if(HAVE_CUDA) 22 | caffe_cuda_compile(test_cuda_objs ${test_cuda}) 23 | list(APPEND test_srcs ${test_cuda_objs} ${test_cuda}) 24 | else() 25 | list(APPEND test_args --gtest_filter="-*GPU*") 26 | endif() 27 | 28 | # ---[ Adding test target 29 | add_executable(${the_target} EXCLUDE_FROM_ALL ${test_srcs}) 30 | target_link_libraries(${the_target} gtest ${Caffe_LINK}) 31 | caffe_default_properties(${the_target}) 32 | caffe_set_runtime_directory(${the_target} "${PROJECT_BINARY_DIR}/test") 33 | 34 | # ---[ Adding runtest 35 | add_custom_target(runtest COMMAND ${the_target} ${test_args} 36 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) 37 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_caffe_main.cpp: -------------------------------------------------------------------------------- 1 | // The main caffe test code. Your test cpp code should include this hpp 2 | // to allow a main function to be compiled into the binary. 3 | 4 | #include "caffe/caffe.hpp" 5 | #include "caffe/test/test_caffe_main.hpp" 6 | 7 | namespace caffe { 8 | #ifndef CPU_ONLY 9 | cudaDeviceProp CAFFE_TEST_CUDA_PROP; 10 | #endif 11 | } 12 | 13 | #ifndef CPU_ONLY 14 | using caffe::CAFFE_TEST_CUDA_PROP; 15 | #endif 16 | 17 | int main(int argc, char** argv) { 18 | ::testing::InitGoogleTest(&argc, argv); 19 | caffe::GlobalInit(&argc, &argv); 20 | #ifndef CPU_ONLY 21 | // Before starting testing, let's first print out a few cuda defice info. 22 | int device; 23 | cudaGetDeviceCount(&device); 24 | cout << "Cuda number of devices: " << device << endl; 25 | if (argc > 1) { 26 | // Use the given device 27 | device = atoi(argv[1]); 28 | cudaSetDevice(device); 29 | cout << "Setting to use device " << device << endl; 30 | } else if (CUDA_TEST_DEVICE >= 0) { 31 | // Use the device assigned in build configuration; but with a lower priority 32 | device = CUDA_TEST_DEVICE; 33 | } 34 | cudaGetDevice(&device); 35 | cout << "Current device id: " << device << endl; 36 | cudaGetDeviceProperties(&CAFFE_TEST_CUDA_PROP, device); 37 | #endif 38 | // invoke the test. 39 | return RUN_ALL_TESTS(); 40 | } 41 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_data/sample_data.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/src/caffe/test/test_data/sample_data.h5 -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_data/sample_data_2_gzip.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/src/caffe/test/test_data/sample_data_2_gzip.h5 -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_data/sample_data_list.txt: -------------------------------------------------------------------------------- 1 | src/caffe/test/test_data/sample_data.h5 2 | src/caffe/test/test_data/sample_data_2_gzip.h5 3 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_data/solver_data.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/caffe/src/caffe/test/test_data/solver_data.h5 -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_data/solver_data_list.txt: -------------------------------------------------------------------------------- 1 | src/caffe/test/test_data/solver_data.h5 2 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_internal_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "glog/logging.h" 2 | #include "gtest/gtest.h" 3 | 4 | #include "caffe/internal_thread.hpp" 5 | #include "caffe/util/math_functions.hpp" 6 | 7 | #include "caffe/test/test_caffe_main.hpp" 8 | 9 | namespace caffe { 10 | 11 | 12 | class InternalThreadTest : public ::testing::Test {}; 13 | 14 | TEST_F(InternalThreadTest, TestStartAndExit) { 15 | InternalThread thread; 16 | EXPECT_FALSE(thread.is_started()); 17 | thread.StartInternalThread(); 18 | EXPECT_TRUE(thread.is_started()); 19 | thread.StopInternalThread(); 20 | EXPECT_FALSE(thread.is_started()); 21 | } 22 | 23 | class TestThreadA : public InternalThread { 24 | void InternalThreadEntry() { 25 | EXPECT_EQ(4244559767, caffe_rng_rand()); 26 | } 27 | }; 28 | 29 | class TestThreadB : public InternalThread { 30 | void InternalThreadEntry() { 31 | EXPECT_EQ(1726478280, caffe_rng_rand()); 32 | } 33 | }; 34 | 35 | TEST_F(InternalThreadTest, TestRandomSeed) { 36 | TestThreadA t1; 37 | Caffe::set_random_seed(9658361); 38 | t1.StartInternalThread(); 39 | t1.StopInternalThread(); 40 | 41 | TestThreadA t2; 42 | Caffe::set_random_seed(9658361); 43 | t2.StartInternalThread(); 44 | t2.StopInternalThread(); 45 | 46 | TestThreadB t3; 47 | Caffe::set_random_seed(3435563); 48 | t3.StartInternalThread(); 49 | t3.StopInternalThread(); 50 | } 51 | 52 | } // namespace caffe 53 | 54 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_layer_factory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "boost/scoped_ptr.hpp" 5 | #include "gtest/gtest.h" 6 | 7 | #include "caffe/common.hpp" 8 | #include "caffe/layer.hpp" 9 | #include "caffe/layer_factory.hpp" 10 | #include "caffe/util/db.hpp" 11 | #include "caffe/util/io.hpp" 12 | 13 | #include "caffe/test/test_caffe_main.hpp" 14 | 15 | namespace caffe { 16 | 17 | template 18 | class LayerFactoryTest : public MultiDeviceTest {}; 19 | 20 | TYPED_TEST_CASE(LayerFactoryTest, TestDtypesAndDevices); 21 | 22 | TYPED_TEST(LayerFactoryTest, TestCreateLayer) { 23 | typedef typename TypeParam::Dtype Dtype; 24 | typename LayerRegistry::CreatorRegistry& registry = 25 | LayerRegistry::Registry(); 26 | shared_ptr > layer; 27 | for (typename LayerRegistry::CreatorRegistry::iterator iter = 28 | registry.begin(); iter != registry.end(); ++iter) { 29 | // Special case: PythonLayer is checked by pytest 30 | if (iter->first == "Python") { continue; } 31 | LayerParameter layer_param; 32 | // Data layers expect a DB 33 | if (iter->first == "Data") { 34 | string tmp; 35 | MakeTempDir(&tmp); 36 | boost::scoped_ptr db(db::GetDB(DataParameter_DB_LEVELDB)); 37 | db->Open(tmp, db::NEW); 38 | db->Close(); 39 | layer_param.mutable_data_param()->set_source(tmp); 40 | } 41 | layer_param.set_type(iter->first); 42 | layer = LayerRegistry::CreateLayer(layer_param); 43 | EXPECT_EQ(iter->first, layer->type()); 44 | } 45 | } 46 | 47 | } // namespace caffe 48 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_protobuf.cpp: -------------------------------------------------------------------------------- 1 | // This is simply a script that tries serializing protocol buffer in text 2 | // format. Nothing special here and no actual code is being tested. 3 | #include 4 | 5 | #include "google/protobuf/text_format.h" 6 | #include "gtest/gtest.h" 7 | 8 | #include "caffe/proto/caffe.pb.h" 9 | 10 | #include "caffe/test/test_caffe_main.hpp" 11 | 12 | namespace caffe { 13 | 14 | class ProtoTest : public ::testing::Test {}; 15 | 16 | TEST_F(ProtoTest, TestSerialization) { 17 | LayerParameter param; 18 | param.set_name("test"); 19 | param.set_type("Test"); 20 | std::cout << "Printing in binary format." << std::endl; 21 | std::cout << param.SerializeAsString() << std::endl; 22 | std::cout << "Printing in text format." << std::endl; 23 | std::string str; 24 | google::protobuf::TextFormat::PrintToString(param, &str); 25 | std::cout << str << std::endl; 26 | EXPECT_TRUE(true); 27 | } 28 | 29 | } // namespace caffe 30 | -------------------------------------------------------------------------------- /caffe/src/caffe/test/test_reverse_layer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | #include "caffe/blob.hpp" 8 | #include "caffe/common.hpp" 9 | #include "caffe/filler.hpp" 10 | #include "caffe/common_layers.hpp" 11 | 12 | #include "caffe/test/test_caffe_main.hpp" 13 | #include "caffe/test/test_gradient_check_util.hpp" 14 | 15 | namespace caffe { 16 | template 17 | class ReverseLayerTest : public MultiDeviceTest { 18 | typedef typename TypeParam::Dtype Dtype; 19 | protected: 20 | ReverseLayerTest() 21 | : blob_bottom_(new Blob(5, 2, 3, 4)), 22 | blob_top_(new Blob()) { 23 | // fill the values 24 | FillerParameter filler_param; 25 | GaussianFiller filler(filler_param); 26 | filler.Fill(this->blob_bottom_); 27 | blob_bottom_vec_.push_back(blob_bottom_); 28 | blob_top_vec_.push_back(blob_top_); 29 | } 30 | virtual ~ReverseLayerTest() { delete blob_bottom_; delete blob_top_; } 31 | Blob* const blob_bottom_; 32 | Blob* const blob_top_; 33 | vector*> blob_bottom_vec_; 34 | vector*> blob_top_vec_; 35 | }; 36 | 37 | TYPED_TEST_CASE(ReverseLayerTest, TestDtypesAndDevices); 38 | 39 | TYPED_TEST(ReverseLayerTest, TestGradient) { 40 | typedef typename TypeParam::Dtype Dtype; 41 | LayerParameter layer_param; 42 | layer_param.mutable_reverse_param()->set_axis(0); 43 | ReverseLayer layer(layer_param); 44 | GradientChecker checker(1e-2, 1e-4); 45 | checker.CheckGradientExhaustive(&layer, this->blob_bottom_vec_, 46 | this->blob_top_vec_); 47 | } 48 | 49 | } // namespace caffe 50 | -------------------------------------------------------------------------------- /caffe/src/caffe/util/cudnn.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDNN 2 | #include "caffe/util/cudnn.hpp" 3 | 4 | namespace caffe { 5 | namespace cudnn { 6 | 7 | float dataType::oneval = 1.0; 8 | float dataType::zeroval = 0.0; 9 | const void* dataType::one = 10 | static_cast(&dataType::oneval); 11 | const void* dataType::zero = 12 | static_cast(&dataType::zeroval); 13 | 14 | double dataType::oneval = 1.0; 15 | double dataType::zeroval = 0.0; 16 | const void* dataType::one = 17 | static_cast(&dataType::oneval); 18 | const void* dataType::zero = 19 | static_cast(&dataType::zeroval); 20 | 21 | } // namespace cudnn 22 | } // namespace caffe 23 | #endif 24 | -------------------------------------------------------------------------------- /caffe/src/caffe/util/db.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/util/db.hpp" 2 | #include "caffe/util/db_leveldb.hpp" 3 | #include "caffe/util/db_lmdb.hpp" 4 | 5 | #include 6 | 7 | namespace caffe { namespace db { 8 | 9 | DB* GetDB(DataParameter::DB backend) { 10 | switch (backend) { 11 | case DataParameter_DB_LEVELDB: 12 | return new LevelDB(); 13 | case DataParameter_DB_LMDB: 14 | return new LMDB(); 15 | default: 16 | LOG(FATAL) << "Unknown database backend"; 17 | } 18 | } 19 | 20 | DB* GetDB(const string& backend) { 21 | if (backend == "leveldb") { 22 | return new LevelDB(); 23 | } else if (backend == "lmdb") { 24 | return new LMDB(); 25 | } else { 26 | LOG(FATAL) << "Unknown database backend"; 27 | } 28 | } 29 | 30 | } // namespace db 31 | } // namespace caffe 32 | -------------------------------------------------------------------------------- /caffe/src/caffe/util/db_leveldb.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/util/db_leveldb.hpp" 2 | 3 | #include 4 | 5 | namespace caffe { namespace db { 6 | 7 | void LevelDB::Open(const string& source, Mode mode) { 8 | leveldb::Options options; 9 | options.block_size = 65536; 10 | options.write_buffer_size = 268435456; 11 | options.max_open_files = 100; 12 | options.error_if_exists = mode == NEW; 13 | options.create_if_missing = mode != READ; 14 | leveldb::Status status = leveldb::DB::Open(options, source, &db_); 15 | CHECK(status.ok()) << "Failed to open leveldb " << source 16 | << std::endl << status.ToString(); 17 | LOG(INFO) << "Opened leveldb " << source; 18 | } 19 | 20 | } // namespace db 21 | } // namespace caffe 22 | -------------------------------------------------------------------------------- /caffe/src/caffe/util/db_lmdb.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/util/db_lmdb.hpp" 2 | 3 | #include 4 | 5 | #include 6 | 7 | namespace caffe { namespace db { 8 | 9 | const size_t LMDB_MAP_SIZE = 1099511627776; // 1 TB 10 | 11 | void LMDB::Open(const string& source, Mode mode) { 12 | MDB_CHECK(mdb_env_create(&mdb_env_)); 13 | MDB_CHECK(mdb_env_set_mapsize(mdb_env_, LMDB_MAP_SIZE)); 14 | if (mode == NEW) { 15 | CHECK_EQ(mkdir(source.c_str(), 0744), 0) << "mkdir " << source << "failed"; 16 | } 17 | int flags = 0; 18 | if (mode == READ) { 19 | flags = MDB_RDONLY | MDB_NOTLS; 20 | } 21 | MDB_CHECK(mdb_env_open(mdb_env_, source.c_str(), flags, 0664)); 22 | LOG(INFO) << "Opened lmdb " << source; 23 | } 24 | 25 | LMDBCursor* LMDB::NewCursor() { 26 | MDB_txn* mdb_txn; 27 | MDB_cursor* mdb_cursor; 28 | MDB_CHECK(mdb_txn_begin(mdb_env_, NULL, MDB_RDONLY, &mdb_txn)); 29 | MDB_CHECK(mdb_dbi_open(mdb_txn, NULL, 0, &mdb_dbi_)); 30 | MDB_CHECK(mdb_cursor_open(mdb_txn, mdb_dbi_, &mdb_cursor)); 31 | return new LMDBCursor(mdb_txn, mdb_cursor); 32 | } 33 | 34 | LMDBTransaction* LMDB::NewTransaction() { 35 | MDB_txn* mdb_txn; 36 | MDB_CHECK(mdb_txn_begin(mdb_env_, NULL, 0, &mdb_txn)); 37 | MDB_CHECK(mdb_dbi_open(mdb_txn, NULL, 0, &mdb_dbi_)); 38 | return new LMDBTransaction(&mdb_dbi_, mdb_txn); 39 | } 40 | 41 | void LMDBTransaction::Put(const string& key, const string& value) { 42 | MDB_val mdb_key, mdb_value; 43 | mdb_key.mv_data = const_cast(key.data()); 44 | mdb_key.mv_size = key.size(); 45 | mdb_value.mv_data = const_cast(value.data()); 46 | mdb_value.mv_size = value.size(); 47 | MDB_CHECK(mdb_put(mdb_txn_, *mdb_dbi_, &mdb_key, &mdb_value, 0)); 48 | } 49 | 50 | } // namespace db 51 | } // namespace caffe 52 | -------------------------------------------------------------------------------- /caffe/src/gtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(gtest STATIC EXCLUDE_FROM_ALL gtest.h gtest-all.cpp) 2 | caffe_default_properties(gtest) 3 | 4 | #add_library(gtest_main gtest_main.cc) 5 | #target_link_libraries(gtest_main gtest) 6 | -------------------------------------------------------------------------------- /caffe/src/gtest/gtest_main.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2006, Google Inc. 2 | // All rights reserved. 3 | // 4 | // Redistribution and use in source and binary forms, with or without 5 | // modification, are permitted provided that the following conditions are 6 | // met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // * Redistributions in binary form must reproduce the above 11 | // copyright notice, this list of conditions and the following disclaimer 12 | // in the documentation and/or other materials provided with the 13 | // distribution. 14 | // * Neither the name of Google Inc. nor the names of its 15 | // contributors may be used to endorse or promote products derived from 16 | // this software without specific prior written permission. 17 | // 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | #include 31 | 32 | #include "gtest/gtest.h" 33 | 34 | GTEST_API_ int main(int argc, char **argv) { 35 | std::cout << "Running main() from gtest_main.cc\n"; 36 | 37 | testing::InitGoogleTest(&argc, argv); 38 | return RUN_ALL_TESTS(); 39 | } 40 | -------------------------------------------------------------------------------- /caffe/tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Collect source files 2 | file(GLOB_RECURSE srcs ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) 3 | 4 | # Build each source file independently 5 | foreach(source ${srcs}) 6 | get_filename_component(name ${source} NAME_WE) 7 | 8 | # caffe target already exits 9 | if(name MATCHES "caffe") 10 | set(name ${name}.bin) 11 | endif() 12 | 13 | # target 14 | add_executable(${name} ${source}) 15 | target_link_libraries(${name} ${Caffe_LINK}) 16 | caffe_default_properties(${name}) 17 | 18 | # set back RUNTIME_OUTPUT_DIRECTORY 19 | caffe_set_runtime_directory(${name} "${PROJECT_BINARY_DIR}/tools") 20 | caffe_set_solution_folder(${name} tools) 21 | 22 | # restore output name without suffix 23 | if(name MATCHES "caffe.bin") 24 | set_target_properties(${name} PROPERTIES OUTPUT_NAME caffe) 25 | endif() 26 | 27 | # Install 28 | install(TARGETS ${name} DESTINATION bin) 29 | endforeach(source) 30 | -------------------------------------------------------------------------------- /caffe/tools/device_query.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/common.hpp" 2 | 3 | int main(int argc, char** argv) { 4 | LOG(FATAL) << "Deprecated. Use caffe device_query " 5 | "[--device_id=0] instead."; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /caffe/tools/extra/launch_resize_and_crop_images.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #### https://github.com/Yangqing/mincepie/wiki/Launch-Your-Mapreducer 3 | 4 | # If you encounter error that the address already in use, kill the process. 5 | # 11235 is the port of server process 6 | # https://github.com/Yangqing/mincepie/blob/master/mincepie/mince.py 7 | # sudo netstat -ap | grep 11235 8 | # The last column of the output is PID/Program name 9 | # kill -9 PID 10 | # Second solution: 11 | # nmap localhost 12 | # fuser -k 11235/tcp 13 | # Or just wait a few seconds. 14 | 15 | ## Launch your Mapreduce locally 16 | # num_clients: number of processes 17 | # image_lib: OpenCV or PIL, case insensitive. The default value is the faster OpenCV. 18 | # input: the file containing one image path relative to input_folder each line 19 | # input_folder: where are the original images 20 | # output_folder: where to save the resized and cropped images 21 | ./resize_and_crop_images.py --num_clients=8 --image_lib=opencv --input=/home/user/Datasets/ImageNet/ILSVRC2010/ILSVRC2010_images.txt --input_folder=/home/user/Datasets/ImageNet/ILSVRC2010/ILSVRC2010_images_train/ --output_folder=/home/user/Datasets/ImageNet/ILSVRC2010/ILSVRC2010_images_train_resized/ 22 | 23 | ## Launch your Mapreduce with MPI 24 | # mpirun -n 8 --launch=mpi resize_and_crop_images.py --image_lib=opencv --input=/home/user/Datasets/ImageNet/ILSVRC2010/ILSVRC2010_images.txt --input_folder=/home/user/Datasets/ImageNet/ILSVRC2010/ILSVRC2010_images_train/ --output_folder=/home/user/Datasets/ImageNet/ILSVRC2010/ILSVRC2010_images_train_resized/ 25 | -------------------------------------------------------------------------------- /caffe/tools/extra/parse_log.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Usage parse_log.sh caffe.log 3 | # It creates the following two text files, each containing a table: 4 | # caffe.log.test (columns: '#Iters Seconds TestAccuracy TestLoss') 5 | # caffe.log.train (columns: '#Iters Seconds TrainingLoss LearningRate') 6 | 7 | 8 | # get the dirname of the script 9 | DIR="$( cd "$(dirname "$0")" ; pwd -P )" 10 | 11 | if [ "$#" -lt 1 ] 12 | then 13 | echo "Usage parse_log.sh /path/to/your.log" 14 | exit 15 | fi 16 | LOG=`basename $1` 17 | grep -B 1 'Test ' $1 > aux.txt 18 | grep 'Iteration ' aux.txt | sed 's/.*Iteration \([[:digit:]]*\).*/\1/g' > aux0.txt 19 | grep 'Test net output #0' aux.txt | awk '{print $11}' > aux1.txt 20 | grep 'Test net output #1' aux.txt | awk '{print $11}' > aux2.txt 21 | 22 | # Extracting elapsed seconds 23 | # For extraction of time since this line contains the start time 24 | grep '] Solving ' $1 > aux3.txt 25 | grep 'Testing net' $1 >> aux3.txt 26 | $DIR/extract_seconds.py aux3.txt aux4.txt 27 | 28 | # Generating 29 | echo '#Iters Seconds TestAccuracy TestLoss'> $LOG.test 30 | paste aux0.txt aux4.txt aux1.txt aux2.txt | column -t >> $LOG.test 31 | rm aux.txt aux0.txt aux1.txt aux2.txt aux3.txt aux4.txt 32 | 33 | # For extraction of time since this line contains the start time 34 | grep '] Solving ' $1 > aux.txt 35 | grep ', loss = ' $1 >> aux.txt 36 | grep 'Iteration ' aux.txt | sed 's/.*Iteration \([[:digit:]]*\).*/\1/g' > aux0.txt 37 | grep ', loss = ' $1 | awk '{print $9}' > aux1.txt 38 | grep ', lr = ' $1 | awk '{print $9}' > aux2.txt 39 | 40 | # Extracting elapsed seconds 41 | $DIR/extract_seconds.py aux.txt aux3.txt 42 | 43 | # Generating 44 | echo '#Iters Seconds TrainingLoss LearningRate'> $LOG.train 45 | paste aux0.txt aux3.txt aux1.txt aux2.txt | column -t >> $LOG.train 46 | rm aux.txt aux0.txt aux1.txt aux2.txt aux3.txt 47 | -------------------------------------------------------------------------------- /caffe/tools/finetune_net.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/caffe.hpp" 2 | 3 | int main(int argc, char** argv) { 4 | LOG(FATAL) << "Deprecated. Use caffe train --solver=... " 5 | "[--weights=...] instead."; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /caffe/tools/net_speed_benchmark.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/caffe.hpp" 2 | 3 | int main(int argc, char** argv) { 4 | LOG(FATAL) << "Deprecated. Use caffe time --model=... " 5 | "[--iterations=50] [--gpu] [--device_id=0]"; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /caffe/tools/test_net.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/caffe.hpp" 2 | 3 | int main(int argc, char** argv) { 4 | LOG(FATAL) << "Deprecated. Use caffe test --model=... " 5 | "--weights=... instead."; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /caffe/tools/train_net.cpp: -------------------------------------------------------------------------------- 1 | #include "caffe/caffe.hpp" 2 | 3 | int main(int argc, char** argv) { 4 | LOG(FATAL) << "Deprecated. Use caffe train --solver=... " 5 | "[--snapshot=...] instead."; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /caffe/tools/upgrade_net_proto_binary.cpp: -------------------------------------------------------------------------------- 1 | // This is a script to upgrade "V0" network prototxts to the new format. 2 | // Usage: 3 | // upgrade_net_proto_binary v0_net_proto_file_in net_proto_file_out 4 | 5 | #include 6 | #include // NOLINT(readability/streams) 7 | #include // NOLINT(readability/streams) 8 | #include 9 | 10 | #include "caffe/caffe.hpp" 11 | #include "caffe/util/io.hpp" 12 | #include "caffe/util/upgrade_proto.hpp" 13 | 14 | using std::ofstream; 15 | 16 | using namespace caffe; // NOLINT(build/namespaces) 17 | 18 | int main(int argc, char** argv) { 19 | ::google::InitGoogleLogging(argv[0]); 20 | if (argc != 3) { 21 | LOG(ERROR) << "Usage: " 22 | << "upgrade_net_proto_binary v0_net_proto_file_in net_proto_file_out"; 23 | return 1; 24 | } 25 | 26 | NetParameter net_param; 27 | string input_filename(argv[1]); 28 | if (!ReadProtoFromBinaryFile(input_filename, &net_param)) { 29 | LOG(ERROR) << "Failed to parse input binary file as NetParameter: " 30 | << input_filename; 31 | return 2; 32 | } 33 | bool need_upgrade = NetNeedsUpgrade(net_param); 34 | bool success = true; 35 | if (need_upgrade) { 36 | success = UpgradeNetAsNeeded(input_filename, &net_param); 37 | if (!success) { 38 | LOG(ERROR) << "Encountered error(s) while upgrading prototxt; " 39 | << "see details above."; 40 | } 41 | } else { 42 | LOG(ERROR) << "File already in V1 proto format: " << argv[1]; 43 | } 44 | 45 | WriteProtoToBinaryFile(net_param, argv[2]); 46 | 47 | LOG(ERROR) << "Wrote upgraded NetParameter binary proto to " << argv[2]; 48 | return !success; 49 | } 50 | -------------------------------------------------------------------------------- /caffe/tools/upgrade_net_proto_text.cpp: -------------------------------------------------------------------------------- 1 | // This is a script to upgrade "V0" network prototxts to the new format. 2 | // Usage: 3 | // upgrade_net_proto_text v0_net_proto_file_in net_proto_file_out 4 | 5 | #include 6 | #include // NOLINT(readability/streams) 7 | #include // NOLINT(readability/streams) 8 | #include 9 | 10 | #include "caffe/caffe.hpp" 11 | #include "caffe/util/io.hpp" 12 | #include "caffe/util/upgrade_proto.hpp" 13 | 14 | using std::ofstream; 15 | 16 | using namespace caffe; // NOLINT(build/namespaces) 17 | 18 | int main(int argc, char** argv) { 19 | ::google::InitGoogleLogging(argv[0]); 20 | if (argc != 3) { 21 | LOG(ERROR) << "Usage: " 22 | << "upgrade_net_proto_text v0_net_proto_file_in net_proto_file_out"; 23 | return 1; 24 | } 25 | 26 | NetParameter net_param; 27 | string input_filename(argv[1]); 28 | if (!ReadProtoFromTextFile(input_filename, &net_param)) { 29 | LOG(ERROR) << "Failed to parse input text file as NetParameter: " 30 | << input_filename; 31 | return 2; 32 | } 33 | bool need_upgrade = NetNeedsUpgrade(net_param); 34 | bool need_data_upgrade = NetNeedsDataUpgrade(net_param); 35 | bool success = true; 36 | if (need_upgrade) { 37 | success = UpgradeNetAsNeeded(input_filename, &net_param); 38 | if (!success) { 39 | LOG(ERROR) << "Encountered error(s) while upgrading prototxt; " 40 | << "see details above."; 41 | } 42 | } else { 43 | LOG(ERROR) << "File already in latest proto format: " << input_filename; 44 | } 45 | 46 | if (need_data_upgrade) { 47 | UpgradeNetDataTransformation(&net_param); 48 | } 49 | 50 | // Save new format prototxt. 51 | WriteProtoToTextFile(net_param, argv[2]); 52 | 53 | LOG(ERROR) << "Wrote upgraded NetParameter text proto to " << argv[2]; 54 | return !success; 55 | } 56 | -------------------------------------------------------------------------------- /demo_images/img_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/demo_images/img_1.jpg -------------------------------------------------------------------------------- /demo_images/img_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/demo_images/img_2.jpg -------------------------------------------------------------------------------- /demo_images/img_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianzhi0549/CTPN/f77ec9e7c4c010c8834cf0f87111370b14a00f33/demo_images/img_3.jpg -------------------------------------------------------------------------------- /src/layers/__init__.py: -------------------------------------------------------------------------------- 1 | __author__ = 'tianzhi' 2 | -------------------------------------------------------------------------------- /src/layers/text_proposal_layer.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import yaml, caffe 3 | from other import clip_boxes 4 | from anchor import AnchorText 5 | 6 | 7 | class ProposalLayer(caffe.Layer): 8 | def setup(self, bottom, top): 9 | # parse the layer parameter string, which must be valid YAML 10 | layer_params = yaml.load(self.param_str_) 11 | 12 | self._feat_stride = layer_params['feat_stride'] 13 | self.anchor_generator=AnchorText() 14 | self._num_anchors = self.anchor_generator.anchor_num 15 | 16 | top[0].reshape(1, 4) 17 | top[1].reshape(1, 1, 1, 1) 18 | 19 | def forward(self, bottom, top): 20 | assert bottom[0].data.shape[0]==1, \ 21 | 'Only single item batches are supported' 22 | 23 | scores = bottom[0].data[:, self._num_anchors:, :, :] 24 | 25 | bbox_deltas = bottom[1].data 26 | im_info = bottom[2].data[0, :] 27 | height, width = scores.shape[-2:] 28 | 29 | anchors=self.anchor_generator.locate_anchors((height, width), self._feat_stride) 30 | 31 | scores=scores.transpose((0, 2, 3, 1)).reshape(-1, 1) 32 | bbox_deltas=bbox_deltas.transpose((0, 2, 3, 1)).reshape((-1, 2)) 33 | 34 | proposals=self.anchor_generator.apply_deltas_to_anchors(bbox_deltas, anchors) 35 | 36 | # clip the proposals in excess of the boundaries of the image 37 | proposals=clip_boxes(proposals, im_info[:2]) 38 | 39 | blob=proposals.astype(np.float32, copy=False) 40 | top[0].reshape(*(blob.shape)) 41 | top[0].data[...]=blob 42 | 43 | top[1].reshape(*(scores.shape)) 44 | top[1].data[...]=scores 45 | 46 | def backward(self, top, propagate_down, bottom): 47 | pass 48 | 49 | def reshape(self, bottom, top): 50 | pass 51 | -------------------------------------------------------------------------------- /src/utils/__init__.py: -------------------------------------------------------------------------------- 1 | __author__ = 'zhitian' 2 | -------------------------------------------------------------------------------- /src/utils/timer.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------- 2 | # Fast R-CNN 3 | # Copyright (c) 2015 Microsoft 4 | # Licensed under The MIT License [see LICENSE for details] 5 | # Written by Ross Girshick 6 | # -------------------------------------------------------- 7 | 8 | import time 9 | 10 | class Timer(object): 11 | """A simple timer.""" 12 | def __init__(self): 13 | self.total_time = 0. 14 | self.calls = 0 15 | self.start_time = 0. 16 | self.diff = 0. 17 | self.average_time = 0. 18 | 19 | def tic(self): 20 | # using time.time instead of time.clock because time time.clock 21 | # does not normalize for multithreading 22 | self.start_time = time.time() 23 | 24 | def toc(self, average=True): 25 | self.diff = time.time() - self.start_time 26 | self.total_time += self.diff 27 | self.calls += 1 28 | self.average_time = self.total_time / self.calls 29 | if average: 30 | return self.average_time 31 | else: 32 | return self.diff 33 | -------------------------------------------------------------------------------- /tools/cfg.py: -------------------------------------------------------------------------------- 1 | # MUST be imported firstly 2 | import sys 3 | import numpy as np 4 | 5 | class Config: 6 | MEAN=np.float32([102.9801, 115.9465, 122.7717]) 7 | TEST_GPU_ID=0 8 | SCALE=600 9 | MAX_SCALE=1000 10 | 11 | LINE_MIN_SCORE=0.7 12 | TEXT_PROPOSALS_MIN_SCORE=0.7 13 | TEXT_PROPOSALS_NMS_THRESH=0.3 14 | MAX_HORIZONTAL_GAP=50 15 | TEXT_LINE_NMS_THRESH=0.3 16 | MIN_NUM_PROPOSALS=2 17 | MIN_RATIO=1.2 18 | MIN_V_OVERLAPS=0.7 19 | MIN_SIZE_SIM=0.7 20 | TEXT_PROPOSALS_WIDTH=16 21 | 22 | def init(): 23 | sys.path.insert(0, "./tools") 24 | sys.path.insert(0, "./caffe/python") 25 | sys.path.insert(0, "./src") 26 | init() 27 | --------------------------------------------------------------------------------