-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
140 lines (102 loc) · 3.51 KB
/
test.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
import torch
import torchvision
# import pudb;pu.db
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import pudb
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
learning_rate = 0.001
K = 96
epochs = 10
droput_p = 0.5
batch_size = 4
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform = transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4096,
shuffle=False, num_workers=2)
class RCNN(nn.Module):
def __init__(self):
super(RCNN, self).__init__()
self.max_pool = nn.MaxPool2d(3,2)
self.lrn = nn.LocalResponseNorm(13)
self.droput = nn.Dropout(droput_p)
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(3, K, 5,1)
self.rcl_1_feed_fwd = nn.Conv2d(K,K,3,1,1)
self.rcl_1_rec = nn.Conv2d(K,K,3,1,1)
self.conv2 = nn.Conv2d(K,K,3,1,1)
self.rcl_2_feed_fwd = nn.Conv2d(K,K,3,1,1)
self.rcl_2_rec = nn.Conv2d(K,K,3,1,1)
self.conv2 = nn.Conv2d(K,K,3,1,1)
self.rcl_3_feed_fwd = nn.Conv2d(K,K,3,1,1)
self.rcl_3_rec = nn.Conv2d(K,K,3,1,1)
self.conv3 = nn.Conv2d(K,K,3,1,1)
self.rcl_4_feed_fwd = nn.Conv2d(K,K,3,1,1)
self.rcl_4_rec = nn.Conv2d(K,K,3,1,1)
self.linear = nn.Linear(K,10)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
out = self.conv1(x)
out = self.max_pool(out)
# First RCL
out_r = self.rcl_1_feed_fwd(out)
for i in range(3):
out_r = self.rcl_1_rec(out_r) + self.rcl_1_feed_fwd(out)
out = out_r
out = self.lrn(self.relu(out))
out = self.droput(out)
# Second RCL
out_r = self.rcl_2_feed_fwd(out)
for i in range(3):
out_r = self.rcl_2_rec(out_r) + self.rcl_2_feed_fwd(out)
out = out_r
out = self.lrn(self.relu(out))
out = self.droput(out)
out = self.max_pool(out)
# Third RCL
out_r = self.rcl_3_feed_fwd(out)
for i in range(3):
out_r = self.rcl_3_rec(out_r) + self.rcl_3_feed_fwd(out)
out = out_r
out = self.lrn(self.relu(out))
out = self.droput(out)
# Fourth RCL
out_r = self.rcl_4_feed_fwd(out)
for i in range(3):
out_r = self.rcl_4_rec(out_r) + self.rcl_4_feed_fwd(out)
out = out_r
out = self.lrn(self.relu(out))
out = self.droput(out)
out = nn.MaxPool2d(out.shape[-1])(out)
out = out.view(-1,K)
out = self.linear(out)
out = self.softmax(out)
return out
use_gpu = torch.cuda.is_available()
net = RCNN()
if use_gpu:
net.cuda()
correct = 0
total = 0
net.load_state_dict(torch.load('./checkpoints/19-24-2.2642.pyt'))
net.eval()
with torch.no_grad():
total_iterations = len(list(testloader))
k = 0
for data in testloader:
k = k+1
images, labels = data
# pu.db
if use_gpu:
images = images.cuda()
labels = labels.cuda()
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print("{} of {} iterations done".format(k,total_iterations))
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))