gen_some_samples.py 6.8 KB
Newer Older
M
Macrobull 已提交
1 2 3 4 5 6 7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 22 11:19:45 2019

@author: Macrobull

M
fix doc  
Macrobull 已提交
8 9
Not all ops in this file are supported by both PyTorch and ONNX
This only demostrates the conversion/validation workflow from PyTorch to ONNX to Paddle fluid
M
Macrobull 已提交
10 11 12 13 14 15 16 17
"""

from __future__ import print_function

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

M
Macrobull 已提交
18
from onnx2fluid.torch_export_helper import export_onnx_with_validation
M
Macrobull 已提交
19

M
Macrobull 已提交
20
prefix = 'sample_'
M
Macrobull 已提交
21 22
idx = 0

M
Macrobull 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
######## example: RNN cell ########


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.gru = nn.GRUCell(6, 5)
        self.lstm = nn.LSTMCell(5, 4)

    def forward(self, x, h1, h2, c2):
        h = self.gru(x, h1)
        h, c = self.lstm(h, (h2, c2))
        return h, c


model = Model()
model.eval()
xb = torch.rand((7, 6))
h1 = torch.zeros((7, 5))
h2 = torch.zeros((7, 4))
c2 = torch.zeros((7, 4))
yp = model(xb, h1, h2, c2)
idx += 1
print('index: ', idx)
export_onnx_with_validation(model, [xb, h1, h2, c2],
                            prefix + str(idx), ['x', 'h1', 'h2', 'c2'],
                            ['h', 'c'],
                            verbose=True,
                            training=False)

M
Macrobull 已提交
53
######## example: RNN ########
M
Macrobull 已提交
54

M
Macrobull 已提交
55 56 57 58

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
M
Macrobull 已提交
59 60
        self.gru = nn.GRU(6, 5, 3)
        self.lstm = nn.LSTM(5, 4, 2)
M
Macrobull 已提交
61

M
Macrobull 已提交
62 63 64
    def forward(self, x, h1, h2, c2):
        y, h1 = self.gru(x, h1)
        y, (h2, c2) = self.lstm(y, (h2, c2))
M
Macrobull 已提交
65 66 67 68 69
        return y


model = Model()
model.eval()
M
Macrobull 已提交
70 71 72 73 74
xb = torch.rand((8, 1, 6))
h1 = torch.zeros((3, 1, 5))
h2 = torch.zeros((2, 1, 4))
c2 = torch.zeros((2, 1, 4))
yp = model(xb, h1, h2, c2)
M
Macrobull 已提交
75 76
idx += 1
print('index: ', idx)
M
Macrobull 已提交
77 78
export_onnx_with_validation(model, [xb, h1, h2, c2],
                            prefix + str(idx), ['x', 'h1', 'h2', 'c2'], ['y'],
M
Macrobull 已提交
79 80 81 82
                            verbose=True,
                            training=False)

######## example: random ########
M
Macrobull 已提交
83 84 85 86 87 88 89 90
"""
    symbolic registration:

    def rand(g, *shapes):
        shapes_list = list(shapes)
        shape = _maybe_get_const(shapes_list[0], "is")
        return g.op('RandomUniform', shape_i=shape)
"""
M
Macrobull 已提交
91 92 93 94 95 96 97


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

    def forward(self, x):
M
Macrobull 已提交
98 99 100
        y = torch.rand((2, 3))  # + torch.rand_like(x)
        y = y + torch.randn((2, 3))  # + torch.randn_like(x)
        y = y + x
M
Macrobull 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
        return y


model = Model()
model.eval()
xb = torch.rand((2, 3))
yp = model(xb)
idx += 1
print('index: ', idx)
export_onnx_with_validation(model, [xb],
                            prefix + str(idx), ['x'], ['y'],
                            verbose=True,
                            training=False)
M
Macrobull 已提交
114 115 116

######## example: fc ########

M
Macrobull 已提交
117

M
Macrobull 已提交
118 119 120 121 122 123 124 125 126 127 128 129
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.fc = nn.Linear(3, 8)

    def forward(self, x):
        y = x
        y = self.fc(y)
        return y


model = Model()
130
model.eval()
M
Macrobull 已提交
131 132 133 134
xb = torch.rand((2, 3))
yp = model(xb)
idx += 1
print('index: ', idx)
M
bugfix  
Macrobull 已提交
135 136 137 138
export_onnx_with_validation(model, [xb],
                            prefix + str(idx), ['x'], ['y'],
                            verbose=True,
                            training=False)
M
Macrobull 已提交
139 140 141

######## example: compare ########

M
Macrobull 已提交
142

M
Macrobull 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

    def forward(self, x0, x1):
        x0 = x0.clamp(-1, 1)
        a = torch.max(x0, x1) == x1
        b = x0 < x1
        c = x0 > x1
        return a, b, c


model = Model()
156
model.eval()
M
Macrobull 已提交
157 158 159 160 161
xb0 = torch.rand((2, 3))
xb1 = torch.rand((2, 3))
ya, yb, yc = model(xb0, xb1)
idx += 1
print('index: ', idx)
M
bugfix  
Macrobull 已提交
162 163 164 165
export_onnx_with_validation(model, [xb0, xb1],
                            prefix + str(idx), ['x0', 'x1'], ['ya', 'yb', 'yc'],
                            verbose=True,
                            training=False)
M
Macrobull 已提交
166 167

######## example: affine_grid ########
M
Macrobull 已提交
168 169 170 171 172 173 174
"""
    symbolic registration:

    @parse_args('v', 'is')
    def affine_grid_generator(g, theta, size):
        return g.op('AffineGrid', theta, size_i=size)
"""
M
Macrobull 已提交
175

M
Macrobull 已提交
176

M
Macrobull 已提交
177 178 179 180 181 182 183 184 185 186
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

    def forward(self, theta):
        grid = F.affine_grid(theta, (2, 2, 8, 8))
        return grid


model = Model()
187
model.eval()
M
Macrobull 已提交
188 189 190 191
theta = torch.rand((2, 2, 3))
grid = model(theta)
idx += 1
print('index: ', idx)
M
bugfix  
Macrobull 已提交
192 193 194 195
export_onnx_with_validation(model, (theta, ),
                            prefix + str(idx), ['theta'], ['grid'],
                            verbose=True,
                            training=False)
M
Macrobull 已提交
196 197 198

######## example: conv2d_transpose ########

M
Macrobull 已提交
199

M
Macrobull 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv = nn.ConvTranspose2d(3, 8, 3)
        self.dropout = nn.Dropout2d()

    def forward(self, x):
        y = x
        y = self.conv(y)
        y = self.dropout(y)
        return y


model = Model()
214
model.eval()
M
Macrobull 已提交
215 216 217 218
xb = torch.rand((2, 3, 4, 5))
yp = model(xb)
idx += 1
print('index: ', idx)
M
bugfix  
Macrobull 已提交
219 220 221 222
export_onnx_with_validation(model, [xb],
                            prefix + str(idx), ['x'], ['y'],
                            verbose=True,
                            training=False)
M
Macrobull 已提交
223 224 225

######## example: conv2d ########

M
Macrobull 已提交
226

M
Macrobull 已提交
227 228 229 230 231
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv = nn.Conv2d(3, 8, 3)
        self.batch_norm = nn.BatchNorm2d(8)
M
Macrobull 已提交
232
        self.pool = nn.AdaptiveAvgPool2d(1)
M
Macrobull 已提交
233 234 235 236 237 238 239 240 241 242

    def forward(self, x):
        y = x
        y = self.conv(y)
        y = self.batch_norm(y)
        y = self.pool(y)
        return y


model = Model()
243
model.eval()
M
Macrobull 已提交
244 245 246 247
xb = torch.rand((2, 3, 4, 5))
yp = model(xb)
idx += 1
print('index: ', idx)
M
bugfix  
Macrobull 已提交
248 249 250 251
export_onnx_with_validation(model, [xb],
                            prefix + str(idx), ['x'], ['y'],
                            verbose=True,
                            training=False)
M
Macrobull 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

######### example: conv1d ########
#
#class Model(nn.Module):
#    def __init__(self):
#        super(Model, self).__init__()
#        self.batch_norm = nn.BatchNorm2d(3)
#
#    def forward(self, x):
#        y = x
#        y = self.batch_norm(y)
#        return y
#
#
#model = Model()
267
#model.eval()
M
Macrobull 已提交
268 269 270 271
#xb = torch.rand((2, 3, 4, 5))
#yp = model(xb)
#idx += 1
#print('index: ', idx)
M
Macrobull 已提交
272 273 274 275
#export_onnx_with_validation(
#        model, [xb], prefix + str(idx),
#        ['x'], ['y'],
#        verbose=True, training=False)
M
Macrobull 已提交
276 277 278

######## example: empty ########

M
Macrobull 已提交
279

M
Macrobull 已提交
280 281 282 283 284 285 286 287 288
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

    def forward(self, x):
        return x


model = Model()
289
model.eval()
M
Macrobull 已提交
290 291 292 293
xb = torch.rand((2, 3))
yp = model(xb)
idx += 1
print('index: ', idx)
M
bugfix  
Macrobull 已提交
294 295 296 297
export_onnx_with_validation(model, [xb],
                            prefix + str(idx), ['y'], ['y'],
                            verbose=True,
                            training=False)