dinov2 使用实例

import cv2
import torch

import torchvision.transforms as T

import numpy as np

from PIL import Image

from sklearn.decomposition import PCA

patch_h = 50
patch_w = 50
feat_dim = 1536  # vitg14

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)), ])

# model = models.resnet50()

# 加载权重
# model.load_state_dict(torch.load(r'C:\Users\Administrator\.cache\torch\hub\checkpoints\dinov2_vitg14_pretrain.pth'))

dinov2_vitb14 = torch.hub.load('', 'dinov2_vitg14', source='local').cuda()

# print(dinov2_vitb14)

# extract features
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'./dog.jpeg'  # 修改图片路径
i

你可能感兴趣的:(深度学习宝典,深度学习)