基于dinoV2分类模型修改

2024-01-16 03:12
文章标签 分类 模型 修改 dinov2

本文主要是介绍基于dinoV2分类模型修改,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

dinoV2已经发布有一段时间了,faecbook豪言直接说前面的结构我们都不需要进行修改,只需要修改最后的全连接层就可以达到一个很好的效果。我们激动的揣摸了下自己激动的小手已经迫不及待了,这里我使用dinoV2进行了实验,来分享下实验结果。

  • dinoV2官方地址:github链接

一、模型介绍

1、预训练模型介绍

# dinov2_vits14_pretrain.pth 结构 
# s,b,l,g 主要是blocks 模块数量不同,DinoVisionTransformer((patch_embed): PatchEmbed((proj): Conv2d(3, 384, kernel_size=(14, 14), stride=(14, 14))(norm): Identity())(blocks): ModuleList((0-11): 12 x NestedTensorBlock((norm1): LayerNorm((384,), eps=1e-06, elementwise_affine=True)(attn): MemEffAttention((qkv): Linear(in_features=384, out_features=1152, bias=True)(attn_drop): Dropout(p=0.0, inplace=False)(proj): Linear(in_features=384, out_features=384, bias=True)(proj_drop): Dropout(p=0.0, inplace=False))(ls1): LayerScale()(drop_path1): Identity()(norm2): LayerNorm((384,), eps=1e-06, elementwise_affine=True)(mlp): Mlp((fc1): Linear(in_features=384, out_features=1536, bias=True)(act): GELU(approximate='none')(fc2): Linear(in_features=1536, out_features=384, bias=True)(drop): Dropout(p=0.0, inplace=False))(ls2): LayerScale()(drop_path2): Identity()))(norm): LayerNorm((384,), eps=1e-06, elementwise_affine=True)(head): Identity()
)

2、项目文件介绍

这里可以直接用hubconf.py文件里面进行调用,大家可以根据需求来进行选择。
在这里插入图片描述
在这里插入图片描述导入模型第一次都是从网络进行导入,对于国内用户可能不成功,这里大家可以修改为本地导入,传入已经下载好的预训练模型就行。这里给大家分享一个百度网盘的地址,提取码:mhdq,更多模型大家从官网下载。
导入代码如下:

  • 注意 : dinov2_vitl14 此为L模型大小导入方法,需要和模型大小进行对应。
# hubconf.py文件 中导入
model = dinov2_vitl14(weights={'LVD142M':'/media/wqg/minio/model/dinoV2/dinov2_vitl14_pretrain.pth'})

这里如果直接使用model.eval()
模型输出是(bs,embed_dim)如果是一张图,使用dinov2_vits14模型,则输出是 (1,384)
b,l,g,的embed_dim大家可以通过model.embed_dim进行查看。

3、模型输出

由于我实验的时候发现仅仅只使用x_norm_clstoken效果一直不理想,我这里用到了x_norm_regtokens。
这里可以参考github中的finetune中的导入方法。

# 实例化模型代码
from functools import partial
from dinov2.eval.linear import create_linear_input
from dinov2.eval.linear import LinearClassifier
from dinov2.eval.utils import ModelWithIntermediateLayersmodel = dinov2_vits14(weights={'LVD142M':'./model/dinoV2/dinov2_vits14_pretrain.pth'})
autocast_ctx = partial(torch.cuda.amp.autocast, enabled=True, dtype=torch.float16)
self.feature_model = ModelWithIntermediateLayers( model, n_last_blocks=1, autocast_ctx=autocast_ctx).to(device)# 实例化分类模型全连接层。
self.embed_dim = model.embed_dim# 100对应的是你需要分类的类别数量
self.classifier = LinearClassifier( self.embed_dim*2, use_n_blocks=1, use_avgpool=True, num_classes=100).to(device)  # 冻结骨干网络
for param in model.feature_model.parameters():param.requires_grad = False

这里的self.feature_model 输出是有2个维度的,一个是x_norm_regtokens,shape为(bs,pach_h*pach_w,embed_dim),pach_h = input_h/14,pach_w = input_w/14.
另一个是x_norm_clstoken,shape为(bs,embed_dim)。一般情况下x_norm_clstoken用来分类就已经足够了

4、完整代码

