-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_create_network.py
77 lines (64 loc) · 1.97 KB
/
test_create_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from ptu.network_util import create_network, create_reverse_network
from ptu.util.types import LayerType
from torchinfo import summary
import torch
import torch.nn as nn
network_config_linear = [
(LayerType.linear, {"size": 64}),
(LayerType.linear, {"size": 128, "bn": True, "p_drop": 0.5}),
(LayerType.linear, {"size": 256, "act_func": nn.Tanh}),
(LayerType.linear, {"size": 10}),
]
network_config_conv = [
(
LayerType.conv2d,
{"filters": 4, "kernel_size": 3, "padding": 1, "stride": 1, "output_padding": 0, "pool": nn.MaxPool2d(2)},
),
(
LayerType.conv2d,
{
"filters": 8,
"kernel_size": 3,
"padding": 1,
"stride": 1,
"output_padding": 0,
"p_drop": 0.2,
"act_func": nn.Tanh,
},
),
(
LayerType.conv2d,
{"filters": 8, "kernel_size": 3, "padding": 1, "stride": 1, "output_padding": 0, "pool": nn.MaxPool2d(2)},
),
(LayerType.flatten, {}),
(LayerType.linear, {"size": 256}),
(LayerType.linear, {"size": 10}),
]
class ModelForward(nn.Module):
def __init__(self):
super().__init__()
self.net, curr_channels, size = create_network(network_config_conv, 3, 64)
print(curr_channels)
print(size)
def forward(self, x):
return self.net(x)
class ModelReversed(nn.Module):
def __init__(self):
super().__init__()
self.net, curr_channels, size = create_reverse_network(network_config_conv, 3, 64)
print(curr_channels)
print(size)
def forward(self, x):
return self.net(x)
model1 = ModelForward()
rand_data = torch.rand(4, 3, 64, 64)
print(model1)
model_stats1 = summary(model1, input_size=(4, 3, 64, 64), verbose=0)
print(model_stats1)
model2 = ModelReversed()
rand_data = torch.rand(4, 10)
print(model2)
model_stats2 = summary(model2, input_size=(4, 10), verbose=0)
print(model_stats2)
model2.cpu()
print(model2(rand_data).shape)