embedding可视化/yale人脸数据集
程序员文章站
2022-05-02 14:02:57
...
环境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:
上一篇: Python 字符串与字节串的相互转换
下一篇: python实现数据可视化
推荐阅读
-
基于jupyter notebook的python编程(Win10通过OpenCv-3.4.1进行人脸口罩数据集的模型训练并进行戴口罩识别检测)
-
Python可视化查看数据集完整性: missingno库(用于数据分析前的数据检查)
-
人脸口罩数据集的模型训练以及戴口罩识别检测
-
分别采用线性LDA、k-means和SVM算法对鸢尾花数据集和月亮数据集进行二分类可视化分析
-
COCO数据集进行自定义可视化,不用cocoapi
-
Mnist数据集转存csv文件,csv文件图像可视化
-
ML:基于自定义数据集利用Logistic、梯度下降算法GD、LoR逻辑回归、Perceptron感知器、SVM支持向量机、LDA线性判别分析算法进行二分类预测(决策边界可视化)
-
荐 Python可视化matplotlib13-iris鸢尾花数据集|histogram直方图
-
【人工智能与机器学习】创新性大作业--人脸口罩数据集的模型训练
-
基于jupyter notebook的python编程(Win10通过OpenCv-3.4.1进行人脸口罩数据集的模型训练并进行戴口罩识别检测)