Skip to content

Fix static padding calculation#235

Merged
lukemelas merged 1 commit into
lukemelas:masterfrom
AlexDut:fix/static-conv
Oct 9, 2020
Merged

Fix static padding calculation#235
lukemelas merged 1 commit into
lukemelas:masterfrom
AlexDut:fix/static-conv

Conversation

@AlexDut
Copy link
Copy Markdown

@AlexDut AlexDut commented Oct 8, 2020

Hello,

I was testing the models and found that the results were different between the EfficientNet model with static padding convolutions and the EfficientNet model with dynamic padding convolutions.
When I looked into the code I noticed that there was a difference in the padding's calculation.

The appropriate calculation is the one used for the dynamic padding ie:
[pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2] (code)

You can test it with the quick & dirty script I made:

import torch
import torchvision.transforms as transforms
from efficientnet_pytorch import EfficientNet

# Tensorflow-like EfficientNet pre-process step
test_input = torch.ones((1, 224, 224, 3))
test_input = test_input/255
mean = torch.Tensor([0.485, 0.456, 0.406])
std = torch.Tensor([0.229, 0.224, 0.225])
test_input = (test_input - mean) / torch.sqrt(std)
test_input = test_input.permute(0,3,2,1)

model = EfficientNet.from_pretrained('efficientnet-b0', image_size=None)  # Model using Conv2dDynamicSamePadding
model.eval()

model_wrong = EfficientNet.from_pretrained('efficientnet-b0') # Model using Conv2dStaticSamePadding
model_wrong.eval()

print(model(test_input).sum())
print(model_wrong(test_input).sum())

@lukemelas
Copy link
Copy Markdown
Owner

Thank you so much for spotting this! Merging.

@lukemelas lukemelas merged commit d7a4b3d into lukemelas:master Oct 9, 2020
ulosc pushed a commit to ulosc/efficientnet that referenced this pull request Aug 18, 2021
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

Successfully merging this pull request may close these issues.

2 participants