Professional Documents
Culture Documents
In [55]:
(500, 250)
(815, 437)
(720, 480)
In [58]:
def collect_natural_patches(num_patches = 500000, patch_width = 16):
""" collects image patches"""
max_tries = num_patches * 50
image_width = 200
patch_sample = np.zeros([patch_width,patch_width],float)
patch = np.zeros([num_pixels,1],float)
img_patches = np.zeros([num_pixels,num_patches],float)
img_first_patch = patch_cnt
img_first_try = try_cnt
px = np.random.randint(0,image_width - patch_width)
py = np.random.randint(0,image_width - patch_width)
patch_sample = image[px:px+patch_width,py:py+patch_width].copy()
patch_std = patch_sample.std()
#this function collects patches from the natural image files in my data folder
(500, 250)
33 percent complete
66 percent complete
In [59]:
def show_patches_mat(pre_patches, show_patch_num = 16, display=True):
""" this function generates a 2D array to display image patches (assuming square) """
patches = pre_patches
tot_patches = patches.shape[1]
data_dim = patches.shape[0]
patch_width = int(np.round(np.sqrt(data_dim)))
patch = patches[:,patch_i].copy()
pmax = patch.max()
pmin = patch.min()
# fix patch range from min to max to 0 to 1
if pmax > pmin:
patch = (patch - pmin) / (pmax - pmin)
disp_patch[:,i] = patch.copy()
bw = 5 # border width
pw = patch_width
patches_y = int(np.sqrt(show_patch_num))
patches_x = int(np.ceil(float(show_patch_num) / patches_y))
patch_img = disp_patch.max() * np.ones([(pw + bw) * patches_x - bw,
patches_y * (pw + bw) - bw], float)
for i in range(0,show_patch_num):
y_i = i // patches_y
x_i = i % patches_y
reshaped = disp_patch[:,i].reshape((pw,pw))
full_patch = np.zeros([pw, pw], float)
full_patch[0:pw,:] = reshaped[:,:].copy()
patch_img[x_i*(pw+bw):x_i*(pw+bw)+pw,y_i*(pw+bw):y_i*(pw+bw)+pw] = full_patch
if display:
py.bone()
py.imshow(patch_img.T, interpolation='nearest')
py.axis('off')
return
show_patches_mat(patches_mat)
#this function shows the patches collected from the natural images
In [60]:
# finding PCA components
pcatemp = sklearn.decomposition.PCA(n_components=10)
pcatemp = sklearn.decomposition.PCA(n_components=10)
pcafit = pcatemp.fit(np.transpose(patches_mat))
print('Quality of image representation using only 40% as much data per patch',
'\n(using only responses along these 10 components instead of the full 64 dimensions):',
'\n',np.sum(100*pcafit.explained_variance_ratio_),'%')
pca_comp = pcafit.components_
show_patches_mat(np.transpose(pca_comp), 25)
Quality of image representation using only 40% as much data per patch
(using only responses along these 10 components instead of the full 64 dimensions):
65.79012792923416 %
In [61]:
# finding ICA components
ica_comp = icafit.components_
# print('shape of the ica component matrix: ',ica_comp.shape)
show_patches_mat(np.transpose(ica_comp), 25)
#This function shows 10 ICA components, resembles visual receptive fields