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

fix latex_ocr inference #14498

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions ppocr/modeling/backbones/rec_resnetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ def __init__(
self.export = is_export
self.eps = eps

running_mean = paddle.empty([self._out_channels], dtype="float32")
Copy link
Collaborator

@GreatV GreatV Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle.empty里面的元素未初始化,会不会造成训练和输出不稳定。https://www.paddlepaddle.org.cn/documentation/api/paddle/empty_cn.html#empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经改成zeros和ones

running_variance = paddle.empty([self._out_channels], dtype="float32")
orin_shape = self.weight.shape

new_weight = F.batch_norm(
self.weight.reshape([1, self._out_channels, -1]),
running_mean,
running_variance,
momentum=0.0,
epsilon=self.eps,
use_global_stats=False,
).reshape(orin_shape)
self.weight.set_value(new_weight.numpy())

def forward(self, x):
if not self.training:
self.export = True
Expand All @@ -96,28 +110,12 @@ def forward(self, x):
x = pad_same_export(x, self._kernel_size, self._stride, self._dilation)
else:
x = pad_same(x, self._kernel_size, self._stride, self._dilation)
running_mean = paddle.to_tensor([0] * self._out_channels, dtype="float32")
running_variance = paddle.to_tensor([1] * self._out_channels, dtype="float32")
if self.export:
weight = paddle.reshape(
F.batch_norm(
self.weight.reshape([1, self._out_channels, -1]).cast(
paddle.float32
),
running_mean,
running_variance,
momentum=0.0,
epsilon=self.eps,
use_global_stats=False,
),
self.weight.shape,
)
weight = self.weight
else:
weight = paddle.reshape(
F.batch_norm(
self.weight.reshape([1, self._out_channels, -1]).cast(
paddle.float32
),
self.weight.reshape([1, self._out_channels, -1]),
running_mean,
running_variance,
training=True,
Expand Down
Loading