【计算机视觉】DINOv2(视觉大模型)代码使用和测试(完整的源代码)

文章目录

  • 一、环境部署
  • 二、导入原图
    • 2.1 使用vit_s14的模型
  • 三、使用其他模型
    • 3.1 使用vit_b14的模型
    • 3.2 使用vit_l14的模型
    • 3.3 使用vit_g14的模型

一、环境部署

!git clone https://ghproxy.com/https://github.com/facebookresearch/dinov2.git

输出为:

Cloning into 'dinov2'...
remote: Enumerating objects: 141, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (74/74), done.  71% (53/74)
remote: Total 141 (delta 40), reused 31 (delta 22), pack-reused 45
Receiving objects: 100% (141/141), 101.01 KiB | 348.00 KiB/s, done.
Resolving deltas: 100% (42/42), done.

命令是一个Git命令,用于克隆(Clone)名为"dinov2"的存储库。它使用了一个名为"ghproxy.com"的代理,用于加速GitHub的克隆操作。

!pip install -r /kaggle/working/dinov2/requirements.txt

在这里插入图片描述
在这里插入图片描述

!pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple

在这里插入图片描述

二、导入原图

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

image = mpimg.imread('/kaggle/input/demo-image/1 (4).png')

plt.imshow(image)
plt.axis('off')
plt.show()

# 输出图像尺寸
print("图像尺寸:{} x {} x {}".format(image.shape[0], image.shape[1], image.shape[2]))

在这里插入图片描述

图像尺寸:1376 x 920 x 3

我们需要切换为output的路径:

import os

input_path = "/kaggle/working/dinov2"
os.chdir(input_path)

2.1 使用vit_s14的模型

import torch
import torchvision.transforms as T
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg 
from PIL import Image
from sklearn.decomposition import PCA
import matplotlib
 
patch_h = 75
patch_w = 50
feat_dim = 384
 
transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),
    T.Resize((patch_h * 14, patch_w * 14)),
    T.CenterCrop((patch_h * 14, patch_w * 14)),
    T.ToTensor(),
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
 
dinov2_vits14 = torch.hub.load('', 'dinov2_vits14',source='local').cuda()
 
features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()
 
img_path = f'/kaggle/input/demo-image/1 (4).png'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():
    features_dict = dinov2_vits14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
pca = PCA(n_components=3)
pca.fit(features)
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())
 
pca_features_fg = pca_features[:, 0] > 0.3
pca_features_bg = ~pca_features_fg
 
b = np.where(pca_features_bg)

pca.fit(features[pca_features_fg])
pca_features_rem = pca.transform(features[pca_features_fg])
for i in range(3):
    pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].min()) / (pca_features_rem[:, i].max() - pca_features_rem[:, i].min())
    # transform using mean and std, I personally found this transformation gives a better visualization
    # pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

pca_features_rgb = pca_features.copy()
pca_features_rgb[pca_features_fg] = pca_features_rem
pca_features_rgb[b] = 0

pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features.png')
plt.show()
plt.close()

以下是代码的逐行中文解读:

import torch
import torchvision.transforms as T
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg 
from PIL import Image
from sklearn.decomposition import PCA
import matplotlib

# 设置补丁(patch)的高度和宽度
patch_h = 75
patch_w = 50
# 特征维度
feat_dim = 384

# 定义图像转换操作
transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),  # 高斯模糊
    T.Resize((patch_h * 14, patch_w * 14)),  # 调整图像大小
    T.CenterCrop((patch_h * 14, patch_w * 14)),  # 中心裁剪
    T.ToTensor(),  # 转换为张量
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),  # 标准化
])

# 使用torch.hub加载dinov2_vits14模型并移至CUDA设备
dinov2_vits14 = torch.hub.load('', 'dinov2_vits14', source='local').cuda()

# 创建用于存储特征和图像张量的零张量
features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()

# 图像路径
img_path = f'/kaggle/input/demo-image/1 (4).png'
# 打开图像并转换为RGB模式
img = Image.open(img_path).convert('RGB')
# 对图像进行转换操作,并将其存储在imgs_tensor的第一个位置
imgs_tensor[0] = transform(img)[:3]

# 禁用梯度计算
with torch.no_grad():
    # 将图像张量传递给dinov2_vits14模型获取特征
    features_dict = dinov2_vits14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
# 重塑特征形状为(4 * patch_h * patch_w, feat_dim)
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()

