-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recurrentconv.py
executable file
·150 lines (129 loc) · 4.79 KB
/
test_recurrentconv.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import unittest
import torch
from recurrentconv import RecurrentConv, SampleRecurrentConvNet
from math import ceil
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import config
from utils import train, test
class TestRecurrentConv(unittest.TestCase):
def setUp(self):
self.in_channels = 3
self.out_channels=10
self.kernel_size = [3,3]
self.padding = [
self.kernel_size[0]//2,
self.kernel_size[0]//2,
self.kernel_size[1]//2,
self.kernel_size[1]//2
]
self.stride = [1,1]
self.batch_size = 16
self.input_width = self.input_height=32
self.recurrent_conv = RecurrentConv(
in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=self.kernel_size,
stride=self.stride,
in_size=[self.input_width,self.input_height]
)
def test_forward(self):
x = torch.rand([self.batch_size,self.in_channels,self.input_width,self.input_height])
self.assertTrue(
self.recurrent_conv(x).size()
==
torch.Size(
(
self.batch_size,self.out_channels,self.input_width,self.input_height
)
)
)
self.stride=[3,3]
self.recurrent_conv = RecurrentConv(
in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=self.kernel_size,
stride=self.stride,
in_size=[self.input_width,self.input_height]
)
self.assertTrue(
self.recurrent_conv(x).size()
==
torch.Size(
(
self.batch_size,
self.out_channels,
ceil(self.input_width/self.stride[0]),
ceil(self.input_height/self.stride[1])
)
)
)
self.kernel_size=[7,7]
self.recurrent_conv = RecurrentConv(
in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=self.kernel_size,
stride=self.stride,
in_size=[self.input_width,self.input_height]
)
self.assertTrue(
self.recurrent_conv(x).size()
==
torch.Size(
(
self.batch_size,
self.out_channels,
ceil(self.input_width/self.stride[0]),
ceil(self.input_height/self.stride[1])
)
)
)
class TestSampleRecurrentConvNet(unittest.TestCase):
def setUp(self):
print()
self.in_channels = 1
self.out_channels = 32
self.kernel_size = [3,3]
self.stride = [2,2]
self.in_size = [28,28]
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = SampleRecurrentConvNet(
in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=self.kernel_size,
stride=self.stride,
in_size=self.in_size
).to(self.device)
torch.manual_seed(config.seed)
train_kwargs = {'batch_size': config.batch_size}
test_kwargs = {'batch_size': config.test_batch_size}
if self.device.type=="cuda":
cuda_kwargs = {'num_workers': 0,
'pin_memory': True,
'shuffle': True}
train_kwargs.update(cuda_kwargs)
cuda_kwargs = {'num_workers': 0,
'pin_memory': True,
'shuffle': False}
test_kwargs.update(cuda_kwargs)
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
dataset1 = datasets.MNIST('../data', train=True, download=True,
transform=transform)
dataset2 = datasets.MNIST('../data', train=False,
transform=transform)
self.train_loader = torch.utils.data.DataLoader(dataset1,**train_kwargs)
self.test_loader = torch.utils.data.DataLoader(dataset2, **test_kwargs)
def test_train_and_test(self):
optimizer = optim.Adadelta(self.model.parameters(), lr=config.lr)
scheduler = StepLR(optimizer, step_size=1, gamma=config.gamma)
for epoch in range(1, config.epochs + 1):
train(self.model, self.device, self.train_loader, optimizer, epoch)
scheduler.step()
print()
test(self.model, self.device, self.test_loader)
if __name__== "__main__":
unittest.main()