示例#1
0
def code_PC_plots(codes_all, generations, trialdir, trial_title):
    pca = PCA(n_components=50, copy=True)
    PC_codes = pca.fit_transform(codes_all)
    plt.figure(figsize=[10, 6])
    for i in range(10):
        plt.scatter(generations, PC_codes[:, i], s=6, alpha=0.6)
    plt.xlabel("Generations")
    plt.ylabel("PC projection")
    plt.title("PC Projections \n %s" % (trial_title))
    plt.savefig(os.path.join(trialdir, "PC_Proj_evolution.png"))
    plt.show()

    plt.figure(figsize=[8, 8])
    cumsumvar = np.cumsum(pca.explained_variance_ratio_)
    plt.plot(cumsumvar)
    for mark in [5, 10, 25, 50]:
        plt.hlines(cumsumvar[mark - 1], 0, mark - 1, 'r')
    plt.ylabel("Ratio")
    plt.xlabel("PC number")
    plt.title(
        "%s\n Explained Variance %.2f in 10 PC \nExplained Variance %.2f in 25 PC"
        % (trial_title, sum(pca.explained_variance_ratio_[:10]),
           sum(pca.explained_variance_ratio_[:20])))
    plt.savefig(os.path.join(trialdir, "exp_variance.png"))
    plt.show()

    plt.figure(figsize=[15, 5])
    for i in range(30):
        plt.subplot(3, 10, i + 1)
        img = generator.visualize(100 * pca.components_[i, :])
        plt.imshow(img.copy())
        plt.axis("off")
    plt.suptitle("Visualize first 10 PC's images \n %s" % (trial_title, ))
    plt.savefig(os.path.join(trialdir, "PC_visualization.png"))
    plt.show()
示例#2
0
def perturb_visulize(curr_code,
                     entry_n,
                     interv=10,
                     total_num=11,
                     code_num=None):
    # curr_score = score_total_list[num]
    img_width = total_num * 1.5 + 2
    fig, axes = plt.subplots(1, total_num, figsize=[img_width, 2.5])
    increment = np.zeros(curr_code.shape)
    for id in range(total_num):
        increment[entry_n] = (id - (total_num - 1) / 2) * interv
        tmp_code = curr_code + increment
        img_tmp = generator.visualize(tmp_code)
        ax = axes[id]
        ax.imshow(img_tmp)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_axis_off()
        ax.set_title("%.1f, %.2f" %
                     (tmp_code[entry_n], TestScorer.test_score(img_tmp)[0]))
    if code_num is not None:
        plt.suptitle("Code %d, Perturb Along Coordinate %d, interval=%.1f" %
                     (code_num, entry_n, interv))
    else:
        plt.suptitle("Perturb Along Coordinate %d, interval=%.1f" %
                     (entry_n, interv))
    plt.show()
    return fig
示例#3
0
    def visualize_image_evolution(self,
                                  save=True,
                                  exp_title_str='',
                                  col_n=10,
                                  savedir=''):
        '''
        # CurDataDir:  "/home/poncelab/Documents/data/with_CNN/caffe-net_fc6_0001/backup/"
        # block_num: the number of block to visualize 20
        # title_cmap: define the colormap to do the code, plt.cm.viridis
        # col_n: number of column in a plot 6
        # FIXED: on Oct. 7th support new name format, and align the score are image correctly
        '''

        image_num = len(self.img_ids)
        gen_num = self.generations.max() + 1
        row_n = np.ceil(gen_num / col_n)
        figW = 12
        figH = figW / col_n * row_n + 1
        fig = plt.figure(figsize=[figW, figH])
        for geni in range(gen_num):
            code_tmp = self.codes_all[self.generations == geni, :].mean(axis=0)
            img_tmp = generator.visualize(code_tmp)
            plt.subplot(row_n, col_n, geni + 1)
            plt.imshow(img_tmp)
            plt.axis('off')
            plt.title("%d" % (geni))

        plt.suptitle(exp_title_str, fontsize=16)
        plt.tight_layout(h_pad=0.1, w_pad=0, rect=(0, 0, 0.95, 0.9))
        if save:
            if savedir == '':
                savedir = self.logdir
            plt.savefig(os.path.join(savedir, exp_title_str))
        # plt.show()
        return fig
示例#4
0
def perturb_images_line(cent_vec, perturb_vec, PC2_step = 18, RNG=range(-5,6)):
    sphere_norm = np.linalg.norm(cent_vec)
    img_list = []
    for j in RNG:
        L = PC2_step * j
        code_vec = cent_vec + L * perturb_vec
        # code_vec = code_vec / np.sqrt((code_vec ** 2).sum()) * sphere_norm
        img = generator.visualize(code_vec)
        img_list.append(img.copy())
    return img_list
示例#5
0
def perturb_images_arc(cent_vec, perturb_vec, PC2_ang_step = 18, RNG=range(-5,6)):
    sphere_norm = np.linalg.norm(cent_vec)
    vectors = np.zeros((2, cent_vec.size))
    vectors[  0, :] = cent_vec / sphere_norm
    vectors[1:2, :] = perturb_vec
    img_list = []
    for j in RNG:
        theta = PC2_ang_step * j / 180 * np.pi
        code_vec = np.array([[np.cos(theta),
                              np.sin(theta)]]) @ vectors[0:2, :]
        code_vec = code_vec / np.sqrt((code_vec ** 2).sum()) * sphere_norm
        img = generator.visualize(code_vec)
        img_list.append(img.copy())
    return img_list
示例#6
0
def perturb_images_sphere(cent_vec, perturb_vec, PC2_ang_step = 18, PC3_ang_step = 18):
    sphere_norm = np.linalg.norm(cent_vec)
    vectors = np.zeros((3, cent_vec.size))
    vectors[  0, :] = cent_vec / sphere_norm
    vectors[1:3, :] = perturb_vec
    img_list = []
    for j in range(-5, 6):
        for k in range(-5, 6):
            theta = PC2_ang_step * j / 180 * np.pi
            phi = PC3_ang_step * k / 180 * np.pi
            code_vec = np.array([[np.cos(theta) * np.cos(phi),
                                  np.sin(theta) * np.cos(phi),
                                  np.sin(phi)]]) @ vectors[0:3, :]
            code_vec = code_vec / np.sqrt((code_vec ** 2).sum()) * sphere_norm
            img = generator.visualize(code_vec)
            img_list.append(img.copy())
    return img_list
示例#7
0
def interpolate_score_curve(codes_array, imgid_tuple, interp_n=40):
    '''Input the
    codes_array in ndarray form
    imgid_tuple: form of id tuple '''
    img_tmp = [
        generator.visualize(simplex_interpolate(i / interp_n, codes_array))
        for i in range(interp_n + 1)
    ]
    scores = TestScorer.test_score(img_tmp)
    dist = distance_metric(code_total_array[img_tuple, :])
    plt.figure()
    plt.plot(np.linspace(0, 1, interp_n + 1), scores)
    plt.title("Interpolation Scores between %d-%d\ndist:%.1f" %
              (*imgid_tuple, dist))
    pos = np.linspace(0, 1, 11)
    labels = ["%.1f" % x for x in pos]
    labels[0] += "\n" + str(imgid_tuple[0])
    labels[-1] += "\n" + str(imgid_tuple[1])
    plt.xticks(pos, labels)
    plt.show()
    return scores
示例#8
0
def GAN_interp_sphere_ang(vectors, sphere_norm=200, theta_ang_step= 180/10, phi_ang_step=180/10, grid_shape=(11,11),
                          saveimg=False, savepath=None, inv_PC1 = True):
    img_list = []
    theta_n, phi_n = grid_shape
    for j in range(-int((theta_n-1)/2), int((theta_n+1)/2)):
        for k in range(-int((phi_n-1)/2), int((phi_n+1)/2)):
            theta = theta_ang_step * j / 180 * np.pi
            phi = phi_ang_step * k / 180 *np.pi
            if inv_PC1:
                code_vec = np.array([[-np.cos(theta) * np.cos(phi),
                                      np.sin(theta) * np.cos(phi),
                                      np.sin(phi)]]) @ vectors
            else:
                code_vec = np.array([[np.cos(theta) * np.cos(phi),
                                      np.sin(theta) * np.cos(phi),
                                      np.sin(phi)]]) @ vectors
            code_vec = code_vec / np.sqrt((code_vec**2).sum()) * sphere_norm
            img = generator.visualize(code_vec)
            img_list.append(img.copy())
            if saveimg:
                plt.imsave(os.path.join(savepath, "norm%d_theta_%d_phi_%d.jpg" % (sphere_norm, j, k)), img)
    return img_list
