PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
22.24k stars 5.58k forks source link

TypeError: object of type 'function' has no len() #9208

Closed shboy closed 6 years ago

shboy commented 6 years ago
import sys, os

import paddle.v2 as paddle
import time

from STResNet import STResNet

def main():
    datadim_c = 6 * 234 * 236 
    datadim_p = 2 * 234 * 236 
    datadim_t = 2 * 234 * 236 
    datadim_ex = 31

    resdim = 2 * 234 * 236 

    if len(sys.argv) == 1:
        print('self def errror: input fomat error')
        print('need to specify  residual units')
        sys.exit(-1)
    else:
        nb_residual_unit = int(sys.argv[1])  # number of residual units

    # PaddlePaddle init
    paddle.init(use_gpu=True)

    input_c = paddle.layer.data(
        name="input_c", type=paddle.data_type.dense_vector(datadim_c), width=234, height=236)
    input_p = paddle.layer.data(
        name="input_p", type=paddle.data_type.dense_vector(datadim_p), width=234, height=236)
    input_t = paddle.layer.data(
        name="input_t", type=paddle.data_type.dense_vector(datadim_t), width=234, height=236)
    input_ex = paddle.layer.data(
        name="input_ex", type=paddle.data_type.dense_vector(datadim_ex))

    # Add neural network config
    # option 1. resnet
    main_output = STResNet(input_c, input_p, input_t, input_ex, external_dim=datadim_ex, nb_residual_unit=nb_residual_unit)

    def infer_data():
        def reader():
            #import numpy as np
            with open('/work/shenhao/test_data/event_day=2017010406070912_shuffle_test/2017010406070912', 'r') as f:
                line = f.readline().strip(' ').split()
                X_C = line[1: 331345]
                #X_C = np.asarray(X_C)
                X_P = line[331345: 331345+110448]
                #X_P = np.asarray(X_P)
                X_T = line[441793: 441793+110448]
                #X_T = np.asarray(X_T)
                X_E = line[552241: 552241+31]
                #X_E = np.asarray(X_E)
                return X_C, X_P, X_T, X_E
        return reader
    para_path = '/work/shenhao/paddle/STResNet_network_ok/params_pass_99.tar'

    with open(para_path, "r") as f:
        parameters = paddle.parameters.Parameters.from_tar(f)
    img = paddle.infer(
        output_layer=main_output,
        parameters=parameters,
        input=infer_data(),
        feeding={'input_c': 0,
               'input_p': 1,
               'input_t': 2,
               'input_ex': 3})
    print img

if __name__ == '__main__':
    ts_batch = time.time()
    ts_pass = time.time()
    main()
shboy commented 6 years ago

我想infer出结果 结果这样写会报错:

Traceback (most recent call last): File "train_infer.py", line 95, in main() File "train_infer.py", line 89, in main 'input_ex': 3}) File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 172, in infer return inferer.infer(field=field, input=input, feeding=feeding) File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 107, in infer for result in self.iter_infer_field(field=field, kwargs): File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 94, in iter_infer_field for result in self.iter_infer(kwargs): File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 77, in iter_infer batch_size = len(input) TypeError: object of type 'function' has no len()

Yancey1989 commented 6 years ago

input参数的类型需要是一个Python的List,而不是一个callable function. 参数说明: http://paddlepaddle.org/docs/develop/api/en/run_logic.html?highlight=infer#inference 样例: https://github.com/PaddlePaddle/book/blob/develop/01.fit_a_line/infer.py#L11

shboy commented 6 years ago
with open('/work/shenhao/test_data/event_day=2017010406070912_shuffle_test/2017010406070912', 'r') as f:
    line = f.readline().strip(' ').split()
    X_C = line[1: 331345]
    #X_C = np.asarray(X_C)
    X_P = line[331345: 331345+110448]
    #X_P = np.asarray(X_P)
    X_T = line[441793: 441793+110448]
    #X_T = np.asarray(X_T)
    X_E = line[552241: 552241+31]
    #X_E = np.asarray(X_E)
    global data_infer
    data_infer = []
    data_infer.append(X_C)
    data_infer.append(X_P)
    data_infer.append(X_T)
    data_infer.append(X_E)

# Create parameters
para_path = '/work/shenhao/paddle/STResNet_network_ok/params_pass_99.tar'

print len(data_infer)
with open(para_path, "r") as f:
    parameters = paddle.parameters.Parameters.from_tar(f)
img = paddle.infer(
    output_layer=main_output, 
    parameters=parameters, 
    input=[item for item in data_infer],
    feeding={'input_c': 0,
           'input_p': 1,
           'input_t': 2,
           'input_ex': 3}) 
print img
shboy commented 6 years ago

我改成这样了 报了另外的错

Traceback (most recent call last):
  File "train_infer.py", line 80, in <module>
    main()
  File "train_infer.py", line 74, in main
    'input_ex': 3})
  File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 172, in infer
    return inferer.infer(field=field, input=input, feeding=feeding)
  File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 107, in infer
    for result in self.iter_infer_field(field=field, **kwargs):
  File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 94, in iter_infer_field
    for result in self.iter_infer(**kwargs):
  File "/usr/local/lib/python2.7/dist-packages/paddle/v2/inference.py", line 87, in iter_infer
    yield self.__gradient_machine__.forwardTest(feeder(data_batch))
  File "/usr/local/lib/python2.7/dist-packages/py_paddle/dataprovider_converter.py", line 282, in __call__
    return self.convert(dat, argument)
  File "/usr/local/lib/python2.7/dist-packages/paddle/v2/data_feeder.py", line 133, in convert
    return DataProviderConverter.convert(self, reorder_data(dat), argument)
  File "/usr/local/lib/python2.7/dist-packages/py_paddle/dataprovider_converter.py", line 267, in convert
    scanner.pre_scan(each_step)
  File "/usr/local/lib/python2.7/dist-packages/py_paddle/dataprovider_converter.py", line 114, in pre_scan
    "The input should be a vector, please check your input data."
ValueError: The input should be a vector, please check your input data.

@Yancey1989

Yancey1989 commented 6 years ago

请按照demo和文档检查下输入的格式,应该是预测数据的List,例如你每天待预测的数据是类似

(1,2,3,4)

这种,那么你的input应该是类似:

[
    (1,2,3,4),
    (3,4,5,5),
    ...
]
Yancey1989 commented 6 years ago

With a short discuss offline this issue has been fixed, and I'll close this issue, please feel free to reopen it.