google / XNNPACK

High-efficiency floating-point neural network inference operators for mobile, server, and Web

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conv padding type in mobilenet models

snnn opened this issue · comments

TF has two padding types: SAME and VALID.
XNN_FLAG_TENSORFLOW_SAME_PADDING matches TF SAME.
So I guess the default one should be VALID? It will determine how output shape will be computed. I just want to be sure I have allocated enough memory for the output.

And, I got my mobilenet v1 model from https://github.com/tensorflow/models/tree/master/research/slim, which set padding type to SAME. But the one in this repo, https://github.com/google/XNNPACK/blob/master/models/fp32-mobilenet-v1.cc, didn't set the flag. However, it works similar to SAME.

For example, in the first Conv2D, according to TF doc, in TF the padding values should be calculated as:

import numpy as np

strides = np.array([1, 2, 2,1])
stride_height = strides[1]
stride_width = strides[2]
filter_height = 3
filter_width = 3
in_height = 224
in_width = 224

if (in_height % strides[1] == 0):
  pad_along_height = max(filter_height - stride_height, 0)
else:
  pad_along_height = max(filter_height - (in_height % stride_height), 0)
if (in_width % strides[2] == 0):
  pad_along_width = max(filter_width - stride_width, 0)
else:
  pad_along_width = max(filter_width - (in_width % stride_width), 0)
  
pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left

print("top=%d,bottom=%d,left=%d,right=%d" % (pad_top,pad_bottom,pad_left,pad_right))

Output:

top=0,bottom=1,left=0,right=1

It is like what you have in XNNPack.

      0 /* top padding */, 1 /* right padding */,
      1 /* bottom padding */, 0 /* left padding */,

But you didn't set the flag?

XNNPACK allows explicit padding specification, and this is what the built-in models use. If the input is never resized, there's no difference between using XNN_FLAG_TENSORFLOW_SAME_PADDING and explicitly specifying equivalent padding. If you want TF VALID padding, set all padding values to 0.

Thank you!