# 创建PCA对象并拟合特征
pca = PCA(n_components=3)
pca.fit(features)

# 对PCA转换后的特征进行归一化处理
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())

# 根据阈值进行前景和背景的区分
pca_features_fg = pca_features[:, 0] > 0.3
pca_features_bg = ~pca_features_fg

# 查找背景特征的索引
b = np.where(pca_features_bg)

# 对前景特征再次进行PCA转换
pca.fit(features[pca_features_fg])
pca_features_rem = pca.transform(features[pca_features_fg])

# 对前景特征进行归一化处理
for i in range(3):
    pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].min()) / (pca_features_rem[:, i].max() - pca_features_rem[:, i].min())
    # 使用均值和标准差进行转换,个人发现这种转换方式可以得到更好的可视化效果
    # pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

# 创建RGB特征数组
pca_features_rgb = pca_features.copy()

# 替换前景特征为转换后的特征
pca_features_rgb[pca_features_fg] = pca_features_rem

# 将背景特征设置为0
pca_features_rgb[b] = 0

# 重塑特征形状为(4, patch_h, patch_w, 3)
pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)

# 显示第一个图像的RGB特征
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features.png')
plt.show()
plt.close()

这段代码的功能是对给定的图像进行一系列处理和特征提取,并使用PCA对特征进行降维。然后,根据特定阈值对前景和背景进行区分,最后将特征可视化为RGB图像。请注意,其中的具体数值和路径可能需要根据您的实际数据和环境进行调整。

在这里插入图片描述

print(features)
print(features.shape)

我们的输出结果为:

tensor([[-1.3500, -4.8793, -1.4393,  ...,  2.3347,  1.6834, -2.9632],
        [-0.4650, -6.4163, -1.5503,  ...,  2.2055,  2.5527, -3.2553],
        [-0.6371, -6.2615, -0.7516,  ...,  3.1827,  2.3861, -2.6838],
        ...,
        [ 1.9385,  0.0726, -0.5395,  ...,  0.3876, -1.4914, -4.5422],
        [ 1.6399, -0.0860,  0.4701,  ...,  1.0180, -0.8897, -5.2614],
        [ 1.6084, -0.0669,  0.7341,  ...,  1.0633, -0.9713, -5.3548]])
torch.Size([15000, 384])

降维后的特征为:

print(pca_features)
print(pca_features.shape)

输出的结果为:

[[  0.81004055   2.458559    12.11051576]
 [  0.79562888   5.65071716  10.84007045]
 [  0.82050109   5.55007889   9.05274001]
 ...
 [  0.27618588 -18.96898667  19.48198916]
 [  0.31861323 -12.21414371  14.19802898]
 [  0.34356016 -10.82144825  13.74648131]]
(15000, 3)
features_dict

我们看一下字典的构成:

