embedding可视化/yale人脸数据集

来源:互联网 发布:抓住那个家伙知乎 编辑:程序博客网 时间:2024/06/05 02:27

环境tensorflow1.1,python3

我们可以把学习向量映射到2维中以便我们观察,其中用到的技术可以参考 t-SNE 降纬技术和PCA。当我们用可视化的方式来观察这些向量,这实际上是非常有用的。
本实验是人脸数据集中,图像经过embedding后在空间可视化

#coding:utf-8from tensorflow.contrib.tensorboard.plugins import projectorimport matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npimport osimport scipy.io as sio#加载数据集#加载数据def read_data(filename):    with open(filename,'rb') as f:        #记载matlab文件        dict = sio.loadmat(f)    return dict['fea'],dict['gnd']train_data,train_labels = read_data('Yale_64x64.mat')log_dir = 'yalesample'name_to_visualise_variable = 'yaleembedding'batch_size = 20#保存数据path_for_face_png = os.path.join(log_dir,'newface.png')path_for_face_data = os.path.join(log_dir,'newface.tsv')#建立embeddingembedding_var = tf.Variable(train_data,name=name_to_visualise_variable)#将信息写入log_dir目录下summary_writer = tf.summary.FileWriter(log_dir)config = projector.ProjectorConfig()embedding = config.embeddings.add()embedding.tensor_name = embedding_var.nameembedding.metadata_path = path_for_face_dataembedding.sprite.image_path = path_for_face_pngembedding.sprite.single_image_dim.extend([64,64])#将embedding可视化projector.visualize_embeddings(summary_writer,config)sess = tf.InteractiveSession()sess.run(tf.global_variables_initializer())saver = tf.train.Saver()saver.save(sess,os.path.join(log_dir,'model.ckpt'),1)#将图片拼成一张大图def create_sprite_image(images):    if isinstance(images, list):        images = np.array(images)    img_h = images.shape[1]    img_w = images.shape[2]    n_plots = int(np.ceil(np.sqrt(images.shape[0])))    spriteimage = np.ones((img_h * n_plots ,img_w * n_plots ))    for i in range(n_plots):        for j in range(n_plots):            this_filter = i * n_plots + j            if this_filter < images.shape[0]:                this_img = images[this_filter]                spriteimage[i * img_h:(i + 1) * img_h,                  j * img_w:(j + 1) * img_w] = this_img    return spriteimage#将矩阵转为图片def vector_to_matrix_face(face_digits):    return np.reshape(face_digits,(-1,64,64))#将图片转为黑白def invert_grayscale(face_digits):    return 1-face_digitsto_visualise = train_datato_visualise = vector_to_matrix_face(to_visualise)to_visualise = invert_grayscale(to_visualise)sprite_image = create_sprite_image(to_visualise)plt.imsave(path_for_face_png,sprite_image,cmap='gray')plt.imshow(sprite_image,cmap='gray')with open(path_for_face_data,'w') as f:    f.write('Index\tLabel\n')    for index,label in enumerate(train_labels):        f.write('%d\t%d\n' %(index,label))

结果:

PCA:
这里写图片描述

t-SNE:
这里写图片描述

这里写图片描述

原创粉丝点击