示例#9
0
from utils import generator, add_trial_subdir
# from Optimizer import CMAES, Genetic, CholeskyCMAES
from cv2 import imread, imwrite
import matplotlib.pylab as plt
import re

#%%

matdata = loadmat(
    r"C:\Users\ponce\OneDrive\Documents\MATLAB\CodesEvolution1.mat")
codes_all = matdata['codes_all']
generations = matdata['generations']
del matdata
#%% Cropping effect

imgs = generator.visualize(codes_all[1000, :])
plt.figure()
plt.hist(np.reshape(imgs, (-1, 1)), 255, (0, 255))
plt.xlim((0, 255))
plt.show()

#%%
plt.imshow(imgs)
plt.show()
#%%
raw_img = generator.raw_output(0.1 * codes_all[4000, :])
deproc_raw_img = generator._detransformer.deprocess('data', raw_img)
plt.hist(deproc_raw_img.flatten(), 255)  # (0, 255))
#plt.xlim((0,255))
plt.show()
#%%
示例#10
0
    if iblock != -1:
        t0 = time()
        # wait for matfile
        matfn = 'block%03d_code.mat' % iblock
        matfpath = os.path.join(respdir, matfn)
        print('waiting for %s' % matfn)
        while not os.path.isfile(matfpath):
            sleep(0.001)
        remove_bmp(respdir)
        copy_nature_image(natstimdir, expdir)
        sleep(0.9)  # ensures mat file finish writing
        t1 = time()
        # load .mat file results
        imgid_list, codes = load_block_mat_code(matfpath)
    else:
        matfpath = initmat_path
        imgid_list, codes = load_block_mat_code(matfpath)
        imgid_list = ["gen_" + imgid for imgid in imgid_list
                      ]  # use gen as marker to distinguish from natural images
        copy_nature_image(natstimdir, expdir)  # , prefix='block000')
    iblock += 1
    # TODO Permutation and mixing can be added here ! But not needed!
    names = ['block%03d_' % (iblock) + id for id in imgid_list]
    # TODO More complex naming rule can be applied
    imgs = [generator.visualize(code) for code in codes]
    utils.write_images(
        imgs, names, respdir,
        format='bmp')  # use jpg to compress size, use bmp to speed up
    utils.write_images(imgs, names, backupdir, format='jpg')
    copy(matfpath, backupdir)
示例#11
0
# %% Spherical interpolation

PC2_ang_step = 180 / 10
PC3_ang_step = 180 / 10
# sphere_norm = 300
print("Generating images on PC1, PC2, PC3 sphere (rad = %d)" % sphere_norm)
img_list = []
for j in range(-5, 6):
    for k in range(-5, 6):
        theta = PC2_ang_step * j / 180 * np.pi
        phi = PC3_ang_step * k / 180 * np.pi
        code_vec = np.array([[PC1_sign* np.cos(theta) * np.cos(phi),
                                        np.sin(theta) * np.cos(phi),
                                        np.sin(phi)]]) @ PC_vectors[0:3, :]
        code_vec = code_vec / np.sqrt((code_vec**2).sum()) * sphere_norm
        img = generator.visualize(code_vec)
        img_list.append(img.copy())
        plt.imsave(os.path.join(newimg_dir, "norm_%d_PC2_%d_PC3_%d.jpg" % (sphere_norm, PC2_ang_step * j, PC3_ang_step* k)), img)