{'x_norm_clstoken': tensor([[ 2.2549, -1.5661,  4.4978,  ...,  1.4984, -5.8642, -0.8560],
         [ 1.8816,  2.4343,  1.4931,  ..., -1.3401, -2.5460,  1.3967],
         [ 1.8816,  2.4343,  1.4931,  ..., -1.3401, -2.5460,  1.3967],
         [ 1.8816,  2.4343,  1.4931,  ..., -1.3401, -2.5460,  1.3967]],
        device='cuda:0'),
 'x_norm_patchtokens': tensor([[[-1.3500, -4.8793, -1.4393,  ...,  2.3347,  1.6834, -2.9632],
          [-0.4650, -6.4163, -1.5503,  ...,  2.2055,  2.5527, -3.2553],
          [-0.6371, -6.2615, -0.7516,  ...,  3.1827,  2.3861, -2.6838],
          ...,
          [-0.8778, -0.0251, -0.2867,  ...,  4.7801, -2.0887, -4.5910],
          [-1.2309,  0.2852,  0.7693,  ...,  5.0635, -1.1529, -6.0175],
          [-1.7551,  1.1333, -0.0898,  ...,  4.1885, -3.3197, -5.7227]],
 
         [[ 0.9131, -4.9736, -0.6238,  ...,  0.2835, -0.3494, -0.4916],
          [ 1.0967, -6.0392, -0.7900,  ...,  0.2323,  0.0510,  0.0176],
          [ 1.3852, -5.8056, -1.2573,  ...,  0.0549, -0.3270, -0.4510],
          ...,
          [ 1.9385,  0.0726, -0.5395,  ...,  0.3877, -1.4914, -4.5422],
          [ 1.6399, -0.0860,  0.4701,  ...,  1.0180, -0.8897, -5.2614],
          [ 1.6084, -0.0669,  0.7341,  ...,  1.0633, -0.9713, -5.3548]],
 
         [[ 0.9131, -4.9736, -0.6238,  ...,  0.2835, -0.3494, -0.4916],
          [ 1.0967, -6.0392, -0.7900,  ...,  0.2323,  0.0510,  0.0176],
          [ 1.3852, -5.8056, -1.2573,  ...,  0.0549, -0.3270, -0.4510],
          ...,
          [ 1.9385,  0.0726, -0.5395,  ...,  0.3877, -1.4914, -4.5422],
          [ 1.6399, -0.0860,  0.4701,  ...,  1.0180, -0.8897, -5.2614],
          [ 1.6085, -0.0669,  0.7341,  ...,  1.0633, -0.9713, -5.3548]],
 
         [[ 0.9131, -4.9736, -0.6238,  ...,  0.2835, -0.3494, -0.4916],
          [ 1.0967, -6.0392, -0.7900,  ...,  0.2323,  0.0510,  0.0176],
          [ 1.3852, -5.8056, -1.2573,  ...,  0.0549, -0.3270, -0.4511],
          ...,
          [ 1.9385,  0.0726, -0.5395,  ...,  0.3876, -1.4914, -4.5422],
          [ 1.6399, -0.0860,  0.4701,  ...,  1.0180, -0.8897, -5.2614],
          [ 1.6084, -0.0669,  0.7341,  ...,  1.0633, -0.9713, -5.3548]]],
        device='cuda:0'),
 'x_prenorm': tensor([[[ 4.7546e-01, -3.4794e-02,  1.1905e+00,  ...,  3.3896e-01,
           -1.2591e+00, -8.1724e-03],
          [-5.2994e-01, -3.0311e-01, -2.0162e-01,  ...,  9.4372e-01,
            8.7399e-01, -3.2527e-01],
          [-1.5728e-01, -3.9359e-01, -2.1482e-01,  ...,  9.0485e-01,
            1.2325e+00, -3.3923e-01],
          ...,
          [-4.9091e-01,  1.1081e-02,  1.9814e-01,  ...,  2.0630e+00,
           -8.5562e-01, -7.6588e-01],
          [-6.0861e-01,  5.2204e-02,  6.6299e-01,  ...,  2.1127e+00,
           -3.8590e-01, -9.7335e-01],
          [-9.3785e-01,  1.2485e-01,  3.0359e-01,  ...,  1.9137e+00,
           -1.5223e+00, -1.0352e+00]],
 
         [[ 4.4059e-01,  1.4807e-01,  5.9425e-01,  ..., -3.4851e-01,
           -6.1687e-01,  2.0463e-01],
          [ 3.1511e-01, -3.3073e-01,  9.0955e-02,  ...,  1.3627e-01,
            1.8562e-02,  4.2850e-02],
          [ 3.8695e-01, -4.1345e-01,  2.8734e-02,  ...,  1.1916e-01,
            1.8061e-01,  1.2469e-01],
          ...,
          [ 6.3855e-01,  1.9967e-03,  5.6187e-02,  ...,  1.0780e-01,
           -5.0606e-01, -6.6095e-01],
          [ 5.6617e-01,  4.9071e-03,  4.8375e-01,  ...,  3.7527e-01,
           -2.6194e-01, -7.9524e-01],
          [ 5.6790e-01,  1.4408e-02,  6.0538e-01,  ...,  4.0537e-01,
           -2.9182e-01, -8.1226e-01]],
 
         [[ 4.4059e-01,  1.4807e-01,  5.9424e-01,  ..., -3.4851e-01,
           -6.1687e-01,  2.0463e-01],
          [ 3.1511e-01, -3.3073e-01,  9.0957e-02,  ...,  1.3627e-01,
            1.8564e-02,  4.2850e-02],
          [ 3.8695e-01, -4.1345e-01,  2.8733e-02,  ...,  1.1916e-01,
            1.8061e-01,  1.2469e-01],
          ...,
          [ 6.3855e-01,  1.9971e-03,  5.6186e-02,  ...,  1.0780e-01,
           -5.0606e-01, -6.6095e-01],
          [ 5.6617e-01,  4.9067e-03,  4.8375e-01,  ...,  3.7527e-01,
           -2.6194e-01, -7.9524e-01],
          [ 5.6790e-01,  1.4408e-02,  6.0538e-01,  ...,  4.0536e-01,
           -2.9182e-01, -8.1226e-01]],
 
         [[ 4.4059e-01,  1.4807e-01,  5.9424e-01,  ..., -3.4851e-01,
           -6.1687e-01,  2.0463e-01],
          [ 3.1511e-01, -3.3073e-01,  9.0956e-02,  ...,  1.3627e-01,
            1.8562e-02,  4.2849e-02],
          [ 3.8695e-01, -4.1344e-01,  2.8735e-02,  ...,  1.1916e-01,
            1.8061e-01,  1.2469e-01],
          ...,
          [ 6.3855e-01,  1.9964e-03,  5.6189e-02,  ...,  1.0780e-01,
           -5.0607e-01, -6.6095e-01],
          [ 5.6617e-01,  4.9066e-03,  4.8375e-01,  ...,  3.7527e-01,
           -2.6194e-01, -7.9524e-01],
          [ 5.6790e-01,  1.4408e-02,  6.0538e-01,  ...,  4.0537e-01,
           -2.9182e-01, -8.1226e-01]]], device='cuda:0'),
 'masks': None}

我们换一种可视化的方法:

patch_h = 75
patch_w = 50
feat_dim = 384
 
transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),
    T.Resize((patch_h * 14, patch_w * 14)),
    T.CenterCrop((patch_h * 14, patch_w * 14)),
    T.ToTensor(),
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
 
dinov2_vits14 = torch.hub.load('', 'dinov2_vits14',source='local').cuda()
 
features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()
 
img_path = f'/kaggle/input/demo-image/1 (4).png'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():
    features_dict = dinov2_vits14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
pca = PCA(n_components=3)
pca.fit(features)
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())
 
pca_features_fg = pca_features[:, 0] > 0.3
pca_features_bg = ~pca_features_fg
 
b = np.where(pca_features_bg)

pca.fit(features[pca_features_fg])
pca_features_rem = pca.transform(features[pca_features_fg])
for i in range(3):
    # transform using mean and std, I personally found this transformation gives a better visualization
    pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

pca_features_rgb = pca_features.copy()
pca_features_rgb[pca_features_fg] = pca_features_rem
pca_features_rgb[b] = 0

pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features.png')
plt.show()
plt.close()

在这里插入图片描述

三、使用其他模型

3.1 使用vit_b14的模型

patch_h = 75
patch_w = 50
feat_dim = 768
 
transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),
    T.Resize((patch_h * 14, patch_w * 14)),
    T.CenterCrop((patch_h * 14, patch_w * 14)),
    T.ToTensor(),
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
 
dinov2_vitb14 = torch.hub.load('', 'dinov2_vitb14',source='local').cuda()
 
features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()
 
img_path = f'/kaggle/input/demo-image/1 (4).png'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():
    features_dict = dinov2_vitb14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
pca = PCA(n_components=3)
pca.fit(features)
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())
 
pca_features_fg = pca_features[:, 0] > 0.3
pca_features_bg = ~pca_features_fg
 
b = np.where(pca_features_bg)

pca.fit(features[pca_features_fg])
pca_features_rem = pca.transform(features[pca_features_fg])
for i in range(3):
    # transform using mean and std, I personally found this transformation gives a better visualization
    pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

pca_features_rgb = pca_features.copy()
pca_features_rgb[pca_features_fg] = pca_features_rem
pca_features_rgb[b] = 0

pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features.png')
plt.show()
plt.close()

在这里插入图片描述

3.2 使用vit_l14的模型

patch_h = 75
patch_w = 50
feat_dim = 1024
 
transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),
    T.Resize((patch_h * 14, patch_w * 14)),
    T.CenterCrop((patch_h * 14, patch_w * 14)),
    T.ToTensor(),
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
 
dinov2_vitl14 = torch.hub.load('', 'dinov2_vitl14',source='local').cuda()
 
features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()
 
img_path = f'/kaggle/input/demo-image/1 (4).png'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():
    features_dict = dinov2_vitl14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
pca = PCA(n_components=3)
pca.fit(features)
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())
 
pca_features_fg = pca_features[:, 0] > 0.3
pca_features_bg = ~pca_features_fg
 
b = np.where(pca_features_bg)

pca.fit(features[pca_features_fg])
pca_features_rem = pca.transform(features[pca_features_fg])
for i in range(3):
    # transform using mean and std, I personally found this transformation gives a better visualization
    pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

pca_features_rgb = pca_features.copy()
pca_features_rgb[pca_features_fg] = pca_features_rem
pca_features_rgb[b] = 0

pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features.png')
plt.show()
plt.close()

在这里插入图片描述

3.3 使用vit_g14的模型

patch_h = 75
patch_w = 50
feat_dim = 1536
 
transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),
    T.Resize((patch_h * 14, patch_w * 14)),
    T.CenterCrop((patch_h * 14, patch_w * 14)),
    T.ToTensor(),
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
 
dinov2_vitg14 = torch.hub.load('', 'dinov2_vitg14',source='local').cuda()
 
features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()
 
img_path = f'/kaggle/input/demo-image/1 (4).png'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():
    features_dict = dinov2_vitg14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
pca = PCA(n_components=3)
pca.fit(features)
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())
 
pca_features_fg = pca_features[:, 0] > 0.3
pca_features_bg = ~pca_features_fg
 
b = np.where(pca_features_bg)

pca.fit(features[pca_features_fg])
pca_features_rem = pca.transform(features[pca_features_fg])
for i in range(3):
    # transform using mean and std, I personally found this transformation gives a better visualization
    pca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

pca_features_rgb = pca_features.copy()
pca_features_rgb[pca_features_fg] = pca_features_rem
pca_features_rgb[b] = 0

pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features.png')
plt.show()
plt.close()

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/38868.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

详解CPU的态

目录 1.CPU的工作过程 2.寄存器 3.CPU的上下文 4.系统调用 5.CPU的态 1.CPU的工作过程 CPU要执行的指令的地址存在寄存器中,指令存放在内存中,而CPU本质上就是一个去内存中根据地址取指令,然后执行指令的硬件。 举一个例子&#xff1a…

应用级监控方案Spring Boot Admin

1.简介 Spring Boot Admin为项目常用的监控方式,可以动态的监控服务是否运行和运行的参数,如类的调用情况、流量等。其中分为server与client: server: 提供展示UI与监控服务。client:加入server,被监控的…

http1.0、http1.1 http 2.0

HTTP/1.0是无状态、无连接的应用层协议。 无连接 无连接:每次请求都要建立连接,需要使用 keep-alive 参数建立长连接、HTTP1.1默认长连接keep-alive   无法复用连接,每次发送请求都要进行TCP连接,TCP的连接释放都比较费事&…

[爬虫]解决机票网站文本混淆问题-实战讲解

前言 最近有遇到很多小伙伴私信向我求助,遇到的问题基本上都是关于文本混淆或者是字体反爬的问题。今天给大家带来其中一个小伙伴的实际案例给大家讲讲解决方法 📝个人主页→数据挖掘博主ZTLJQ的主页 ​​ 个人推荐python学习系列: ☄️爬虫J…

在Windows下安装Anaconda平台

Anaconda介绍 安装Python的方法有很多,其中利用Anaconda来安装,是最为安全和便捷的方法之一。在Python中安装类库,各个类库之间可能存在相互依赖、版本冲突等问题。为了解决这个问题,Python社区提供了方便的软件包管理工具&#…

在SPringBoot生成验证码

1.引入依赖,这个依赖中包含了生成验证码的工具类 <!--引入hutool --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.3.9</version></dependency> 2.编写配置类 import cn.hu…

【NLP】国外新动态--LLM模型

一、说明 NLP走势如何?这是关于在实践中使用大型语言模型(LLM)的系列文章中的一篇文章。在这里,我将介绍LLM,并介绍使用它们的3个级别。未来的文章将探讨LLM的实际方面,例如如何使用OpenAI的公共API,Hugging Face Transformers Python库,如何微调LLM,以及如何从头开始…

Maven -- <dependencyManagement>管理子项目版本

背景&#xff1a; 一个旧项目&#xff0c;想使用mybatis-plus&#xff0c;想着这是比较基础的依赖包&#xff0c;就在父项目中添加对应依赖&#xff0c;如下: <!-- 依赖声明 --><dependencyManagement><dependencies><!-- mybatis-plus 依赖配置 -->&l…

WMTS 地图切片Web服务 协议数据解析

1. WMTS 描述 WMTS(Web Map Tiles Service):地图切片Web服务。 2. 数据示例&#xff1a; arcgis online 导出的wmts xml&#xff1a; https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer/WMTS 内容解析&#xff1a; contents中可能包…

云计算相关概念

文章目录 一、云计算的三种部署模式&#xff1a;公有云、私有云、混合云--区别和特性二、华为云&#xff1a;简介、主要业务、特点和优势、不同场景和行业中的应用三、华为云-三剑客&#xff1a;IaaS、PaaS、SaaS 一、云计算的三种部署模式&#xff1a;公有云、私有云、混合云–…

uni-app的H5版本下载跨域问题

前端能正常访问图片&#xff0c;但无法下载 因为路径不经过业务代码&#xff0c;所以需要在nginx配置跨域 代码&#xff1a; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET, POST, OPTIONS; add_header Access-Control-Allow-H…

WIN无法访问linux开启的SAMBA服务器

WIN无法访问linux开启的SAMBA服务器 打开搜索框“管理Windows凭据” 点击编辑

[java安全]CommonsCollections1(LazyMap)

文章目录 【java安全】CommonsCollections1(LazyMap)前言LazyMap如何创建LazyMap对象&#xff1f;如何调用LazyMap的get()方法&#xff1f;如何触发AnnotationInvocationHandler#invoke()方法&#xff1f;POC总结参考 【java安全】CommonsCollections1(LazyMap) 前言 前面我们…

【云原生】k8s之HPA,命名空间资源限制

1.HPA的相关知识 HPA&#xff08;Horizontal Pod Autoscaling&#xff09;Pod 水平自动伸缩&#xff0c;Kubernetes 有一个 HPA 的资源&#xff0c;HPA 可以根据 CPU 利用率自动伸缩一个 Replication Controller、 Deployment 或者Replica Set 中的 Pod 数量。 &#xff08;1&a…

ubuntu 20.04 4090 显卡驱动安装 深度学习环境配置

1. 显卡驱动安装 准备工作&#xff1a; 换源安装输入法&#xff1a;重启的步骤先不管&#xff08;自选&#xff09;sudo apt update && sudo apt upgrade 禁用nouveau驱动&#xff08;这个驱动是ubuntu开源小组逆向破解NVIDIA的开源驱动&#xff0c;与英伟达的原有驱…

实训笔记7.19

实训笔记7.19 7.19一、座右铭二、Hadoop的HDFS分布式文件存储系统的相关原理性内容2.1 HDFS上传数据的流程2.2 HDFS下载数据的流程2.3 HDFS中NameNode和SecondaryNameNode工作机制&#xff08;涉及到HDFS的元数据管理操作&#xff09;2.4 HDFS中NameNode和DataNode的工作机制&a…

CVE-2023-1454注入分析复现

简介 JeecgBoot的代码生成器是一种可以帮助开发者快速构建企业级应用的工具&#xff0c;它可以通过一键生成前后端代码&#xff0c;无需写任何代码&#xff0c;让开发者更多关注业务逻辑。 影响版本 Jeecg-Boot<3.5.1 环境搭建 idea 后端源码&#xff1a; https://git…

opencv -12 图像运算之按 《位或》 运算(图像融合图像修复和去除)

位或运算 或运算的规则是&#xff0c;当参与或运算的两个逻辑值中有一个为真时&#xff0c;结果就为真。其逻辑关系可以类比为如图 所示的并联电路&#xff0c;两个开关中只要有任意一个闭合时&#xff0c;灯就会亮。 3-5 对参与或运算的算子的不同情况进行了说明&#xff0c;…

050、事务设计之Percolator事务模型

Percolator 背景 Bigtable: 大表打散每行到各个节点&#xff0c;每一行作为一个kv。解决的问题 一个事务涉及的行在多个节点&#xff0c;如何用单行对一个事务进行控制&#xff0c;实现原子性。 快照隔离级别&#xff08;snapshot &#xff09; 白色点&#xff1a;代表事务开始…

Spring:Bean生命周期

Bean 生命周期生命周期 Bean 生命周期是 bean 对象从创建到销毁的整个过程。 简单的 Bean 生命周期的过程: 1.实例化(调用构造方法对 bean 进行实例化) 2.依赖注入(调用 set 方法对 bean 进行赋值) 3.初始化(手动配置 xml 文件中 bean 标签的 init-method 属性值,来指…