|
- # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ==============================================================================
-
- def preprocess_input(x, data_format=None, mode=None):
- if mode == 'tf':
- x /= 127.5
- x -= 1.
- return x
- elif mode == 'torch':
- x /= 255.
- mean = [0.485, 0.456, 0.406]
- std = [0.229, 0.224, 0.225]
- elif mode == 'caffe':
- if data_format == 'channels_first':
- # 'RGB'->'BGR'
- if x.ndim == 3:
- x = x[::-1, ...]
- else:
- x = x[:, ::-1, ...]
- else:
- # 'RGB'->'BGR'
- x = x[..., ::-1]
- mean = [103.939, 116.779, 123.68]
- std = None
- elif mode == 'tfhub':
- x /= 255.
- return x
- else:
- return x
-
- # Zero-center by mean pixel
- if data_format == 'channels_first':
- if x.ndim == 3:
- x[0, :, :] -= mean[0]
- x[1, :, :] -= mean[1]
- x[2, :, :] -= mean[2]
- if std is not None:
- x[0, :, :] /= std[0]
- x[1, :, :] /= std[1]
- x[2, :, :] /= std[2]
- else:
- x[:, 0, :, :] -= mean[0]
- x[:, 1, :, :] -= mean[1]
- x[:, 2, :, :] -= mean[2]
- if std is not None:
- x[:, 0, :, :] /= std[0]
- x[:, 1, :, :] /= std[1]
- x[:, 2, :, :] /= std[2]
- else:
- x[..., 0] -= mean[0]
- x[..., 1] -= mean[1]
- x[..., 2] -= mean[2]
- if std is not None:
- x[..., 0] /= std[0]
- x[..., 1] /= std[1]
- x[..., 2] /= std[2]
- return x
|