Created
March 16, 2020 14:54
-
-
Save Cospel/0ef616f36a6e4b28de008fc571e2515e to your computer and use it in GitHub Desktop.
preprocess vgg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tf.function | |
def preprocess_caffe(x): | |
""" | |
Preprocessing for Keras (VGG, ResnetV1). | |
! This works only for channels_last | |
:param x: np.asarray([image, image, ...], dtype="float32") in RGB | |
:return: normalized image vgg style (BGR) | |
""" | |
batch, height, width, channels = x.shape | |
x = tf.cast(x, tf.float32) | |
r, g, b = tf.split(x, 3, axis=3) | |
x = tf.concat([b, g, r], 3) | |
mean_tensor = np.asarray([[[[103.939, 116.779, 123.68]]]], dtype=np.float32) | |
result = x - mean_tensor | |
return tf.keras.backend.reshape(result, (-1, height, width, channels)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment