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

@author: Macrobull

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

"""

from __future__ import print_function

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

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

M
Macrobull 已提交
21
prefix = 'sample_'
M
Macrobull 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
idx = 0

######### example: RNN ########
#
#class Model(nn.Module):
#    def __init__(self):
#        super(Model, self).__init__()
#        self.rnn = nn.RNN(4, 6, 2)
#
#    def forward(self, x):
#        y = x
#        y, h = self.rnn(y)
#        return y
#
#
#model = Model()
38
#model.eval()
M
Macrobull 已提交
39 40 41 42
#xb = torch.rand((2, 3, 4))
#yp = model(xb)
#idx += 1
#print('index: ', idx)
M
Macrobull 已提交
43
#export_onnx_with_validation(model, (xb, ), prefix + str(idx),
M
Macrobull 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#                            ['x'], ['y'],
#                            verbose=True, training=False)

######### example: random ########
#
#class Model(nn.Module):
#    def __init__(self):
#        super(Model, self).__init__()
#
#    def forward(self, x):
#        y = torch.rand((2, 3)) # + torch.rand_like(xb)
#        y = y + torch.randn((2, 3)) # + torch.randn_like(xb)
#        return y
#
#
#model = Model()
60
#model.eval()
M
Macrobull 已提交
61 62 63 64
#xb = torch.rand((2, 3))
#yp = model(xb)
#idx += 1
#print('index: ', idx)
M
Macrobull 已提交
65
#export_onnx_with_validation(model, (xb, ), prefix + str(idx),
M
Macrobull 已提交
66 67 68 69 70
#                            ['x'], ['y'],
#                            verbose=True, training=False)

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

M
Macrobull 已提交
71

M
Macrobull 已提交
72 73 74 75 76 77 78 79 80 81 82 83
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()
84
model.eval()
M
Macrobull 已提交
85 86 87 88
xb = torch.rand((2, 3))
yp = model(xb)
idx += 1
print('index: ', idx)
M
Macrobull 已提交
89
export_onnx_with_validation(
M
Macrobull 已提交
90 91 92 93
    model, (xb, ),
    prefix + str(idx), ['x'], ['y'],
    verbose=True,
    training=False)
M
Macrobull 已提交
94 95 96

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

M
Macrobull 已提交
97

M
Macrobull 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
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()
111
model.eval()
M
Macrobull 已提交
112 113 114 115 116
xb0 = torch.rand((2, 3))
xb1 = torch.rand((2, 3))
ya, yb, yc = model(xb0, xb1)
idx += 1
print('index: ', idx)
M
Macrobull 已提交
117 118
export_onnx_with_validation(
    model, (xb0, xb1),
M
Macrobull 已提交
119
    prefix + str(idx), ['x0', 'x1'], ['ya', 'yb', 'yc'],
M
Macrobull 已提交
120 121
    verbose=True,
    training=False)
M
Macrobull 已提交
122 123 124

######## example: affine_grid ########

M
Macrobull 已提交
125

M
Macrobull 已提交
126 127 128 129 130 131 132 133 134 135
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()
136
model.eval()
M
Macrobull 已提交
137 138 139 140
theta = torch.rand((2, 2, 3))
grid = model(theta)
idx += 1
print('index: ', idx)
M
Macrobull 已提交
141 142
export_onnx_with_validation(
    model, (theta, ),
M
Macrobull 已提交
143
    prefix + str(idx), ['theta'], ['grid'],
M
Macrobull 已提交
144 145
    verbose=True,
    training=False)
M
Macrobull 已提交
146 147 148

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

M
Macrobull 已提交
149

M
Macrobull 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
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()
164
model.eval()
M
Macrobull 已提交
165 166 167 168
xb = torch.rand((2, 3, 4, 5))
yp = model(xb)
idx += 1
print('index: ', idx)
M
Macrobull 已提交
169
export_onnx_with_validation(
M
Macrobull 已提交
170 171 172 173
    model, (xb, ),
    prefix + str(idx), ['x'], ['y'],
    verbose=True,
    training=False)
M
Macrobull 已提交
174 175 176

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

M
Macrobull 已提交
177

M
Macrobull 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv = nn.Conv2d(3, 8, 3)
        self.batch_norm = nn.BatchNorm2d(8)
        self.pool = nn.AdaptiveAvgPool2d(2)

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


model = Model()
194
model.eval()
M
Macrobull 已提交
195 196 197 198
xb = torch.rand((2, 3, 4, 5))
yp = model(xb)
idx += 1
print('index: ', idx)
M
Macrobull 已提交
199
export_onnx_with_validation(
M
Macrobull 已提交
200 201 202 203
    model, (xb, ),
    prefix + str(idx), ['x'], ['y'],
    verbose=True,
    training=False)
M
Macrobull 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

######### 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()
219
#model.eval()
M
Macrobull 已提交
220 221 222 223
#xb = torch.rand((2, 3, 4, 5))
#yp = model(xb)
#idx += 1
#print('index: ', idx)
M
Macrobull 已提交
224
#export_onnx_with_validation(model, (xb, ), prefix + str(idx),
M
Macrobull 已提交
225 226 227 228 229
#                            ['x'], ['y'],
#                            verbose=True, training=False)

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

M
Macrobull 已提交
230

M
Macrobull 已提交
231 232 233 234 235 236 237 238 239
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

    def forward(self, x):
        return x


model = Model()
240
model.eval()
M
Macrobull 已提交
241 242 243 244
xb = torch.rand((2, 3))
yp = model(xb)
idx += 1
print('index: ', idx)
M
Macrobull 已提交
245
export_onnx_with_validation(
M
Macrobull 已提交
246 247 248 249
    model, (xb, ),
    prefix + str(idx), ['y'], ['y'],
    verbose=True,
    training=False)