Skip to content

Commit

Permalink
Add Python support (luoyetx#25)
Browse files Browse the repository at this point in the history
* refactor headers

* fix build

* optimize PReLU layer

* optimize prelu

* add python api

* try to fix install numpy

* fix

* fix appveyor

* optim prelu

* fix type

* update

* add layer crafter

* list network params

* update README

* fix py3

* add python3 test

* fix travis
  • Loading branch information
luoyetx authored Feb 27, 2017
1 parent 23b97ef commit 082f47d
Show file tree
Hide file tree
Showing 49 changed files with 832 additions and 250 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ addons:
- libatlas-dev
- gcc-4.8
- g++-4.8
- python-numpy
- python3-numpy
- python3-setuptools

install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Mini-Caffe now can be cross compiled for Android platform, checkout the document

### With CUDA and CUDNN

Install CUDA and CUDNN in your system, then we can compile Mini-Caffe with GPU support. Run CMake command below.
Install [CUDA8](https://developer.nvidia.com/cuda-downloads) and [cuDNN5.1](https://developer.nvidia.com/cudnn) in your system, then we can compile Mini-Caffe with GPU support. Run CMake command below.

```
$ cmake .. -DUSE_CUDA=ON -DUSE_CUDNN=ON
Expand All @@ -79,6 +79,10 @@ Install Java and set environment variable `JAVA_HOME`. Run CMake command below.
$ cmake .. -DUSE_JAVA=ON
```

### With Python support

checkout Python API [here](python), install package via `python setup.py install`.

### How to use Mini-Caffe

To use Mini-Caffe as a library, you may refer to [example](example).
20 changes: 19 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ image: Visual Studio 2013
environment:
URL_3rdparty: https://github.com/luoyetx/misc/blob/master/mini-caffe/3rdparty.zip?raw=true
URL_model: https://github.com/luoyetx/misc/blob/master/mini-caffe/model.zip?raw=true
PYTHON2: "C:\\Python27-x64"
PYTHON3: "C:\\Python35-x64"

install:
- ps: |
Expand Down Expand Up @@ -41,7 +43,7 @@ before_test:
}
- cmd: |
7z x model.zip -y -o"%APPVEYOR_BUILD_FOLDER%/build/Release" >NUL
set PATH=%PATH%;%cd%\3rdparty\bin;%cd%\build\Release
set PATH=%cd%\3rdparty\bin;%cd%\build\Release;%PATH%
test_script:
- cmd: |
Expand All @@ -53,6 +55,22 @@ test_script:
- cmd: |
cd java
gradlew.bat clean build --info
cd ..
- cmd: |
cd python
set PATH=%PYTHON2%;%PYTHON2%\Scripts;%PATH%
python --version
pip install numpy
python tests/test.py
python setup.py install
python setup.py clean
set PATH=%PYTHON3%;%PYTHON3%\Scripts;%PATH%
python --version
pip install numpy
python tests/test.py
python setup.py install
python setup.py clean
cd ..
branches:
only:
Expand Down
6 changes: 0 additions & 6 deletions cmake/Cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ if(NOT USE_CUDA)
return()
endif()

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-std=c++11" SUPPORT_CXX11)

################################################################################################
# Removes duplicates from list(s)
# Usage:
Expand Down Expand Up @@ -172,9 +169,6 @@ macro(caffe_cuda_compile objlist_variable)

if(UNIX OR APPLE)
list(APPEND CUDA_NVCC_FLAGS -Xcompiler -fPIC)
if(SUPPORT_CXX11)
list(APPEND CUDA_NVCC_FLAGS -Xcompiler -fPIC --std=c++11)
endif()
endif()

if(APPLE)
Expand Down
50 changes: 25 additions & 25 deletions example/models/wgan/g.prototxt
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ layer {
name: "gbn1"
type: "BatchNorm"
bottom: "gconv1"
top: "gbn1"
top: "gconv1"
batch_norm_param {
use_global_stats: false
}
}
layer {
name: "gbn1_scale"
type: "Scale"
bottom: "gbn1"
top: "gbn1_scale"
bottom: "gconv1"
top: "gconv1"
scale_param {
bias_term: true
}
}
layer {
name: "gact1"
type: "ReLU"
bottom: "gbn1_scale"
top: "gact1"
bottom: "gconv1"
top: "gconv1"
}
# 4x4
layer {
name: "gconv2"
type: "Deconvolution"
bottom: "gact1"
bottom: "gconv1"
top: "gconv2"
convolution_param {
num_output: 256
Expand All @@ -65,31 +65,31 @@ layer {
name: "gbn2"
type: "BatchNorm"
bottom: "gconv2"
top: "gbn2"
top: "gconv2"
batch_norm_param {
use_global_stats: false
}
}
layer {
name: "gbn2_scale"
type: "Scale"
bottom: "gbn2"
top: "gbn2_scale"
bottom: "gconv2"
top: "gconv2"
scale_param {
bias_term: true
}
}
layer {
name: "gact2"
type: "ReLU"
bottom: "gbn2_scale"
top: "gact2"
bottom: "gconv2"
top: "gconv2"
}
# 8x8
layer {
name: "gconv3"
type: "Deconvolution"
bottom: "gact2"
bottom: "gconv2"
top: "gconv3"
convolution_param {
num_output: 128
Expand All @@ -103,31 +103,31 @@ layer {
name: "gbn3"
type: "BatchNorm"
bottom: "gconv3"
top: "gbn3"
top: "gconv3"
batch_norm_param {
use_global_stats: false
}
}
layer {
name: "gbn3_scale"
type: "Scale"
bottom: "gbn3"
top: "gbn3_scale"
bottom: "gconv3"
top: "gconv3"
scale_param {
bias_term: true
}
}
layer {
name: "gact3"
type: "ReLU"
bottom: "gbn3_scale"
top: "gact3"
bottom: "gconv3"
top: "gconv3"
}
# 16x16
layer {
name: "gconv4"
type: "Deconvolution"
bottom: "gact3"
bottom: "gconv3"
top: "gconv4"
convolution_param {
num_output: 64
Expand All @@ -141,31 +141,31 @@ layer {
name: "gbn4"
type: "BatchNorm"
bottom: "gconv4"
top: "gbn4"
top: "gconv4"
batch_norm_param {
use_global_stats: false
}
}
layer {
name: "gbn4_scale"
type: "Scale"
bottom: "gbn4"
top: "gbn4_scale"
bottom: "gconv4"
top: "gconv4"
scale_param {
bias_term: true
}
}
layer {
name: "gact4"
type: "ReLU"
bottom: "gbn4_scale"
top: "gact4"
bottom: "gconv4"
top: "gconv4"
}
# 32x32
layer {
name: "gconv5"
type: "Deconvolution"
bottom: "gact4"
bottom: "gconv4"
top: "gconv5"
convolution_param {
num_output: 3
Expand All @@ -179,5 +179,5 @@ layer {
name: "gact5"
type: "TanH"
bottom: "gconv5"
top: "gact5"
top: "gconv5"
}
9 changes: 8 additions & 1 deletion example/wgan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Timer {
};

int main(int argc, char **argv) {
if (caffe::GPUAvailable()) {
caffe::SetMode(caffe::GPU, 0);
}
caffe::Net net("../models/wgan/g.prototxt");
net.CopyTrainedLayersFrom("../models/wgan/g.caffemodel");
// random noise
Expand All @@ -45,13 +48,17 @@ int main(int argc, char **argv) {
data[i] = nd(gen);
}
// forward
caffe::Profiler *profiler = caffe::Profiler::Get();
profiler->TurnON();
Timer timer;
timer.Tic();
net.Forward();
timer.Toc();
profiler->TurnOFF();
profiler->DumpProfile("profile.json");
std::cout << "generate costs " << timer.Elasped() << " ms" << std::endl;
// visualization
auto images = net.blob_by_name("gact5");
auto images = net.blob_by_name("gconv5");
const int num = images->num();
const int channels = images->channels();
const int height = images->height();
Expand Down
76 changes: 76 additions & 0 deletions include/caffe/base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef CAFFE_BASE_HPP_
#define CAFFE_BASE_HPP_

#include <string>
#include <vector>
#include <memory>

#include <caffe/logging.hpp>

#ifdef _MSC_VER
#ifdef CAFFE_EXPORTS
#define CAFFE_API __declspec(dllexport)
#else
#define CAFFE_API __declspec(dllimport)
#endif
#else
#define CAFFE_API
#endif

#ifdef _MSC_VER
#pragma warning(disable:4251)
#endif

// Convert macro to string
#define STRINGIFY(m) #m
#define AS_STRING(m) STRINGIFY(m)

// Disable the copy and assignment operator for a class.
#define DISABLE_COPY_AND_ASSIGN(classname) \
private: \
classname(const classname&) = delete; \
classname(classname&&) = delete; \
classname& operator=(const classname&) = delete; \
classname& operator=(classname&&) = delete

// A simple macro to mark codes that are not implemented, so that when the code
// is executed we will see a fatal log.
#define NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented Yet"
#define NO_GPU LOG(FATAL) << "Cannot use GPU in CPU-only Caffe: check mode."

#define STUB_GPU(classname) \
void classname::Forward_gpu(const vector<Blob*>& bottom, \
const vector<Blob*>& top) { NO_GPU; }

#define STUB_GPU_FORWARD(classname, funcname) \
void classname::funcname##_##gpu(const vector<Blob*>& bottom, \
const vector<Blob*>& top) { NO_GPU; }

namespace caffe {

// Common functions and classes from std that caffe often uses.
using std::vector;
using std::string;
using std::shared_ptr;

typedef float real_t;

enum DeviceMode {
CPU, GPU
};

/*!
* \brief gpu avariable
* \return true if gpu available
*/
CAFFE_API bool GPUAvailable();
/*!
* \brief set caffe mode
* \param mode GPU or CPU
* \param device GPU device id, -1 for CPU
*/
CAFFE_API void SetMode(DeviceMode mode, int device);

} // namespace caffe

#endif // CAFFE_COMMON_HPP_
2 changes: 1 addition & 1 deletion include/caffe/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string>
#include <vector>

#include "caffe/common.hpp"
#include "caffe/base.hpp"

const int kMaxBlobAxes = 32;

Expand Down
18 changes: 17 additions & 1 deletion include/caffe/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,27 @@ CAFFE_API int CaffeNetGetBlob(NetHandle net,
BlobHandle *blob);
/*!
* \brief list all network internal data buffer
* \param net net handle
* \param n number of blobs
* \param names list of string to blob names
* \param blobs list of BlobHandle
*/
CAFFE_API int CaffeNetListBlob(NetHandle net,
int *n,
const char ***names,
BlobHandle **blobs);
/*!
* \brief list all network parameters
* \param net net handle
* \param n number of params
* \param names list of string to param names,
same layer has same name prefix
* \param params list of BlobHandle
*/
CAFFE_API int CaffeNetListParam(NetHandle net,
int *n,
const char ***names,
BlobHandle **params);

// Helper

Expand All @@ -112,4 +128,4 @@ CAFFE_API const char *CaffeGetLastError();
}
#endif // __cplusplus

#endif // CAFFE_C_API_HPP_
#endif // CAFFE_C_API_H_
Loading

0 comments on commit 082f47d

Please sign in to comment.