Skip to content

Instantly share code, notes, and snippets.

@joelouismarino
Created July 30, 2017 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelouismarino/aa565be7cc5a450b117fefee8d9440f0 to your computer and use it in GitHub Desktop.
Save joelouismarino/aa565be7cc5a450b117fefee8d9440f0 to your computer and use it in GitHub Desktop.
img = imread('cat.jpg', mode='RGB')
height,width = img.shape[:2]
img = img.astype('float32')
# subtract means
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.779
img[:, :, 2] -= 103.939
img[:,:,[0,1,2]] = img[:, :, [2, 1, 0]] # swap channels
img = img.transpose((2, 0, 1)) # re-order dimensions
img = img[:,(height-224)//2:(height+224)//2, (width-224)//2:(width+224)//2] #crop
img = np.expand_dims(img, axis=0) # add dimension for batch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment