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

Need to normalize #4

Closed
qzchenwl opened this issue Jan 6, 2018 · 2 comments
Closed

Need to normalize #4

qzchenwl opened this issue Jan 6, 2018 · 2 comments

Comments

@qzchenwl
Copy link

qzchenwl commented Jan 6, 2018

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/_impl/keras/applications/imagenet_utils.py#L82

def _preprocess_symbolic_input(x, data_format, mode):
  """Preprocesses a symbolic image tensor.
  Arguments:
      x: symoblic tensor, 3D or 4D.
      data_format: data format of the image tensor.
      mode: One of "caffe", "tf".
          - caffe: will convert the images from RGB to BGR,
              then will zero-center each color channel with
              respect to the ImageNet dataset,
              without scaling.
          - tf: will scale pixels between -1 and 1,
              sample-wise.
  Returns:
      Preprocessed tensor.
  """
  global _IMAGENET_MEAN

  if mode == 'tf':
    x /= 127.5
    x -= 1.
    return x

  if data_format == 'channels_first':
    # 'RGB'->'BGR'
    if K.ndim(x) == 3:
      x = x[::-1, ...]
    else:
      x = x[:, ::-1, ...]
  else:
    # 'RGB'->'BGR'
    x = x[..., ::-1]

  if _IMAGENET_MEAN is None:
    _IMAGENET_MEAN = K.constant(-np.array([103.939, 116.779, 123.68]))
  # Zero-center by mean pixel
  if K.dtype(x) != K.dtype(_IMAGENET_MEAN):
    x = K.bias_add(x, K.cast(_IMAGENET_MEAN, K.dtype(x)), data_format)
  else:
    x = K.bias_add(x, _IMAGENET_MEAN, data_format)
  return x

https://github.com/titu1994/neural-image-assessment/blob/master/data_loader.py#L59

  1. Which order RGB/BGR does MobileNet accept?
  2. Should we use IMAGENET_MEAN = [103.939, 116.779, 123.68] or just [127, 127, 127]?
@qzchenwl
Copy link
Author

qzchenwl commented Jan 6, 2018

I see keras.applications.mobilenet.preprocess_input in evaluate_mobilenet, but not in data_loader. Why not preprocess_input before training?

@titu1994
Copy link
Owner

titu1994 commented Jan 6, 2018

MobileNet preprocess_input is in -1 to 1 range. I use that directly for evaluation purposes.

For the data loaders, the two parse methods that map the file path to an image also have the img - 127.5 / 127.5 line which normalizes to -1 to 1 range.

@titu1994 titu1994 closed this as completed Jan 6, 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

2 participants