Skip to content

Commit

Permalink
Merge branch 'master' into LazyInstallPaddleWheels
Browse files Browse the repository at this point in the history
  • Loading branch information
reyoung authored Sep 9, 2016
2 parents ddb0f8a + 30bb383 commit 6ef0b7c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 deletions.
9 changes: 4 additions & 5 deletions demo/image_classification/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

import paddle.utils.image_util as image_util

from py_paddle import swig_paddle, util
from py_paddle import DataProviderWrapperConverter
from paddle.trainer.PyDataProviderWrapper import DenseSlot
from py_paddle import swig_paddle, DataProviderConverter
from paddle.trainer.PyDataProvider2 import dense_vector
from paddle.trainer.config_parser import parse_config

logging.basicConfig(format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
Expand Down Expand Up @@ -75,8 +74,8 @@ def __init__(self,
self.network.loadParameters(self.model_dir)

data_size = 3 * self.crop_dims[0] * self.crop_dims[1]
slots = [DenseSlot(data_size)]
self.converter = util.DataProviderWrapperConverter(False, slots)
slots = [dense_vector(data_size)]
self.converter = DataProviderConverter(slots)

def get_data(self, img_path):
"""
Expand Down
10 changes: 4 additions & 6 deletions demo/model_zoo/resnet/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

import paddle.utils.image_util as image_util

from py_paddle import swig_paddle, util
from py_paddle import DataProviderWrapperConverter
from paddle.trainer.PyDataProviderWrapper import DenseSlot
from py_paddle import swig_paddle, DataProviderConverter
from paddle.trainer.PyDataProvider2 import dense_vector
from paddle.trainer.config_parser import parse_config

logging.basicConfig(format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
Expand Down Expand Up @@ -85,9 +84,8 @@ def __init__(self, train_conf, model_dir=None,
self.network.loadParameters(self.model_dir)

data_size = 3 * self.crop_dims[0] * self.crop_dims[1]
slots = [DenseSlot(data_size)]
is_sequence = False
self.converter = util.DataProviderWrapperConverter(is_sequence, slots)
slots = [dense_vector(data_size)]
self.converter = DataProviderConverter(slots)

def get_data(self, img_path):
"""
Expand Down
22 changes: 14 additions & 8 deletions demo/semantic_role_labeling/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import os
import numpy as np
from optparse import OptionParser
from py_paddle import swig_paddle, util, DataProviderWrapperConverter
from paddle.trainer.PyDataProviderWrapper import IndexSlot
from py_paddle import swig_paddle, DataProviderConverter
from paddle.trainer.PyDataProvider2 import integer_value_sequence
from paddle.trainer.config_parser import parse_config
"""
Usage: run following command to show help message.
python predict.py -h
python predict.py -h
"""
UNK_IDX = 0

Expand All @@ -43,16 +43,22 @@ def __init__(self, train_conf, dict_file, model_dir, label_file):

conf = parse_config(
train_conf,
'dict_len=' + str(len_dict) +
'dict_len=' + str(len_dict) +
',label_len=' + str(len_label) +
',is_predict=True')
self.network = swig_paddle.GradientMachine.createFromConfigProto(
conf.model_config)
self.network.loadParameters(model_dir)

slots = [IndexSlot(len_dict), IndexSlot(len_dict), IndexSlot(len_dict),
IndexSlot(len_dict), IndexSlot(len_dict), IndexSlot(2)]
self.converter = util.DataProviderWrapperConverter(True, slots)
slots = [
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(2)
]
self.converter = DataProviderConverter(slots)

def load_dict_label(self, dict_file, label_file):
"""
Expand Down Expand Up @@ -109,7 +115,7 @@ def predict(self, data_file):


def option_parser():
usage = ("python predict.py -c config -w model_dir "
usage = ("python predict.py -c config -w model_dir "
"-d word dictionary -l label_file -i input_file")
parser = OptionParser(usage="usage: %s [options]" % usage)
parser.add_option(
Expand Down
10 changes: 5 additions & 5 deletions demo/sentiment/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import os
import numpy as np
from optparse import OptionParser
from py_paddle import swig_paddle, util, DataProviderWrapperConverter
from paddle.trainer.PyDataProviderWrapper import IndexSlot
from py_paddle import swig_paddle, DataProviderConverter
from paddle.trainer.PyDataProvider2 import integer_value_sequence
from paddle.trainer.config_parser import parse_config

"""
Usage: run following command to show help message.
python predict.py -h
python predict.py -h
"""

class SentimentPrediction():
Expand All @@ -46,8 +46,8 @@ def __init__(self, train_conf, dict_file, model_dir=None, label_file = None):
conf = parse_config(train_conf, "is_predict=1")
self.network = swig_paddle.GradientMachine.createFromConfigProto(conf.model_config)
self.network.loadParameters(self.model_dir)
slots = [IndexSlot(self.dict_dim)]
self.converter = util.DataProviderWrapperConverter(True, slots)
slots = [integer_value_sequence(self.dict_dim)]
self.converter = DataProviderConverter(slots)

def load_dict(self):
"""
Expand Down
34 changes: 21 additions & 13 deletions doc_cn/ui/predict/swig_py_paddle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ PaddlePaddle目前使用Swig对其常用的预测接口进行了封装,使在P
* 准备数据
* 预测

典型的预测代码如下,使用mnist手写识别作为样例。
典型的预测代码如下,使用mnist手写识别作为样例, 完整代码见
:code:`src_root/doc/ui/predict/predict_sample.py` 。

.. literalinclude:: ../../../doc/ui/predict/predict_sample.py
:language: python
:linenos:

主要的软件包为py_paddle.swig_paddle,这个软件包文档相对完善。可以使用python的 :code:`help()` 函数查询文档。主要步骤为:

* 在程序开始阶段,使用命令行参数初始化PaddlePaddle
* 在98行载入PaddlePaddle的训练文件。读取config
* 在100行创建神经网络,并在83行载入参数。
* 103行创建一个从工具类,用来转换数据。
:lines: 15-18,90-100,101-104

主要的软件包为py_paddle.swig_paddle,这个软件包文档相对完善。可以使用python的
:code:`help()` 函数查询文档。主要步骤为:

* 在程序开始阶段,使用 :code:`swig_paddle.initPaddle()` 传入命令行参数初始化
PaddlePaddle。详细的命令行参数请参考
`命令行参数 <../cmd_argument/detail_introduction.html>`_ 。
* 接下来使用 :code:`parse_config()` 解析训练时的配置文件。这里要注意预测数据通常
不包含label, 而且预测网络通常直接输出最后一层的结果而不是像训练时一样以cost
layer作为输出,所以用于预测的配置文件要做相应的修改。
* 使用 :code:`swig_paddle.GradientMachine.createFromConfigproto()` 根据上一步解
析好的配置创建神经网络。
* 创建一个 :code:`DataProviderConverter` 对象converter。
- swig_paddle接受的原始数据是C++的Matrix,也就是直接写内存的float数组。
- 这个接口并不用户友好。所以,我们提供了一个工具类DataProviderWrapperConverter.
- 这个工具类接收和PyDataProviderWrapper一样的输入数据,请参考PyDataProviderWrapper的文档。
* 在第105行执行预测。forwardTest是一个工具类,直接提取出神经网络Output层的输出结果。典型的输出结果为\:
这个接口并不用户友好。所以,我们提供了一个工具类DataProviderConverter。
这个工具类接收和PyDataProvider2一样的输入数据,详情请参考
`PyDataProvider2文档 <../../../doc/ui/data_provider/pydataprovider2.html>`_ 。
* 最后使用 :code:`forwardTest()` 直接提取出神经网络Output层的输出结果。典型的输出结果为\:

.. code-block:: text
Expand All @@ -37,4 +45,4 @@ PaddlePaddle目前使用Swig对其常用的预测接口进行了封装,使在P
2.70634608e-08, 3.48565123e-08, 5.25639710e-09,
4.48684503e-08]], dtype=float32)}]
其中,value即为softmax层的输出。由于数据是两个,所以输出的value
其中,value即为softmax层的输出。由于数据是两条,所以输出的value包含两个向量 
1 change: 1 addition & 0 deletions paddle/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ HPPL_ERROR_LOG
unittest.list
proto
dist
setup.py
10 changes: 6 additions & 4 deletions paddle/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
set(AVX_SOURCES
src/hl_math.cc
src/hl_avx_functions.cc
)
set(CUDA_SOURCES
src/hl_time.cc
src/hl_math.cc
src/hl_cpu_functions.cc
src/hl_avx_functions.cc)
${AVX_SOURCES})

set(CUDA_CXX_WITH_GPU_SOURCES
src/hl_cuda_cublas.cc
Expand All @@ -12,7 +15,7 @@ set(CUDA_CXX_WITH_GPU_SOURCES
set_source_files_properties(${CUDA_CXX_WITH_GPU_SOURCES}
PROPERTIES COMPILE_FLAGS "-D__NVCC__")

set_source_files_properties(${CUDA_SOURCES}
set_source_files_properties(${AVX_SOURCES}
PROPERTIES COMPILE_FLAGS "-mavx")

set(CUDA_DSO_SOURCES
Expand Down Expand Up @@ -73,4 +76,3 @@ endif()

add_style_check_target(paddle_cuda ${CUDA_SOURCES})
add_style_check_target(paddle_cuda ${CUDA_HEADERS})
# add_style_check_target(hppl ${HPPL_CU_SOURCES}) # TODO(yuyang18): Format hppl style

0 comments on commit 6ef0b7c

Please sign in to comment.