Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何处理网络模型中的BatchNorm层? #3

Closed
wuxler opened this issue Nov 2, 2017 · 4 comments
Closed

如何处理网络模型中的BatchNorm层? #3

wuxler opened this issue Nov 2, 2017 · 4 comments

Comments

@wuxler
Copy link

wuxler commented Nov 2, 2017

我想用TensorRT来加速YOLO v2模型,但是YOLO v2模型中包含有BatchNorm层,程序会在ICudaEngine* engine = builder->buildCudaEngine(*network)这一步中断报错,官方文档中指的Batch Normalization可以用Scale层代替,具体应该怎么做呢?

@rencheng123456
Copy link

你好,我最近也在研究使用TensorRT来加速YOLO v2模型,想问下,但不知道如何着手。希望能给予我一些指导

@jsmlay
Copy link

jsmlay commented Dec 27, 2017

TensorRT 3.0 Have YOLO plugins headers. Plz double check before usage.
Find it in NvInferPlugin.h in /usr/src/TensorRT/include.
It need Jetpack 3.2 support.
When deploy it, you need to write plugin, plz refer to FasterRCNN sample.
Sorry for using English because i'm on TX2.

@LitLeo
Copy link
Owner

LitLeo commented Dec 27, 2017

抱歉最近没怎么关注git。BN的实现可以用scale层代替,直接将BN层的公式转换成scale层公式就可以了。我之前的实现代码如下,仅供参考。

                float *scale_values = new float[l_ptr->out_c];
                float *shift_values = new float[l_ptr->out_c];
                for (int i = 0; i < l_ptr->out_c; ++i)
                {
                    scale_values[i] = 1 / (sqrt(l_ptr->rolling_variance[i] + .00001f)) * l_ptr->scales[i];
                    shift_values[i] = -l_ptr->rolling_mean[i] / (sqrt(l_ptr->rolling_variance[i] + .00001f)) * l_ptr->scales[i] + l_ptr->biases[i];
                }
                Weights scale {DataType::kFLOAT, scale_values, l_ptr->out_c};
                Weights shift {DataType::kFLOAT, shift_values, l_ptr->out_c};
                Weights power {DataType::kFLOAT, nullptr, 0};

                auto scale_1 = network->addScale(*inputs_v[0], ScaleMode::kCHANNEL, shift, scale, power);
                assert(scale_1 != nullptr);
                inputs_v[0] = scale_1->getOutput(0);

@wuxler
Copy link
Author

wuxler commented Dec 27, 2017

已经解决了,非常感谢!

@LitLeo LitLeo closed this as completed Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants