Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

在Fluid版本中,在预测的时候为什么还有克隆一个预测程序?

Open yeyupiaoling opened this issue 8 years ago • 6 comments

在以下的例子中,我看到在预测的时候,还克隆了一个预测程序,为什么要这做呢? https://github.com/PaddlePaddle/Paddle/blob/60783a753adc0429d796c7b77f2f4eeea23a8e15/python/paddle/fluid/tests/book/test_image_classification.py#L225-L227

在此之前就有了一个预测程序,是从参数文件中加载的,如下: https://github.com/PaddlePaddle/Paddle/blob/60783a753adc0429d796c7b77f2f4eeea23a8e15/python/paddle/fluid/tests/book/test_image_classification.py#L216-L217

同时也不知道它要在这里干什么,应该是比较吧: https://github.com/PaddlePaddle/Paddle/blob/60783a753adc0429d796c7b77f2f4eeea23a8e15/python/paddle/fluid/tests/book/test_image_classification.py#L241-L242

在预测模型的时候,它为什么又要保存模型呢? https://github.com/PaddlePaddle/Paddle/blob/60783a753adc0429d796c7b77f2f4eeea23a8e15/python/paddle/fluid/tests/book/test_image_classification.py#L246-L248

yeyupiaoling avatar May 30 '18 01:05 yeyupiaoling

https://github.com/PaddlePaddle/Paddle/blob/60783a753adc0429d796c7b77f2f4eeea23a8e15/python/paddle/fluid/tests/book/test_image_classification.py#L224

注意这一行的注释,用inference_transpiler_program的主要目的是加速计算,后面的比较也是为了验证inference_transpiler_program的正确性,最后保存的也是inference_transpiler_program。

kuke avatar May 30 '18 12:05 kuke

@kuke 都是同一个参数文件加载出来的程序,难道准确性还会不一样的?最后保存的参数文件跟之前的有会优化多少?

yeyupiaoling avatar May 30 '18 15:05 yeyupiaoling

@yeyupiaoling 因为两个program都在一个scope里,如果不clone的话,原始program在scope里的一些变量就直接发生了变化,会导致结果出错。更多细节可以看下 https://github.com/PaddlePaddle/Paddle/pull/9792 里的评论。

最后保存的参数文件跟之前的有会优化多少?

请问是指速度方面么?the elapsed time on ResNet (test_inference_image_classification) is from 11.2s to 9.3s, about 10% speedup on inference.

luotao1 avatar May 31 '18 01:05 luotao1

@luotao1

这个clone我不确定是不是还是必须的,我特意把这里注释了,单独跑例子也没有出现问题。不过这里多一遍clone也没有关系。

我没使用过clone的Program,但是我使用原始program也没见出错呢。

请问是指速度方面么?the elapsed time on ResNet (test_inference_image_classification) is from 11.2s to 9.3s, about 10% speedup on inference.

为什么会导致预测速度的提高呢?谢谢

yeyupiaoling avatar May 31 '18 02:05 yeyupiaoling

因为将batch norm的参数(weight和bias)都融合进conv的参数(weight和bias)中了,所以在预测的时候去掉了batch_norm(如果之前的conv没有bias,会增加一部分bias的计算),速度当然会快。

luotao1 avatar May 31 '18 02:05 luotao1

好的,了解了,谢谢 @luotao1

yeyupiaoling avatar May 31 '18 02:05 yeyupiaoling