mjq11302010044 / RRPN_pytorch

RRPN in pytorch 1.0 ----- Arbitrary-Oriented Scene Text Detection via Rotation Proposals
MIT License
262 stars 56 forks source link

VGG16 implementation try #56

Open yoyoyo-yo opened 4 years ago

yoyoyo-yo commented 4 years ago

Thank you for your great achievement!

I am trying to implement VGG16, but got below error

Traceback (most recent call last):
  File "tools/train_net.py", line 177, in <module>
    main()
  File "tools/train_net.py", line 170, in main
    model = train(cfg, args.local_rank, args.distributed)
  File "tools/train_net.py", line 78, in train
    arguments,
  File "/var/autofs/home/casa/usrs/nagayosi/TextDet/RRPN_pytorch/maskrcnn_benchmark/engine/trainer.py", line 71, in do_train
    loss_dict = model(images, targets)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/var/autofs/home/casa/usrs/nagayosi/TextDet/RRPN_pytorch/maskrcnn_benchmark/modeling/detector/generalized_rrpn_rcnn.py", line 61, in forward
    x, result, detector_losses = self.roi_heads(features, proposals, targets)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/var/autofs/home/casa/usrs/nagayosi/TextDet/RRPN_pytorch/maskrcnn_benchmark/modeling/roi_heads/rroi_heads.py", line 27, in forward
    x, detections, loss_box = self.box(features, proposals, targets)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/var/autofs/home/casa/usrs/nagayosi/TextDet/RRPN_pytorch/maskrcnn_benchmark/modeling/roi_heads/rbox_head/box_head.py", line 57, in forward
    class_logits, box_regression = self.predictor(x)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/var/autofs/home/casa/usrs/nagayosi/TextDet/RRPN_pytorch/maskrcnn_benchmark/modeling/roi_heads/rbox_head/roi_box_predictors.py", line 41, in forward
    cls_logit = self.cls_score(x)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 67, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/usrs/nagayosi/dev/anaconda3/envs/rrpn_pytorch/lib/python3.7/site-packages/torch/nn/functional.py", line 1352, in linear
    ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
RuntimeError: cublas runtime error : resource allocation failed at /pytorch/aten/src/THC/THCGeneral.cpp:250

