├── LICENSE
├── README.md
├── setup.py
└── xml2pytorch
├── __init__.py
├── converter.py
└── xml_examples
└── simpleCNN.xml
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 yfzhoucs
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # xml2pytorch
2 | Using xml to define pytorch neural networks
3 | ## What can it Do
4 | With xml2pytorch, you can easily define neural networks in xml, and then declare them in pytorch.
5 |
6 | RNN and LSTM are not supported currently.
7 | ## Installation
8 | ### Environment
9 | OS independent. Python3. (Not tested on Python2, but it should work.)
10 | ### Install requirements
11 | torch>=0.4.1
12 | numpy>=1.15.1
13 | ### Installing by pip3
14 | pip3 install xml2pytorch
15 | ## Quick Start
16 | ### How to declare the CNN defined by a xml file
17 | ```
18 | import torch
19 | import xml2pytorch as xm
20 |
21 | # declare the net defined in .xml
22 | net = xm.convertXML(xml_filename)
23 |
24 | # input a random tensor
25 | x = torch.randn(1, 3, 32, 32)
26 | y = net(x)
27 | print(y)
28 | ```
29 | ### How to define a simple CNN in xml
30 | ```
31 |
32 |
33 |
34 | Conv2d
35 | 3
36 | 6
37 | 5
38 |
39 |
40 | ELU
41 |
42 |
43 | MaxPool2d
44 | 2
45 | 2
46 | logsigmoid
47 |
48 |
49 | Conv2d
50 | 6
51 | 16
52 | 5
53 | relu
54 |
55 |
56 | MaxPool2d
57 | 2
58 | 2
59 | relu
60 |
61 |
62 | reshape
63 | [-1, 16*5*5]
64 |
65 |
66 | Linear
67 | 400
68 | 120
69 | tanh
70 |
71 |
72 | Linear
73 | 120
74 | 84
75 | sigmoid
76 |
77 |
78 | Linear
79 | 84
80 | 10
81 | softmax
82 |
83 |
84 |
85 | ```
86 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | import setuptools
2 |
3 | with open("./README.md", "r") as fh:
4 | long_description = fh.read()
5 |
6 | setuptools.setup(
7 | name="xml2pytorch",
8 | version="0.0.9",
9 | author="Yifan Zhou",
10 | author_email="yfzhou.cs@gmail.com",
11 | description="Using xml to define pytorch neural networks",
12 | long_description=long_description,
13 | long_description_content_type="text/markdown",
14 | url="https://github.com/yfzhoucs/xml2pytorch",
15 | packages=setuptools.find_packages(),
16 | install_requires = [
17 | "torch>=0.4.1",
18 | "numpy>=1.15.1"
19 | ],
20 | classifiers=[
21 | "Programming Language :: Python :: 3",
22 | "License :: OSI Approved :: MIT License",
23 | "Operating System :: OS Independent",
24 | ],
25 | )
--------------------------------------------------------------------------------
/xml2pytorch/__init__.py:
--------------------------------------------------------------------------------
1 | from xml2pytorch.converter import convertXML
2 | name = "xml2pytorch"
--------------------------------------------------------------------------------
/xml2pytorch/converter.py:
--------------------------------------------------------------------------------
1 | import collections
2 | import torch.nn as nn
3 | import torch.nn.functional as F
4 | import torch
5 | from xml.dom.minidom import parse
6 | import xml.dom.minidom
7 | import numpy as np
8 |
9 | def main():
10 | simple_cnn = convertXML("./xml_examples/simpleCNN.xml")
11 | print(simple_cnn)
12 | x = torch.randn(1, 3, 32, 32)
13 | y = simple_cnn(x)
14 | print(y)
15 |
16 |
17 | def convertXML(xml_filename):
18 | # 使用minidom解析器打开 XML 文档
19 | DOMTree = xml.dom.minidom.parse(xml_filename)
20 | graph = DOMTree.documentElement
21 |
22 | nets = graph.getElementsByTagName("net")
23 | net = nets[0]
24 | net_model = Net(net)
25 |
26 | return net_model
27 |
28 |
29 | class Net(nn.Module):
30 | def __init__(self, net_xmlNode):
31 | super(Net, self).__init__()
32 | self.net_xmlNode = net_xmlNode
33 | self.layers = []
34 |
35 | layers_xmlNode = net_xmlNode.getElementsByTagName("layer")
36 |
37 | for layer_xmlNode in layers_xmlNode:
38 | net_style = None
39 | args_dict = None
40 | activation = None
41 |
42 | # 去除 xml 里的换行符
43 | for i in range(len(layer_xmlNode.childNodes)-1, -1, -1):
44 | if(layer_xmlNode.childNodes[i].nodeName == "#text"):
45 | layer_xmlNode.removeChild(layer_xmlNode.childNodes[i])
46 |
47 | # 隐藏层类型
48 | net_styles = layer_xmlNode.getElementsByTagName("net_style")
49 | if len(net_styles) > 0:
50 | net_style = net_styles[0].childNodes[0].data
51 |
52 | # 隐藏层的默认参数
53 | args_dict = self.__default_args_for_layer(net_style)
54 | # 隐藏层,xml 追加的参数
55 | args_dict = self.__check_args_from_xml(args_dict, layer_xmlNode)
56 |
57 | # 判断激活函数
58 | activations = layer_xmlNode.getElementsByTagName("activation")
59 | if len(activations) > 0:
60 | activation = activations[0].childNodes[0].data
61 |
62 |
63 | # 声明网络和激活函数
64 | if net_style is not None:
65 | self.__append_layer(net_style, args_dict)
66 | if activation is not None:
67 | self.__append_activation(activation)
68 |
69 |
70 | def __append_activation(self, activation):
71 | if activation == "relu":
72 | self.layers.append(F.relu)
73 | elif activation == "elu":
74 | self.layers.append(F.elu)
75 | elif activation == "selu":
76 | self.layers.append(F.selu)
77 | elif activation == "celu":
78 | self.layers.append(F.celu)
79 | elif activation == "leaky_relu":
80 | self.layers.append(F.leaky_relu)
81 | elif activation == "logsigmoid":
82 | self.layers.append(F.logsigmoid)
83 | elif activation == "softmax":
84 | self.layers.append(F.softmax)
85 | elif activation == "tanh":
86 | self.layers.append(torch.tanh)
87 | elif activation == "sigmoid":
88 | self.layers.append(torch.sigmoid)
89 |
90 |
91 | def __append_layer(self, net_style, args_dict):
92 | args_values_list = list(args_dict.values())
93 | if net_style == "Conv2d":
94 | self.layers.append(nn.Conv2d(args_values_list[0],
95 | args_values_list[1],
96 | args_values_list[2],
97 | args_values_list[3],
98 | args_values_list[4],
99 | args_values_list[5],
100 | args_values_list[6],
101 | args_values_list[7]))
102 | elif net_style == "MaxPool2d":
103 | self.layers.append(nn.MaxPool2d(args_values_list[0],
104 | args_values_list[1],
105 | args_values_list[2],
106 | args_values_list[3],
107 | args_values_list[4],
108 | args_values_list[5]))
109 | elif net_style == "Linear":
110 | self.layers.append(nn.Linear(args_values_list[0],
111 | args_values_list[1],
112 | args_values_list[2]))
113 | elif net_style == "reshape":
114 | # 如果是特殊情况 reshape,就直接将目标向量尺寸传入
115 | # print(type(args_values_list[0]))
116 | self.layers.append(args_values_list[0])
117 | elif net_style == "Conv1d":
118 | self.layers.append(nn.Conv1d(args_values_list[0],
119 | args_values_list[1],
120 | args_values_list[2],
121 | args_values_list[3],
122 | args_values_list[4],
123 | args_values_list[5],
124 | args_values_list[6],
125 | args_values_list[7]))
126 | elif net_style == "Conv3d":
127 | self.layers.append(nn.Conv3d(args_values_list[0],
128 | args_values_list[1],
129 | args_values_list[2],
130 | args_values_list[3],
131 | args_values_list[4],
132 | args_values_list[5],
133 | args_values_list[6],
134 | args_values_list[7]))
135 | elif net_style == "ConvTranspose1d":
136 | self.layers.append(nn.ConvTranspose1d(args_values_list[0],
137 | args_values_list[1],
138 | args_values_list[2],
139 | args_values_list[3],
140 | args_values_list[4],
141 | args_values_list[5],
142 | args_values_list[6],
143 | args_values_list[7],
144 | args_values_list[8]))
145 | elif net_style == "ConvTranspose2d":
146 | self.layers.append(nn.ConvTranspose2d(args_values_list[0],
147 | args_values_list[1],
148 | args_values_list[2],
149 | args_values_list[3],
150 | args_values_list[4],
151 | args_values_list[5],
152 | args_values_list[6],
153 | args_values_list[7],
154 | args_values_list[8]))
155 | elif net_style == "ConvTranspose3d":
156 | self.layers.append(nn.ConvTranspose3d(args_values_list[0],
157 | args_values_list[1],
158 | args_values_list[2],
159 | args_values_list[3],
160 | args_values_list[4],
161 | args_values_list[5],
162 | args_values_list[6],
163 | args_values_list[7],
164 | args_values_list[8]))
165 | elif net_style == "Unfold":
166 | self.layers.append(nn.Unfold(args_values_list[0],
167 | args_values_list[1],
168 | args_values_list[2],
169 | args_values_list[3]))
170 | elif net_style == "Fold":
171 | self.layers.append(nn.Unfold(args_values_list[0],
172 | args_values_list[1],
173 | args_values_list[2],
174 | args_values_list[3],
175 | args_values_list[4]))
176 | elif net_style == "MaxPool1d":
177 | self.layers.append(nn.MaxPool1d(args_values_list[0],
178 | args_values_list[1],
179 | args_values_list[2],
180 | args_values_list[3],
181 | args_values_list[4],
182 | args_values_list[5]))
183 | elif net_style == "MaxPool3d":
184 | self.layers.append(nn.MaxPool3d(args_values_list[0],
185 | args_values_list[1],
186 | args_values_list[2],
187 | args_values_list[3],
188 | args_values_list[4],
189 | args_values_list[5]))
190 | elif net_style == "MaxUnpool1d":
191 | self.layers.append(nn.MaxUnpool1d(args_values_list[0],
192 | args_values_list[1],
193 | args_values_list[2]))
194 | elif net_style == "MaxUnpool2d":
195 | self.layers.append(nn.MaxUnpool2d(args_values_list[0],
196 | args_values_list[1],
197 | args_values_list[2]))
198 | elif net_style == "MaxUnpool3d":
199 | self.layers.append(nn.MaxUnpool3d(args_values_list[0],
200 | args_values_list[1],
201 | args_values_list[2]))
202 | elif net_style == "AvgPool1d":
203 | self.layers.append(nn.AvgPool1d(args_values_list[0],
204 | args_values_list[1],
205 | args_values_list[2],
206 | args_values_list[3],
207 | args_values_list[4]))
208 | elif net_style == "AvgPool2d":
209 | self.layers.append(nn.AvgPool2d(args_values_list[0],
210 | args_values_list[1],
211 | args_values_list[2],
212 | args_values_list[3],
213 | args_values_list[4]))
214 | elif net_style == "AvgPool3d":
215 | self.layers.append(nn.AvgPool3d(args_values_list[0],
216 | args_values_list[1],
217 | args_values_list[2],
218 | args_values_list[3],
219 | args_values_list[4]))
220 | elif net_style == "FractionalMaxPool2d":
221 | self.layers.append(nn.FractionalMaxPool2d(args_values_list[0],
222 | args_values_list[1],
223 | args_values_list[2],
224 | args_values_list[3],
225 | args_values_list[4]))
226 | elif net_style == "LPPool1d":
227 | self.layers.append(nn.LPPool1d(args_values_list[0],
228 | args_values_list[1],
229 | args_values_list[2],
230 | args_values_list[3]))
231 | elif net_style == "LPPool2d":
232 | self.layers.append(nn.LPPool2d(args_values_list[0],
233 | args_values_list[1],
234 | args_values_list[2],
235 | args_values_list[3]))
236 | elif net_style == "AdaptiveMaxPool1d":
237 | self.layers.append(nn.AdaptiveMaxPool1d(args_values_list[0],
238 | args_values_list[1]))
239 | elif net_style == "AdaptiveMaxPool2d":
240 | self.layers.append(nn.AdaptiveMaxPool2d(args_values_list[0],
241 | args_values_list[1]))
242 | elif net_style == "AdaptiveMaxPool3d":
243 | self.layers.append(nn.AdaptiveMaxPool3d(args_values_list[0],
244 | args_values_list[1]))
245 | elif net_style == "AdaptiveAvgPool1d":
246 | self.layers.append(nn.AdaptiveAvgPool1d(args_values_list[0]))
247 | elif net_style == "AdaptiveAvgPool2d":
248 | self.layers.append(nn.AdaptiveAvgPool2d(args_values_list[0]))
249 | elif net_style == "AdaptiveAvgPool3d":
250 | self.layers.append(nn.AdaptiveAvgPool3d(args_values_list[0]))
251 | elif net_style == "ReflectionPad1d":
252 | self.layers.append(nn.ReflectionPad1d(args_values_list[0]))
253 | elif net_style == "ReflectionPad2d":
254 | self.layers.append(nn.ReflectionPad2d(args_values_list[0]))
255 | elif net_style == "ReplicationPad1d":
256 | self.layers.append(nn.ReplicationPad1d(args_values_list[0]))
257 | elif net_style == "ReplicationPad2d":
258 | self.layers.append(nn.ReplicationPad2d(args_values_list[0]))
259 | elif net_style == "ReplicationPad3d":
260 | self.layers.append(nn.ReplicationPad3d(args_values_list[0]))
261 | elif net_style == "ZeroPad2d":
262 | self.layers.append(nn.ZeroPad2d(args_values_list[0]))
263 | elif net_style == "ConstantPad1d":
264 | self.layers.append(nn.ConstantPad1d(args_values_list[0],
265 | args_values_list[1]))
266 | elif net_style == "ConstantPad2d":
267 | self.layers.append(nn.ConstantPad2d(args_values_list[0],
268 | args_values_list[1]))
269 | elif net_style == "ConstantPad3d":
270 | self.layers.append(nn.ConstantPad3d(args_values_list[0],
271 | args_values_list[1]))
272 | elif net_style == "ELU":
273 | self.layers.append(nn.ELU(args_values_list[0],
274 | args_values_list[1]))
275 | elif net_style == "Hardshrink":
276 | self.layers.append(nn.Hardshrink(args_values_list[0]))
277 | elif net_style == "Hardtanh":
278 | self.layers.append(nn.Hardtanh(args_values_list[0],
279 | args_values_list[1],
280 | args_values_list[2],
281 | args_values_list[3],
282 | args_values_list[4]))
283 | elif net_style == "LeakyReLU":
284 | self.layers.append(nn.LeakyReLU(args_values_list[0],
285 | args_values_list[1]))
286 | elif net_style == "LogSigmoid":
287 | self.layers.append(nn.LogSigmoid())
288 | elif net_style == "PReLU":
289 | self.layers.append(nn.PReLU(args_values_list[0],
290 | args_values_list[1]))
291 | elif net_style == "ReLU":
292 | self.layers.append(nn.ReLU(args_values_list[0]))
293 | elif net_style == "ReLU6":
294 | self.layers.append(nn.ReLU6(args_values_list[0]))
295 | elif net_style == "RReLU":
296 | self.layers.append(nn.RReLU(args_values_list[0],
297 | args_values_list[1],
298 | args_values_list[2]))
299 | elif net_style == "SELU":
300 | self.layers.append(nn.SELU(args_values_list[0]))
301 | elif net_style == "CELU":
302 | self.layers.append(nn.CELU(args_values_list[0],
303 | args_values_list[1]))
304 | elif net_style == "Sigmoid":
305 | self.layers.append(nn.Sigmoid())
306 | elif net_style == "Softplus":
307 | self.layers.append(nn.Softplus(args_values_list[0],
308 | args_values_list[1]))
309 | elif net_style == "Softshrink":
310 | self.layers.append(nn.Softshrink(args_values_list[0]))
311 | elif net_style == "Softsign":
312 | self.layers.append(nn.Softsign())
313 | elif net_style == "Tanh":
314 | self.layers.append(nn.Tanh())
315 | elif net_style == "Tanhshrink":
316 | self.layers.append(nn.Tanhshrink())
317 | elif net_style == "Threshold":
318 | self.layers.append(nn.Threshold(args_values_list[0],
319 | args_values_list[1],
320 | args_values_list[2]))
321 | elif net_style == "Softmin":
322 | self.layers.append(nn.Softmin(args_values_list[0]))
323 | elif net_style == "Softmax":
324 | self.layers.append(nn.Softmax(args_values_list[0]))
325 | elif net_style == "Softmax2d":
326 | self.layers.append(nn.Softmax2d())
327 | elif net_style == "LogSoftmax":
328 | self.layers.append(nn.LogSoftmax(args_values_list[0]))
329 | elif net_style == "AdaptiveLogSoftmaxWithLoss":
330 | self.layers.append(nn.AdaptiveLogSoftmaxWithLoss(args_values_list[0],
331 | args_values_list[1],
332 | args_values_list[2],
333 | args_values_list[3],
334 | args_values_list[4]))
335 | elif net_style == "BatchNorm1d":
336 | self.layers.append(nn.BatchNorm1d(args_values_list[0],
337 | args_values_list[1],
338 | args_values_list[2],
339 | args_values_list[3],
340 | args_values_list[4]))
341 | elif net_style == "BatchNorm2d":
342 | self.layers.append(nn.BatchNorm2d(args_values_list[0],
343 | args_values_list[1],
344 | args_values_list[2],
345 | args_values_list[3],
346 | args_values_list[4]))
347 | elif net_style == "BatchNorm3d":
348 | self.layers.append(nn.BatchNorm3d(args_values_list[0],
349 | args_values_list[1],
350 | args_values_list[2],
351 | args_values_list[3],
352 | args_values_list[4]))
353 | elif net_style == "GroupNorm":
354 | self.layers.append(nn.GroupNorm(args_values_list[0],
355 | args_values_list[1],
356 | args_values_list[2],
357 | args_values_list[3]))
358 | elif net_style == "InstanceNorm1d":
359 | self.layers.append(nn.InstanceNorm1d(args_values_list[0],
360 | args_values_list[1],
361 | args_values_list[2],
362 | args_values_list[3],
363 | args_values_list[4]))
364 | elif net_style == "InstanceNorm2d":
365 | self.layers.append(nn.InstanceNorm2d(args_values_list[0],
366 | args_values_list[1],
367 | args_values_list[2],
368 | args_values_list[3],
369 | args_values_list[4]))
370 | elif net_style == "InstanceNorm3d":
371 | self.layers.append(nn.InstanceNorm3d(args_values_list[0],
372 | args_values_list[1],
373 | args_values_list[2],
374 | args_values_list[3],
375 | args_values_list[4]))
376 | elif net_style == "LayerNorm":
377 | self.layers.append(nn.LayerNorm(args_values_list[0],
378 | args_values_list[1],
379 | args_values_list[2]))
380 | elif net_style == "LocalResponseNorm":
381 | self.layers.append(nn.LocalResponseNorm(args_values_list[0],
382 | args_values_list[1],
383 | args_values_list[2],
384 | args_values_list[3]))
385 | elif net_style == "Linear":
386 | self.layers.append(nn.Linear(args_values_list[0],
387 | args_values_list[1],
388 | args_values_list[2]))
389 | elif net_style == "Dropout":
390 | self.layers.append(nn.Dropout(args_values_list[0],
391 | args_values_list[1]))
392 | elif net_style == "Dropout2d":
393 | self.layers.append(nn.Dropout2d(args_values_list[0],
394 | args_values_list[1]))
395 | elif net_style == "Dropout3d":
396 | self.layers.append(nn.Dropout3d(args_values_list[0],
397 | args_values_list[1]))
398 | elif net_style == "AlphaDropout":
399 | self.layers.append(nn.AlphaDropout(args_values_list[0],
400 | args_values_list[1]))
401 |
402 |
403 | # 从 xml 中读取参数并覆盖默认参数
404 | def __check_args_from_xml(self, args_dict, layer_xml_node):
405 | args_keys = list(args_dict.keys())
406 | for args_key in args_keys:
407 | args_value_xml_node_list = layer_xml_node.getElementsByTagName(args_key)
408 | if len(args_value_xml_node_list) > 0:
409 | args_value_string = args_value_xml_node_list[0].childNodes[0].data
410 | args_value = eval(args_value_string)
411 | args_dict[args_key] = args_value
412 | return args_dict
413 |
414 |
415 | # 网络的默认参数
416 | def __default_args_for_layer(self, net_style):
417 | args_dict = collections.OrderedDict()
418 | if net_style == "Conv2d":
419 | args_dict["in_channels"] = None
420 | args_dict["out_channels"] = None
421 | args_dict["kernel_size"] = None
422 | args_dict["stride"] = 1
423 | args_dict["padding"] = 0
424 | args_dict["dilation"] = 1
425 | args_dict["groups"] = 1
426 | args_dict["bias"] = True
427 | elif net_style == "MaxPool2d":
428 | args_dict["kernel_size"] = None
429 | args_dict["stride"] = None
430 | args_dict["padding"] = 0
431 | args_dict["dilation"] = 1
432 | args_dict["return_indices"] = False
433 | args_dict["ceil_mode"] = False
434 | elif net_style == "Linear":
435 | args_dict["in_features"] = None
436 | args_dict["out_features"] = None
437 | args_dict["bias"] = True
438 | elif net_style == "reshape":
439 | args_dict["dimensions"] = None
440 | elif net_style == "Conv1d":
441 | args_dict["in_channels"] = None
442 | args_dict["out_channels"] = None
443 | args_dict["kernel_size"] = None
444 | args_dict["stride"] = 1
445 | args_dict["padding"] = 0
446 | args_dict["dilation"] = 1
447 | args_dict["groups"] = 1
448 | args_dict["bias"] = True
449 | elif net_style == "Conv3d":
450 | args_dict["in_channels"] = None
451 | args_dict["out_channels"] = None
452 | args_dict["kernel_size"] = None
453 | args_dict["stride"] = 1
454 | args_dict["padding"] = 0
455 | args_dict["dilation"] = 1
456 | args_dict["groups"] = 1
457 | args_dict["bias"] = True
458 | elif net_style == "ConvTranspose1d":
459 | args_dict["in_channels"] = None
460 | args_dict["out_channels"] = None
461 | args_dict["kernel_size"] = None
462 | args_dict["stride"] = 1
463 | args_dict["padding"] = 0
464 | args_dict["output_padding"] = 0
465 | args_dict["groups"] = 1
466 | args_dict["bias"] = True
467 | args_dict["dilation"] = 1
468 | elif net_style == "ConvTranspose2d":
469 | args_dict["in_channels"] = None
470 | args_dict["out_channels"] = None
471 | args_dict["kernel_size"] = None
472 | args_dict["stride"] = 1
473 | args_dict["padding"] = 0
474 | args_dict["output_padding"] = 0
475 | args_dict["groups"] = 1
476 | args_dict["bias"] = True
477 | args_dict["dilation"] = 1
478 | elif net_style == "ConvTranspose3d":
479 | args_dict["in_channels"] = None
480 | args_dict["out_channels"] = None
481 | args_dict["kernel_size"] = None
482 | args_dict["stride"] = 1
483 | args_dict["padding"] = 0
484 | args_dict["output_padding"] = 0
485 | args_dict["groups"] = 1
486 | args_dict["bias"] = True
487 | args_dict["dilation"] = 1
488 | elif net_style == "Unfold":
489 | args_dict["kernel_size"] = None
490 | args_dict["dilation"] = 1
491 | args_dict["padding"] = 0
492 | args_dict["stride"] = 1
493 | elif net_style == "Fold":
494 | args_dict["output_size"] = None
495 | args_dict["kernel_size"] = None
496 | args_dict["dilation"] = 1
497 | args_dict["padding"] = 0
498 | args_dict["stride"] = 1
499 | elif net_style == "MaxPool1d":
500 | args_dict["kernel_size"] = None
501 | args_dict["stride"] = None
502 | args_dict["padding"] = 0
503 | args_dict["dilation"] = 1
504 | args_dict["return_indices"] = False
505 | args_dict["ceil_mode"] = False
506 | elif net_style == "MaxPool3d":
507 | args_dict["kernel_size"] = None
508 | args_dict["stride"] = None
509 | args_dict["padding"] = 0
510 | args_dict["dilation"] = 1
511 | args_dict["return_indices"] = False
512 | args_dict["ceil_mode"] = False
513 | elif net_style == "MaxUnpool1d":
514 | args_dict["kernel_size"] = None
515 | args_dict["stride"] = None
516 | args_dict["padding"] = 0
517 | elif net_style == "MaxUnpool2d":
518 | args_dict["kernel_size"] = None
519 | args_dict["stride"] = None
520 | args_dict["padding"] = 0
521 | elif net_style == "MaxUnpool3d":
522 | args_dict["kernel_size"] = None
523 | args_dict["stride"] = None
524 | args_dict["padding"] = 0
525 | elif net_style == "AvgPool1d":
526 | args_dict["kernel_size"] = None
527 | args_dict["stride"] = None
528 | args_dict["padding"] = 0
529 | args_dict["ceil_mode"] = False
530 | args_dict["count_include_pad"] = True
531 | elif net_style == "AvgPool2d":
532 | args_dict["kernel_size"] = None
533 | args_dict["stride"] = None
534 | args_dict["padding"] = 0
535 | args_dict["ceil_mode"] = False
536 | args_dict["count_include_pad"] = True
537 | elif net_style == "AvgPool3d":
538 | args_dict["kernel_size"] = None
539 | args_dict["stride"] = None
540 | args_dict["padding"] = 0
541 | args_dict["ceil_mode"] = False
542 | args_dict["count_include_pad"] = True
543 | elif net_style == "FractionalMaxPool2d":
544 | args_dict["kernel_size"] = None
545 | args_dict["output_size"] = None
546 | args_dict["output_ratio"] = None
547 | args_dict["return_indices"] = False
548 | args_dict["_random_samples"] = None
549 | elif net_style == "LPPool1d":
550 | args_dict["norm_type"] = None
551 | args_dict["kernel_size"] = None
552 | args_dict["stride"] = None
553 | args_dict["ceil_mode"] = False
554 | elif net_style == "LPPool2d":
555 | args_dict["norm_type"] = None
556 | args_dict["kernel_size"] = None
557 | args_dict["stride"] = None
558 | args_dict["ceil_mode"] = False
559 | elif net_style == "AdaptiveMaxPool1d":
560 | args_dict["output_size"] = None
561 | args_dict["return_indices"] = False
562 | elif net_style == "AdaptiveMaxPool2d":
563 | args_dict["output_size"] = None
564 | args_dict["return_indices"] = False
565 | elif net_style == "AdaptiveMaxPool3d":
566 | args_dict["output_size"] = None
567 | args_dict["return_indices"] = False
568 | elif net_style == "AdaptiveAvgPool1d":
569 | args_dict["output_size"] = None
570 | elif net_style == "AdaptiveAvgPool2d":
571 | args_dict["output_size"] = None
572 | elif net_style == "AdaptiveAvgPool3d":
573 | args_dict["output_size"] = None
574 | elif net_style == "ReflectionPad1d":
575 | args_dict["padding"] = None
576 | elif net_style == "ReflectionPad2d":
577 | args_dict["padding"] = None
578 | elif net_style == "ReplicationPad1d":
579 | args_dict["padding"] = None
580 | elif net_style == "ReplicationPad2d":
581 | args_dict["padding"] = None
582 | elif net_style == "ReplicationPad3d":
583 | args_dict["padding"] = None
584 | elif net_style == "ZeroPad2d":
585 | args_dict["padding"] = None
586 | elif net_style == "ConstantPad1d":
587 | args_dict["padding"] = None
588 | args_dict["value"] = None
589 | elif net_style == "ConstantPad2d":
590 | args_dict["padding"] = None
591 | args_dict["value"] = None
592 | elif net_style == "ConstantPad3d":
593 | args_dict["padding"] = None
594 | args_dict["value"] = None
595 | elif net_style == "ELU":
596 | args_dict["alpha"] = 1.0
597 | args_dict["inplace"] = False
598 | elif net_style == "Hardshrink":
599 | args_dict["lambd"] = 0.5
600 | elif net_style == "Hardtanh":
601 | args_dict["min_val"] = -1.0
602 | args_dict["max_val"] = 1.0
603 | args_dict["inplace"] = False
604 | args_dict["min_value"] = None
605 | args_dict["max_value"] = None
606 | elif net_style == "LeakyReLU":
607 | args_dict["negative_slope"] = 0.01
608 | args_dict["inplace"] = False
609 | elif net_style == "LogSigmoid":
610 | args_dict = None
611 | elif net_style == "PReLU":
612 | args_dict["num_parameters"] = 1
613 | args_dict["init"] = 0.25
614 | elif net_style == "ReLU":
615 | args_dict["inplace"] = False
616 | elif net_style == "ReLU6":
617 | args_dict["inplace"] = False
618 | elif net_style == "RReLU":
619 | args_dict["lower"] = 0.125
620 | args_dict["upper"] = 1.0 / 3.0
621 | args_dict["inplace"] = False
622 | elif net_style == "SELU":
623 | args_dict["inplace"] = False
624 | elif net_style == "CELU":
625 | args_dict["alpha"] = 1.0
626 | args_dict["inplace"] = False
627 | elif net_style == "Sigmoid":
628 | args_dict = None
629 | elif net_style == "Softplus":
630 | args_dict["beta"] = 1
631 | args_dict["threshold"] = 20
632 | elif net_style == "Softshrink":
633 | args_dict["lambd"] = 0.5
634 | elif net_style == "Softsign":
635 | args_dict = None
636 | elif net_style == "Tanh":
637 | args_dict = None
638 | elif net_style == "Tanhshrink":
639 | args_dict = None
640 | elif net_style == "Threshold":
641 | args_dict["threshold"] = None
642 | args_dict["value"] = None
643 | args_dict["inplace"] = False
644 | elif net_style == "Softmin":
645 | args_dict["dim"] = None
646 | elif net_style == "Softmax":
647 | args_dict["dim"] = None
648 | elif net_style == "Softmax2d":
649 | args_dict = None
650 | elif net_style == "LogSoftmax":
651 | args_dict["dim"] = None
652 | elif net_style == "AdaptiveLogSoftmaxWithLoss":
653 | args_dict["in_features"] = None
654 | args_dict["n_classes"] = None
655 | args_dict["cutoffs"] = None
656 | args_dict["div_value"] = 4.0
657 | args_dict["head_bias"] = False
658 | elif net_style == "BatchNorm1d":
659 | args_dict["num_features"] = None
660 | args_dict["eps"] = 1e-05
661 | args_dict["momentum"] = 0.1
662 | args_dict["affine"] = True
663 | args_dict["track_running_stats"] = True
664 | elif net_style == "BatchNorm2d":
665 | args_dict["num_features"] = None
666 | args_dict["eps"] = 1e-05
667 | args_dict["momentum"] = 0.1
668 | args_dict["affine"] = True
669 | args_dict["track_running_stats"] = True
670 | elif net_style == "BatchNorm3d":
671 | args_dict["num_features"] = None
672 | args_dict["eps"] = 1e-05
673 | args_dict["momentum"] = 0.1
674 | args_dict["affine"] = True
675 | args_dict["track_running_stats"] = True
676 | elif net_style == "GroupNorm":
677 | args_dict["num_groups"] = None
678 | args_dict["num_channels"] = None
679 | args_dict["eps"] = 1e-05
680 | args_dict["affine"] = True
681 | elif net_style == "InstanceNorm1d":
682 | args_dict["num_features"] = None
683 | args_dict["eps"] = 1e-05
684 | args_dict["momentum"] = 0.1
685 | args_dict["affine"] = True
686 | args_dict["track_running_stats"] = False
687 | elif net_style == "InstanceNorm2d":
688 | args_dict["num_features"] = None
689 | args_dict["eps"] = 1e-05
690 | args_dict["momentum"] = 0.1
691 | args_dict["affine"] = True
692 | args_dict["track_running_stats"] = False
693 | elif net_style == "InstanceNorm3d":
694 | args_dict["num_features"] = None
695 | args_dict["eps"] = 1e-05
696 | args_dict["momentum"] = 0.1
697 | args_dict["affine"] = True
698 | args_dict["track_running_stats"] = False
699 | elif net_style == "LayerNorm":
700 | args_dict["normalized_shape"] = None
701 | args_dict["eps"] = 1e-05
702 | args_dict["elementwise_affine"] = True
703 | elif net_style == "LocalResponseNorm":
704 | args_dict["size"] = None
705 | args_dict["alpha"] = 0.0001
706 | args_dict["beta"] = 0.75
707 | args_dict["k"] = 1.0
708 | elif net_style == "Linear":
709 | args_dict["in_features"] = None
710 | args_dict["out_features"] = None
711 | args_dict["bias"] = True
712 | elif net_style == "Dropout":
713 | args_dict["p"] = 0.5
714 | args_dict["inplace"] = False
715 | elif net_style == "Dropout2d":
716 | args_dict["p"] = 0.5
717 | args_dict["inplace"] = False
718 | elif net_style == "Dropout3d":
719 | args_dict["p"] = 0.5
720 | args_dict["inplace"] = False
721 | elif net_style == "AlphaDropout":
722 | args_dict["p"] = 0.5
723 | args_dict["inplace"] = False
724 | # elif net_style == "RNNCell":
725 | # args_dict["input_size"] = None
726 | # args_dict["hidden_size"] = None
727 | # args_dict["bias"] = True
728 | # args_dict["nonlinearity"] = 'tanh'
729 |
730 | return args_dict
731 |
732 | def forward(self, x):
733 | for i in range(len(self.layers)):
734 | # 是.view,转换 Tensor 的形状
735 | if isinstance(self.layers[i], list):
736 | # print(x.shape)
737 | # print(self.layers[i])
738 | x = x.view(self.layers[i])
739 | # 是一个隐藏层
740 | else:
741 | # print(x.shape)
742 | x = self.layers[i](x)
743 | return x
744 |
745 |
746 |
747 | class Net2(nn.Module):
748 | def __init__(self):
749 | super(Net2, self).__init__()
750 | self.conv1 = nn.Conv2d(3, 6, 5)
751 | self.pool = nn.MaxPool2d(2, 2)
752 | self.conv2 = nn.Conv2d(6, 16, 5)
753 | self.fc1 = nn.Linear(16 * 5 * 5, 120)
754 | self.fc2 = nn.Linear(120, 84)
755 | self.fc3 = nn.Linear(84, 10)
756 |
757 | def forward(self, x):
758 | x = self.pool(F.relu(self.conv1(x)))
759 | print(x.shape)
760 | x = self.pool(F.relu(self.conv2(x)))
761 | print(x.shape)
762 | x = x.view([-1, 16 * 5 * 5])
763 | print(x.shape)
764 | x = F.relu(self.fc1(x))
765 | print(x.shape)
766 | x = F.relu(self.fc2(x))
767 | print(x.shape)
768 | x = self.fc3(x)
769 | return x
770 |
771 | if __name__ == "__main__":
772 | main()
--------------------------------------------------------------------------------
/xml2pytorch/xml_examples/simpleCNN.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Conv2d
5 | 3
6 | 6
7 | 5
8 |
9 |
10 | ELU
11 |
12 |
13 | MaxPool2d
14 | 2
15 | 2
16 | logsigmoid
17 |
18 |
19 | Conv2d
20 | 6
21 | 16
22 | 5
23 | relu
24 |
25 |
26 | MaxPool2d
27 | 2
28 | 2
29 | relu
30 |
31 |
32 | reshape
33 | [-1, 16*5*5]
34 |
35 |
36 | Linear
37 | 400
38 | 120
39 | tanh
40 |
41 |
42 | Linear
43 | 120
44 | 84
45 | sigmoid
46 |
47 |
48 | Linear
49 | 84
50 | 10
51 | softmax
52 |
53 |
54 |
--------------------------------------------------------------------------------