fig1 = utils.visualize_img_list(img_list)
# %% Spherical interpolation
PC2_ang_step = 180 / 10
PC3_ang_step = 180 / 10
# sphere_norm = 300
print("Generating images on PC1, PC49, PC50 sphere (rad = %d)" % sphere_norm)
PC_nums = [0, 48, 49]  # can tune here to change the selected PC to generate images
img_list = []
for j in range(-5, 6):
    for k in range(-5, 6):
        theta = PC2_ang_step * j / 180 * np.pi
        phi = PC3_ang_step * k / 180 * np.pi
示例#12
0
    })
#%%
coordinate_grad_list = np.argsort(end_score_array.min(axis=0))
# find the perturbation axis yielding the largest change in score.
#%%
for i in range(10):
    entry_n = coordinate_grad_list[i]
    perturb_visulize_contrast(curr_code, entry_n, interv=10)
#%%
entry_n = 1855  # 1642, 2567, 1689
perturb_visulize(curr_code, entry_n, interv=20, code_num=15886)
#%% Title: Image Difference map
entry_n = 1689
increment = np.zeros(curr_code.shape)
increment[entry_n] = 20
img_1 = generator.visualize(curr_code)
img_2 = generator.visualize(curr_code + increment)
#%%
plt.figure(figsize=[8, 3])
plt.subplot(1, 3, 1)
plt.imshow(img_1)
plt.subplot(1, 3, 2)
plt.imshow(img_2)
plt.subplot(1, 3, 3)
diff_map = np.abs(img_1.astype(np.int) - img_2.astype(np.int)).max(axis=2)
plt.imshow(diff_map)
plt.colorbar()
plt.show()


#%%
示例#13
0
# %% Spherical interpolation
# PC1_step = PC1_Amp / 10  # TODO: Control the step size and range of the images.
PC2_ang_step = 180 / 10
PC3_ang_step = 180 / 10
sphere_norm = 200

img_list = []
for j in range(-5, 6):
    for k in range(-5, 6):
        theta = PC2_ang_step * j / 180 * np.pi
        phi = PC3_ang_step * k / 180 * np.pi
        code_vec = np.array([[PC1_sign* np.cos(theta) * np.cos(phi),
                                        np.sin(theta) * np.cos(phi),
                                        np.sin(phi)]]) @ PC_vectors[0:3, :]
        code_vec = code_vec / np.sqrt((code_vec**2).sum()) * sphere_norm
        img = generator.visualize(code_vec)
        img_list.append(img.copy())
        # plt.imsave(os.path.join(newimg_dir, "PC1_%d_PC2_%d_PC3_%d.jpg" % (i, j, k)), img)

plt.figure(figsize=[30, 30])
for i, img in enumerate(img_list):
    plt.subplot(11, 11, i + 1)
    plt.imshow(img[:])
    plt.axis('off')
plt.show()





#%%
示例#14
0
# PC_Proj_codes1 = code_pca.transform(codes_all_col[0])
# PC_Proj_codes2 = code_pca.transform(codes_all_col[1])
# PC_vectors = code_pca.components_
#%%
ortho_codes_all = codes_all - codes_all @ unit_final_vec1.T @ unit_final_vec1 \
                  - codes_all @ ortho_final_vec2.T @ ortho_final_vec2
#%%
print("Computing PCs of the orthogonal space")
ortho_code_pca = PCA(n_components=50)
ortho_code_pca.fit(ortho_codes_all)
ortho_PC_vectors = ortho_code_pca.components_
# ortho_code_pca.transform()
#%%
plt.figure()
# plt.imshow(generator.visualize((ortho_final_vec2 + ortho_PC_vectors[0:1, :])*400))
plt.imshow(generator.visualize((unit_final_vec2)*400))
plt.axis("off")
plt.show()
#%%
coord_vectors = np.concatenate((unit_final_vec1, ortho_final_vec2, ortho_PC_vectors[0:1, :]), axis=0)
interp_ang_step = 180 / 10
PC3_ang_step = 180 / 10
sphere_norm = mean_final_norm
print("Generating images on interpolating  sphere (rad = %d)" % sphere_norm)
img_list = []
for k in range(-5, 6):
    for j in range(-5, 11):
        if PC3_ang_step * k == -90 and j != 0:
            continue
        if PC3_ang_step * k ==  90 and j != 0:
            continue