from modeling.dinov2.eval.linear import LinearClassifier,create_linear_input
from modeling.dinov2.eval.utils import ModelWithIntermediateLayers
from functools import partialfrom modeling.dinov2.hub.backbones import dinov2_vitb14, dinov2_vitg14, dinov2_vitl14, dinov2_vits14
from modeling.dinov2.hub.backbones import dinov2_vitb14_reg, dinov2_vitg14_reg, dinov2_vitl14_reg, dinov2_vits14_regclass HubConf(nn.Module):def __init__(self,cfg,pretrain_choice = 'frozen'):super(HubConf, self).__init__()model_path = cfg.MODEL.PRETRAIN_PATHself.cfg = cfgself.base = dinov2_vits14(weights={'LVD142M':'./model/dinoV2/dinov2_vits14_pretrain.pth'})self.in_planes = self.base.embed_dimautocast_ctx = partial(torch.cuda.amp.autocast, enabled=True, dtype=torch.float16)self.feature_model = ModelWithIntermediateLayers(self.base, n_last_blocks=1, autocast_ctx=autocast_ctx)if pretrain_choice == 'frozen':for param in self.feature_model.parameters():param.requires_grad = Falseself.classifier = LinearClassifier(self.in_planes*2, use_n_blocks=1, use_avgpool=True, num_classes=cfg.MODEL.nc)def forward(self, x):global_feat = self.feature_model(x)  # ((b,256, embed_dim ),(b, embed_dim )) ((1,256,384),(1,384))out = self.classifier(global_feat)return  outdef load_param(self, trained_path, device='cpu'):param_dict = torch.load(trained_path, map_location=device)for i in param_dict:#if 'classifier' in i:if i not in self.state_dict():print('not load param ', i)continueself.state_dict()[i].copy_(param_dict[i])

二、模型修改

这里骨干网络已经完全冻结,没有什么需要修改的,只需要对x_norm_regtokens进行添加卷积操作。

1、添加卷积

# neck结构,在输出后添加卷积的过程。def autopad(k, p=None):  # kernel, padding# Pad to 'same'if p is None:p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-padreturn pclass Conv(nn.Module):# Standard convolutiondef __init__(self, c1, c2, k=1, s=1, p=None, g=1,act=True):  # ch_in, ch_out, kernel, stride, padding, groupssuper().__init__()self.conv = nn.Conv1d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)self.bn = nn.BatchNorm1d(c2)self.act = nn.ReLU()def forward(self, x):return self.act(self.bn(self.conv(x)))class neck_dinov2(nn.Module):def __init__(self,c0,c1,nc,dropout= 0.5):super().__init__()self.conv1 = Conv(c0,c0*2)self.conv2 = Conv(c0*2,c0)self.drop = nn.Dropout(p=dropout, inplace=True)self.line = LinearClassifier(c1*2, use_n_blocks=1, use_avgpool=True, num_classes=nc)def forward(self,x):x1 = copy.copy(x[0][0])x1 = self.drop(self.conv2(self.conv1(x1)))x = [[x1,copy.copy(x[0][1])]]return self.line(x)

2、完整代码

我这里实验的是多头输出,大家单头的可以只实验一次neck结构就行。


class HubConf(nn.Module):def __init__(self,cfg,pretrain_choice = 'frozen'):super(HubConf, self).__init__()model_path = cfg.MODEL.PRETRAIN_PATHself.cfg = cfgself.base = eval(cfg.MODEL.NAME)(weights={'LVD142M':model_path})self.in_planes = self.base.embed_dimself.consize = int((cfg.INPUT.SIZE_TRAIN[0]/14)*(cfg.INPUT.SIZE_TRAIN[1]/14))autocast_ctx = partial(torch.cuda.amp.autocast, enabled=True, dtype=torch.float16)self.feature_model = ModelWithIntermediateLayers(self.base, n_last_blocks=1, autocast_ctx=autocast_ctx)if pretrain_choice == 'frozen':for param in self.feature_model.parameters():param.requires_grad = Falseself.line = LinearClassifier(self.in_planes * 2, use_n_blocks=1, use_avgpool=True, num_classes=100)self.country_cls = neck_dinov2(self.consize, self.in_planes, cfg.MODEL.nc1, dropout=cfg.MODEL.DROPOUT)  # 分类头1self.cn_cls = neck_dinov2(self.consize,self.in_planes, cfg.MODEL.nc2, dropout=cfg.MODEL.DROPOUT)  # 分类头2self.ct_cls = neck_dinov2(self.consize,self.in_planes, cfg.MODEL.nc3, dropout=cfg.MODEL.DROPOUT)  # 分类头3def forward(self, x):global_feat = self.feature_model(x)  # ((bs, pach_h*pach_w,embed_dim ),(bs, embed_dim ))    ((1,(224/14)*(224/14), 384),(1, 384))country_score = self.country_cls(global_feat)cn_score = self.cn_cls(global_feat)ct_score = self.ct_cls(global_feat)return (country_score, cn_score,ct_score)def load_param(self, trained_path, device='cuda:0'):param_dict = torch.load(trained_path, map_location=device)for i in param_dict:#if 'classifier' in i:if i not in self.state_dict():print('not load param ', i)continueself.state_dict()[i].copy_(param_dict[i])

三、实验自己的数据

1、车辆品牌分类。

  • 车辆品牌为单分类,目前类别有178类,输入图像大小为(126,252),输入图片为车头或者车辆尾部截图。
  • 使用单一的LinearClassifier分类效果不如resnet50的全训练效果,个人分析主要原因是车标太小了,全连接无法准确的学习到,所以我在x_norm_regtokens维度添加了卷积操作。
  • 可视化特征图。使用的骨干为dinov2_vitb14_pretrain,可视化效果如下

在这里插入图片描述

  • 可视化代码
import torch
import torchvision.transforms as T
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from sklearn.decomposition import PCA
import matplotlib
from dinov2.hub.backbones import dinov2_vitb14, dinov2_vitg14, dinov2_vitl14, dinov2_vits14patch_h = 50
patch_w = 100
feat_dim = 384transform = 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()
vits14 = torch.hub.load('', 'dinov2_vits14', weights={'LVD142M':'./model/dinoV2/dinov2_vits14_pretrain.pth'},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'/home/wqg/桌面/car_face_crop/face/face_0003600_111963.jpg'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():features_dict = 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_fgb = 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 visualizationpca_features_rem[:, i] = (pca_features_rem[:, i] - pca_features_rem[:, i].mean()) / (pca_features_rem[:, i].std() ** 2) + 0.5pca_features_rgb = pca_features.copy()
pca_features_rgb[pca_features_fg] = pca_features_rem
pca_features_rgb[b] = 0pca_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()

2、车辆属性分类。

  • 车辆属性分类为多头输出,其中需要输出车辆类型,车辆颜色,车辆朝向等。
  • 只使用LinearClassifier作为每个分类头进行输出既可获得较好的效果。

四、结论

  • 使用dinoV2在大图上做细粒度分类效果不如整体训练效果,需要再通过卷积获得更小区域目标的强化学习。
  • 使用dinoV2在分类整体图像效果时,可以直接得到一个较好的效果,比原有的模型输出效果更好,无须再训练backbone部分,

相关引用链接:

  • dinoV2github: https://github.com/facebookresearch/dinov2
  • dinoV2 finetune:https://github.com/xuwangyin/dinov2-finetune/tree/main
  • dinoV2预训练权重:链接: https://pan.baidu.com/s/1ly7JpCu4Oi5gVBKixafXQg 提取码: mhdq

这篇关于基于dinoV2分类模型修改的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/611145

相关文章

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

使用Python在Excel中插入、修改、提取和删除超链接

《使用Python在Excel中插入、修改、提取和删除超链接》超链接是Excel中的常用功能,通过点击超链接可以快速跳转到外部网站、本地文件或工作表中的特定单元格,有效提升数据访问的效率和用户体验,这... 目录引言使用工具python在Excel中插入超链接Python修改Excel中的超链接Python

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

DeepSeek模型本地部署的详细教程

《DeepSeek模型本地部署的详细教程》DeepSeek作为一款开源且性能强大的大语言模型,提供了灵活的本地部署方案,让用户能够在本地环境中高效运行模型,同时保护数据隐私,在本地成功部署DeepSe... 目录一、环境准备(一)硬件需求(二)软件依赖二、安装Ollama三、下载并部署DeepSeek模型选