I implemented VGG16 backbone like below based on resnet.py

 33 class VGG16(torch.nn.Module):                                                                                               
 34    def __init__(self, cfg):                                                                                                
 35        super(VGG16, self).__init__()                                                                                       
 36                                                                                                                            
 37        self.conv1 = torch.nn.Sequential(OrderedDict({                                                                      
 38            'conv1_1': torch.nn.Conv2d(3, 64, kernel_size=3, padding=1, stride=1),                                          
 39            'conv1_2': torch.nn.Conv2d(64, 64, kernel_size=3, padding=1, stride=1),                                         
 40        }))                                                                                                                 
 41            #'pool1' : torch.nn.MaxPool2d(2, stride=2, padding=1),                                                          
 42        self.conv2 = torch.nn.Sequential(OrderedDict({                                                                      
 43            'conv2_1': torch.nn.Conv2d(64, 128, kernel_size=3, padding=1, stride=1),                                        
 44            'conv2_2': torch.nn.Conv2d(128, 128, kernel_size=3, padding=1, stride=1),                                       
 45        }))                                                                                                                 
 46            #'pool2' : torch.nn.MaxPool2d(2, stride=2, padding=1),                                                          
 47        self.conv3 = torch.nn.Sequential(OrderedDict({                                                                      
 48            'conv3_1': torch.nn.Conv2d(128, 256, kernel_size=3, padding=1, stride=1),                                       
 49            'conv3_2': torch.nn.Conv2d(256, 256, kernel_size=3, padding=1, stride=1),                                       
 50            'conv3_3': torch.nn.Conv2d(256, 256, kernel_size=3, padding=1, stride=1),  
self.conv4 = torch.nn.Sequential(OrderedDict({                                                                      
 54            'conv4_1': torch.nn.Conv2d(256, 512, kernel_size=3, padding=1, stride=1),                                       
 55            'conv4_2': torch.nn.Conv2d(512, 512, kernel_size=3, padding=1, stride=1),                                       
 56            'conv4_3': torch.nn.Conv2d(512, 512, kernel_size=3, padding=1, stride=1),                                       
 57        }))                                                                                                                 
 58            #'pool4' : torch.nn.MaxPool2d(2, stride=2, padding=1),                                                          
 59        self.conv5 = torch.nn.Sequential(OrderedDict({                                                                      
 60            'conv5_1': torch.nn.Conv2d(512, 512, kernel_size=3, padding=1, stride=1),                                       
 61            'conv5_2': torch.nn.Conv2d(512, 512, kernel_size=3, padding=1, stride=1),                                       
 62            'conv5_3': torch.nn.Conv2d(512, 512, kernel_size=3, padding=1, stride=1),                                       
 63        }))                                                                                                                 
 64                                                                                                                            
 65        #self.stem = torch.nn.Sequential(stem)                                                                              
 66                                                                                                                            
 67    def forward(self, x):                                                                                                   
 68        outputs = []                                                                                                        
 69                                                                                                                            
 70        x = self.conv1(x)                                                                                                   
 71        #outputs.append(x)                                                                                                  
 72                                                                                                                            
 73        x = F.max_pool2d(x, 2, stride=2, padding=0)                                                                         
 74        x = self.conv2(x)                                                                                                   
 75        #outputs.append(x)                                                                                                  
 76                                                                                                                            
 77        x = F.max_pool2d(x, 2, stride=2, padding=0)                                                                         
 78        x = self.conv3(x)                                                                                                   
 79        #outputs.append(x)                                                                                                  
 80                                                                                                                            
 81        x = F.max_pool2d(x, 2, stride=2, padding=0)   
82        x = self.conv4(x)                                                                                                   
 83        #outputs.append(x)                                                                                                  
 84                                                                                                                            
 85        x = F.max_pool2d(x, 2, stride=2, padding=0)                                                                         
 86        x = self.conv5(x)                                                                                                   
 87        outputs.append(x)                                                                                                   
 88                                                                                                                            
 89        return outputs                                                                                                      
 90                                                                                                                            
 91class VGG16Head(torch.nn.Module):                                                                                           
 92    def __init__(                                                                                                           
 93        self,                                                                                                               
 94        block_module,                                                                                                       
 95        stages=None,                                                                                                        
 96        num_groups=1,                                                                                                       
 97        width_per_group=64,                                                                                                 
 98        stride_in_1x1=True,                                                                                                 
 99        stride_init=None,                                                                                                   
100        res2_out_channels=256,                                                                                              
101        dilation=1                                                                                                          
102    ):                                                                                                                      
103        super(VGG16Head, self).__init__()                                                                                   
104                                                                                                                            
105        #stage2_relative_factor = 2 ** 0#2 ** (stages[0].index - 1)                                                         
106        #stage2_bottleneck_channels = num_groups * width_per_group                                                          
107        #out_channels = res2_out_channels * stage2_relative_factor                                                          
108        out_channels = 1024          
109        #in_channels = out_channels // 2                                                                                    
110        in_channels = 512                                                                                                   
111        bottleneck_channels = 256#stage2_bottleneck_channels * stage2_relative_factor                                       
112                                                                                                                            
113        print('---')                                                                                                        
114        print('VGG16Head config')                                                                                           
115        print('---')                                                                                                        
116        print('- out_channels :', out_channels)                                                                             
117        print('- in_channels :', in_channels)                                                                               
118        print('- bottleneck_channels :', bottleneck_channels)                                                               
119        print()                                                                                                             
120                                                                                                                            
121        block_module = _TRANSFORMATION_MODULES[block_module]                                                                
122                                                                                                                            
123        self.stages = []                                                                                                    
124        stride = stride_init                                                                                                
125                                                                                                                            
126        """                                                                                                                 
127        for stage in stages:                                                                                                
128            name = "layer" + str(stage.index)                                                                               
129            if not stride:                                                                                                  
130                stride = int(stage.index > 1) + 1                                                                           
131            module = _make_stage(                                                                                           
132                block_module,                                                                                               
133                in_channels,                                                                                                
134                bottleneck_channels,                                                                                        
135                out_channels,                                                                                               
136                stage.block_count,                                                                                          
137                num_groups,                                                                                                 
138                stride_in_1x1,                                                                                              
139                first_stride=stride,                                                                                        
140                dilation=dilation                                                                                           
141            )                                                                                                               
142            stride = None                                                                                                   
143            self.add_module(name, module)                                                                                   
144            self.stages.append(name)                                                                                        
145        """                                                                                                                 
146                                                                                                                            
147        name = 'layer0'                                                                                                     
148                                                                                                                            
149        if not stride:                                                                                                      
150            stride = 2
152        module = _make_stage(                                                                                               
153            block_module,                                                                                                   
154            in_channels,                                                                                                    
155            bottleneck_channels,                                                                                            
156            out_channels,                                                                                                   
157            3,                                                                                                              
158            num_groups,                                                                                                     
159            stride_in_1x1,                                                                                                  
160            first_stride=stride,                                                                                            
161            dilation=dilation                                                                                               
162        )                                                                                                                   
163        self.add_module(name, module)                                                                                       
164        self.stages.append(name)                                                                                            
165                                                                                                                            
166                                                                                                                            
167    def forward(self, x):                                                                                                   
168        print('VGG16Head In', x.size())                                                                                     
169        for stage in self.stages:                                                                                           
170            x = getattr(self, stage)(x)                                                                                     
171        print('VGG16Head Out', x.size())                                                                                    
172        return x

I changed maskrcnn_benchmark/modling/roi_heads/rbox_head/roi_box_predictors.py to below.

  5 class FastRCNNPredictor(nn.Module):
  6     def __init__(self, config, pretrained=None):
  7         super(FastRCNNPredictor, self).__init__()
  8 
  9         if 'R-' in config.MODEL.BACKBONE.CONV_BODY:
 10             stage_index = 4
 11             stage2_relative_factor = 2 ** (stage_index - 1)
 12             res2_out_channels = config.MODEL.RESNETS.RES2_OUT_CHANNELS
 13             num_inputs = res2_out_channels * stage2_relative_factor
 14             #self.avgpool = nn.AvgPool2d(kernel_size=7, stride=7)
 15             print('FastRCNNPredictor ResNet')
 16         elif 'VGG16' == config.MODEL.BACKBONE.CONV_BODY:
 17             num_inputs = 1024 #512
 18             #self.avgpool = nn.AvgPool2d(kernel_size=14, stride=14)
 19             print('FastRCNNPredictor VGG16')
 20 
 21 
 22         num_classes = config.MODEL.ROI_BOX_HEAD.NUM_CLASSES
 23         self.avgpool = nn.AvgPool2d(kernel_size=7, stride=7)
 24         self.cls_score = nn.Linear(num_inputs, num_classes)
 25         self.bbox_pred = nn.Linear(num_inputs, num_classes * 5)
 26 
 27 
 28         nn.init.normal_(self.cls_score.weight, mean=0, std=0.01)
 29         nn.init.constant_(self.cls_score.bias, 0)
 30 
 31         nn.init.normal_(self.bbox_pred.weight, mean=0, std=0.001)
 32         nn.init.constant_(self.bbox_pred.bias, 0)
 33 
 34 
 35     def forward(self, x):
 36         print('FastRCNNPredictor AVGbefore', x.size())
 37         x = self.avgpool(x)
 38         x = x.view(x.size(0), -1)
 39         print('FastRCNNPredictor AVGPool after', x.size())
 40 
 41         cls_logit = self.cls_score(x)
 42         bbox_pred = self.bbox_pred(x)
 43 
 44         return cls_logit, bbox_pred

my error is occured in line 41, but I have no idea Please help!!

yoyoyo-yo commented 4 years ago

Sorry,,,, I solved this issue. I added new code to modeling/roi_heads/rbox_head/roi_box_feature_extractors.py for vgg implementation, and that code was wrong...