Skip to content

Instantly share code, notes, and snippets.

@zaidalyafeai
Created September 7, 2018 08:02
Show Gist options
  • Save zaidalyafeai/9ff6856c37a47db3862f0f2abe5d3e9c to your computer and use it in GitHub Desktop.
Save zaidalyafeai/9ff6856c37a47db3862f0f2abe5d3e9c to your computer and use it in GitHub Desktop.
function preprocess(imgData) {
return tf.tidy(() => {
//convert to a tensor
const tensor = tf.fromPixels(imgData).toFloat()
//resize
const resized = tf.image.resizeBilinear(tensor, [256, 256])
//normalize
const offset = tf.scalar(127.5);
const normalized = resized.div(offset).sub(tf.scalar(1.0));
//We add a dimension to get a batch shape
const batched = normalized.expandDims(0)
return batched
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment