Skip to content

Instantly share code, notes, and snippets.

@Cospel
Created March 16, 2020 14:54
Show Gist options
  • Save Cospel/0ef616f36a6e4b28de008fc571e2515e to your computer and use it in GitHub Desktop.
Save Cospel/0ef616f36a6e4b28de008fc571e2515e to your computer and use it in GitHub Desktop.
preprocess vgg
@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