Python MemoryDataLayer interface suggestion
When using caffe's python interface to do regression. I find it will check the dimension of labels:
_caffe.cpp:
void PyNet::set_input_arrays(bp::object data_obj, bp::object labels_obj) {
// check that this network has an input MemoryDataLayer
...
// check that we were passed appropriately-sized contiguous memory
PyArrayObject* data_arr =
reinterpret_cast<PyArrayObject*>(data_obj.ptr());
PyArrayObject* labels_arr =
reinterpret_cast<PyArrayObject*>(labels_obj.ptr());
check_contiguous_array(data_arr, "data array", md_layer->datum_channels(),
md_layer->datum_height(), md_layer->datum_width());
check_contiguous_array(labels_arr, "labels array", 1, 1, 1);
...
}
The code check_contiguous_array(labels_arr, "labels array", 1, 1, 1) restricts each sample to have one label only. Maybe it is unnecessary to do this check?
Yes, see https://github.com/BVLC/caffe/pull/294#issuecomment-40776588.
This was done with the existing data layers in mind, but it's easy and natural to just take arbitrary data. I haven't written the generalization yet and won't get to it right away, but PRs are welcome.
Hi mender05
Any chance I could get a code snippet of your python code for regression. I'm trying to learn how to use the python interface to train a network.
Thanks,
@mikejmills You can refer to #1396 for my python code.
@mender05 i am referring to your same output issue https://github.com/BVLC/caffe/issues/1396 Did you fix it? i am struggling with this problem and i am doing regression to predict 30 outputs but results are the same, no matter what input is given.
I checked the code pycaffe.py, _caffe.cpp and the code looks like caffe can only predict 1D labels
def _Net_set_input_arrays(self, data, labels):
if labels.ndim == 1:
labels = np.ascontiguousarray(labels[:, np.newaxis, np.newaxis, np.newaxis])
return self._set_input_arrays(data, labels)```
In _caffe.cpp
check_contiguous_array(labels_arr, "labels array", 1, 1, 1);
memory_layer.cpp
added_label_.Reshape(batch_size_, 1, 1, 1);
I am trying to predict a (64,30) labels shaped into (64,30,1,1) but it is not working with pycaffe wrapper. 64 is nothing but BATCH_SIZE. The caffe model is already trained with these labels.
Please may you help :) Thank you very much
I made some hand modifications to perform multilevel training. It's by no means proper or general fix, but it seems to do the job for me.
Hope this helps
On Tue Feb 10 2015 at 2:46:00 PM MST olddocks [email protected] wrote:
@mender05 https://github.com/mender05 i am referring to your same output issue #1396 https://github.com/BVLC/caffe/issues/1396 Did you fix it? i am struggling with this problem and i am doing regression to predict 30 outputs but results are the same, no matter what input is given.
I checked the code pycaffe.py, _caffe.cpp and the code looks like caffe can only predict 1D labels
""" Set input arrays of the in-memory MemoryDataLayer. (Note: this is only for networks declared with the memory data layer.) """ if labels.ndim == 1: labels = np.ascontiguousarray(labels[:, np.newaxis, np.newaxis, np.newaxis]) return self._set_input_arrays(data, labels)``` _caffe.cpp `check_contiguous_array(labels_arr, "labels array", 1, 1, 1);` memory_layer.cpp added_label_.Reshape(batch_size_, 1, 1, 1); I am trying to predict a (64,30) labels shaped into (64,30,1,1) but it is not working with pycaffe wrapper. 64 is nothing but BATCH_SIZE. The caffe model is already trained with these labels. Please may you help :) Thank you very much — Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1366#issuecomment-73790618.
@mikejmills What did you modify? May i ask? I have to get to the bottom of this to get this thing running.
I'm sorry I included a diff file but it didn't seem to make it to the form.
diff --git a/Makefile b/Makefile
index 6a660f5..19c4775 100644
--- a/Makefile
+++ b/Makefile
@@ -165,11 +165,12 @@ ifneq ($(CPU_ONLY), 1)
LIBRARY_DIRS += $(CUDA_LIB_DIR)
LIBRARIES := cudart cublas curand
endif
-LIBRARIES += glog gflags protobuf leveldb snappy
+LIBRARIES += pthread \
- glog gflags protobuf leveldb snappy
lmdb
boost_system
hdf5_hl hdf5 \ - opencv_core opencv_highgui opencv_imgproc pthread
- opencv_core opencv_highgui opencv_imgproc PYTHON_LIBRARIES := boost_python python2.7 WARNINGS := -Wall -Wno-sign-compare
@@ -242,8 +243,8 @@ ifeq ($(OSX), 1) CXX := /usr/bin/clang++
clang throws this warning for cuda headers
WARNINGS += -Wno-unneeded-internal-declaration
- ifneq ($(findstring 10.9, $(shell sw_vers -productVersion)),)
- CXXFLAGS += -stdlib=libstdc++
- ifneq ($(findstring 10.10, $(shell sw_vers -productVersion)),)
- CXXFLAGS += -stdlib=libstdc++
LINKFLAGS += -stdlib=libstdc++
endif
boost::thread is called boost_thread-mt to mark multithreading on OS X
@@ -300,9 +301,10 @@ else endif else ifeq ($(OSX), 1)OS X packages atlas as the vecLib framework
- BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
- BLAS_INCLUDE ?= /System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
- #/System//Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers/ #/System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/ LIBRARIES += cblas
- LDFLAGS += -framework vecLib
- LDFLAGS += /System//Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Versions/Current/libBLAS.dylib #-framework vecLib endif endif INCLUDE_DIRS += $(BLAS_INCLUDE) diff --git a/python/caffe/_caffe.cpp b/python/caffe/_caffe.cpp index 5a81a42..b83fb58 100644 --- a/python/caffe/_caffe.cpp +++ b/python/caffe/_caffe.cpp @@ -107,7 +107,7 @@ void PyNet::set_input_arrays(bp::object data_obj, bp::object labels_obj) { reinterpret_cast<PyArrayObject*>(labels_obj.ptr()); check_contiguous_array(data_arr, "data array", md_layer->datum_channels(), md_layer->datum_height(), md_layer->datum_width());
- check_contiguous_array(labels_arr, "labels array", 1, 1, 1);
- check_contiguous_array(labels_arr, "labels array", 2, 1, 1); if (PyArray_DIMS(data_arr)[0] != PyArray_DIMS(labels_arr)[0]) { throw std::runtime_error("data and labels must have the same first" " dimension"); diff --git a/python/caffe/pycaffe.py b/python/caffe/pycaffe.py index 31dc1f9..d4b2666 100644 --- a/python/caffe/pycaffe.py +++ b/python/caffe/pycaffe.py @@ -339,9 +339,17 @@ def _Net_set_input_arrays(self, data, labels): Set input arrays of the in-memory MemoryDataLayer. (Note: this is only for networks declared with the memory data layer.) """
if labels.ndim == 1: labels = np.ascontiguousarray(labels[:, np.newaxis, np.newaxis, np.newaxis]) +
- if labels.ndim == 2:
-
print "2 DIMS" -
labels = np.ascontiguousarray(labels[:, :, np.newaxis, -
np.newaxis]) -
print labels.ndim
diff --git a/src/caffe/layers/memory_data_layer.cpp
b/src/caffe/layers/memory_data_layer.cpp
index ab631a8..1acb8ef 100644
--- a/src/caffe/layers/memory_data_layer.cpp
+++ b/src/caffe/layers/memory_data_layer.cpp
@@ -20,10 +20,10 @@ void MemoryDataLayer
- (*top)[1]->Reshape(batch_size_, 1, 1, 1);
- (*top)[1]->Reshape(batch_size_, 2, 1, 1); added_data_.Reshape(batch_size_, this->datum_channels_, this->datum_height_, this->datum_width_);
- added_label_.Reshape(batch_size_, 1, 1, 1);
- added_label_.Reshape(batch_size_, 2, 1, 1); data_ = NULL; labels_ = NULL; added_data_.cpu_data();
On Wed Feb 11 2015 at 6:56:26 AM MST olddocks [email protected] wrote:
@mikejmills https://github.com/mikejmills What did you modify? May i ask? I have to get to the bottom of this to get this thing running.
— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1366#issuecomment-73884070.
Closing as solved by the Python layer interface. It is is more general and simpler to work with than the MemoryDataLayer.