gen_yolov2.py 11.1 KB
Newer Older
M
Macrobull 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 22 11:19:45 2019

@author: Macrobull
"""

from __future__ import print_function

import torch
import torch.nn as nn
import torch.nn.functional as F

from onnx2fluid.torch_export_helper import export_onnx_with_validation


# from https://github.com/santoshgsk/yolov2-pytorch/blob/master/yolotorch.ipynb
class Yolov2(nn.Module):
    def __init__(self):
        super(Yolov2, self).__init__()

M
bugfix  
Macrobull 已提交
23 24 25 26 27 28
        self.conv1 = nn.Conv2d(in_channels=3,
                               out_channels=32,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
29 30
        self.batchnorm1 = nn.BatchNorm2d(32)

M
bugfix  
Macrobull 已提交
31 32 33 34 35 36
        self.conv2 = nn.Conv2d(in_channels=32,
                               out_channels=64,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
37 38
        self.batchnorm2 = nn.BatchNorm2d(64)

M
bugfix  
Macrobull 已提交
39 40 41 42 43 44
        self.conv3 = nn.Conv2d(in_channels=64,
                               out_channels=128,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
45
        self.batchnorm3 = nn.BatchNorm2d(128)
M
bugfix  
Macrobull 已提交
46 47 48 49 50 51
        self.conv4 = nn.Conv2d(in_channels=128,
                               out_channels=64,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
M
Macrobull 已提交
52
        self.batchnorm4 = nn.BatchNorm2d(64)
M
bugfix  
Macrobull 已提交
53 54 55 56 57 58
        self.conv5 = nn.Conv2d(in_channels=64,
                               out_channels=128,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
59 60
        self.batchnorm5 = nn.BatchNorm2d(128)

M
bugfix  
Macrobull 已提交
61 62 63 64 65 66
        self.conv6 = nn.Conv2d(in_channels=128,
                               out_channels=256,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
67
        self.batchnorm6 = nn.BatchNorm2d(256)
M
bugfix  
Macrobull 已提交
68 69 70 71 72 73
        self.conv7 = nn.Conv2d(in_channels=256,
                               out_channels=128,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
M
Macrobull 已提交
74
        self.batchnorm7 = nn.BatchNorm2d(128)
M
bugfix  
Macrobull 已提交
75 76 77 78 79 80
        self.conv8 = nn.Conv2d(in_channels=128,
                               out_channels=256,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
81 82
        self.batchnorm8 = nn.BatchNorm2d(256)

M
bugfix  
Macrobull 已提交
83 84 85 86 87 88
        self.conv9 = nn.Conv2d(in_channels=256,
                               out_channels=512,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
M
Macrobull 已提交
89
        self.batchnorm9 = nn.BatchNorm2d(512)
M
bugfix  
Macrobull 已提交
90 91 92 93 94 95
        self.conv10 = nn.Conv2d(in_channels=512,
                                out_channels=256,
                                kernel_size=1,
                                stride=1,
                                padding=0,
                                bias=False)
M
Macrobull 已提交
96
        self.batchnorm10 = nn.BatchNorm2d(256)
M
bugfix  
Macrobull 已提交
97 98 99 100 101 102
        self.conv11 = nn.Conv2d(in_channels=256,
                                out_channels=512,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
103
        self.batchnorm11 = nn.BatchNorm2d(512)
M
bugfix  
Macrobull 已提交
104 105 106 107 108 109
        self.conv12 = nn.Conv2d(in_channels=512,
                                out_channels=256,
                                kernel_size=1,
                                stride=1,
                                padding=0,
                                bias=False)
M
Macrobull 已提交
110
        self.batchnorm12 = nn.BatchNorm2d(256)
M
bugfix  
Macrobull 已提交
111 112 113 114 115 116
        self.conv13 = nn.Conv2d(in_channels=256,
                                out_channels=512,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
117 118
        self.batchnorm13 = nn.BatchNorm2d(512)

M
bugfix  
Macrobull 已提交
119 120 121 122 123 124
        self.conv14 = nn.Conv2d(in_channels=512,
                                out_channels=1024,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
125
        self.batchnorm14 = nn.BatchNorm2d(1024)
M
bugfix  
Macrobull 已提交
126 127 128 129 130 131
        self.conv15 = nn.Conv2d(in_channels=1024,
                                out_channels=512,
                                kernel_size=1,
                                stride=1,
                                padding=0,
                                bias=False)
M
Macrobull 已提交
132
        self.batchnorm15 = nn.BatchNorm2d(512)
M
bugfix  
Macrobull 已提交
133 134 135 136 137 138
        self.conv16 = nn.Conv2d(in_channels=512,
                                out_channels=1024,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
139
        self.batchnorm16 = nn.BatchNorm2d(1024)
M
bugfix  
Macrobull 已提交
140 141 142 143 144 145
        self.conv17 = nn.Conv2d(in_channels=1024,
                                out_channels=512,
                                kernel_size=1,
                                stride=1,
                                padding=0,
                                bias=False)
M
Macrobull 已提交
146
        self.batchnorm17 = nn.BatchNorm2d(512)
M
bugfix  
Macrobull 已提交
147 148 149 150 151 152
        self.conv18 = nn.Conv2d(in_channels=512,
                                out_channels=1024,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
153 154
        self.batchnorm18 = nn.BatchNorm2d(1024)

M
bugfix  
Macrobull 已提交
155 156 157 158 159 160
        self.conv19 = nn.Conv2d(in_channels=1024,
                                out_channels=1024,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
161
        self.batchnorm19 = nn.BatchNorm2d(1024)
M
bugfix  
Macrobull 已提交
162 163 164 165 166 167
        self.conv20 = nn.Conv2d(in_channels=1024,
                                out_channels=1024,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
168 169
        self.batchnorm20 = nn.BatchNorm2d(1024)

M
bugfix  
Macrobull 已提交
170 171 172 173 174 175
        self.conv21 = nn.Conv2d(in_channels=3072,
                                out_channels=1024,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                bias=False)
M
Macrobull 已提交
176 177
        self.batchnorm21 = nn.BatchNorm2d(1024)

M
bugfix  
Macrobull 已提交
178 179 180 181 182
        self.conv22 = nn.Conv2d(in_channels=1024,
                                out_channels=125,
                                kernel_size=1,
                                stride=1,
                                padding=0)
M
Macrobull 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    def reorg_layer(self, x):
        stride = 2
        if hasattr(self, 'batch_size'):
            batch_size, channels, height, width = self.batch_size, self.channels, self.height, self.width
            new_ht = self.new_ht
            new_wd = self.new_wd
            new_channels = self.new_channels
        else:
            batch_size, channels, height, width = self.batch_size, self.channels, self.height, self.width = x.size(
            )
            new_ht = self.new_ht = height // stride
            new_wd = self.new_wd = width // stride
            new_channels = self.new_channels = channels * stride * stride

        passthrough = x.permute(0, 2, 3, 1)
        passthrough = passthrough.contiguous().view(-1, new_ht, stride, new_wd,
                                                    stride, channels)
        passthrough = passthrough.permute(0, 1, 3, 2, 4, 5)
        passthrough = passthrough.contiguous().view(-1, new_ht, new_wd,
                                                    new_channels)
        passthrough = passthrough.permute(0, 3, 1, 2)
        return passthrough

    def forward(self, x):
M
bugfix  
Macrobull 已提交
208 209 210 211 212 213 214 215
        out = F.max_pool2d(F.leaky_relu(self.batchnorm1(self.conv1(x)),
                                        negative_slope=0.1),
                           2,
                           stride=2)
        out = F.max_pool2d(F.leaky_relu(self.batchnorm2(self.conv2(out)),
                                        negative_slope=0.1),
                           2,
                           stride=2)
M
Macrobull 已提交
216 217 218 219 220 221 222 223 224 225 226 227

        out = F.leaky_relu(self.batchnorm3(self.conv3(out)), negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm4(self.conv4(out)), negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm5(self.conv5(out)), negative_slope=0.1)
        out = F.max_pool2d(out, 2, stride=2)

        out = F.leaky_relu(self.batchnorm6(self.conv6(out)), negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm7(self.conv7(out)), negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm8(self.conv8(out)), negative_slope=0.1)
        out = F.max_pool2d(out, 2, stride=2)

        out = F.leaky_relu(self.batchnorm9(self.conv9(out)), negative_slope=0.1)
M
bugfix  
Macrobull 已提交
228 229 230 231 232 233 234 235
        out = F.leaky_relu(self.batchnorm10(self.conv10(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm11(self.conv11(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm12(self.conv12(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm13(self.conv13(out)),
                           negative_slope=0.1)
M
Macrobull 已提交
236 237 238
        passthrough = self.reorg_layer(out)
        out = F.max_pool2d(out, 2, stride=2)

M
bugfix  
Macrobull 已提交
239 240 241 242 243 244 245 246 247 248
        out = F.leaky_relu(self.batchnorm14(self.conv14(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm15(self.conv15(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm16(self.conv16(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm17(self.conv17(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm18(self.conv18(out)),
                           negative_slope=0.1)
M
Macrobull 已提交
249

M
bugfix  
Macrobull 已提交
250 251 252 253
        out = F.leaky_relu(self.batchnorm19(self.conv19(out)),
                           negative_slope=0.1)
        out = F.leaky_relu(self.batchnorm20(self.conv20(out)),
                           negative_slope=0.1)
M
Macrobull 已提交
254 255

        out = torch.cat([passthrough, out], 1)
M
bugfix  
Macrobull 已提交
256 257
        out = F.leaky_relu(self.batchnorm21(self.conv21(out)),
                           negative_slope=0.1)
M
Macrobull 已提交
258 259 260 261 262 263 264 265 266
        out = self.conv22(out)

        return out


model = Yolov2()
model.eval()
xb = torch.rand((1, 3, 224, 224))
yp = model(xb)
M
bugfix  
Macrobull 已提交
267 268 269 270
export_onnx_with_validation(model, [xb],
                            'sample_yolov2', ['image'], ['pred'],
                            verbose=True,
                            training=False)