Skip to content

Mobilenet v2 width multiplier incorrect #973

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

Closed
yaysummeriscoming opened this issue May 30, 2019 · 2 comments · Fixed by #1005
Closed

Mobilenet v2 width multiplier incorrect #973

yaysummeriscoming opened this issue May 30, 2019 · 2 comments · Fixed by #1005

Comments

@yaysummeriscoming
Copy link
Contributor

There seems to be a small issue with the width multiplier in mobilenet v2. The official implementation rounds filter channels to a multiple of 8.

For example, mobilenet v2 width 1.4. The first conv layer has 44 channels as opposed to 48 in the official implementation:
model = torchvision.models.mobilenet_v2(width_mult=1.4)

for module in model.modules():
if isinstance(module, nn.Conv2d):
print(module.weight.shape)

torch.Size([44, 3, 3, 3])
torch.Size([44, 1, 3, 3])
torch.Size([22, 44, 1, 1])
torch.Size([132, 22, 1, 1])
torch.Size([132, 1, 3, 3])
torch.Size([33, 132, 1, 1])

Corresponding tensorflow 2.0 keras code:
model = tf.keras.applications.MobileNetV2(
weights="imagenet", input_shape=(224, 224, 3), alpha=1.4)
model.summary()


Layer (type) Output Shape Param # Connected to

input_1 (InputLayer) [(None, 224, 224, 3) 0


Conv1_pad (ZeroPadding2D) (None, 225, 225, 3) 0 input_1[0][0]


Conv1 (Conv2D) (None, 112, 112, 48) 1296 Conv1_pad[0][0]


bn_Conv1 (BatchNormalization) (None, 112, 112, 48) 192 Conv1[0][0]


Conv1_relu (ReLU) (None, 112, 112, 48) 0 bn_Conv1[0][0]


expanded_conv_depthwise (Depthw (None, 112, 112, 48) 432 Conv1_relu[0][0]


expanded_conv_depthwise_BN (Bat (None, 112, 112, 48) 192 expanded_conv_depthwise[0][0]


expanded_conv_depthwise_relu (R (None, 112, 112, 48) 0 expanded_conv_depthwise_BN[0][0]


expanded_conv_project (Conv2D) (None, 112, 112, 24) 1152 expanded_conv_depthwise_relu[0][0


expanded_conv_project_BN (Batch (None, 112, 112, 24) 96 expanded_conv_project[0][0]


block_1_expand (Conv2D) (None, 112, 112, 144 3456 expanded_conv_project_BN[0][0]


block_1_expand_BN (BatchNormali (None, 112, 112, 144 576 block_1_expand[0][0]


block_1_expand_relu (ReLU) (None, 112, 112, 144 0 block_1_expand_BN[0][0]


block_1_pad (ZeroPadding2D) (None, 113, 113, 144 0 block_1_expand_relu[0][0]


block_1_depthwise (DepthwiseCon (None, 56, 56, 144) 1296 block_1_pad[0][0]


block_1_depthwise_BN (BatchNorm (None, 56, 56, 144) 576 block_1_depthwise[0][0]


block_1_depthwise_relu (ReLU) (None, 56, 56, 144) 0 block_1_depthwise_BN[0][0]


block_1_project (Conv2D) (None, 56, 56, 32) 4608 block_1_depthwise_relu[0][0]


I've implemented a fix here:
https://github.com/yaysummeriscoming/vision/blob/master/torchvision/models/mobilenet.py

Can I merge it in?

@fmassa
Copy link
Member

fmassa commented May 31, 2019

Thanks for spotting this difference!

Yes, sending a PR would be great!

One question: does it change the behavior of the model for width_multiplier=1?

@yaysummeriscoming
Copy link
Contributor Author

Ok cool, as the base network widths are already multiples of 8, width multiplier 1 is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants