}
template <typename Dtype>
-const shared_ptr<Blob<Dtype> > Net<Dtype>::blob_by_name(const string& blob_name) {
+const shared_ptr<Blob<Dtype> > Net<Dtype>::blob_by_name(
+ const string& blob_name) {
shared_ptr<Blob<Dtype> > blob_ptr;
if (has_blob(blob_name)) {
blob_ptr = blobs_[blob_names_index_[blob_name]];
} else {
blob_ptr.reset((Blob<Dtype>*)(NULL));
- LOG(ERROR) << "Unknown blob name " << blob_name;
+ LOG(WARNING) << "Unknown blob name " << blob_name;
}
return blob_ptr;
}
}
template <typename Dtype>
-const shared_ptr<Layer<Dtype> > Net<Dtype>::layer_by_name(const string& layer_name) {
+const shared_ptr<Layer<Dtype> > Net<Dtype>::layer_by_name(
+ const string& layer_name) {
shared_ptr<Layer<Dtype> > layer_ptr;
if (has_layer(layer_name)) {
layer_ptr = layers_[layer_names_index_[layer_name]];
} else {
layer_ptr.reset((Layer<Dtype>*)(NULL));
- LOG(ERROR) << "Unknown layer name " << layer_name;
+ LOG(WARNING) << "Unknown layer name " << layer_name;
}
return layer_ptr;
}
// Copyright 2014 kloudkl@github
-#include <stdint.h> // for uint32_t & uint64_t
+#include <stdint.h> // for uint32_t & uint64_t
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
typedef ::testing::Types<float, double> Dtypes;
TYPED_TEST_CASE(MathFunctionsTest, Dtypes);
-TYPED_TEST(MathFunctionsTest, TestHammingDistance){
+TYPED_TEST(MathFunctionsTest, TestHammingDistance) {
int n = this->blob_bottom_->count();
const TypeParam* x = this->blob_bottom_->cpu_data();
const TypeParam* y = this->blob_top_->cpu_data();
caffe_hamming_distance<TypeParam>(n, x, y));
}
-}
+} // namespace caffe
// Copyright 2014 kloudkl@github
-#include <sstream>
#include <google/protobuf/text_format.h>
#include <leveldb/db.h>
+#include <sstream>
+#include <string>
#include "gtest/gtest.h"
#include "caffe/common.hpp"
class NetTest : public ::testing::Test {
protected:
NetTest() : filename(NULL) {
- };
- virtual void SetUp() {
- // Create the leveldb
- filename = tmpnam(NULL); // get temp name
+ }
+
+ virtual void SetUp() { // Create the leveldb
+ filename = tmpnam(NULL); // get temp name
LOG(INFO) << "Using temporary leveldb " << filename;
leveldb::DB* db;
leveldb::Options options;
"layers: { "
" layer { "
" name: 'data' "
- " type: 'data' "
- ;
+ " type: 'data' ";
const string& proto_suffix =
" batchsize: 1 "
" } "
" } "
" bottom: 'innerproduct' "
" bottom: 'label' "
- "} "
- ;
+ "} ";
proto = proto_prefix + "source: '" + string(this->filename) +
"' " + proto_suffix;
}
EXPECT_FALSE(net.layer_by_name("label"));
}
-
-}
+} // namespace caffe
// Copyright 2014 kloudkl@github
-#include <cmath> // for std::signbit
#include <cuda_runtime.h>
#include <google/protobuf/text_format.h>
+#include <cmath> // for std::signbit
+#include <string>
+#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
-using namespace caffe;
+using namespace caffe; // NOLINT(build/namespaces)
template<typename Dtype>
void binarize(const vector<shared_ptr<Blob<Dtype> > >& feature_blob_vector,
if (argc < num_required_args) {
LOG(ERROR)<<
"This program compresses real valued features into compact binary codes.\n"
- "Usage: demo_binarize_features real_valued_feature_prototxt feature_blob_name"
- " save_binarized_feature_binaryproto_file num_mini_batches [CPU/GPU] [DEVICE_ID=0]";
+ "Usage: demo_binarize_features real_valued_feature_prototxt"
+ " feature_blob_name save_binarized_feature_binaryproto_file"
+ " num_mini_batches [CPU/GPU] [DEVICE_ID=0]";
return 1;
}
int arg_pos = num_required_args;
arg_pos = 0; // the name of the executable
- // Expected prototxt contains at least one data layer as the real valued features.
+ // Expected prototxt contains at least one data layer as the real valued
+ // features.
/*
layers {
layer {
string feature_blob_name(argv[++arg_pos]);
CHECK(real_valued_feature_net->has_blob(feature_blob_name))
- << "Unknown feature blob name " << feature_blob_name << " in the network "
- << real_valued_feature_prototxt;
+ << "Unknown feature blob name " << feature_blob_name
+ << " in the network " << real_valued_feature_prototxt;
string save_binarized_feature_binaryproto_file(argv[++arg_pos]);
BlobProto blob_proto;
feature_binary_codes->ToProto(&blob_proto);
WriteProtoToBinaryFile(blob_proto, save_binarized_feature_binaryproto_file);
- LOG(ERROR)<< "Successfully binarized " << feature_binary_codes->num() << " features!";
+ LOG(ERROR) << "Successfully binarized " << feature_binary_codes->num()
+ << " features!";
return 0;
}
-// http://scikit-learn.org/stable/modules/preprocessing.html#feature-binarization
+// http://scikit-learn.org/stable/modules/preprocessing.html
+// #feature-binarization
template<typename Dtype>
void binarize(const vector<shared_ptr<Blob<Dtype> > >& feature_blob_vector,
shared_ptr<Blob<Dtype> > binary_codes) {
// Copyright 2014 kloudkl@github
-#include <stdio.h> // for snprintf
+#include <stdio.h> // for snprintf
#include <cuda_runtime.h>
#include <google/protobuf/text_format.h>
#include <leveldb/db.h>
#include <leveldb/write_batch.h>
+#include <string>
+#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
-using namespace caffe;
+using namespace caffe; // NOLINT(build/namespaces)
template<typename Dtype>
int feature_extraction_pipeline(int argc, char** argv);
}
*/
NetParameter feature_extraction_net_param;
- ;
string feature_extraction_proto(argv[++arg_pos]);
ReadProtoFromTextFile(feature_extraction_proto,
&feature_extraction_net_param);
Datum datum;
leveldb::WriteBatch* batch = new leveldb::WriteBatch();
- const int max_key_str_length = 100;
- char key_str[max_key_str_length];
+ const int kMaxKeyStrLength = 100;
+ char key_str[kMaxKeyStrLength];
int num_bytes_of_binary_code = sizeof(Dtype);
vector<Blob<float>*> input_vec;
int image_index = 0;
datum.set_channels(1);
datum.clear_data();
datum.clear_float_data();
- feature_blob_data = feature_blob->mutable_cpu_data() + feature_blob->offset(n);
+ feature_blob_data = feature_blob->mutable_cpu_data() +
+ feature_blob->offset(n);
for (int d = 0; d < dim_features; ++d) {
datum.add_float_data(feature_blob_data[d]);
}
string value;
datum.SerializeToString(&value);
- snprintf(key_str, max_key_str_length, "%d", image_index);
+ snprintf(key_str, kMaxKeyStrLength, "%d", image_index);
batch->Put(string(key_str), value);
++image_index;
if (image_index % 1000 == 0) {
db->Write(leveldb::WriteOptions(), batch);
- LOG(ERROR)<< "Extracted features of " << image_index << " query images.";
+ LOG(ERROR)<< "Extracted features of " << image_index <<
+ " query images.";
delete batch;
batch = new leveldb::WriteBatch();
}
// write the last batch
if (image_index % 1000 != 0) {
db->Write(leveldb::WriteOptions(), batch);
- LOG(ERROR)<< "Extracted features of " << image_index << " query images.";
+ LOG(ERROR)<< "Extracted features of " << image_index <<
+ " query images.";
delete batch;
batch = new leveldb::WriteBatch();
}
// Copyright 2014 kloudkl@github
-#include <fstream> // for std::ofstream
-#include <queue> // for std::priority_queue
#include <cuda_runtime.h>
#include <google/protobuf/text_format.h>
+#include <stdio.h>
+#include <queue> // for std::priority_queue
+#include <string>
+#include <utility> // for pair
+#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/math_functions.hpp"
-using namespace caffe;
+using namespace caffe; // NOLINT(build/namespaces)
template<typename Dtype>
void similarity_search(
string save_retrieval_result_filename(argv[++arg_pos]);
LOG(ERROR)<< "Opening result file " << save_retrieval_result_filename;
- std::ofstream retrieval_result_ofs(save_retrieval_result_filename.c_str(),
- std::ofstream::out);
+ FILE * result_fileid = fopen(save_retrieval_result_filename.c_str(),
+ "w");
LOG(ERROR)<< "Retrieving images";
vector<vector<std::pair<int, int> > > retrieval_results;
&retrieval_results);
int num_results = retrieval_results.size();
for (int i = 0; i < num_results; ++i) {
- retrieval_result_ofs << query_image_index++;
+ fprintf(result_fileid, "%d", query_image_index++);
for (int j = 0; j < retrieval_results[i].size(); ++j) {
- retrieval_result_ofs << " " << retrieval_results[i][j].first << ":"
- << retrieval_results[i][j].second;
+ fprintf(result_fileid, " %d:%d", retrieval_results[i][j].first,
+ retrieval_results[i][j].second);
}
- retrieval_result_ofs << "\n";
+ fprintf(result_fileid, "\n");
}
-
- retrieval_result_ofs.close();
- LOG(ERROR)<< "Successfully retrieved similar images for " << num_results << " queries!";
+ if (result_fileid != NULL) {
+ fclose(result_fileid);
+ }
+ LOG(ERROR) << "Successfully retrieved similar images for " << num_results
+ << " queries!";
return 0;
}
int num_samples = sample_images_feature_blob->num();
int num_queries = query_binary_feature_blob->num();
int dim = query_binary_feature_blob->count() / num_queries;
- LOG(ERROR)<< "num_samples " << num_samples << ", num_queries " << num_queries << ", dim " << dim;
+ LOG(ERROR)<< "num_samples " << num_samples << ", num_queries " <<
+ num_queries << ", dim " << dim;
int hamming_dist;
int neighbor_index;
retrieval_results->resize(num_queries);
hamming_dist = caffe_hamming_distance(dim, query_data, sample_data);
if (results.size() < top_k_results) {
results.push(std::make_pair(-hamming_dist, k));
- } else if (-hamming_dist > results.top().first) { // smaller hamming dist, nearer neighbor
+ } else if (-hamming_dist > results.top().first) {
+ // smaller hamming dist, nearer neighbor
results.pop();
results.push(std::make_pair(-hamming_dist, k));
}
for (int k = results.size() - 1; k >= 0; --k) {
hamming_dist = -results.top().first;
neighbor_index = results.top().second;
- retrieval_results->at(i)[k] = std::make_pair<int, int>(neighbor_index, hamming_dist);
+ retrieval_results->at(i)[k] = std::make_pair(neighbor_index,
+ hamming_dist);
results.pop();
}
} // for (int i = 0; i < num_queries; ++i) {