精华内容
下载资源
问答
  • Pytorch加载模型

    2020-01-09 18:51:57
    一、假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net() 1. cpu -> cpu或者gpu -> gpu: checkpoint = torch.load('modelparameters.pth') model.load_state_...

    一、假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net()

    1. cpu -> cpu或者gpu -> gpu:

    checkpoint = torch.load('modelparameters.pth')
    
    model.load_state_dict(checkpoint)

    2. cpu -> gpu 0

    checkpoint = torch.load('modelparameters.pth', map_location=lambda storage, loc: storage.cuda(0))
    
    model.load_state_dict(checkpoint)

    3. gpu 0 -> gpu 1

    checkpoint = torch.load('modelparameters.pth', map_location={'cuda:1':'cuda:0'})
    
    model.load_state_dict(checkpoint)
    4. gpu -> cpu
    checkpoint = torch.load('modelparameters.pth', map_location=lambda storage, loc: storage)
    
    model.load_state_dict(checkpoint)
    二、pytorch完整加载模型实例
    # 模型是GPU训练的
    def load_models(model_param_path):
        torch.manual_seed(14)
        np.random.seed(14)
        random.seed(14)
        if torch.cuda.is_available():
            cudnn.benchmark = True
            torch.cuda.manual_seed_all(14)
    
        model = FeatherNetB()
        model = torch.nn.DataParallel(model)
        
        device = torch.device('cuda:' + str(0) if torch.cuda.is_available() else "cpu")
        model.to(device)
    
        print("=> loading checkpoint '{}'".format(param_path))
        if torch.cuda.is_available():
            #gpu测试
            checkpoint = torch.load(model_param_path)
        else:
            #cpu测试
            checkpoint = torch.load(model_param_path, map_location=lambda storage, loc: storage)
        model.load_state_dict(checkpoint['state_dict'])
        
        #测试的时候必须加上model.eval(),训练就用model.train()
        model.eval()             
           
        return model

     

    展开全文
  • pytorch加载模型与冻结

    2020-04-23 22:43:45
    pytorch加载模型与冻结 加载模型 读取torch.save保存的模型 weights=torch.load(path) 读取pickle.dump保存的模型 with open('a.pkl', 'wb') as f: pickle.dump(score_dict, f) weights=pickle.load(f) #pickle....

    pytorch加载模型、冻结、优化

    加载模型

    读取torch.save保存的模型

    weights=torch.load(path)
    

    读取pickle.dump保存的模型

    with open('a.pkl', 'wb') as f:
    	pickle.dump(score_dict, f)
    weights=pickle.load(f)        
    

    model加载模型

    直接加载

    model.load_state_dict(weights)
    

    字典生成式加载

    self.load_state_dict({k:v for k,v in weights.items()})
    #self.load_state_dict({k.replace("module.",""):v for k,v in weights.items()})
    

    collections.OrderedDict()加载

    b = collections.OrderedDict()
    for k,v in weights.items():
    	b[k]=v
    	#b[k.replace("module.","")]=v
    self.load_state_dict(b)
    

    更新参数

    new_weights=model.state_dict()
    new_weights.update(weights) # 将weights中参数更新至new_weights中
    self.load_state_dict(new_weights)
    

    冻结模型

    for key, value in model.named_parameters():# named_parameters()包含网络模块名称 key为模型模块名称 value为模型模块值,可以通过判断模块名称进行对应模块冻结
    	value.requires_grad = True
    
    for value in model.parameters()():#不包含网络模块名称 value为模型模块值
    	value.requires_grad = True
    

    局部优化模型

    使用filter过滤需要的模块参数

    optimizer = optim.SGD(
     	filter(lambda p: p.requires_grad, model.parameters()),   #只更新			requires_grad=True的参数,即进行反向传播的参数
    	lr=,
    	momentum=,
    	weight_decay=,
    	nesterov=
    )
    
    展开全文
  • pytorch加载模型时报错

    2019-09-26 10:01:51
    [转自]...pytorch加载模型时报错 RuntimeError: Error(s) in loading state_dict for DataParallel: Missing key(s) in state_dict: “module.backbone.layers.0.stage_1.layers.0....

    [转自]https://www.lizenghai.com/archives/4610.html

    pytorch加载模型时报错

    RuntimeError: Error(s) in loading state_dict for DataParallel:

    Missing key(s) in state_dict: “module.backbone.layers.0.stage_1.layers.0.weight”,

    这是因为加载的预训练模型之前使用了torch.nn.DataParallel(),而此时没有使用,所以可以加上该模块或者去掉。

    加上torch.nn.DataParallel()模块

    model = torch.nn.DataParallel(model)

    cudnn.benchmark = True

    创建一个没有module.的新字典,即将原来字典中module.删除掉

    model.load_state_dict = torch.load(‘model/path’)
    create new OrderedDict that does not contain module.

    from collections import OrderedDict

    new_state_dict = OrderedDict()

    for k, v in state_dict.items():
    	 # remove `module.
    	 name = k[7:] 
         new_state_dict[name] = v
    model.load_state_dict(torch.load(new_state_dict))
    

    model.load_state_dict({k.replace(‘module.’,”):v for k,v in torch.load(‘model/path’).items()})

    展开全文
  • pytorch 加载模型 模型大小测试速度

    千次阅读 2019-06-22 14:29:17
    Pytorch保存和加载整个模型: torch.save(model, 'model.pth') model = torch.load('model.pth') Pytorch保存和加载预训练模型参数: torch.save(model.state_dict(), 'params.pth') model.load_state_dict(t...

    直接加载整个模型

    Pytorch保存和加载整个模型:

         torch.save(model, 'model.pth')
         model = torch.load('model.pth')



    Pytorch保存和加载预训练模型参数:

         torch.save(model.state_dict(), 'params.pth')
         model.load_state_dict(torch.load('params.pth'))


     

    网络加层了,但是预训练没有,加载模型方法:

    model.load_state_dict(state_dict,strict=False)

    网络是cpu版本时:

      model_path = "../best_mode.pth"
    
        a_net.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))

    测试并获取模型大小: 

        size=112
        import torch
        import os
    
        model = CNNModel(scale=1, num_classes=100,width=size,height=size)
    
        model.cuda()
        model.eval()
        model_path="dicenet.pth"
        torch.save(model.state_dict(),model_path)
    
        import os
        import time
        fsize = os.path.getsize(model_path)
        fsize = fsize / float(1024 * 1024)
    
        print(f"model size {round(fsize, 2)} m")
    
        input = torch.rand(100, 3, size, size).cuda()
        for i in range(15):
            t1 = time.time()
            loc = model(input)
    
            cnt = time.time() - t1
            print(cnt, loc.size())
    

    yolov5的加载方法,但是仍然需要原来的网络组件都在,否则也会报异常 

    class Ensemble(nn.ModuleList):
        # Ensemble of models
        def __init__(self):
            super(Ensemble, self).__init__()
    
        def forward(self, x, augment=False):
            y = []
            for module in self:
                y.append(module(x, augment)[0])
            # y = torch.stack(y).max(0)[0]  # max ensemble
            # y = torch.stack(y).mean(0)  # mean ensemble
            y = torch.cat(y, 1)  # nms ensemble
            return y, None  # inference, train output
    
    def attempt_load(weights, map_location=None):
        # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
        model = Ensemble()
        for w in weights if isinstance(weights, list) else [weights]:
            attempt_download(w)
            model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval())  # load FP32 model
    
        # Compatibility updates
        for m in model.modules():
            if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]:
                m.inplace = True  # pytorch 1.7.0 compatibility
            elif type(m) is Conv:
                m._non_persistent_buffers_set = set()  # pytorch 1.6.0 compatibility
    
        if len(model) == 1:
            return model[-1]  # return model
        else:
            print('Ensemble created with %s\n' % weights)
            for k in ['names', 'stride']:
                setattr(model, k, getattr(model[-1], k))
            return model  # return ensemble

    下面是resnet系列的测试代码:

    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function
    
    import os
    
    import time
    import torch
    import torch.nn as nn
    import torch.utils.model_zoo as model_zoo
    
    BN_MOMENTUM = 0.1
    
    model_urls = {
        'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
        'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
        'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
        'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
        'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
    }
    
    def conv3x3(in_planes, out_planes, stride=1):
        """3x3 convolution with padding"""
        return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                         padding=1, bias=False)
    
    
    class BasicBlock(nn.Module):
        expansion = 1
    
        def __init__(self, inplanes, planes, stride=1, downsample=None):
            super(BasicBlock, self).__init__()
            self.conv1 = conv3x3(inplanes, planes, stride)
            self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
            self.relu = nn.ReLU(inplace=True)
            self.conv2 = conv3x3(planes, planes)
            self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
            self.downsample = downsample
            self.stride = stride
    
        def forward(self, x):
            residual = x
    
            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu(out)
    
            out = self.conv2(out)
            out = self.bn2(out)
    
            if self.downsample is not None:
                residual = self.downsample(x)
    
            out += residual
            out = self.relu(out)
    
            return out
    
    
    class Bottleneck(nn.Module):
        expansion = 4
    
        def __init__(self, inplanes, planes, stride=1, downsample=None):
            super(Bottleneck, self).__init__()
            self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
            self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
            self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                                   padding=1, bias=False)
            self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
            self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1,
                                   bias=False)
            self.bn3 = nn.BatchNorm2d(planes * self.expansion,
                                      momentum=BN_MOMENTUM)
            self.relu = nn.ReLU(inplace=True)
            self.downsample = downsample
            self.stride = stride
    
        def forward(self, x):
            residual = x
    
            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu(out)
    
            out = self.conv2(out)
            out = self.bn2(out)
            out = self.relu(out)
    
            out = self.conv3(out)
            out = self.bn3(out)
    
            if self.downsample is not None:
                residual = self.downsample(x)
    
            out += residual
            out = self.relu(out)
    
            return out
    
    
    class PoseResNet(nn.Module):
    
        def __init__(self, block, layers, heads, head_conv, **kwargs):
            self.inplanes = 64
            self.deconv_with_bias = False
            self.heads = heads
    
            super(PoseResNet, self).__init__()
            self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                                   bias=False)
            self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
            self.relu = nn.ReLU(inplace=True)
            self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
            self.layer1 = self._make_layer(block, 64, layers[0])
            self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
            self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
            self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
    
            # used for deconv layers
            self.deconv_layers = self._make_deconv_layer(
                3,
                [256, 256, 256],
                [4, 4, 4],
            )
            # self.final_layer = []
    
            for head in sorted(self.heads):
              num_output = self.heads[head]
              if head_conv > 0:
                fc = nn.Sequential(
                    nn.Conv2d(256, head_conv,
                      kernel_size=3, padding=1, bias=True),
                    nn.ReLU(inplace=True),
                    nn.Conv2d(head_conv, num_output, 
                      kernel_size=1, stride=1, padding=0))
              else:
                fc = nn.Conv2d(
                  in_channels=256,
                  out_channels=num_output,
                  kernel_size=1,
                  stride=1,
                  padding=0
              )
              self.__setattr__(head, fc)
    
            # self.final_layer = nn.ModuleList(self.final_layer)
    
        def _make_layer(self, block, planes, blocks, stride=1):
            downsample = None
            if stride != 1 or self.inplanes != planes * block.expansion:
                downsample = nn.Sequential(
                    nn.Conv2d(self.inplanes, planes * block.expansion,
                              kernel_size=1, stride=stride, bias=False),
                    nn.BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM),
                )
    
            layers = []
            layers.append(block(self.inplanes, planes, stride, downsample))
            self.inplanes = planes * block.expansion
            for i in range(1, blocks):
                layers.append(block(self.inplanes, planes))
    
            return nn.Sequential(*layers)
    
        def _get_deconv_cfg(self, deconv_kernel, index):
            if deconv_kernel == 4:
                padding = 1
                output_padding = 0
            elif deconv_kernel == 3:
                padding = 1
                output_padding = 1
            elif deconv_kernel == 2:
                padding = 0
                output_padding = 0
    
            return deconv_kernel, padding, output_padding
    
        def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
            assert num_layers == len(num_filters), \
                'ERROR: num_deconv_layers is different len(num_deconv_filters)'
            assert num_layers == len(num_kernels), \
                'ERROR: num_deconv_layers is different len(num_deconv_filters)'
    
            layers = []
            for i in range(num_layers):
                kernel, padding, output_padding = \
                    self._get_deconv_cfg(num_kernels[i], i)
    
                planes = num_filters[i]
                layers.append(
                    nn.ConvTranspose2d(
                        in_channels=self.inplanes,
                        out_channels=planes,
                        kernel_size=kernel,
                        stride=2,
                        padding=padding,
                        output_padding=output_padding,
                        bias=self.deconv_with_bias))
                layers.append(nn.BatchNorm2d(planes, momentum=BN_MOMENTUM))
                layers.append(nn.ReLU(inplace=True))
                self.inplanes = planes
    
            return nn.Sequential(*layers)
    
        def forward(self, x):
            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)
            # print("out",x.size())
            x = self.deconv_layers(x)
            ret = {}
            for head in self.heads:
                ret[head] = self.__getattr__(head)(x)
            return [ret]
    
        def init_weights(self, num_layers, pretrained=True):
            if pretrained:
                # print('=> init resnet deconv weights from normal distribution')
                for _, m in self.deconv_layers.named_modules():
                    if isinstance(m, nn.ConvTranspose2d):
                        # print('=> init {}.weight as normal(0, 0.001)'.format(name))
                        # print('=> init {}.bias as 0'.format(name))
                        nn.init.normal_(m.weight, std=0.001)
                        if self.deconv_with_bias:
                            nn.init.constant_(m.bias, 0)
                    elif isinstance(m, nn.BatchNorm2d):
                        # print('=> init {}.weight as 1'.format(name))
                        # print('=> init {}.bias as 0'.format(name))
                        nn.init.constant_(m.weight, 1)
                        nn.init.constant_(m.bias, 0)
                # print('=> init final conv weights from normal distribution')
                for head in self.heads:
                  final_layer = self.__getattr__(head)
                  for i, m in enumerate(final_layer.modules()):
                      if isinstance(m, nn.Conv2d):
                          # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                          # print('=> init {}.weight as normal(0, 0.001)'.format(name))
                          # print('=> init {}.bias as 0'.format(name))
                          if m.weight.shape[0] == self.heads[head]:
                              if 'hm' in head:
                                  nn.init.constant_(m.bias, -2.19)
                              else:
                                  nn.init.normal_(m.weight, std=0.001)
                                  nn.init.constant_(m.bias, 0)
                #pretrained_state_dict = torch.load(pretrained)
                url = model_urls['resnet{}'.format(num_layers)]
                pretrained_state_dict = model_zoo.load_url(url)
                print('=> loading pretrained model {}'.format(url))
                self.load_state_dict(pretrained_state_dict, strict=False)
            else:
                print('=> imagenet pretrained model dose not exist')
                print('=> please download it first')
                raise ValueError('imagenet pretrained model does not exist')
    
    
    resnet_spec = {18: (BasicBlock, [2, 2, 2, 2]),
                   34: (BasicBlock, [3, 4, 6, 3]),
                   50: (Bottleneck, [3, 4, 6, 3]),
                   101: (Bottleneck, [3, 4, 23, 3]),
                   152: (Bottleneck, [3, 8, 36, 3])}
    
    
    def get_pose_net(num_layers, heads, head_conv):
      block_class, layers = resnet_spec[num_layers]
    
      model = PoseResNet(block_class, layers, heads, head_conv=head_conv)
      model.init_weights(num_layers, pretrained=True)
      return model
    
    
    if __name__ == '__main__':
        # heads = {'hm': 5, 'wh': 2, 'hps': 2}
        # model= get_pose_net(34,heads,64)
    
        model = torch.load('34.weights')
        model.cuda()
        model.eval()
        x = torch.rand(1, 3, 512, 512).cuda()
        for i in range(10):
            t1 = time.time()
            # out3, out4, out5 = model(x)
            out3 = model(x)
            # print(out3)
            # print(out3.size())
            # print(out4.size())
            # print(out5.size())
            cnt = time.time() - t1
            print(cnt)
        # torch.save(model, '34.weights')

    展开全文
  • pytorch加载模型:举例 resnet152 = torch.load(’/models/resnet152-b121ed2d.pth’) 报错: TypeError: ‘collections.OrderedDict’ object is not callable 解决: 上面pth文件包含的是 state_dict 而不是模型...
  • Pytorch加载模型时报错

    2019-12-28 16:44:26
    反复排查问题没发现为何如此,查看pytorch中文文档发现保存和加载模型方法都完全正确,模型保存和加载代码对比中文文档截图如下: 其中一个方法是在加载模型时添加参数strict=False,可以只保留键值相同的参数避免...
  • 应用pytorch加载模型报错: Traceback (most recent call last): File "/mnt/sdc/OCR/dev/ocr/recognize/crnn_recognizer.py", line 110, in __init__ self.model.load_state_dict({k.replace('module.', ''): v ...
  • 在utils.py里调用doCheakByBytes(image),该方法里有torch模型加载,报错AttributeError: Can't get attribute 'Model' on <module '__main__' from 'xxxxx/utils.py'> 解决方法:在utils.py文件里import ...
  • 加载 PyTorch 深度学习框架模型报错 RuntimeError: version_ <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED at /pytorch/caffe2/serialize/inline_container.cc:132, please report a bug to ...
  • 如上面题目所描述的,PyTorch加载之前保存的模型参数的时候,遇到了问题。 Unexpected key(s) in state_dict: "module.features. ...".,Expected ".features....". 直接原因是key值名字不对应。 表明了加载...
  • 使用pytorch加载模型的时候,通常语句写法 model = torch.load(model_file) 只有CPU时,修改为:model = torch.load(model_path, map_location='cpu') 只有一个GPU时,修改为:model = torch.load(model_path, ...
  • 在深度学习领域,经常需要使用其他人已训练好的模型进行改进或微调,这个时候我们通常会希望加载预训练模型文件的参数,如果网络结构不变,只需要使用load_state_dict方法即可。而当我们改动网络结构后,由于load_...
  • Pytorch加载模型时报错:UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc3 in position 875: ordinal not in range(128) 问题原因: Pytorch在加载模型时候,如果训练保存模型的python环境是2.7,然而...
  • Pytorch加载模型的问题及解决方法

    千次阅读 2019-12-14 21:21:59
    一般来说,保存模型是把参数全部用model.cpu().state_dict(), 然后加载模型时一般用 model.load_state_dict(torch.load(model_path))。 值得注意的是:torch.load 返回的是一个 OrderedDict. import torch import ...
  • Pytorch加载模型时报错: 此时的加载部分代码为: model = torch.nn.DataParallel(resnet.__dict__[args.arch]()) model.load_state_dict(torch.load(args.weights)) model.cuda() 修改第二行代码为: model =...
  • 在使用pytorch加载模型时(具体命令为:torch.load("xxxxx.ckpt"),报错如下: D:\Python36\python.exe model2android.py Traceback (most recent call last): File "model2android.py", line 3, in <module>...
  • 当使用torch.load加载模型参数时,会默认加载在第一块GPU0上,当GPU0没有在使用时,问题不大,但是显存被占满时这时候就加载不了了。 解决方法: model_recover = torch.load(args.model_recove
  • https://stackoverflow.com/questions/42703500/best-way-to-save-a-trained-model-in-pytorch
  • https://www.jianshu.com/p/7a7d45b8e0ee
  • PyTorch训练好模型以后,需要加载模型加载模型代码如下 ckpt = torch.load(model_path_len) model.load_state_dict(ckpt['state_dict']) 结果碰到的问题为:Missing key(s) in state_dict raise Runtime...
  • pytorch加载训练好的模型时报错: 解决方法:在加载模型的这个文件中,将模型的定义代码复制过来。 参考博客:pytorch 在加载模型时出现的错误
  • torch.save(model,“模型名字”) ...不依赖 源码代码 只用一个模型在任意环境加载的办法 torchscript c++环境中调用pytorch模型 trace 和 script trace_module = torch.jit.trace(model,torch.rand(1,1,224,224))
  • 保存模型时应是: torch.save(model.state_dict(), model_path) 如果写成: torch.save(model.state_dict, model_path) 就会报错: state_dict = state_dict.copy() AttributeError: 'function' object has...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 16,026
精华内容 6,410
关键字:

pytorch加载模型