Skip to content

Instantly share code, notes, and snippets.

@kechan
Last active April 12, 2018 03:11
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 kechan/9a9f4d76f40500b85ce4493e785019ea to your computer and use it in GitHub Desktop.
Save kechan/9a9f4d76f40500b85ce4493e785019ea to your computer and use it in GitHub Desktop.
PCA Color Augmentation
import numpy as np
# original image is 224x224x3 of dtype uint8
renorm_image = np.reshape(original_image, (original_image.shape[0]*original_image.shape[1],3))
renorm_image = renorm_image.astype('float32')
renorm_image -= np.mean(renorm_image, axis=0)
renorm_image /= np.std(renorm_image, axis=0)
cov = np.cov(renorm_image, rowvar=False)
lambdas, p = np.linalg.eig(cov)
alphas = np.random.normal(0, 0.1, 3)
#delta = p[:,0]*alphas[0]*lambdas[0] + p[:,1]*alphas[1]*lambdas[1] + p[:,2]*alphas[2]*lambdas[2]
delta = np.dot(p, alphas*lambdas)
delta = (delta*255.).astype('int8')
pca_color_image = np.maximum(np.minimum(original_image + delta, 255), 0).astype('uint8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment