PaddlePaddle / Paddle

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

convert paddle to c++ occurs error ” Tensor holds the wrong type, it holds float, but desires to be int64_t“ #29935

Closed GeorgeBohw closed 2 years ago

GeorgeBohw commented 3 years ago

os environment: ubuntu18.04 with gpu 10.0,cudnn 7 paddle2.0

operation: when convert paddle ocr_attention to ++,whcih occurs error as following:

error:

C++ Call Stacks (More useful to developers):

0 std::string paddle::platform::GetTraceBackString<std::string const&>(std::string const&, char const, int) 1 paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const, int) 2 long const paddle::framework::Tensor::data() const 3 paddle::operators::LookupTableV2CUDAKernel::Compute(paddle::framework::ExecutionContext const&) const 4 std::_Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CUDAPlace, false, 0ul, paddle::operators::LookupTableV2CUDAKernel, paddle::operators::LookupTableV2CUDAKernel, paddle::operators::LookupTableV2CUDAKernel >::operator()(char const, char const, int) const::{lambda(paddle::framework::ExecutionContext const&)#1}>::_M_invoke(std::_Any_data const&, paddle::framework::ExecutionContext const&) 5 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&, paddle::framework::RuntimeContext) const 6 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&) const 7 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, paddle::platform::Place const&) 8 paddle::framework::Executor::RunPartialPreparedContext(paddle::framework::ExecutorPrepareContext, paddle::framework::Scope, long, long, bool, bool, bool) 9 paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext, paddle::framework::Scope, bool, bool, bool) 10 paddle::operators::WhileOp::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&) const 11 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, paddle::platform::Place const&) 12 paddle::framework::NaiveExecutor::Run() 13 paddle::AnalysisPredictor::ZeroCopyRun()


Python Call Stacks (More useful to users):

File "infer.py", line 182, in main() File "infer.py", line 176, in main inference(args) File "infer.py", line 60, in inference ids = infer(images, num_classes, use_cudnn=True if args.use_gpu else False) File "/home/george/my_ocr_recognition/models/PaddleCV/ocr_recognition/attention_model.py", line 291, in attention_infer pre_ids_emb = fluid.embedding( File "", line 2, in embedding File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in impl return wrapped_func(*args, kwargs) File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/framework.py", line 232, in impl return func(*args, *kwargs) File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/input.py", line 322, in embedding helper.append_op( File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op return self.main_program.current_block().append_op(args, kwargs) File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/framework.py", line 2840, in append_op op = Operator( File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/framework.py", line 1931, in init for frame in traceback.extract_stack():

Error Message Summary:

InvalidArgumentError: Tensor holds the wrong type, it holds float, but desires to be int64_t. [Hint: Expected valid == true, but received valid:0 != true:1.] at (/home/george/paddle/paddle/fluid/framework/tensor_impl.h:33) [operator < lookup_table_v2 > error] Aborted (core dumped)

我该怎么修改,我改写了attention_model.py中的def attention_infer()里的pre_ids_emb = fluid.embedding相关参数,怎么搞都没有成功,def attention_infer函数如下:

def attention_infer(images, num_classes, use_cudnn=True):

max_length = 20
gru_backward, encoded_vector, encoded_proj = encoder_net(
    images, is_test=True, use_cudnn=use_cudnn)

backward_first = fluid.layers.sequence_pool(
    input=gru_backward, pool_type='first')
decoder_boot = fluid.layers.fc(input=backward_first,
                               size=decoder_size,
                               bias_attr=False,
                               act="relu")
init_state = decoder_boot
array_len = fluid.layers.fill_constant(
    shape=[1], dtype='int64', value=max_length)
counter = fluid.layers.zeros(shape=[1], dtype='int64', force_cpu=True)

# fill the first element with init_state
state_array = fluid.layers.create_array('float32')
fluid.layers.array_write(init_state, array=state_array, i=counter)

# ids, scores as memory
ids_array = fluid.layers.create_array('int64')
scores_array = fluid.layers.create_array('float32')

init_ids = fluid.data(
    name="init_ids", shape=[None, 1], dtype="int64", lod_level=2)
init_scores = fluid.data(
    name="init_scores", shape=[None, 1], dtype="float32", lod_level=2)

fluid.layers.array_write(init_ids, array=ids_array, i=counter)
fluid.layers.array_write(init_scores, array=scores_array, i=counter)

cond = fluid.layers.less_than(x=counter, y=array_len)
while_op = fluid.layers.While(cond=cond)
with while_op.block():
    pre_ids = fluid.layers.array_read(array=ids_array, i=counter)
    pre_state = fluid.layers.array_read(array=state_array, i=counter)
    pre_score = fluid.layers.array_read(array=scores_array, i=counter)

    #pre_ids = paddle.reshape(pre_ids, shape=[-1,1])
    print('----------------')
    print(pre_ids)
    #pre_ids = fluid.layers.cast(x=pre_ids, dtype='float32')
    print('----------------')
    pre_ids_emb = fluid.embedding(
        input=pre_ids,
        size=[num_classes + 2, word_vector_dim],
        dtype='float32')

    context = simple_attention(encoded_vector, encoded_proj, pre_state,
                               decoder_size)

    # expand the recursive_sequence_lengths of pre_state to be the same with pre_score
    pre_state_expanded = fluid.layers.sequence_expand(pre_state, pre_score)
    context_expanded = fluid.layers.sequence_expand(context, pre_score)
    fc_1 = fluid.layers.fc(input=context_expanded,
                           size=decoder_size * 3,
                           bias_attr=False)
    fc_2 = fluid.layers.fc(input=pre_ids_emb,
                           size=decoder_size * 3,
                           bias_attr=False)

    decoder_inputs = fc_1 + fc_2
    current_state, _, _ = fluid.layers.gru_unit(
        input=decoder_inputs,
        hidden=pre_state_expanded,
        size=decoder_size * 3)

    current_state_with_lod = fluid.layers.lod_reset(
        x=current_state, y=pre_score)
    # use score to do beam search
    current_score = fluid.layers.fc(input=current_state_with_lod,
                                    size=num_classes + 2,
                                    bias_attr=True,
                                    act='softmax')
    topk_scores, topk_indices = fluid.layers.topk(
        current_score, k=beam_size)

    # calculate accumulated scores after topk to reduce computation cost
    accu_scores = fluid.layers.elementwise_add(
        x=fluid.layers.log(topk_scores),
        y=fluid.layers.reshape(
            pre_score, shape=[-1]),
        axis=0)
    selected_ids, selected_scores = fluid.layers.beam_search(
        pre_ids,
        pre_score,
        topk_indices,
        accu_scores,
        beam_size,
        eos,  # end_id
        #level=0
    )

    fluid.layers.increment(x=counter, value=1, in_place=True)

    # update the memories
    fluid.layers.array_write(current_state, array=state_array, i=counter)
    fluid.layers.array_write(selected_ids, array=ids_array, i=counter)
    fluid.layers.array_write(selected_scores, array=scores_array, i=counter)

    # update the break condition: up to the max length or all candidates of
    # source sentences have ended.
    length_cond = fluid.layers.less_than(x=counter, y=array_len)
    finish_cond = fluid.layers.logical_not(
        fluid.layers.is_empty(x=selected_ids))
    fluid.layers.logical_and(x=length_cond, y=finish_cond, out=cond)

ids, scores = fluid.layers.beam_search_decode(ids_array, scores_array,
                                              beam_size, eos)
return ids

infer.py的程序如下: def inference(args): """OCR inference""" if args.model == "crnn_ctc": infer = ctc_infer get_feeder_data = get_ctc_feeder_for_infer else: infer = attention_infer get_feeder_data = get_attention_feeder_for_infer eos = 1 sos = 0 num_classes = data_reader.num_classes() data_shape = data_reader.data_shape()

define network

if len(list(data_shape)) == 3:
    data_shape = [None] + list(data_shape)
images = fluid.data(name='pixel', shape=data_shape, dtype='float32')
ids = infer(images, num_classes, use_cudnn=True if args.use_gpu else False)
# data reader
infer_reader = data_reader.inference(
    batch_size=args.batch_size,
    infer_images_dir=args.input_images_dir,
    infer_list_file=args.input_images_list,
    cycle=True if args.iterations > 0 else False,
    model=args.model)
# prepare environment
place = fluid.CPUPlace()
if args.use_gpu:
    place = fluid.CUDAPlace(0)

exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

# load dictionary
dict_map = None
if args.dict is not None and os.path.isfile(args.dict):
    dict_map = {}
    with open(args.dict) as dict_file:
        for i, word in enumerate(dict_file):
            dict_map[i] = word.strip()
    print("Loaded dict from %s" % args.dict)

# load init model
model_dir = args.model_path
fluid.load(
    program=fluid.default_main_program(),
    model_path=model_dir,
    executor=exe,
    var_list=fluid.io.get_program_parameter(fluid.default_main_program()))
print("Init model from: %s." % args.model_path)

fluid.io.save_inference_model(
    dirname="./mobilenet/",
    feeded_var_names=['pixel'],
    target_vars=[ids],
    executor=exe,
    model_filename='model',
    params_filename='params')

batch_times = []
iters = 0
for data in infer_reader():
    feed_dict = get_feeder_data(data, place)
    if args.iterations > 0 and iters == args.iterations + args.skip_batch_num:
        break
    if iters < args.skip_batch_num:
        print("Warm-up itaration")
    if iters == args.skip_batch_num:
        profiler.reset_profiler()

    start = time.time()
    result = exe.run(fluid.default_main_program(),
                     feed=feed_dict,
                     fetch_list=[ids],
                     return_numpy=False)
    indexes = prune(np.array(result[0]).flatten(), 0, 1)
    batch_time = time.time() - start
    fps = args.batch_size / batch_time
    batch_times.append(batch_time)
    if dict_map is not None:
        print("Iteration %d, latency: %.5f s, fps: %f, result: %s" % (
            iters,
            batch_time,
            fps,
            [dict_map[index] for index in indexes], ))
    else:
        print("Iteration %d, latency: %.5f s, fps: %f, result: %s" % (
            iters,
            batch_time,
            fps,
            indexes, ))

    iters += 1

latencies = batch_times[args.skip_batch_num:]
latency_avg = np.average(latencies)
latency_pc99 = np.percentile(latencies, 99)
fpses = np.divide(args.batch_size, latencies)
fps_avg = np.average(fpses)
fps_pc99 = np.percentile(fpses, 1)

# Benchmark output
print('\nTotal examples (incl. warm-up): %d' % (iters * args.batch_size))
print('average latency: %.5f s, 99pc latency: %.5f s' % (latency_avg,
                                                         latency_pc99))
print('average fps: %.5f, fps for 99pc latency: %.5f' % (fps_avg, fps_pc99))

谁可以告诉我,谢谢!

paddle-bot-old[bot] commented 3 years ago

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

willthefrog commented 3 years ago

建议提到paddleocr的repo,方便相关同学后续跟进。

paddle-bot-old[bot] commented 2 years ago

Since you haven\'t replied for more than a year, we have closed this issue/pr. If the problem is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up. 由于您超过一年未回复,我们将关闭这个issue/pr。 若问题未解决或有后续问题,请随时重新打开,我们会继续跟进。