# Caffe 2 Caffe2

Caffe2 中基本计算单元之一是 Operators。每个 Operator 包含给定适当数量和类型的输入和参数来计算输出所需的逻辑。Caffe 和 Caffe2 功能的总体差异如下图所示：

One of basic units of computation in Caffe2 are the Operators. Each operator contains the logic necessary to compute the output given the appropriate number and types of inputs and parameters. The overall difference between operators’ functionality in Caffe and Caffe2 is illustrated in the following graphic, respectively:

# Create the input data
data = np.random.rand(16, 100).astype(np.float32)

# Create labels for the data as integers [0, 9].
label = (np.random.rand(16) * 10).astype(np.int32)

workspace.FeedBlob("data", data)
workspace.FeedBlob("label", label)

# Create model using a model helper
m = cnn.CNNModelHelper(name="my first net")
fc_1 = m.FC("data", "fc1", dim_in=100, dim_out=10)
pred = m.Sigmoid(fc_1, "pred")
[softmax, loss] = m.SoftmaxWithLoss([pred, "label"], ["softmax", "loss"])

# 安装

4.18发布的版本号为v0.7.0，官网上的安装教程比较详细，也比较好操作Install

## 依赖库

sudo apt-get update
sudo apt-get install -y --no-install-recommends \
build-essential \
cmake \
git \
libprotobuf-dev \
protobuf-compiler \
python-dev \
python-pip
sudo pip install numpy protobuf


## 可选库

# for both Ubuntu 14.04 and 16.04
sudo apt-get install -y --no-install-recommends \
libgtest-dev \
libiomp-dev \
libleveldb-dev \
liblmdb-dev \
libopencv-dev \
libopenmpi-dev \
libsnappy-dev \
openmpi-bin \
openmpi-doc \
python-pydot
sudo pip install \
graphviz \
hypothesis \
jupyter \
matplotlib \
pydot python-nvd3 \
pyyaml \
requests \
scikit-image \
scipy \
setuptools \
tornado
• 针对Ubuntu 14.04：
sudo apt-get install -y --no-install-recommends libgflags2
• 针对Ubuntu 16.04：
sudo apt-get install -y --no-install-recommends libgflags-dev


## Clone & Build

git clone --recursive https://github.com/caffe2/caffe2.git && cd caffe2
make && cd build && sudo make install
python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

GPU测试可以执行：

python -m caffe2.python.operator_test.relu_op_test

## 设置环境变量

echo $PYTHONPATH # export PYTHONPATH=/usr/local:$PYTHONPATH
# export PYTHONPATH=$PYTHONPATH:/home/ubuntu/caffe2/build echo$LD_LIBRARY_PATH
# export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH 具体设置更改对应shell的配置文件，一般来说 sudo vim /etc/profile 在最后加上 export PYTHONPATH=/usr/local:$PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/caffe2/build export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH