【计算机视觉】DINOv2(Facebook自监督视觉学习)的环境部署和使用代码示范(含源代码)

news2024/9/24 23:28:49

文章目录

  • 一、导读
  • 二、环境部署
  • 三、使用示例

我的代码示范已经上传了Kaggle平台,具体的笔记地址为:

https://www.kaggle.com/code/holmes0610/dinov2

在这里插入图片描述

一、导读

DINOv2:在没有监督的情况下学习鲁棒的视觉特征

这是第一种训练计算机视觉模型的方法,它使用自我监督学习来实现与该领域使用的标准方法相匹配或超过标准方法的结果。

最近,自然语言处理在大量数据上进行模型预训练方面取得了突破,为计算机视觉中的类似基础模型开辟了道路。 这些模型可以通过产生通用的视觉特征(即无需微调即可跨图像分布和任务工作的特征)来极大地简化任何系统中图像的使用。

这项工作表明,现有的预训练方法,尤其是自监督方法,如果使用来自不同来源的足够的精选数据进行训练,可以产生此类特征。 我们重新审视现有的方法并结合不同的技术来扩展我们的数据和模型大小的预训练。 大多数技术贡献旨在加速和稳定大规模培训。 在数据方面,我们提出了一个自动管道来构建专用的、多样化的和精选的图像数据集,而不是像自我监督文献中通常所做的那样未经精选的数据。

在模型方面,我们使用 1B 参数训练 ViT 模型(Dosovitskiy 等人,2020),并将其提炼成一系列较小的模型,这些模型超越了最佳可用的通用功能 OpenCLIP(Ilharco 等人,2021) 大多数图像和像素级别的基准测试。

论文地址:

https://arxiv.org/abs/2304.07193

项目地址:

https://github.com/facebookresearch/dinov2/tree/main

在这里插入图片描述

demo的地址:

https://dinov2.metademolab.com/demos

深度估计的结果:

DINOv2 冻结特征可以很容易地用于预测单个图像的每像素深度的模型,无论是分布内还是分布外。

在这里插入图片描述

语义分割的结果:

DINOv2 冻结特征可以很容易地用于预测单个图像中每像素对象类的模型。

在这里插入图片描述

二、环境部署

先下载代码到本地:

在这里插入图片描述

或者可以直接使用git进行拉取项目文件:

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

在这里插入图片描述
按照源码要求,需要使用11.7的CUDA,需要查看一下自己的电脑是否符合要求,可以通过cmd输入:

nvidia-smi

查询支持的CUDA Version:

在这里插入图片描述

在上述文件夹中打开cmd命令(处于当前位置),输入以下指令:

创建新python的环境,按照conda.yml的形式:

conda env create -f conda.yaml 

然后激活环境:

conda activate dinov2

会遇到报错,用记事本打开conda.yml将红色部分先删除:

在这里插入图片描述

再次按照上面cmd命令运行,完成后会显示done。

然后手动安装剩余的包。

我是直接选择全部安装的:

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

在这里插入图片描述

最后虽然有报错,但是还可以顺利使用:

在这里插入图片描述
还需要安装sklearn:

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

在这里插入图片描述

到这里没有什么意外的话,环境就部署成功了!

三、使用示例

DINOv2提供的训练好的模型为:

import torch

dinov2_vits14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
dinov2_vitb14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14')
dinov2_vitl14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')
dinov2_vitg14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14')

我们提供 4 个模型:1 个从头开始训练的 ViT-g,以及从 ViT-g 中提取的 3 个 ViT-S/B/L 模型。

该模型将图像作为输入并返回类标记和补丁标记。

嵌入维数为:

在这里插入图片描述

这些模型遵循 Transformer 架构,补丁大小为 14。对于 224x224 图像,这会产生 1 个类标记 + 256 个补丁标记。

如果图像形状是块大小 (14) 的倍数,则模型可以接受更大的图像。 如果未验证此条件,模型将裁剪为最接近的小块大小倍数。

我们使用的测试原图为:

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

在这里插入图片描述

我们测试几个不同的模型,首先是dinov2_vits14:

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


patch_h = 60
patch_w = 40
feat_dim = 384  # vits14

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_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_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_bg = pca_features[:, 0] < 10
pca_features_fg = ~pca_features_bg

# PCA for only foreground patches
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].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5

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

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

输出的结果为:

在这里插入图片描述
dinov2_vitl14模型的使用:

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 = 1024 # vitl14
 
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_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_vitb14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
# print(features)
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)
# print("1",pca_features[:, 0])
# print(pca_features_fg)
# PCA for only foreground patches
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
# print("digtial",pca_features_rgb)
pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features3.png')
plt.show()
plt.close()

输出结果为:

在这里插入图片描述

最后使用vitg14:

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 = 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)),
])
 
dinov2_vitb14 = 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_vitb14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']
    
features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
# print(features)
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)
# print("1",pca_features[:, 0])
# print(pca_features_fg)
# PCA for only foreground patches
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
# print("digtial",pca_features_rgb)
pca_features_rgb = pca_features_rgb.reshape(4, patch_h, patch_w, 3)
plt.imshow(pca_features_rgb[0][...,::-1])
plt.savefig('features2.png')
plt.show()
plt.close()

最后的输出结果为:

在这里插入图片描述

明显最后的效果更好一点!

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2023爱分析·AIGC厂商全景报告|爱分析报告

关键发现 通用大模型市场当前虽入局者众多&#xff0c;但终局将高度集中&#xff0c;未来将由5-6家厂商主导&#xff1b;应用层厂商及甲方企业应着眼终局&#xff0c;从大算力、巨量数据集、端到端AI工程化能力以及应用生态伙伴等维度综合考虑&#xff0c;慎重选择合作伙伴。 …

【LeetCode每日一题合集】2023.7.10-2023.7.16

文章目录 16. 最接近的三数之和排序 双指针 1911. 最大子序列交替和解法——动态规划 2544. 交替数字和&#xff08;简单模拟&#xff09;931. 下降路径最小和&#xff08;线性DP&#xff09;979. 在二叉树中分配硬币⭐⭐⭐⭐⭐&#xff08;dfs&#xff09;18. 四数之和&#…

[JAVA]程序逻辑控制,输入输出

&#x1f349;内容专栏&#xff1a;【JAVA】 &#x1f349;本文脉络&#xff1a;程序逻辑控制&#xff0c;if语句&#xff0c;switch循环&#xff0c;while循环&#xff0c;for循环&#xff0c;do while循环输入输出&#xff0c;例子 &#x1f349;本文作者&#xff1a;Melon_西…

Python:基于matplotlib与mayavi的3D可视化

文章目录 &#xff08;1&#xff09;基于matplotlib的3D可视化&#xff08;2&#xff09;基于mayavi的3D可视化&#xff08;2.1&#xff09;立方体&#xff08;2.2&#xff09;体素体 3D可视化是一种用于呈现三维数据的方法&#xff0c;它可以帮助我们更好地理解和分析复杂的空…

搭建 Java 部署环境,部署 Web 项目到 Linux

为了进行部署&#xff0c;把写好的 java web 程序放到 Linux 上&#xff0c;需要先把对应的依赖的软件 (环境) 搭建好&#xff0c;安装一些必要的软件程序 JDKTomcatMySqL jdk 直接使用包管理器进行安装(基于yum安装) 一、yum 1、认识 yum yum (Yellow dog Updater, Modified…

随机产生50个55以内的不重复的整数,要求查找出一个缺失的整数。

一、设计思路 为随机产生50个55以内的整数且不能重复&#xff0c;应先将已经产生的随机数保存下来&#xff0c;然后在获取到新的随机数时&#xff0c;与之前保存下来的值进行对比&#xff0c;如果已经出现过则抛弃&#xff0c;并再次获取&#xff0c;直到获取到不同的随机值为止…

Redis 从入门到精通【进阶篇】之过期和淘汰策略详解

文章目录 0. 前言Redis过期策略&#xff1a;1. 定期删除&#xff08;定时器方式&#xff09;&#xff1a;2. 惰性删除&#xff08;访问时检查方式&#xff09;&#xff1a; Redis淘汰策略&#xff1a;1. noeviction&#xff1a;2. allkeys-lru&#xff1a;3. volatile-lru&…

GOPATH、GOROOT(VSCode编写第一个go程序)

1. GOROOT和GOPATH GOROOT 和 GOPATH 都是 Go 语言中的环境变量&#xff0c;用于指定 Go 工具链和工作区的路径。 GOROOT 指定了 Go 工具链的安装路径&#xff0c;它包含了 Go 语言的标准库、编译器等工具。在使用 Go 编译器、运行时等工具时&#xff0c;它们会默认从 GOROOT…

Linux ➾ 端口占用检查

Linux ➾ 端口占用检查 &#x1f53b; 前言&#x1f53b; 一、什么是监听端口&#x1f53b; 二、使用lsof命令查看端口占用情况&#x1f53b; 三、使用netstat命令检查监听端口&#x1f53b; 四、使用ss 检查监听端口&#x1f53b; 总结—温故知新 &#x1f53b; 前言 在Linux系…

举例说明基于线性回归的单层神经网络网络(以梯度下降算法来求解权重的过程)...

我们将通过一个简单的例子来说明基于线性回归的单层神经网络&#xff0c;以及如何使用梯度下降算法来求解权重。 假设我们有以下数据集&#xff0c;表示学生的学习时间&#xff08;小时&#xff09;与他们的考试分数&#xff1a; 学习时间&#xff08;X&#xff09;&#xff1a…

C# 反转无头链。

给定一个单链表的头结点pHead(该头节点是有值的&#xff0c;比如在下图&#xff0c;它的val是1)&#xff0c;长度为n&#xff0c;反转该链表后&#xff0c;返回新链表的表头。 数据范围&#xff1a; 0≤n≤1000 要求&#xff1a;空间复杂度 O(1) &#xff0c;时间复杂度 O(n) 。…

【SpringBoot应用篇】SpringBoot集成atomikos实现多数据源配置和分布式事务管理

【SpringBoot应用篇】SpringBoot集成atomikos实现多数据源配置和分布式事务管理 分布式事务概念XA和JTA概述SpringBoot集成atomikos数据库结构pom通用工具类RBaseControllerBaseExceptionCodeExceptionCodeBaseExceptionBaseUncheckedExceptionBizException application.yml数据…

【C语言】扫雷----详解(扩展版)

&#x1f341; 博客主页:江池俊的博客_CSDN博客 &#x1f341; 如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏&#x1f31f; 三连支持一下博主&#x1f49e; ✉️每一次努力都是一次进步&#xff0c;每一次尝试都是一次机会。无论遇到什么困难&#xff0c;…

SpringBoot 使用前缀树实现敏感词过滤

文章目录 前缀树介绍节点初始化前缀树添加敏感词删除敏感词敏感词过滤代码实现 前缀树介绍 前缀树&#xff08;Trie&#xff09;&#xff0c;也称为字典树或前缀字典树&#xff0c;是一种特殊的多叉树数据结构。它用于高效地存储和检索字符串集合。以下是前缀树的常见数据结构…

verilog实现led闪烁

文章目录 verilog实现led闪烁一、介绍二、代码三、仿真代码四、仿真结果五、总结 verilog实现led闪烁 一、介绍 使用verilog实现代码&#xff0c;实现led闪烁&#xff0c;每间隔200ms进行切换led灯 二、代码 module led (input wire clk,input wire rstn,output wire[3:0] …

深入解析向量数据库:定义、原理和应用的全面指南

&#x1f337;&#x1f341; 博主 libin9iOak带您 Go to New World.✨&#x1f341; &#x1f984; 个人主页——libin9iOak的博客&#x1f390; &#x1f433; 《面试题大全》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33…

C++智能指针(3/3)

目录 上一节内容 share_ptr用法 share_ptr指针可以用于上一节所说的错误 例子&#xff08;类定义&#xff09; 主函数代码 执行的结果 解释说明 share_ptr 相关构造 空的share指针可以指向其他相同类型的变量来进行托管 可以shared_ptr< T > sp2(new T())也可以s…

RocketMQ高阶使用

RocketMQ高阶使用 1. 流程 2. 探讨功能点 RocketMQ的顺序消息消息投递策略消息保障 3. 顺序消息 3.1 顺序类型 3.1.1 无序消息 无序消息也指普通的消息&#xff0c;Producer 只管发送消息&#xff0c;Consumer 只管接收消息&#xff0c;至于消息和消息之间的顺序并没有保证…

macOS搭建C++开发环境CLion

首先我是一个java开发者&#xff0c;最近对C产生点兴趣。想开发点C程序玩一玩。 下载IDE 本人是java开发者&#xff0c;习惯使用IDEA了。所以也下载jetbrains的C开发工具:clion 下载地址&#xff1a; https://www.jetbrains.com/clion/download/#sectionmac Hello world Fi…

利用ArcGIS Pro制作三维效果图

1、新建工程 打开Arcgispro,新建工程,这里我们要用到的模板为全局场景。 2、添加数据 这里添加的数据需要有一个字段内容是数值的,这个字段也是接下来要进行拉伸的字段。 3、高度拉伸 数据添加进来后,如下图所示,这时图层处于2D图层里。 这时我们点中该图层,回到菜单栏…