pytorch模型参数迁移(三种方法),解密Linux运维开发常见误区

    out = self.relu(out)

    out = self.conv3(out)
    out = self.bn3(out)

    if self.downsample is not None:
        identity = self.downsample(x)

    out += identity
    out = self.relu(out)

    return out

class ResNet(nn.Module):

def __init__(self, num\_outputs=None,  #输出的分类数
             backbone=None,
             pretrained=False,
             curriculum\_steps=None,
             extra\_outputs=0,
             share\_top\_y=True,
             pred\_category=False,
             block=BasicBlock, block\_num=[2,2,2,2],include_top=True, groups=1,
             width\_per\_group=64):
    # blocks\_num:残差结构中每个block存在多少个layer层
    super(ResNet, self).__init__()

    self.include_top = include_top
    self.in_channel = 64  # 输入图片经过第一层卷积的通道数
    self.groups = groups
    self.width_per_group = width_per_group
    self.conv1 = nn.Conv2d(3, self.in_channel, kernel\_size=7, stride=2, padding=3,
                           bias=False)
    self.bn1 = nn.BatchNorm2d(self.in_channel)
    self.relu = nn.ReLU(inplace=True)

    self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

    self.layer1 = self._make_layer(block, 64, block_num[0])
    self.layer2 = self._make_layer(block, 128, block_num[1], stride=2)
    self.layer3 = self._make_layer(block, 256, block_num[2], stride=2)
    self.layer4 = self._make_layer(block, 512, block_num[3], stride=2)

    self.avgpool = nn.AdaptiveAvgPool2d((1, 1))  # output size =(1,1)
    self.fc = nn.Linear(512 * block.expansion, 1000)

    image_size = 12
    patch_size = 3  # 后期尝试改为2
    dim = 128
    depth = 2
    num_classes = 35
    expansion_factor = 4
    num_patches = (image_size // patch_size) ** 2


    self.curriculum_steps = [0, 0, 0, 0] if curriculum_steps is None else curriculum_steps
    self.share_top_y = share_top_y
    self.extra_outputs = extra_outputs
    self.pred_category = pred_category
    self.sigmoid = nn.Sigmoid()

def _make_layer(self, block, channel, block_num, stride=1):
    downsample = None
    if stride != 1 or self.in_channel != channel * block.expansion:
        downsample = nn.Sequential(
            nn.Conv2d(self.in_channel, channel * block.expansion, kernel\_size=1, stride=stride, bias=False),
            nn.BatchNorm2d(channel * block.expansion))

    layers = []
    layers.append(block(self.in_channel,
                        channel,
                        downsample=downsample,
                        stride=stride,
                        groups=self.groups,
                        width\_per\_group=self.width_per_group))
    self.in_channel = channel * block.expansion
    for \_ in range(1, block_num):
        layers.append(block(self.in_channel,
                            channel,
                            groups=self.groups,
                            width\_per\_group=self.width_per_group))

    return nn.Sequential(*layers)

def forward(self, x, epoch=None, **kwargs):

    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu(x)
    x = self.maxpool(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)  # torch.Size[B 128 12 20]

    x = self.avgpool(x)
    x = x.view(x.size(0), -1)
    x = self.fc(x)

    return x

if name == “__main__”:
# device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)
device = ‘cpu’
print(“-----device:{}”.format(device))
print(“-----Pytorch version:{}”.format(torch.version))

input_tensor = torch.zeros(1, 3, 100, 100)
print('input\_tensor:', input_tensor.shape)



pretrained_file = "./model\_resnet18.pt"
model = ResNet()
model.load_state_dict(torch.load(pretrained_file))
model.eval()
out = model(input_tensor)
print("out:", out.shape, out[0, 0:10])

运行结果如下:



-----device:cpu
-----Pytorch version:1.5.0
input_tensor: torch.Size([1, 3, 100, 100])
out: torch.Size([1, 1000]) tensor([ 0.4010, 0.8436, 0.3071, 0.0627, 0.4446, 0.8470, 0.1882, 0.7012,
0.2988, -0.7574], grad_fn=)


3.修改resnet18的网络架构后,如何加载原来已经训练好的模型参数。  
 例如:



#将114行的代码修改成
self.layer44 = self._make_layer(block, 512, block_num[3], stride=2)
#将166行的代码修改成
x = self.layer44(x)


直接加载模型,运行结果:



RuntimeError: Error(s) in loading state_dict for ResNet:
Missing key(s) in state_dict: “layer44.0.conv1.weight”, “layer44.0.bn1.weight”, “layer44.0.bn1.bias”, “layer44.0.bn1.running_mean”, “layer44.0.bn1.running_var”, “layer44.0.conv2.weight”, “layer44.0.bn2.weight”, “layer44.0.bn2.bias”, “layer44.0.bn2.running_mean”, “layer44.0.bn2.running_var”, “layer44.0.downsample.0.weight”, “layer44.0.downsample.1.weight”, “layer44.0.downsample.1.bias”, “layer44.0.downsample.1.running_mean”, “layer44.0.downsample.1.running_var”, “layer44.1.conv1.weight”, “layer44.1.bn1.weight”, “layer44.1.bn1.bias”, “layer44.1.bn1.running_mean”, “layer44.1.bn1.running_var”, “layer44.1.conv2.weight”, “layer44.1.bn2.weight”, “layer44.1.bn2.bias”, “layer44.1.bn2.running_mean”, “layer44.1.bn2.running_var”.
Unexpected key(s) in state_dict: “layer4.0.conv1.weight”, “layer4.0.bn1.weight”, “layer4.0.bn1.bias”, “layer4.0.bn1.running_mean”, “layer4.0.bn1.running_var”, “layer4.0.bn1.num_batches_tracked”, “layer4.0.conv2.weight”, “layer4.0.bn2.weight”, “layer4.0.bn2.bias”, “layer4.0.bn2.running_mean”, “layer4.0.bn2.running_var”, “layer4.0.bn2.num_batches_tracked”, “layer4.0.downsample.0.weight”, “layer4.0.downsample.1.weight”, “layer4.0.downsample.1.bias”, “layer4.0.downsample.1.running_mean”, “layer4.0.downsample.1.running_var”, “layer4.0.downsample.1.num_batches_tracked”, “layer4.1.conv1.weight”, “layer4.1.bn1.weight”, “layer4.1.bn1.bias”, “layer4.1.bn1.running_mean”, “layer4.1.bn1.running_var”, “layer4.1.bn1.num_batches_tracked”, “layer4.1.conv2.weight”, “layer4.1.bn2.weight”, “layer4.1.bn2.bias”, “layer4.1.bn2.running_mean”, “layer4.1.bn2.running_var”, “layer4.1.bn2.num_batches_tracked”.


方法一:将原来预训练好的模型参数迁移到新的resnet18网络架构中,只有迁移两者相同的模型参数,不同的参数还是随机初始化。



**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/77e2dc0acba7703a5d7129f7fc675d87.png)
![img](https://img-blog.csdnimg.cn/img_convert/702876a721bad64b276974f4745cf90a.png)
![img](https://img-blog.csdnimg.cn/img_convert/6e550f4e053bc549d807a5696fe0676b.png)
![img](https://img-blog.csdnimg.cn/img_convert/744603d945e05b14684b5647d0ad040e.png)
![img](https://img-blog.csdnimg.cn/img_convert/2381aeaf556f10a0fb7efff80ce048fc.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)**
![img](https://img-blog.csdnimg.cn/img_convert/5872837a4d1618ff3f193654dbde5550.jpeg)



为了做好运维面试路上的助攻手,特整理了上百道 **【运维技术栈面试题集锦】** ,让你面试不慌心不跳,高薪offer怀里抱!

这次整理的面试题,**小到shell、MySQL,大到K8s等云原生技术栈,不仅适合运维新人入行面试需要,还适用于想提升进阶跳槽加薪的运维朋友。**

![](https://img-blog.csdnimg.cn/img_convert/2fdbaa2ed670fd079395408dc633b08c.png)

本份面试集锦涵盖了

*   **174 道运维工程师面试题**
*   **128道k8s面试题**
*   **108道shell脚本面试题**
*   **200道Linux面试题**
*   **51道docker面试题**
*   **35道Jenkis面试题**
*   **78道MongoDB面试题**
*   **17道ansible面试题**
*   **60道dubbo面试题**
*   **53道kafka面试**
*   **18道mysql面试题**
*   **40道nginx面试题**
*   **77道redis面试题**
*   **28道zookeeper**

**总计 1000+ 道面试题, 内容 又全含金量又高**

*   **174道运维工程师面试题**

> 1、什么是运维?

> 2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

> 3、现在给你三百台服务器,你怎么对他们进行管理?

> 4、简述raid0 raid1raid5二种工作模式的工作原理及特点

> 5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

> 6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

> 7、Tomcat和Resin有什么区别,工作中你怎么选择?

> 8、什么是中间件?什么是jdk?

> 9、讲述一下Tomcat8005、8009、8080三个端口的含义?

> 10、什么叫CDN?

> 11、什么叫网站灰度发布?

> 12、简述DNS进行域名解析的过程?

> 13、RabbitMQ是什么东西?

> 14、讲一下Keepalived的工作原理?

> 15、讲述一下LVS三种模式的工作过程?

> 16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

> 17、如何重置mysql root密码?

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
![img](https://img-blog.csdnimg.cn/img_convert/f8b36d362ffabbcfccec9a53cdd5e911.jpeg)

> 15、讲述一下LVS三种模式的工作过程?

> 16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

> 17、如何重置mysql root密码?

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
[外链图片转存中...(img-27Kx4FQX-1712543809353)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值