Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gineshidalgo99 committed Mar 12, 2018
2 parents 569352c + f049522 commit 3cb22ee
Show file tree
Hide file tree
Showing 35 changed files with 1,437 additions and 210 deletions.
5 changes: 3 additions & 2 deletions cmake/Misc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ endif()
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOLEAN "Use link paths for shared library rpath")
set(CMAKE_MACOSX_RPATH TRUE)

list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES ${CMAKE_INSTALL_PREFIX}/lib __is_systtem_dir)
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} __is_systtem_dir)
if(${__is_systtem_dir} STREQUAL -1)
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
endif()

# ---[ Funny target
Expand Down
4 changes: 2 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The official Makefile and `Makefile.config` build are complemented by a [communi
- [RHEL / CentOS / Fedora installation](install_yum.html)
- [Windows](https://github.com/BVLC/caffe/tree/windows) *see the Windows branch led by Guillaume Dumont*
- [OpenCL](https://github.com/BVLC/caffe/tree/opencl) *see the OpenCL branch led by Fabian Tschopp*
- [AWS AMI](https://github.com/bitfusionio/amis/tree/master/awsmrkt-bfboost-ubuntu14-cuda75-caffe) *pre-configured for AWS*
- [AWS AMI](https://aws.amazon.com/marketplace/pp/B01M0AXXQB) *official deep learning amazon machine image from AWS*

**Overview**:

Expand Down Expand Up @@ -80,7 +80,7 @@ The main requirements are `numpy` and `boost.python` (provided by boost). `panda

You can install the dependencies with

for req in $(cat requirements.txt); do pip install $req; done
pip install -r requirements.txt

but we suggest first installing the [Anaconda](https://store.continuum.io/cshop/anaconda/) Python distribution, which provides most of the necessary packages, as well as the `hdf5` library dependency.

Expand Down
18 changes: 12 additions & 6 deletions include/caffe/filler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class PositiveUnitballFiller : public Filler<Dtype> {
caffe_rng_uniform<Dtype>(blob->count(), 0, 1, blob->mutable_cpu_data());
// We expect the filler to not be called very frequently, so we will
// just use a simple implementation
int dim = blob->count() / blob->num();
int dim = blob->count() / blob->shape(0);
CHECK(dim);
for (int i = 0; i < blob->num(); ++i) {
for (int i = 0; i < blob->shape(0); ++i) {
Dtype sum = 0;
for (int j = 0; j < dim; ++j) {
sum += data[i * dim + j];
Expand Down Expand Up @@ -147,8 +147,11 @@ class XavierFiller : public Filler<Dtype> {
: Filler<Dtype>(param) {}
virtual void Fill(Blob<Dtype>* blob) {
CHECK(blob->count());
int fan_in = blob->count() / blob->num();
int fan_out = blob->count() / blob->channels();
int fan_in = blob->count() / blob->shape(0);
// Compatibility with ND blobs
int fan_out = blob->num_axes() > 1 ?
blob->count() / blob->shape(1) :
blob->count();
Dtype n = fan_in; // default to fan_in
if (this->filler_param_.variance_norm() ==
FillerParameter_VarianceNorm_AVERAGE) {
Expand Down Expand Up @@ -189,8 +192,11 @@ class MSRAFiller : public Filler<Dtype> {
: Filler<Dtype>(param) {}
virtual void Fill(Blob<Dtype>* blob) {
CHECK(blob->count());
int fan_in = blob->count() / blob->num();
int fan_out = blob->count() / blob->channels();
int fan_in = blob->count() / blob->shape(0);
// Compatibility with ND blobs
int fan_out = blob->num_axes() > 1 ?
blob->count() / blob->shape(1) :
blob->count();
Dtype n = fan_in; // default to fan_in
if (this->filler_param_.variance_norm() ==
FillerParameter_VarianceNorm_AVERAGE) {
Expand Down
68 changes: 68 additions & 0 deletions include/caffe/layers/cudnn_deconv_layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef CAFFE_CUDNN_DECONV_LAYER_HPP_
#define CAFFE_CUDNN_DECONV_LAYER_HPP_

#include <vector>

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

#include "caffe/layers/deconv_layer.hpp"

namespace caffe {

#ifdef USE_CUDNN
/*
* @brief cuDNN implementation of DeConvolutionLayer.
* Fallback to DeConvolutionLayer for CPU mode.
*
* cuDNN accelerates deconvolution through forward kernels for filtering and
* bias plus backward kernels for the gradient w.r.t. the filters, biases, and
* inputs. Caffe + cuDNN further speeds up the computation through forward
* parallelism across groups and backward parallelism across gradients.
*/
template <typename Dtype>
class CuDNNDeconvolutionLayer : public DeconvolutionLayer<Dtype> {
public:
explicit CuDNNDeconvolutionLayer(const LayerParameter& param)
: DeconvolutionLayer<Dtype>(param), handles_setup_(false) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual ~CuDNNDeconvolutionLayer();

protected:
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom);

bool handles_setup_;
cudnnHandle_t* handle_;
cudaStream_t* stream_;

// algorithms for forward and backwards convolutions
cudnnConvolutionFwdAlgo_t *fwd_algo_;
cudnnConvolutionBwdFilterAlgo_t *bwd_filter_algo_;
cudnnConvolutionBwdDataAlgo_t *bwd_data_algo_;

vector<cudnnTensorDescriptor_t> bottom_descs_, top_descs_;
cudnnTensorDescriptor_t bias_desc_;
cudnnFilterDescriptor_t filter_desc_;
vector<cudnnConvolutionDescriptor_t> conv_descs_;
int bottom_offset_, top_offset_, bias_offset_;

size_t *workspace_fwd_sizes_;
size_t *workspace_bwd_data_sizes_;
size_t *workspace_bwd_filter_sizes_;
size_t workspaceSizeInBytes; // size of underlying storage
void *workspaceData; // underlying storage
void **workspace; // aliases into workspaceData
};
#endif

} // namespace caffe

#endif // CAFFE_CUDNN_DECONV_LAYER_HPP_
2 changes: 1 addition & 1 deletion include/caffe/layers/euclidean_loss_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace caffe {
* This can be used for least-squares regression tasks. An InnerProductLayer
* input to a EuclideanLossLayer exactly formulates a linear least squares
* regression problem. With non-zero weight decay the problem becomes one of
* ridge regression -- see src/caffe/test/test_sgd_solver.cpp for a concrete
* ridge regression -- see src/caffe/test/test_gradient_based_solver.cpp for a concrete
* example wherein we check that the gradients computed for a Net with exactly
* this structure match hand-computed gradient formulas for ridge regression.
*
Expand Down
2 changes: 1 addition & 1 deletion python/caffe/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Classifier(caffe.Net):
def __init__(self, model_file, pretrained_file, image_dims=None,
mean=None, input_scale=None, raw_scale=None,
channel_swap=None):
caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
caffe.Net.__init__(self, model_file, caffe.TEST, weights=pretrained_file)

# configure pre-processing
in_ = self.inputs[0]
Expand Down
2 changes: 1 addition & 1 deletion python/caffe/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Detector(caffe.Net):
def __init__(self, model_file, pretrained_file, mean=None,
input_scale=None, raw_scale=None, channel_swap=None,
context_pad=None):
caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
caffe.Net.__init__(self, model_file, caffe.TEST, weights=pretrained_file)

# configure pre-processing
in_ = self.inputs[0]
Expand Down
144 changes: 107 additions & 37 deletions python/caffe/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,60 @@ def get_edge_label(layer):
return edge_label


def get_layer_label(layer, rankdir):
def get_layer_lr_mult(layer):
"""Get the learning rate multipliers.
Get the learning rate multipliers for the given layer. Assumes a
Convolution/Deconvolution/InnerProduct layer.
Parameters
----------
layer : caffe_pb2.LayerParameter
A Convolution, Deconvolution, or InnerProduct layer.
Returns
-------
learning_rates : tuple of floats
the learning rate multipliers for the weights and biases.
"""
if layer.type not in ['Convolution', 'Deconvolution', 'InnerProduct']:
raise ValueError("%s layers do not have a "
"learning rate multiplier" % layer.type)

if not hasattr(layer, 'param'):
return (1.0, 1.0)

params = getattr(layer, 'param')

if len(params) == 0:
return (1.0, 1.0)

if len(params) == 1:
lrm0 = getattr(params[0],'lr_mult', 1.0)
return (lrm0, 1.0)

if len(params) == 2:
lrm0, lrm1 = [getattr(p,'lr_mult', 1.0) for p in params]
return (lrm0, lrm1)

raise ValueError("Could not parse the learning rate multiplier")


def get_layer_label(layer, rankdir, display_lrm=False):
"""Define node label based on layer type.
Parameters
----------
layer : ?
layer : caffe_pb2.LayerParameter
rankdir : {'LR', 'TB', 'BT'}
Direction of graph layout.
display_lrm : boolean, optional
If True include the learning rate multipliers in the label (default is
False).
Returns
-------
string :
node_label : string
A label for the current layer
"""

Expand All @@ -81,36 +123,54 @@ def get_layer_label(layer, rankdir):
else:
# If graph orientation is horizontal, vertical space is free and
# horizontal space is not; separate words with newlines
separator = '\\n'

if layer.type == 'Convolution' or layer.type == 'Deconvolution':
# Outer double quotes needed or else colon characters don't parse
# properly
node_label = '"%s%s(%s)%skernel size: %d%sstride: %d%spad: %d"' %\
(layer.name,
separator,
layer.type,
separator,
layer.convolution_param.kernel_size[0] if len(layer.convolution_param.kernel_size) else 1,
separator,
layer.convolution_param.stride[0] if len(layer.convolution_param.stride) else 1,
separator,
layer.convolution_param.pad[0] if len(layer.convolution_param.pad) else 0)
elif layer.type == 'Pooling':
separator = r'\n'

# Initializes a list of descriptors that will be concatenated into the
# `node_label`
descriptors_list = []
# Add the layer's name
descriptors_list.append(layer.name)
# Add layer's type
if layer.type == 'Pooling':
pooling_types_dict = get_pooling_types_dict()
node_label = '"%s%s(%s %s)%skernel size: %d%sstride: %d%spad: %d"' %\
(layer.name,
separator,
pooling_types_dict[layer.pooling_param.pool],
layer.type,
separator,
layer.pooling_param.kernel_size,
separator,
layer.pooling_param.stride,
separator,
layer.pooling_param.pad)
layer_type = '(%s %s)' % (layer.type,
pooling_types_dict[layer.pooling_param.pool])
else:
node_label = '"%s%s(%s)"' % (layer.name, separator, layer.type)
layer_type = '(%s)' % layer.type
descriptors_list.append(layer_type)

# Describe parameters for spatial operation layers
if layer.type in ['Convolution', 'Deconvolution', 'Pooling']:
if layer.type == 'Pooling':
kernel_size = layer.pooling_param.kernel_size
stride = layer.pooling_param.stride
padding = layer.pooling_param.pad
else:
kernel_size = layer.convolution_param.kernel_size[0] if \
len(layer.convolution_param.kernel_size) else 1
stride = layer.convolution_param.stride[0] if \
len(layer.convolution_param.stride) else 1
padding = layer.convolution_param.pad[0] if \
len(layer.convolution_param.pad) else 0
spatial_descriptor = separator.join([
"kernel size: %d" % kernel_size,
"stride: %d" % stride,
"pad: %d" % padding,
])
descriptors_list.append(spatial_descriptor)

# Add LR multiplier for learning layers
if display_lrm and layer.type in ['Convolution', 'Deconvolution', 'InnerProduct']:
lrm0, lrm1 = get_layer_lr_mult(layer)
if any([lrm0, lrm1]):
lr_mult = "lr mult: %.1f, %.1f" % (lrm0, lrm1)
descriptors_list.append(lr_mult)

# Concatenate the descriptors into one label
node_label = separator.join(descriptors_list)
# Outer double quotes needed or else colon characters don't parse
# properly
node_label = '"%s"' % node_label
return node_label


Expand All @@ -127,7 +187,7 @@ def choose_color_by_layertype(layertype):
return color


def get_pydot_graph(caffe_net, rankdir, label_edges=True, phase=None):
def get_pydot_graph(caffe_net, rankdir, label_edges=True, phase=None, display_lrm=False):
"""Create a data structure which represents the `caffe_net`.
Parameters
Expand All @@ -140,6 +200,9 @@ def get_pydot_graph(caffe_net, rankdir, label_edges=True, phase=None):
phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional
Include layers from this network phase. If None, include all layers.
(the default is None)
display_lrm : boolean, optional
If True display the learning rate multipliers when relevant (default is
False).
Returns
-------
Expand All @@ -164,7 +227,7 @@ def get_pydot_graph(caffe_net, rankdir, label_edges=True, phase=None):
included = included and not layer_phase.phase == phase
if not included:
continue
node_label = get_layer_label(layer, rankdir)
node_label = get_layer_label(layer, rankdir, display_lrm=display_lrm)
node_name = "%s_%s" % (layer.name, layer.type)
if (len(layer.bottom) == 1 and len(layer.top) == 1 and
layer.bottom[0] == layer.top[0]):
Expand Down Expand Up @@ -202,7 +265,7 @@ def get_pydot_graph(caffe_net, rankdir, label_edges=True, phase=None):
return pydot_graph


def draw_net(caffe_net, rankdir, ext='png', phase=None):
def draw_net(caffe_net, rankdir, ext='png', phase=None, display_lrm=False):
"""Draws a caffe net and returns the image string encoded using the given
extension.
Expand All @@ -214,16 +277,20 @@ def draw_net(caffe_net, rankdir, ext='png', phase=None):
phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional
Include layers from this network phase. If None, include all layers.
(the default is None)
display_lrm : boolean, optional
If True display the learning rate multipliers for the learning layers
(default is False).
Returns
-------
string :
Postscript representation of the graph.
"""
return get_pydot_graph(caffe_net, rankdir, phase=phase).create(format=ext)
return get_pydot_graph(caffe_net, rankdir, phase=phase,
display_lrm=display_lrm).create(format=ext)


def draw_net_to_file(caffe_net, filename, rankdir='LR', phase=None):
def draw_net_to_file(caffe_net, filename, rankdir='LR', phase=None, display_lrm=False):
"""Draws a caffe net, and saves it to file using the format given as the
file extension. Use '.raw' to output raw text that you can manually feed
to graphviz to draw graphs.
Expand All @@ -238,7 +305,10 @@ def draw_net_to_file(caffe_net, filename, rankdir='LR', phase=None):
phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional
Include layers from this network phase. If None, include all layers.
(the default is None)
display_lrm : boolean, optional
If True display the learning rate multipliers for the learning layers
(default is False).
"""
ext = filename[filename.rfind('.')+1:]
with open(filename, 'wb') as fid:
fid.write(draw_net(caffe_net, rankdir, ext, phase))
fid.write(draw_net(caffe_net, rankdir, ext, phase, display_lrm))
Loading

0 comments on commit 3cb22ee

Please sign in to comment.