-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassification_test.py
161 lines (127 loc) · 5.63 KB
/
classification_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import argparse
import torch
import torch.nn as nn
import numpy as np
from data_loader import build_vocab, get_loader
from model import ResNet, ResidualBlock, EncoderCNN
from torch.autograd import Variable
from torchvision import transforms
def to_var(x, volatile=False):
if torch.cuda.is_available():
x = x.cuda(1)
return Variable(x, volatile=volatile)
def rearrange_tensor(x, batch_size, caption_size):
for i in range(caption_size):
temp = x[i*batch_size:(i+1)*batch_size].view(batch_size, -1)
if i == 0:
temp_cat = temp
else:
temp_cat = torch.cat((temp_cat, temp), 1)
return temp_cat
def main(args):
# Image preprocessing
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.033, 0.032, 0.033),
(0.027, 0.027, 0.027))])
vocab = build_vocab(args.root_path, threshold=0)
num_class = 9
# Build data loader
data_loader = get_loader(args.root_path, vocab,
transform, args.batch_size,
shuffle=True, num_workers=args.num_workers)
# Build the models
cnn = ResNet(ResidualBlock, [3, 3, 3], num_class)
if torch.cuda.is_available():
cnn.cuda(1)
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
params = list(cnn.parameters())
optimizer = torch.optim.Adam(params, lr=args.learning_rate)
# Train the Models
total_step = len(data_loader)
for epoch in range(args.num_epochs):
for i, (images, captions, lengths) in enumerate(data_loader):
#if i > 1 :
# break;
idx_arr = []
for element in captions[:,1]:
idx_arr.append(int(vocab.idx2word[element]) - 1)
temp_arr= np.array(idx_arr)
trg_arr = torch.from_numpy(temp_arr)
target = to_var(trg_arr)
images = to_var(images)
optimizer.zero_grad()
features = cnn(images)
loss = criterion(features, target)
loss.backward()
optimizer.step()
# Print log info
if i % args.log_step == 0:
print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Perplexity: %5.4f'
%(epoch, args.num_epochs, i, total_step,
loss.data[0], np.exp(loss.data[0])))
#print(features)
#print(target)
##test set accuracy
#rearrange tensor to batch_size * caption_size
re_target = rearrange_tensor(target, captions.size(0), 1)
re_out_max = rearrange_tensor(features.max(1)[1], captions.size(0), 1)
#convert to numpy
outputs_np = re_out_max.cpu().data.numpy()
targets_np = re_target.cpu().data.numpy()
location_match = 0
for i in range(len(targets_np)):
if(outputs_np[i] == targets_np[i]):
location_match +=1
print('location match accuracy: %.4f'
%(location_match/len(targets_np)))
#test model
print('---------------------------------')
cnn.eval()
test_loader = get_loader(args.test_path, vocab,
transform, args.batch_size,
shuffle=True, num_workers=args.num_workers)
for images, captions, lengths in test_loader:
idx_arr = []
for element in captions[:,1]:
idx_arr.append(int(vocab.idx2word[element]) - 1)
temp_arr= np.array(idx_arr)
trg_arr = torch.from_numpy(temp_arr)
target = to_var(trg_arr)
images = to_var(images)
features = cnn(images)
re_target = rearrange_tensor(target, captions.size(0), 1)
re_out_max = rearrange_tensor(features.max(1)[1], captions.size(0), 1)
#convert to numpy
outputs_np = re_out_max.cpu().data.numpy()
targets_np = re_target.cpu().data.numpy()
location_match = 0
for i in range(len(targets_np)):
if(outputs_np[i] == targets_np[i]):
location_match +=1
print('location match accuracy: %.4f'
%(location_match/len(targets_np)))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, default='./models/' ,
help='path for saving trained models')
parser.add_argument('--root_path', type=str, default='data/bitmap2svg_samples2/',
help='path for root')
parser.add_argument('--test_path', type=str, default='data/bitmap2_test/',
help='path for root')
parser.add_argument('--log_step', type=int , default=5,
help='step size for prining log info')
parser.add_argument('--vocab_path', type=str, default='./data/vocab.pkl',
help='path for saving vocabulary wrapper')
parser.add_argument('--hidden_size', type=int , default=128 ,
help='dimension of lstm hidden states')
parser.add_argument('--num_layers', type=int , default=1 ,
help='number of layers in lstm')
parser.add_argument('--num_epochs', type=int, default=10)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--num_workers', type=int, default=4)
parser.add_argument('--learning_rate', type=float, default=0.001)
args = parser.parse_args()
print(args)
main(args)