训练权重匹配策略

rom torch import nn
import torch
import os#环境代理设置
import torchvision.models as models
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
model=timm.create_model('vit_small_patch16_224.dino',pretrained=True,in_chans=3,img_size=(1280,1280))
#m=nn.Sequential(*(m.children()))
class CustomConvit(nn.Module):
    def __init__(self):
        super().__init__()
        # 直接将Convit模型的基础部分添加到Sequential中
        self.features = model
    def forward(self,x):
        x = self.features(x)
        return x
data=torch.ones(1,3,1280,1280)
# 创建自定义的Convit模型
m= CustomConvit()
print(m(data).shape)
# 打印模型的参数字典的键
print("Model State Dict Keys:")
for key in m.features.state_dict().keys():
    print(key,end='')
    print((m.features.state_dict().get(key)).shape)
# 加载你自定义的权重
checkpoint_path = 'vit_small_patch16_224.dino.bin'
checkpoint = torch.load(checkpoint_path)
#rint(checkpoint)
# 打印自定义权重的键
print("\nCustom Weights Dict Keys:")
for key in checkpoint.keys():
    print(key,end='')
    print((m.features.state_dict().get(key)).shape)
# 创建新的权重字典,仅包含相等的键
matching_weights = {key: checkpoint[key] for key in m.features.state_dict().keys() if key in checkpoint and checkpoint[key].shape==m.features.state_dict().get(key).shape}

# 打印匹配的键的信息
for key, value in matching_weights.items():
    print(f"Key: {key}, Shape: {value.shape}")

# 加载匹配的权重到模型
model.load_state_dict(matching_weights, strict=False)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值