Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ linter #163

Merged
merged 17 commits into from
Feb 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ TOOL_SRCS := $(shell find tools -name "*.cpp")
EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
# PROTO_SRCS are the protocol buffer definitions
PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/*.proto)
# NONGEN_CXX_SRCS includes all source/header files except those generated by
# proto.
NONGEN_CXX_SRCS := $(shell find \
src/$(PROJECT) \
include/$(PROJECT) \
python/$(PROJECT) \
matlab/$(PROJECT) \
examples \
tools \
-regex ".*\.\(cpp\|hpp\|cu\|cuh\)")
# PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
PY$(PROJECT)_SRC := python/$(PROJECT)/py$(PROJECT).cpp
PY$(PROJECT)_SO := python/$(PROJECT)/py$(PROJECT).so
Expand Down Expand Up @@ -110,6 +120,9 @@ init:
linecount: clean
cloc --read-lang-def=$(PROJECT).cloc src/$(PROJECT)/

lint:
./scripts/cpp_lint.py $(NONGEN_CXX_SRCS)

test: init $(TEST_BINS)

tools: init $(TOOL_BINS)
Expand Down
47 changes: 23 additions & 24 deletions examples/cifar/convert_cifar_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@
#include <leveldb/db.h>

#include <stdint.h>
#include <iostream>
#include <fstream>
#include <fstream> // NOLINT(readability/streams)
#include <string>

#include "caffe/proto/caffe.pb.h"

using std::string;

const int kCIFARSize = 32;
const int kCIFARImageNBytes = 3072;
const int kCIFARBatchSize = 10000;
const int kCIFARTrainBatches = 5;

const int kCIFAR_SIZE=32;
const int kCIFAR_IMAGE_NBYTES=3072;
const int kCIFAR_BATCHSIZE=10000;
const int kCIFAR_TRAIN_BATCHES=5;

void read_image(std::ifstream& file, int* label, char* buffer) {
void read_image(std::ifstream* file, int* label, char* buffer) {
char label_char;
file.read(&label_char, 1);
file->read(&label_char, 1);
*label = label_char;
file.read(buffer, kCIFAR_IMAGE_NBYTES);
file->read(buffer, kCIFARImageNBytes);
return;
}

Expand All @@ -41,32 +39,33 @@ void convert_dataset(const string& input_folder, const string& output_folder) {
options.error_if_exists = true;
// Data buffer
int label;
char str_buffer[kCIFAR_IMAGE_NBYTES];
char str_buffer[kCIFARImageNBytes];
string value;
caffe::Datum datum;
datum.set_channels(3);
datum.set_height(kCIFAR_SIZE);
datum.set_width(kCIFAR_SIZE);
datum.set_height(kCIFARSize);
datum.set_width(kCIFARSize);

LOG(INFO) << "Writing Training data";
leveldb::DB* train_db;
leveldb::Status status;
status = leveldb::DB::Open(options, output_folder + "/cifar-train-leveldb",
&train_db);
CHECK(status.ok()) << "Failed to open leveldb.";
for (int fileid = 0; fileid < kCIFAR_TRAIN_BATCHES; ++fileid) {
for (int fileid = 0; fileid < kCIFARTrainBatches; ++fileid) {
// Open files
LOG(INFO) << "Training Batch " << fileid + 1;
sprintf(str_buffer, "/data_batch_%d.bin", fileid + 1);
snprintf(str_buffer, kCIFARImageNBytes, "/data_batch_%d.bin", fileid + 1);
std::ifstream data_file((input_folder + str_buffer).c_str(),
std::ios::in | std::ios::binary);
CHECK(data_file) << "Unable to open train file #" << fileid + 1;
for (int itemid = 0; itemid < kCIFAR_BATCHSIZE; ++itemid) {
read_image(data_file, &label, str_buffer);
for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) {
read_image(&data_file, &label, str_buffer);
datum.set_label(label);
datum.set_data(str_buffer, kCIFAR_IMAGE_NBYTES);
datum.set_data(str_buffer, kCIFARImageNBytes);
datum.SerializeToString(&value);
sprintf(str_buffer, "%05d", fileid * kCIFAR_BATCHSIZE + itemid);
snprintf(str_buffer, kCIFARImageNBytes, "%05d",
fileid * kCIFARBatchSize + itemid);
train_db->Put(leveldb::WriteOptions(), string(str_buffer), value);
}
}
Expand All @@ -79,20 +78,20 @@ void convert_dataset(const string& input_folder, const string& output_folder) {
std::ifstream data_file((input_folder + "/test_batch.bin").c_str(),
std::ios::in | std::ios::binary);
CHECK(data_file) << "Unable to open test file.";
for (int itemid = 0; itemid < kCIFAR_BATCHSIZE; ++itemid) {
read_image(data_file, &label, str_buffer);
for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) {
read_image(&data_file, &label, str_buffer);
datum.set_label(label);
datum.set_data(str_buffer, kCIFAR_IMAGE_NBYTES);
datum.set_data(str_buffer, kCIFARImageNBytes);
datum.SerializeToString(&value);
sprintf(str_buffer, "%05d", itemid);
snprintf(str_buffer, kCIFARImageNBytes, "%05d", itemid);
test_db->Put(leveldb::WriteOptions(), string(str_buffer), value);
}

delete train_db;
delete test_db;
}

int main (int argc, char** argv) {
int main(int argc, char** argv) {
if (argc != 3) {
printf("This script converts the CIFAR dataset to the leveldb format used\n"
"by caffe to perform classification.\n"
Expand Down
28 changes: 14 additions & 14 deletions examples/lenet/convert_mnist_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
#include <leveldb/db.h>

#include <stdint.h>
#include <iostream>
#include <fstream>
#include <fstream> // NOLINT(readability/streams)
#include <string>

#include "caffe/proto/caffe.pb.h"

uint32_t swap_endian( uint32_t val )
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
uint32_t swap_endian(uint32_t val) {
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16);
}

Expand All @@ -37,20 +36,20 @@ void convert_dataset(const char* image_filename, const char* label_filename,
uint32_t rows;
uint32_t cols;

image_file.read((char*)(&magic), 4);
image_file.read(reinterpret_cast<char*>(&magic), 4);
magic = swap_endian(magic);
CHECK_EQ(magic, 2051) << "Incorrect image file magic.";
label_file.read((char*)(&magic), 4);
label_file.read(reinterpret_cast<char*>(&magic), 4);
magic = swap_endian(magic);
CHECK_EQ(magic, 2049) << "Incorrect label file magic.";
image_file.read((char*)(&num_items), 4);
image_file.read(reinterpret_cast<char*>(&num_items), 4);
num_items = swap_endian(num_items);
label_file.read((char*)(&num_labels), 4);
label_file.read(reinterpret_cast<char*>(&num_labels), 4);
num_labels = swap_endian(num_labels);
CHECK_EQ(num_items, num_labels);
image_file.read((char*)(&rows), 4);
image_file.read(reinterpret_cast<char*>(&rows), 4);
rows = swap_endian(rows);
image_file.read((char*)(&cols), 4);
image_file.read(reinterpret_cast<char*>(&cols), 4);
cols = swap_endian(cols);

// Open leveldb
Expand All @@ -65,7 +64,8 @@ void convert_dataset(const char* image_filename, const char* label_filename,

char label;
char* pixels = new char[rows * cols];
char key[10];
const int kMaxKeyLength = 10;
char key[kMaxKeyLength];
std::string value;

caffe::Datum datum;
Expand All @@ -80,15 +80,15 @@ void convert_dataset(const char* image_filename, const char* label_filename,
datum.set_data(pixels, rows*cols);
datum.set_label(label);
datum.SerializeToString(&value);
sprintf(key, "%08d", itemid);
snprintf(key, kMaxKeyLength, "%08d", itemid);
db->Put(leveldb::WriteOptions(), std::string(key), value);
}

delete db;
delete pixels;
}

int main (int argc, char** argv) {
int main(int argc, char** argv) {
if (argc != 4) {
printf("This script converts the MNIST dataset to the leveldb format used\n"
"by caffe to perform classification.\n"
Expand Down
2 changes: 1 addition & 1 deletion include/caffe/filler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ConstantFiller : public Filler<Dtype> {
for (int i = 0; i < count; ++i) {
data[i] = value;
}
};
}
};

template <typename Dtype>
Expand Down
8 changes: 4 additions & 4 deletions include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Layer {
vector<Blob<Dtype>*>* top) {
// LOG(WARNING) << "Using CPU code as backup.";
Forward_cpu(bottom, top);
};
}

// Backward functions: the backward function will compute the gradients for
// any parameters and also for the bottom blobs if propagate_down is true.
Expand All @@ -80,7 +80,7 @@ class Layer {
vector<Blob<Dtype>*>* bottom) {
// LOG(WARNING) << "Using CPU code as backup.";
return Backward_cpu(top, propagate_down, bottom);
};
}

DISABLE_COPY_AND_ASSIGN(Layer);
}; // class Layer
Expand All @@ -101,7 +101,7 @@ inline void Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
default:
LOG(FATAL) << "Unknown caffe mode.";
}
};
}

template <typename Dtype>
inline Dtype Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
Expand All @@ -115,7 +115,7 @@ inline Dtype Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
default:
LOG(FATAL) << "Unknown caffe mode.";
}
};
}

template <typename Dtype>
void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
Expand Down
4 changes: 2 additions & 2 deletions include/caffe/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace caffe {
template <typename Dtype>
class Net {
public:
Net(const NetParameter& param);
Net(const string& param_file);
explicit Net(const NetParameter& param);
explicit Net(const string& param_file);
virtual ~Net() {}

// Initialize a network with the network parameter.
Expand Down
3 changes: 2 additions & 1 deletion include/caffe/solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef CAFFE_OPTIMIZATION_SOLVER_HPP_
#define CAFFE_OPTIMIZATION_SOLVER_HPP_

#include <string>
#include <vector>

namespace caffe {
Expand Down Expand Up @@ -66,6 +67,6 @@ class SGDSolver : public Solver<Dtype> {
};


} // namspace caffe
} // namespace caffe

#endif // CAFFE_OPTIMIZATION_SOLVER_HPP_
16 changes: 8 additions & 8 deletions include/caffe/util/im2col.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ namespace caffe {

template <typename Dtype>
void im2col_cpu(const Dtype* data_im, const int channels,
const int height, const int width, const int ksize, const int pad, const int stride,
Dtype* data_col);
const int height, const int width, const int ksize, const int pad,
const int stride, Dtype* data_col);

template <typename Dtype>
void col2im_cpu(const Dtype* data_col, const int channels,
const int height, const int width, const int psize, const int pad, const int stride,
Dtype* data_im);
const int height, const int width, const int psize, const int pad,
const int stride, Dtype* data_im);

template <typename Dtype>
void im2col_gpu(const Dtype* data_im, const int channels,
const int height, const int width, const int ksize, const int pad, const int stride,
Dtype* data_col);
const int height, const int width, const int ksize, const int pad,
const int stride, Dtype* data_col);

template <typename Dtype>
void col2im_gpu(const Dtype* data_col, const int channels,
const int height, const int width, const int psize, const int pad, const int stride,
Dtype* data_im);
const int height, const int width, const int psize, const int pad,
const int stride, Dtype* data_im);

} // namespace caffe

Expand Down
2 changes: 2 additions & 0 deletions include/caffe/util/insert_splits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef _CAFFE_UTIL_INSERT_SPLITS_HPP_
#define _CAFFE_UTIL_INSERT_SPLITS_HPP_

#include <string>

#include "caffe/proto/caffe.pb.h"

using std::pair;
Expand Down
9 changes: 4 additions & 5 deletions include/caffe/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
#ifndef CAFFE_UTIL_IO_H_
#define CAFFE_UTIL_IO_H_

#include <google/protobuf/message.h>
#include <string>

#include <boost/scoped_ptr.hpp>
#include "google/protobuf/message.h"
#include "hdf5.h"
#include "hdf5_hl.h"
#include "caffe/proto/caffe.pb.h"

#include <string>

#include "boost/scoped_ptr.hpp"
#include "caffe/blob.hpp"
#include "caffe/proto/caffe.pb.h"

using std::string;
using ::google::protobuf::Message;
Expand Down
9 changes: 4 additions & 5 deletions include/caffe/vision_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
#ifndef CAFFE_VISION_LAYERS_HPP_
#define CAFFE_VISION_LAYERS_HPP_

#include <leveldb/db.h>
#include <pthread.h>
#include <boost/scoped_ptr.hpp>
#include <vector>

#include "leveldb/db.h"
#include "pthread.h"
#include "boost/scoped_ptr.hpp"
#include "hdf5.h"

#include <vector>

#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"

Expand Down
Loading