faster-rcnn vgg16

载入模型时出错
TypeError: argument of type ‘NoneType‘ is not iterable

img

  • 这篇博客: Simple-fasterrcnn源码学习笔记(3)中的 faster_rcnn_vgg16.py 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • from __future__ import absolute_import
    import torch as t
    from torch import nn
    from torchvision.models import vgg16
    from model.region_proposal_network import RegionProposalNetwork
    from model.faster_rcnn import FasterRCNN
    from model.roi_module import RoIPooling2D
    from utils import array_tool as at
    from utils._config import opt
    
    def decom_vgg16():
        if opt.caffe_pretrain:
            model=vgg16(pretrained=False)
            if not opt.load_path:
                model.load_state_dict(t.load(opt.caffe_pretrain_path))
        else:
            model=vgg16(not opt.load_path)
        features=list(model.features)[:30]#去掉最后最大池化
        classifier=model.classifier #Linear Relu Dropout
        classifier=list(classifier)
        del classifier[6]
    
        #冻结前4个卷基层的参数 加快训练速度 不需要秋梯度 不用bp过程
        for layer in features[:10]:
            for p in layer.parameters():
                p.requires_grad=False
    
        return nn.Sequential(*features),classifier
    
    
    class FasterRCNNVGG16(FasterRCNN):
        feat_stride=16#VGG对5个stage下采样16倍
        def __init__(self,n_fg_class=20,ratios=[0.5,1,2],anchor_scales=[8,16,32]):
            extractor,classifier=decom_vgg16()
            rpn=RegionProposalNetwork(512,512,ratios=ratios,anchor_scales=anchor_scales,
                                      feat_stride=self.feat_stride)
            head=VGG16RoIHead(n_class=n_fg_class+1,roi_size=7,spatial_scale=(1./self.feat_stride),
                              classifier=classifier)
            super(FasterRCNNVGG16, self).__init__(extractor,rpn,head)
    
    class VGG16RoIHead(nn.Module):
        def __init__(self,n_class,roi_size,spatial_scale,classifier):
            super(VGG16RoIHead, self).__init__()
            self.classifier=classifier
            self.cls_loc=nn.Linear(4096,n_class*4)
            self.score=nn.Linear(4096,n_class)
    
            normal_init(self.cls_loc,0,0.001)
            normal_init(self.score,0,0.001)
    
            self.n_class=n_class
            self.roi_size=roi_size
            self.spatial_scale=spatial_scale
            self.roi=RoIPooling2D(self.roi_size,self.roi_size,self.spatial_scale)
        def forward(self, x,rois,roi_indices):
            roi_indices=at.totensor(roi_indices).float() #shape(128,)
            rois=at.totensor(rois).float()  #shape(128,4)
            indices_and_rois=t.cat([roi_indices[:,None],rois],dim=1)#shape (128,5)
            xy_indices_and_rois=indices_and_rois[:,[0,2,1,4,3]] #index ymin xmin ymax xmax TO
            # index xmin ymin xmax ymax
            indices_and_rois=xy_indices_and_rois.contiguous()#调整维度之后要返回连续的内存
    
            pool=self.roi(x,indices_and_rois)#roi_pooling return (128,512,7,7)
            pool=pool.view(pool.size(0),-1)#(128,25088) 正好和vgg16的全链接层相连接
            fc7=self.classifier(pool)
            roi_cls_locs=self.cls_loc(fc7)
            roi_scores=self.score(fc7)
            return roi_cls_locs,roi_scores
    
    def normal_init(m,mean,stddev,truncated=False):#正态分布初始化参数
        if truncated:#截断高斯
            m.weight.data.normal_().fmod_(2).mul_(stddev).add_(mean)#fmod_计算除法的余数
        else:
            m.weight.data.normal_(mean,stddev)
            m.bias.data.zero_()
    

引用chatGPT作答,这个错误信息 "TypeError: argument of type 'NoneType' is not iterable" 通常出现在将 None 或 null 传递给期望可迭代对象的操作时。

在使用 Faster RCNN 和 VGG16 时,这个错误信息可能指示输入数据或网络配置存在问题。

以下是可能导致错误的原因:

1.输入数据:检查输入数据的格式是否正确,并且是否包含网络正常工作所需的必要信息。确保图像以正确的格式存在,并且注释与图像对齐。

2.网络配置:检查网络配置是否正确设置,并且所有必要的变量和参数是否正确定义。确保传递的变量不为 None 或 null。

3.安装和依赖:检查所有必要的依赖项和软件包是否已正确安装并且为最新版本。确保已安装正确版本的 TensorFlow,以及任何其他必需的库或软件包。

4.没有更具体的信息,我们无法提供更具体的指导。然而,您可以从检查代码中开始,并检查输入数据、网络配置或依赖项安装中可能存在的任何潜在问题。

以下回答参考GPT,并由JoseKe整理完成,希望您能采纳:
根据您提供的信息来看,这是一个代码错误。可能的原因是输入的参数是空的,在代码中无法迭代。

针对具体的错误信息的分析还需要查看代码,能否提供更多上下文的信息,比如代码的哪一行报错了,报错的上下文是什么等等。这样我才能更准确地给您答复。