<jupyter_start><jupyter_text>Load MNIST<jupyter_code>import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
%matplotlib inline

mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)

target_idx = np.where(mnist.train.labels<2)[0]
foreground = mnist.train.images[target_idx,:][:5000]
target_labels = mnist.train.labels[target_idx][:5000]<jupyter_output><empty_output><jupyter_text>PCA on Regular MNIST<jupyter_code>from sklearn.decomposition import PCA

pca = PCA(n_components=2)
fg = pca.fit_transform(foreground)
colors = ['k','r']

for i, l in enumerate(np.sort(np.unique(target_labels))):
    plt.scatter(fg[np.where(target_labels==l),0],fg[np.where(target_labels==l),1], 
                color=colors[i], label='Digit ' +str(l))
plt.legend()<jupyter_output><empty_output><jupyter_text>Load Natural Images of Grass These pictures are found in this [OneDrive link](https://1drv.ms/f/s!AgLi37o1j88ahrJTLeycjuEoHpVhQw), or they can be downloaded from [ImageNet](http://image-net.org/download) using the synset 'grass'. (*Note*: replace IMAGE_PATH with path to the downloaded images)<jupyter_code>from PIL import Image
from utils import resize_and_crop
import os

IMAGE_PATH = 'C:\\Users\\aabid\\OneDrive\\Pictures\\Imagenet-Grass\\' #Replace with your own path to downloaded images
        
natural_images = list() #dictionary of pictures indexed by the pic # and each value is 100x100 image
for filename in os.listdir(IMAGE_PATH):
    if filename.endswith(".JPEG") or filename.endswith(".JPG") or filename.endswith(".jpg"):
        try:
            im = Image.open(os.path.join(IMAGE_PATH,filename))
            im = im.convert(mode="L") #convert to grayscale
            im = resize_and_crop(im) #resize and crop each picture to be 100px by 100px
            natural_images.append(np.reshape(im, [10000])) 
        except Exception as e:
            pass #print(e)
            
natural_images=np.asarray(natural_images,dtype=float)
natural_images/=255 #rescale to be 0-1
print("Array of grass images:",natural_images.shape)<jupyter_output><empty_output><jupyter_text>Corrupt MNIST by Superimposing Images of Grass To create each of the 5000 corrupted digits, randomly chosen a 28px by 28px region from a grass image to be superimposed on top of the digits.<jupyter_code>np.random.seed(0) # for reproducibility

rand_indices =  np.random.permutation(natural_images.shape[0]) # just shuffles the indices
split = int(len(rand_indices)/2)
target_indices = rand_indices[0:split] # choose the first half of images to be superimposed on target
background_indices = rand_indices[split:] # choose the second half of images to be background dataset

target = np.zeros(foreground.shape)
background = np.zeros(foreground.shape)

for i in range(target.shape[0]):
    idx = np.random.choice(target_indices) # randomly pick a image 
    loc = np.random.randint(70,size=(2)) # randomly pick a region in the image
    superimposed_patch = np.reshape(np.reshape(natural_images[idx,:],[100,100])[loc[0]:loc[0]+28,:][:,loc[1]:loc[1]+28] ,[1,784])    
    target[i] = 0.25*foreground[i] + superimposed_patch
    
    idx = np.random.choice(background_indices) # randomly pick a image 
    loc = np.random.randint(70,size=(2)) # randomly pick a region in the image
    background_patch = np.reshape(np.reshape(natural_images[idx,:],[100,100])[loc[0]:loc[0]+28,:][:,loc[1]:loc[1]+28] ,[1,784])    
    background[i] = background_patch<jupyter_output><empty_output><jupyter_text>Some Example Images<jupyter_code>n_show=6

plt.figure(figsize=[12,12])
for i in range(n_show):
    plt.subplot(1, n_show, i+1)
    idx=np.random.randint(5000)
    plt.imshow(np.reshape(target[idx,:],[28,28]),cmap='gray', interpolation="bicubic")
    plt.axis('off')

plt.figure(figsize=[12,12])
for i in range(n_show):
    plt.subplot(1, n_show, i+1)
    idx=np.random.randint(5000)
    plt.imshow(np.reshape(background[idx,:],[28,28]),cmap='gray', interpolation="bicubic")
    plt.axis('off')<jupyter_output><empty_output><jupyter_text>PCA on Corrupted MNIST<jupyter_code>from sklearn.decomposition import PCA

pca = PCA(n_components=2)
fg = pca.fit_transform(target)

for i, l in enumerate(np.sort(np.unique(target_labels))):
    plt.scatter(fg[np.where(target_labels==l),0],fg[np.where(target_labels==l),1], 
                color=colors[i], label='Digit ' +str(l))
plt.legend()<jupyter_output><empty_output><jupyter_text>Perform Contrastive PCA<jupyter_code>from contrastive import CPCA

mdl = CPCA()
projected_data = mdl.fit_transform(target, background, plot=True, active_labels=target_labels)<jupyter_output><empty_output><jupyter_text>Features Captured by PCA vs. cPCA<jupyter_code>plt.figure(figsize=[12,5])
w, v = np.linalg.eig(mdl.fg_cov)
idx = np.argmax(w)
plt.subplot(1,2,1)
plt.imshow(v[:,idx].reshape(28,28),cmap='Reds')
plt.colorbar()
plt.title('Contributions of Pixels to PC1')

w, v = np.linalg.eig(mdl.fg_cov - 4.38*mdl.bg_cov) # the value 4.38 is taken from the previous diagram, where alpha=4.38 separates the clusters most clearly
idx = np.argmax(w)
plt.subplot(1,2,2)
plt.imshow(v[:,idx].reshape(28,28),cmap='Reds')
plt.colorbar()
plt.title('Contributions of Pixels to cPC1')<jupyter_output><empty_output><jupyter_text>Denoising with PCA vs. cPCA<jupyter_code>n_components = 5
for idx in range(4):
    image = target[idx:idx+1]
    plt.figure(figsize=[12,12])
    plt.subplot(1,3,1)
    plt.imshow(image.reshape(28,28),cmap='gray')
    plt.title('Original Image')

    w, v = np.linalg.eig(mdl.fg_cov)
    indices = np.argsort(w)[::-1][:n_components]
    image_ = image.dot(v[:,indices])
    image_ = image_.dot(v[:,indices].T) #keep only the top components of the image
    plt.subplot(1,3,2)
    plt.imshow(image_.reshape(28,28),cmap='gray')
    plt.title('PCA Denoising')

    w, v = np.linalg.eig(mdl.fg_cov - 4.38*mdl.bg_cov) # the value 4.38 is taken from the plot that is two plots previous to this one, where alpha=4.38 separates the clusters most clearly
    indices = np.argsort(w)[::-1][:n_components]
    image_ = image.dot(v[:,indices])
    image_ = image_.dot(v[:,indices].T) #keep only the top components of the image
    plt.subplot(1,3,3)
    plt.imshow(image_.reshape(28,28),cmap='gray')
    plt.title('cPCA Denoising')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1414765525982256, 'fraction_numerical': 0.03136882129277566, 'mean_word_length': 5.238142292490118, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '259679', 'license': 'MIT', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2284, 'n_words': 465, 'path': 'experiments/.ipynb_checkpoints/MNIST Corrupted by Natural Background (Figures 1, 5)-checkpoint.ipynb', 'repo_name': 'jkobject/contrastive', 'size': '6312'}