fix matcaffe and pycaffe linter errors
authorJeff Donahue <jeff.donahue@gmail.com>
Tue, 25 Feb 2014 20:13:31 +0000 (12:13 -0800)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Wed, 26 Feb 2014 23:42:38 +0000 (15:42 -0800)
matlab/caffe/matcaffe.cpp
python/caffe/pycaffe.cpp

index d137b31..d3bcba9 100644 (file)
@@ -4,12 +4,15 @@
 // caffe::Caffe functions so that one could easily call it from matlab.
 // Note that for matlab, we will simply use float as the data type.
 
+#include <string>
+#include <vector>
+
 #include "mex.h"
 #include "caffe/caffe.hpp"
 
 #define MEX_ARGS int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs
 
-using namespace caffe;
+namespace caffe {
 
 // The pointer to the internal caffe::Net instance
 static shared_ptr<Net<float> > net_;
@@ -21,8 +24,8 @@ static shared_ptr<Net<float> > net_;
 //   matlab uses RGB color channel order
 //   images need to have the data mean subtracted
 //
-// Data coming in from matlab needs to be in the order 
-//   [batch_images, channels, height, width] 
+// Data coming in from matlab needs to be in the order
+//   [batch_images, channels, height, width]
 // where width is the fastest dimension.
 // Here is the rough matlab for putting image data into the correct
 // format:
@@ -38,14 +41,14 @@ static shared_ptr<Net<float> > net_;
 // If you have multiple images, cat them with cat(4, ...)
 //
 // The actual forward function. It takes in a cell array of 4-D arrays as
-// input and outputs a cell array. 
+// input and outputs a cell array.
 static mxArray* do_forward(const mxArray* const bottom) {
   vector<Blob<float>*>& input_blobs = net_->input_blobs();
-  CHECK_EQ(static_cast<unsigned int>(mxGetDimensions(bottom)[0]), 
+  CHECK_EQ(static_cast<unsigned int>(mxGetDimensions(bottom)[0]),
       input_blobs.size());
   for (unsigned int i = 0; i < input_blobs.size(); ++i) {
     const mxArray* const elem = mxGetCell(bottom, i);
-    const float* const data_ptr = 
+    const float* const data_ptr =
         reinterpret_cast<const float* const>(mxGetPr(elem));
     switch (Caffe::mode()) {
     case Caffe::CPU:
@@ -63,7 +66,7 @@ static mxArray* do_forward(const mxArray* const bottom) {
   const vector<Blob<float>*>& output_blobs = net_->ForwardPrefilled();
   mxArray* mx_out = mxCreateCellMatrix(output_blobs.size(), 1);
   for (unsigned int i = 0; i < output_blobs.size(); ++i) {
-    mxArray* mx_blob = mxCreateNumericMatrix(output_blobs[i]->count(), 
+    mxArray* mx_blob = mxCreateNumericMatrix(output_blobs[i]->count(),
         1, mxSINGLE_CLASS, mxREAL);
     mxSetCell(mx_out, i, mx_blob);
     float* data_ptr = reinterpret_cast<float*>(mxGetPr(mx_blob));
@@ -85,30 +88,30 @@ static mxArray* do_forward(const mxArray* const bottom) {
 }
 
 // The caffe::Caffe utility functions.
-static void set_mode_cpu(MEX_ARGS) { 
-  Caffe::set_mode(Caffe::CPU); 
+static void set_mode_cpu(MEX_ARGS) {
+  Caffe::set_mode(Caffe::CPU);
 }
 
-static void set_mode_gpu(MEX_ARGS) { 
-  Caffe::set_mode(Caffe::GPU); 
+static void set_mode_gpu(MEX_ARGS) {
+  Caffe::set_mode(Caffe::GPU);
 }
 
-static void set_phase_train(MEX_ARGS) { 
-  Caffe::set_phase(Caffe::TRAIN); 
+static void set_phase_train(MEX_ARGS) {
+  Caffe::set_phase(Caffe::TRAIN);
 }
 
-static void set_phase_test(MEX_ARGS) { 
-  Caffe::set_phase(Caffe::TEST); 
+static void set_phase_test(MEX_ARGS) {
+  Caffe::set_phase(Caffe::TEST);
 }
 
-static void set_device(MEX_ARGS) { 
+static void set_device(MEX_ARGS) {
   if (nrhs != 1) {
     LOG(ERROR) << "Only given " << nrhs << " arguments";
     mexErrMsgTxt("Wrong number of arguments");
   }
 
   int device_id = static_cast<int>(mxGetScalar(prhs[0]));
-  Caffe::SetDevice(device_id); 
+  Caffe::SetDevice(device_id);
 }
 
 static void init(MEX_ARGS) {
@@ -186,3 +189,5 @@ void mexFunction(MEX_ARGS) {
     mxFree(cmd);
   }
 }
+
+}  // namespace caffe
index 7e17d56..6c87d0f 100644 (file)
@@ -5,9 +5,12 @@
 
 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
 
-#include <boost/python.hpp>
-#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
-#include <numpy/arrayobject.h>
+#include <string>
+#include <vector>
+
+#include "boost/python.hpp"
+#include "boost/python/suite/indexing/vector_indexing_suite.hpp"
+#include "numpy/arrayobject.h"
 #include "caffe/caffe.hpp"
 
 // Temporary solution for numpy < 1.7 versions: old macro, no promises.
@@ -17,8 +20,6 @@
 #define PyArray_SetBaseObject(arr, x) (PyArray_BASE(arr) = (x))
 #endif
 
-
-using namespace caffe;
 using boost::python::extract;
 using boost::python::len;
 using boost::python::list;
@@ -26,16 +27,16 @@ using boost::python::object;
 using boost::python::handle;
 using boost::python::vector_indexing_suite;
 
+namespace caffe {
 
 // wrap shared_ptr<Blob<float> > in a class that we construct in C++ and pass
 //  to Python
 class CaffeBlob {
  public:
-
   CaffeBlob(const shared_ptr<Blob<float> > &blob, const string& name)
       : blob_(blob), name_(name) {}
 
-  CaffeBlob(const shared_ptr<Blob<float> > &blob)
+  explicit CaffeBlob(const shared_ptr<Blob<float> > &blob)
       : blob_(blob) {}
 
   CaffeBlob()
@@ -48,8 +49,7 @@ class CaffeBlob {
   int width() const { return blob_->width(); }
   int count() const { return blob_->count(); }
 
-  bool operator == (const CaffeBlob &other)
-  {
+  bool operator == (const CaffeBlob &other) {
       return this->blob_ == other.blob_;
   }
 
@@ -64,14 +64,13 @@ class CaffeBlob {
 //  is not freed while still being used in Python
 class CaffeBlobWrap : public CaffeBlob {
  public:
-  CaffeBlobWrap(PyObject *p, shared_ptr<Blob<float> > &blob)
+  CaffeBlobWrap(PyObject *p, const shared_ptr<Blob<float> > &blob)
       : CaffeBlob(blob), self_(p) {}
 
   CaffeBlobWrap(PyObject *p, const CaffeBlob &blob)
       : CaffeBlob(blob), self_(p) {}
 
-  object get_data()
-  {
+  object get_data() {
       npy_intp dims[] = {num(), channels(), height(), width()};
 
       PyObject *obj = PyArray_SimpleNewFromData(4, dims, NPY_FLOAT32,
@@ -83,8 +82,7 @@ class CaffeBlobWrap : public CaffeBlob {
       return object(h);
   }
 
-  object get_diff()
-  {
+  object get_diff() {
       npy_intp dims[] = {num(), channels(), height(), width()};
 
       PyObject *obj = PyArray_SimpleNewFromData(4, dims, NPY_FLOAT32,
@@ -103,8 +101,7 @@ class CaffeBlobWrap : public CaffeBlob {
 
 
 // A simple wrapper over CaffeNet that runs the forward process.
-struct CaffeNet
-{
+struct CaffeNet {
   CaffeNet(string param_file, string pretrained_param_file) {
     net_.reset(new Net<float>(param_file));
     net_->CopyTrainedLayersFrom(pretrained_param_file);
@@ -126,7 +123,8 @@ struct CaffeNet
 
   // The actual forward function. It takes in a python list of numpy arrays as
   // input and a python list of numpy arrays as output. The input and output
-  // should all have correct shapes, are single-precisionabcdnt- and c contiguous.
+  // should all have correct shapes, are single-precisionabcdnt- and
+  // c contiguous.
   void Forward(list bottom, list top) {
     vector<Blob<float>*>& input_blobs = net_->input_blobs();
     CHECK_EQ(len(bottom), input_blobs.size());
@@ -149,9 +147,9 @@ struct CaffeNet
         LOG(FATAL) << "Unknown Caffe mode.";
       }  // switch (Caffe::mode())
     }
-    //LOG(INFO) << "Start";
+    // LOG(INFO) << "Start";
     const vector<Blob<float>*>& output_blobs = net_->ForwardPrefilled();
-    //LOG(INFO) << "End";
+    // LOG(INFO) << "End";
     for (int i = 0; i < output_blobs.size(); ++i) {
       object elem = top[i];
       PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(elem.ptr());
@@ -194,9 +192,9 @@ struct CaffeNet
         LOG(FATAL) << "Unknown Caffe mode.";
       }  // switch (Caffe::mode())
     }
-    //LOG(INFO) << "Start";
+    // LOG(INFO) << "Start";
     net_->Backward();
-    //LOG(INFO) << "End";
+    // LOG(INFO) << "End";
     for (int i = 0; i < input_blobs.size(); ++i) {
       object elem = bottom_diff[i];
       PyArrayObject* arr = reinterpret_cast<PyArrayObject*>(elem.ptr());
@@ -236,7 +234,8 @@ struct CaffeNet
       int ix = 0;
       for (int i = 0; i < net_->layers().size(); ++i) {
         for (int j = 0; j < net_->layers()[i]->blobs().size(); ++j) {
-          result.push_back(CaffeBlob(net_->params()[ix], net_->layer_names()[i]));
+          result.push_back(
+              CaffeBlob(net_->params()[ix], net_->layer_names()[i]));
           ix++;
         }
       }
@@ -250,9 +249,7 @@ struct CaffeNet
 
 
 // The boost python module definition.
-BOOST_PYTHON_MODULE(pycaffe)
-{
-
+BOOST_PYTHON_MODULE(pycaffe) {
   boost::python::class_<CaffeNet>(
       "CaffeNet", boost::python::init<string, string>())
       .def("Forward",         &CaffeNet::Forward)
@@ -263,8 +260,7 @@ BOOST_PYTHON_MODULE(pycaffe)
       .def("set_phase_test",  &CaffeNet::set_phase_test)
       .def("set_device",      &CaffeNet::set_device)
       .def("blobs",           &CaffeNet::blobs)
-      .def("params",          &CaffeNet::params)
-  ;
+      .def("params",          &CaffeNet::params);
 
   boost::python::class_<CaffeBlob, CaffeBlobWrap>(
       "CaffeBlob", boost::python::no_init)
@@ -275,12 +271,12 @@ BOOST_PYTHON_MODULE(pycaffe)
       .add_property("width",    &CaffeBlob::width)
       .add_property("count",    &CaffeBlob::count)
       .add_property("data",     &CaffeBlobWrap::get_data)
-      .add_property("diff",     &CaffeBlobWrap::get_diff)
-  ;
+      .add_property("diff",     &CaffeBlobWrap::get_diff);
 
   boost::python::class_<vector<CaffeBlob> >("BlobVec")
       .def(vector_indexing_suite<vector<CaffeBlob>, true>());
 
   import_array();
-
 }
+
+}  // namespace caffe