您的位置:首页 > 其它

embedding可视化/yale人脸数据集

2017-07-26 11:17 411 查看

环境tensorflow1.1,python3

我们可以把学习向量映射到2维中以便我们观察,其中用到的技术可以参考 t-SNE 降纬技术和PCA。当我们用可视化的方式来观察这些向量,这实际上是非常有用的。

本实验是人脸数据集中,图像经过embedding后在空间可视化

#coding:utf-8
from tensorflow.contrib.tensorboard.plugins import projector
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os
import 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')

#建立embedding
embedding_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.name
embedding.metadata_path = path_for_face_data
embedding.sprite.image_path = path_for_face_png
embedding.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_digits

to_visualise = train_data
to_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:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息