使用t-SNE可视化图像embedding 图像特征可视化

本文介绍使用t-SNE降维技术对图像特征进行可视化的方法,包括如何利用sklearn包实现散点图和网格图的绘制,以及在处理图像遮挡问题上的尝试。


t-SNE(t-Distributed Stochastic Neighbor Embedding)是一种非线性降维技术,主要用途为对高维数据进行可视化。

easy sample(使用sklearn包):

from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from sklearn.manifold import TSNE
import cv2
import numpy as np
import matplotlib.pyplot as plt


# 传入图像的embedding特征和对应的图像路径
def draw_tsne(features, imgs):
    """
    Args:
        feature: [n_samples  embed_dim], full data embedding of test samples.
        imgs:        list [n_samples], list of datapaths corresponding to <feature>
    """
    #print(imgs)
    # 初始化一个TSNE模型,这里的参数设置可以查看SKlearn的官网
    tsne = TSNE(n_components=2, init='pca', perplexity=30)
    # Y是降成两维后的数据
    Y = tsne.fit_transform(features)
    fig, ax = plt.subplots()
    # 设置图像大小
    # fig.set_size_inches(21.6, 14.4)
    plt.axis('off')
    imscatter(Y[:, 0], Y[:, 1], imgs, zoom=0.1, ax=ax)
    plt.savefig(fname='figure.jpg', format='jpg')
    plt.show()


def imscatter(x, y, images, zoom, ax=None):
    artists = []
    for x0, y0, image in zip(x, y, images):
        print(image)
        im = cv2.imread(image)
        im = cv2.resize(im, (224, 224))
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        im_f = OffsetImage(im, zoom=zoom)
        ab = AnnotationBbox(im_f, (x0, y0), xycoords='data', frameon=False)
        artists.append(ax.add_artist(ab))
    ax.update_datalim(np.column_stack([x, y]))
    ax.autoscale()
    return artists


x = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值