├── 2016-09-29-your-filename.md ├── LICENSE ├── README.md ├── docs └── wiki │ ├── caffe_comparison.png │ ├── caffe_network_mnist.png │ └── ex1_hellocaffe │ ├── main.png │ └── project.png └── examples ├── .directory ├── ex0_cudatest ├── CMakeLists.txt ├── axpygpu.cu ├── axpygpu.h └── main.cpp ├── ex1_hellocaffe ├── CMakeLists.txt └── main.cpp ├── ex2_blob ├── CMakeLists.txt ├── ex_blob.cpp └── ex_math.cpp ├── ex3_dataset ├── CMakeLists.txt └── ex_dataset.cpp ├── ex4_layer ├── CMakeLists.txt └── main.cpp └── ex5_net ├── CMakeLists.txt ├── bindb.cpp ├── logreg_test.cpp └── logreg_train.cpp /2016-09-29-your-filename.md: -------------------------------------------------------------------------------- 1 | ## A New Post 2 | 3 | Enter text in [Markdown](http://daringfireball.net/projects/markdown/). Use the toolbar above, or click the **?** button for formatting help. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, koosyong 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 met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 본 저장소는 [딥러닝 오픈소스 스터디 그룹](https://github.com/DeepLearningStudy)으로 통합되고 [caffe 저장소](https://github.com/DeepLearningStudy/caffe)로 옮겨졌습니다. 2 | 3 | [Wiki에 있는 자료](https://github.com/koosyong/caffestudy/wiki) 역시 스터디 그룹 [깃헙 페이지](http://deeplearningstudy.github.io/material/)로 옮겨져서 계속 업데이트 중 입니다. 4 | 5 | 앞으로도 많은 관심과 참여 부탁드립니다. 6 | -------------------------------------------------------------------------------- /docs/wiki/caffe_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koosyong/caffestudy/23396cfaabe17443d554f3fdf7a6bc29176597a1/docs/wiki/caffe_comparison.png -------------------------------------------------------------------------------- /docs/wiki/caffe_network_mnist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koosyong/caffestudy/23396cfaabe17443d554f3fdf7a6bc29176597a1/docs/wiki/caffe_network_mnist.png -------------------------------------------------------------------------------- /docs/wiki/ex1_hellocaffe/main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koosyong/caffestudy/23396cfaabe17443d554f3fdf7a6bc29176597a1/docs/wiki/ex1_hellocaffe/main.png -------------------------------------------------------------------------------- /docs/wiki/ex1_hellocaffe/project.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koosyong/caffestudy/23396cfaabe17443d554f3fdf7a6bc29176597a1/docs/wiki/ex1_hellocaffe/project.png -------------------------------------------------------------------------------- /examples/.directory: -------------------------------------------------------------------------------- 1 | [Dolphin] 2 | Timestamp=2015,8,4,16,23,37 3 | Version=3 4 | -------------------------------------------------------------------------------- /examples/ex0_cudatest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.4.6) 2 | 3 | project (cudatest) 4 | 5 | ## CUDA 6 | FIND_PACKAGE(CUDA REQUIRED) 7 | INCLUDE(FindCUDA) 8 | 9 | set (SOURCES 10 | main.cpp 11 | axpygpu.cu 12 | ) 13 | 14 | set (HEADERS 15 | axpygpu.h 16 | ) 17 | 18 | CUDA_ADD_LIBRARY (axpygpulib 19 | axpygpu.cu 20 | axpygpu.h 21 | ) 22 | 23 | CUDA_ADD_EXECUTABLE(cudatest axpygpu.cu) 24 | ADD_EXECUTABLE (cudatest ${SOURCES} ${HEADERS}) 25 | TARGET_LINK_LIBRARIES (cudatest) 26 | -------------------------------------------------------------------------------- /examples/ex0_cudatest/axpygpu.cu: -------------------------------------------------------------------------------- 1 | #include "axpygpu.h" 2 | 3 | __global__ void axpy(float a, float *x, float *y) 4 | { 5 | int i = blockIdx.x*blockDim.x + threadIdx.x; 6 | y[i] = a*x[i] + y[i]; 7 | } 8 | 9 | AXPYGPU::AXPYGPU(int n_block_, int n_thread_, float a_) 10 | :n_block(n_block_), n_thread(n_thread_), a(a_) 11 | { 12 | n = n_block * n_thread; 13 | cudaMalloc((void **) &x, n*sizeof(float)); 14 | cudaMalloc((void **) &y, n*sizeof(float)); 15 | } 16 | 17 | void AXPYGPU::compute(float* x_, float* y_, float* z_) 18 | { 19 | cudaMemcpy(x, x_, n*sizeof(float), cudaMemcpyHostToDevice); 20 | cudaMemcpy(y, y_, n*sizeof(float), cudaMemcpyHostToDevice); 21 | 22 | axpy<<>>(a,x,y); 23 | 24 | cudaMemcpy(z_, y, n*sizeof(float), cudaMemcpyDeviceToHost); 25 | 26 | cudaFree(x); 27 | cudaFree(y); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /examples/ex0_cudatest/axpygpu.h: -------------------------------------------------------------------------------- 1 | #ifndef AXPYGPU_H 2 | #define AXPYGPU_H 3 | 4 | #include 5 | #include 6 | 7 | class AXPYGPU 8 | { 9 | public: 10 | AXPYGPU() {} 11 | AXPYGPU(int n_block_, int n_thread_, float a_); 12 | ~AXPYGPU() {} 13 | 14 | int n_block, n_thread, n; 15 | float a; 16 | float *x; 17 | float *y; 18 | 19 | void compute(float* x_, float* y_, float* z_); 20 | }; 21 | 22 | #endif // AXPYGPU_H 23 | -------------------------------------------------------------------------------- /examples/ex0_cudatest/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "axpygpu.h" 3 | 4 | using namespace std; 5 | 6 | int main (int argc, char** argv) 7 | { 8 | int n_block = 10; 9 | int n_thread = 5; 10 | int a = 2.0; 11 | AXPYGPU axpy_gpu(n_block, n_thread, a); 12 | 13 | int n = n_block*n_thread; 14 | float *x, *y, *z; 15 | x = new float[n]; 16 | y = new float[n]; 17 | z = new float[n]; 18 | 19 | for(int i=0;i 2 | 3 | #include "caffe/caffe.hpp" 4 | #include "caffe/util/io.hpp" 5 | #include "caffe/blob.hpp" 6 | #include "caffe/common.hpp" 7 | #include "caffe/filler.hpp" 8 | 9 | using namespace caffe; 10 | using namespace std; 11 | typedef double Dtype; 12 | 13 | int main(int argc, char** argv) { 14 | Blob* const blob = new Blob(20, 30, 40, 50); 15 | if(blob){ 16 | cout<<"Size of blob:"; 17 | cout<<" N="<num(); 18 | cout<<" K="<channels(); 19 | cout<<" H="<height(); 20 | cout<<" W="<width(); 21 | cout<<" C="<count(); 22 | cout< filler(filler_param); 30 | filler.Fill(blob); 31 | 32 | // absolute sum of blob 33 | Dtype expected_asum = 0; 34 | const Dtype* data = blob->cpu_data(); 35 | for (int i = 0; i < blob->count(); ++i) { 36 | expected_asum += fabs(data[i]); 37 | } 38 | cout<<"expected asum of blob: "<asum_data()<* const blob = new Blob(20, 30, 40, 50); 20 | if(blob){ 21 | cout<<"Size of blob:"; 22 | cout<<" N="<num(); 23 | cout<<" K="<channels(); 24 | cout<<" H="<height(); 25 | cout<<" W="<width(); 26 | cout<<" C="<count(); 27 | cout<Reshape(50, 40, 30, 20); 32 | if(blob){ 33 | cout<<"Size of reshaped blob:"; 34 | cout<<" N="<num(); 35 | cout<<" K="<channels(); 36 | cout<<" H="<height(); 37 | cout<<" W="<width(); 38 | cout<<" C="<count(); 39 | cout< filler(filler_param); 47 | filler.Fill(blob); 48 | 49 | //// sum of squares 50 | // access data on the host 51 | Dtype expected_sumsq = 0; 52 | const Dtype* data = blob->cpu_data(); 53 | for (int i = 0; i < blob->count(); ++i) { 54 | expected_sumsq += data[i] * data[i]; 55 | } 56 | cout<sumsq_data()<gpu_data(); // memcopy host to device (to_gpu() in syncedmem.cpp) 67 | tEnd = clock(); 68 | COMPTIME("cpu->gpu time"); 69 | 70 | tStart = clock(); 71 | cout<<"sumsq of blob on gpu: "<sumsq_data()<gpu_data(); // no data copy since both have up-to-date contents. 79 | tEnd = clock(); 80 | COMPTIME("cpu->gpu time"); 81 | 82 | // gpu data manipulation 83 | const Dtype kDataScaleFactor = 2; 84 | blob->scale_data(kDataScaleFactor); // change data on gpu 85 | 86 | tStart = clock(); 87 | blob->cpu_data(); // memcopy device to host (to_cpu() in syncedmem.cpp) 88 | tEnd = clock(); 89 | COMPTIME("gpu->cpu time"); 90 | 91 | tStart = clock(); 92 | cout<<"sumsq of blob on gpu: "<sumsq_data()<* blob_in = new Blob(20, 30, 40, 50); 15 | Blob* blob_out = new Blob(20, 30, 40, 50); 16 | int n = blob_in->count(); 17 | 18 | // random number generation 19 | caffe_gpu_rng_uniform(n, -3, 3, blob_in->mutable_gpu_data()); 20 | 21 | // asum 22 | Dtype asum; 23 | caffe_gpu_asum(n, blob_in->gpu_data(), &asum); 24 | cout<<"asum: "<(n, blob_in->gpu_data(), blob_out->mutable_gpu_data()); 28 | caffe_gpu_sgnbit(n, blob_in->gpu_data(), blob_out->mutable_gpu_data()); 29 | caffe_gpu_abs(n, blob_in->gpu_data(), blob_out->mutable_gpu_data()); 30 | caffe_gpu_scale(n, 10, blob_in->gpu_data(), blob_out->mutable_gpu_data()); 31 | 32 | // 33 | // caffe_gpu_gemm 34 | // caffe_gpu_gemv 35 | // caffe_gpu_axpy 36 | // caffe_gpu_axpby 37 | // caffe_gpu_add_scalar 38 | // caffe_gpu_add 39 | // caffe_gpu_sub 40 | // caffe_gpu_mul 41 | // caffe_gpu_div 42 | // caffe_gpu_exp 43 | // caffe_gpu_powx 44 | // caffe_copy 45 | 46 | const Dtype* x = blob_out->cpu_data(); 47 | for (int i = 0; i < n; ++i) cout< 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | #include "caffe/caffe.hpp" 12 | #include "caffe/util/io.hpp" 13 | #include "caffe/blob.hpp" 14 | #include "caffe/common.hpp" 15 | #include "caffe/filler.hpp" 16 | 17 | using namespace caffe; 18 | using namespace std; 19 | using namespace cv; 20 | 21 | typedef double Dtype; 22 | 23 | uint32_t swap_endian(uint32_t val) { 24 | val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); 25 | return (val << 16) | (val >> 16); 26 | } 27 | 28 | int main(int argc, char** argv) { 29 | 30 | const char* image_filename = "/home/koosy/caffe/caffe-rc2/data/mnist/train-images-idx3-ubyte"; 31 | const char* label_filename = "/home/koosy/caffe/caffe-rc2/data/mnist/train-labels-idx1-ubyte"; 32 | 33 | // Open files 34 | std::ifstream image_file(image_filename, std::ios::in | std::ios::binary); 35 | std::ifstream label_file(label_filename, std::ios::in | std::ios::binary); 36 | 37 | CHECK(image_file) << "Unable to open file " << image_filename; 38 | CHECK(label_file) << "Unable to open file " << label_filename; 39 | 40 | // Read the magic and the meta data 41 | uint32_t magic; 42 | uint32_t num_items; 43 | uint32_t num_labels; 44 | uint32_t rows; 45 | uint32_t cols; 46 | 47 | image_file.read(reinterpret_cast(&magic), 4); 48 | magic = swap_endian(magic); 49 | CHECK_EQ(magic, 2051) << "Incorrect image file magic."; 50 | label_file.read(reinterpret_cast(&magic), 4); 51 | magic = swap_endian(magic); 52 | CHECK_EQ(magic, 2049) << "Incorrect label file magic."; 53 | 54 | image_file.read(reinterpret_cast(&num_items), 4); 55 | num_items = swap_endian(num_items); 56 | label_file.read(reinterpret_cast(&num_labels), 4); 57 | num_labels = swap_endian(num_labels); 58 | CHECK_EQ(num_items, num_labels); 59 | image_file.read(reinterpret_cast(&rows), 4); 60 | rows = swap_endian(rows); 61 | image_file.read(reinterpret_cast(&cols), 4); 62 | cols = swap_endian(cols); 63 | 64 | cout << "A total of " << num_items << " items." << endl; 65 | cout << "Rows: " << rows << " Cols: " << cols << endl; 66 | char label; 67 | char* pixels = new char[rows * cols]; 68 | 69 | for (int item_id = 0; item_id < num_items; ++item_id) { 70 | image_file.read(pixels, rows * cols); 71 | label_file.read(&label, 1); 72 | // use pixels 73 | // use label 74 | } 75 | delete pixels; 76 | 77 | return 0; 78 | } 79 | 80 | 81 | -------------------------------------------------------------------------------- /examples/ex4_layer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.8) 2 | 3 | project (test_layer) 4 | 5 | find_package(Caffe) 6 | include_directories(${Caffe_INCLUDE_DIRS}) 7 | add_definitions(${Caffe_DEFINITIONS}) 8 | 9 | set(headers 10 | ) 11 | 12 | set(sources 13 | main.cpp 14 | ) 15 | 16 | add_executable(test_layer ${headers} ${sources}) 17 | target_link_libraries(test_layer ${Caffe_LIBRARIES}) 18 | -------------------------------------------------------------------------------- /examples/ex4_layer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "caffe/caffe.hpp" 10 | #include "caffe/blob.hpp" 11 | #include "caffe/common.hpp" 12 | #include "caffe/filler.hpp" 13 | 14 | using namespace caffe; 15 | using namespace std; 16 | typedef double Dtype; 17 | 18 | // parameters 19 | int nTrainData = 200; 20 | int nTestData = 100; 21 | int dim = 2; 22 | int clas = 2; 23 | Dtype mean_0 = -1; 24 | Dtype str_0 = 0.7; 25 | Dtype mean_1 = 1; 26 | Dtype str_1 = 0.7; 27 | int nIter = 1000; 28 | 29 | int main(int argc, char** argv) { 30 | Caffe::set_mode(Caffe::CPU); 31 | 32 | // generate training data from two 2-d gaussians 33 | Blob* const blob_train_data_ = new Blob(nTrainData, dim, 1, 1); 34 | Blob* const blob_train_label_ = new Blob(nTrainData, 1, 1, 1); 35 | 36 | caffe_rng_gaussian(nTrainData/2*dim, mean_0, str_0, blob_train_data_->mutable_cpu_data()); 37 | caffe_rng_gaussian(nTrainData/2*dim, mean_1, str_1, blob_train_data_->mutable_cpu_data()+blob_train_data_->offset(nTrainData/2,0,0,0)); 38 | for(int n=0; nmutable_cpu_data()[n] = 0; 40 | else blob_train_label_->mutable_cpu_data()[n] = 1; 41 | } 42 | 43 | // show blob label and data 44 | // for (int n=0;nmutable_cpu_data()[blob_train_label_->offset(n,0,0.0)]<<": "; 46 | // for(int k=0;kmutable_cpu_data()[blob_train_data_->offset(n,k,0,0)]<<" "; 48 | // } 49 | // cout<*> blob_bottom_ip_vec_; 54 | vector*> blob_top_ip_vec_; 55 | Blob* const blob_top_ip_ = new Blob(); 56 | 57 | blob_bottom_ip_vec_.push_back(blob_train_data_); 58 | blob_top_ip_vec_.push_back(blob_top_ip_); 59 | 60 | LayerParameter layer_ip_param; 61 | layer_ip_param.mutable_inner_product_param()->set_num_output(clas); 62 | layer_ip_param.mutable_inner_product_param()->mutable_weight_filler()->set_type("xavier"); 63 | layer_ip_param.mutable_inner_product_param()->mutable_bias_filler()->set_type("constant"); 64 | 65 | InnerProductLayer layer_ip(layer_ip_param); 66 | layer_ip.SetUp(blob_bottom_ip_vec_, blob_top_ip_vec_); 67 | 68 | // set softmax loss layer 69 | vector*> blob_bottom_loss_vec_; 70 | vector*> blob_top_loss_vec_; 71 | Blob* const blob_top_loss_ = new Blob(); 72 | 73 | blob_bottom_loss_vec_.push_back(blob_top_ip_); 74 | blob_bottom_loss_vec_.push_back(blob_train_label_); 75 | blob_top_loss_vec_.push_back(blob_top_loss_); 76 | 77 | LayerParameter layer_loss_param; 78 | SoftmaxWithLossLayer layer_loss(layer_loss_param); 79 | layer_loss.SetUp(blob_bottom_loss_vec_, blob_top_loss_vec_);\ 80 | 81 | // forward and backward iteration 82 | for(int n=0;n backpro_vec; 90 | backpro_vec.push_back(1); 91 | backpro_vec.push_back(0); 92 | layer_loss.Backward(blob_top_loss_vec_, backpro_vec, blob_bottom_loss_vec_); 93 | layer_ip.Backward(blob_top_ip_vec_, backpro_vec, blob_bottom_ip_vec_); 94 | 95 | // update weights of layer_ip 96 | Dtype rate = 0.1; 97 | vector > > param = layer_ip.blobs(); 98 | caffe_scal(param[0]->count(), rate, param[0]->mutable_cpu_diff()); 99 | param[0]->Update(); 100 | 101 | // show weight params and derv of the ip layer 102 | // for(int i=0;icount();i++){ 103 | // cout<cpu_data()[i]<<", diff: "<mutable_cpu_diff()[i]<* const blob_test_data_ = new Blob(nTestData, dim, 1, 1); 110 | Blob* const blob_test_label_ = new Blob(nTestData, 1, 1, 1); 111 | caffe_rng_gaussian(nTestData/2*dim, -1, 0.7, blob_test_data_->mutable_cpu_data()); 112 | caffe_rng_gaussian(nTestData/2*dim, 1, 0.7, blob_test_data_->mutable_cpu_data()+blob_test_data_->offset(nTestData/2,0,0,0)); 113 | for(int n=0; nmutable_cpu_data()[n] = 0; 115 | else blob_test_label_->mutable_cpu_data()[n] = 1; 116 | } 117 | 118 | // prediction 119 | vector*> blob_bottom_ip_test_vec_; 120 | vector*> blob_top_ip_test_vec_; 121 | Blob* const blob_top_ip_test_ = new Blob(); 122 | 123 | blob_bottom_ip_test_vec_.push_back(blob_test_data_); 124 | blob_top_ip_test_vec_.push_back(blob_top_ip_test_); 125 | 126 | layer_ip.Reshape(blob_bottom_ip_test_vec_, blob_top_ip_test_vec_); 127 | layer_ip.Forward(blob_bottom_ip_test_vec_, blob_top_ip_test_vec_); 128 | 129 | // evaluation 130 | int score = 0; 131 | for (int n=0;nmutable_cpu_data()[blob_train_label_->offset(n,0,0.0)]; 133 | // argmax evaluate 134 | Dtype score_0 = blob_top_ip_test_->mutable_cpu_data()[blob_top_ip_test_->offset(n,0,0,0)]; 135 | Dtype score_1 = blob_top_ip_test_->mutable_cpu_data()[blob_top_ip_test_->offset(n,1,0,0)]; 136 | 137 | if((score_0 > score_1) && (label == 0)) score ++; 138 | if((score_0 < score_1) && (label == 1)) score ++; 139 | 140 | // cout<<"label "< score_1) cout<<"predict: 0"< 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "caffe/caffe.hpp" 10 | #include "caffe/blob.hpp" 11 | #include "caffe/common.hpp" 12 | #include "caffe/filler.hpp" 13 | #include "caffe/net.hpp" 14 | #include 15 | #include 16 | 17 | using namespace caffe; 18 | using namespace std; 19 | typedef double Dtype; 20 | 21 | int main(int argc, char** argv) { 22 | if (argc != 7){ 23 | cout<<"option: name nData mean0 str0 mean1 str1"<* const blob_data = new Blob(nData, dim, 1, 1); 36 | caffe_rng_gaussian(nData/2*dim, mean_0, str_0, blob_data->mutable_cpu_data()); 37 | caffe_rng_gaussian(nData/2*dim, mean_1, str_1, blob_data->mutable_cpu_data()+blob_data->offset(nData/2,0,0,0)); 38 | 39 | // open leveldb 40 | leveldb::DB* db; 41 | leveldb::Options options; 42 | options.error_if_exists = true; 43 | options.create_if_missing = true; 44 | options.write_buffer_size = 268435456; 45 | leveldb::WriteBatch* batch = NULL; 46 | leveldb::Status status = leveldb::DB::Open(options, path, &db); 47 | CHECK(status.ok()) << "Failed to open leveldb " << path 48 | << ". Is it already existing?"; 49 | batch = new leveldb::WriteBatch(); 50 | 51 | // save to db 52 | const int kMaxKeyLength = 10; 53 | char key_cstr[kMaxKeyLength]; 54 | 55 | for (int item_id = 0; item_id < nData; ++item_id) { 56 | Datum datum; 57 | datum.set_channels(dim); 58 | datum.set_height(1); 59 | datum.set_width(1); 60 | 61 | datum.mutable_float_data()->Reserve(dim); 62 | for(int k=0;k<2;k++){ 63 | datum.add_float_data(blob_data->cpu_data()[blob_data->offset(item_id,k,0,0)]); 64 | } 65 | 66 | if(item_id < nData/2) datum.set_label(0); 67 | else datum.set_label(1); 68 | 69 | snprintf(key_cstr, kMaxKeyLength, "%08d", item_id); 70 | string keystr(key_cstr); 71 | batch->Put(keystr, datum.SerializeAsString()); 72 | } 73 | 74 | db->Write(leveldb::WriteOptions(), batch); 75 | delete batch; 76 | delete db; 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /examples/ex5_net/logreg_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "caffe/caffe.hpp" 10 | #include "caffe/blob.hpp" 11 | #include "caffe/common.hpp" 12 | #include "caffe/filler.hpp" 13 | #include "caffe/net.hpp" 14 | #include "google/protobuf/text_format.h" 15 | 16 | using namespace caffe; 17 | using namespace std; 18 | typedef double Dtype; 19 | 20 | int main(int argc, char** argv) { 21 | 22 | string proto_test = 23 | "name: 'LogReg_test' " 24 | "layer { " 25 | " name: 'data' " 26 | " type: 'Data' " 27 | " top: 'data' " 28 | " top: 'label' " 29 | " data_param { " 30 | " source: 'test_leveldb' " 31 | " batch_size: 200 " 32 | " } " 33 | "} " 34 | "layer { " 35 | " name: 'ip' " 36 | " type: 'InnerProduct' " 37 | " bottom: 'data' " 38 | " top: 'ip' " 39 | " inner_product_param { " 40 | " num_output: 2 " 41 | " } " 42 | "} " 43 | "layer { " 44 | " name: 'loss' " 45 | " type: 'SoftmaxWithLoss' " 46 | " bottom: 'ip' " 47 | " bottom: 'label' " 48 | " top: 'loss' " 49 | "} "; 50 | NetParameter param_test; 51 | google::protobuf::TextFormat::ParseFromString(proto_test, ¶m_test); 52 | 53 | Net net_test(param_test); 54 | net_test.CopyTrainedLayersFrom(argv[1]); 55 | 56 | double loss = 0; 57 | const vector*>& result = net_test.ForwardPrefilled(); 58 | loss = result[0]->cpu_data()[0]; 59 | LOG(ERROR) << "Loss: " << loss; 60 | 61 | // blobs from the forwarded net 62 | shared_ptr > blob_label = net_test.blob_by_name("label"); 63 | shared_ptr > blob_ip = net_test.blob_by_name("ip"); 64 | 65 | // evaluation 66 | int score = 0; 67 | for (int n=0;n<200;n++){ 68 | int label = blob_label->mutable_cpu_data()[blob_label->offset(n,0,0.0)]; 69 | // argmax evaluate 70 | Dtype score_0 = blob_ip->mutable_cpu_data()[blob_ip->offset(n,0,0,0)]; 71 | Dtype score_1 = blob_ip->mutable_cpu_data()[blob_ip->offset(n,1,0,0)]; 72 | 73 | if((score_0 > score_1) && (label == 0)) score ++; 74 | if((score_0 < score_1) && (label == 1)) score ++; 75 | 76 | cout<<"label "< score_1) cout<<"predict: 0"< 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "caffe/caffe.hpp" 10 | #include "caffe/blob.hpp" 11 | #include "caffe/common.hpp" 12 | #include "caffe/filler.hpp" 13 | #include "caffe/net.hpp" 14 | #include "google/protobuf/text_format.h" 15 | 16 | using namespace caffe; 17 | using namespace std; 18 | typedef double Dtype; 19 | 20 | int main(int argc, char** argv) { 21 | // set net 22 | string proto = 23 | "name: 'LogReg_train' " 24 | "layer { " 25 | " name: 'data' " 26 | " type: 'Data' " 27 | " top: 'data' " 28 | " top: 'label' " 29 | " data_param { " 30 | " source: 'train_leveldb' " 31 | " batch_size: 200 " 32 | " } " 33 | "} " 34 | "layer { " 35 | " name: 'ip' " 36 | " type: 'InnerProduct' " 37 | " bottom: 'data' " 38 | " top: 'ip' " 39 | " inner_product_param { " 40 | " num_output: 2 " 41 | " } " 42 | "} " 43 | "layer { " 44 | " name: 'loss' " 45 | " type: 'SoftmaxWithLoss' " 46 | " bottom: 'ip' " 47 | " bottom: 'label' " 48 | " top: 'loss' " 49 | "} "; 50 | 51 | NetParameter param_net; 52 | google::protobuf::TextFormat::ParseFromString(proto, ¶m_net); 53 | 54 | SolverParameter param_solver; 55 | param_solver.set_allocated_net_param(¶m_net); 56 | param_solver.set_base_lr(0.01); 57 | param_solver.set_max_iter(1000); 58 | param_solver.set_lr_policy("inv"); 59 | param_solver.set_momentum(0.9); 60 | param_solver.set_gamma(0.0001); 61 | param_solver.set_snapshot(1000); 62 | param_solver.set_display(10); 63 | param_solver.set_solver_mode(SolverParameter_SolverMode_GPU); 64 | 65 | // training 66 | SGDSolver solver(param_solver); 67 | solver.Solve(); 68 | } 69 | --------------------------------------------------------------------------------