-
Notifications
You must be signed in to change notification settings - Fork 160
/
main.py
286 lines (230 loc) · 9.77 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""""""""
Pytorch implementation of "A simple neural network module for relational reasoning
Code is based on pytorch/examples/mnist (https://github.com/pytorch/examples/tree/master/mnist)
"""""""""
from __future__ import print_function
import argparse
import os
#import cPickle as pickle
import pickle
import random
import numpy as np
import csv
import torch
from torch.utils.tensorboard import SummaryWriter
from torch.autograd import Variable
from model import RN, CNN_MLP
# Training settings
parser = argparse.ArgumentParser(description='PyTorch Relational-Network sort-of-CLVR Example')
parser.add_argument('--model', type=str, choices=['RN', 'CNN_MLP'], default='RN',
help='resume from model stored')
parser.add_argument('--batch-size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--epochs', type=int, default=20, metavar='N',
help='number of epochs to train (default: 20)')
parser.add_argument('--lr', type=float, default=0.0001, metavar='LR',
help='learning rate (default: 0.0001)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--resume', type=str,
help='resume from model stored')
parser.add_argument('--relation-type', type=str, default='binary',
help='what kind of relations to learn. options: binary, ternary (default: binary)')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
summary_writer = SummaryWriter()
if args.model=='CNN_MLP':
model = CNN_MLP(args)
else:
model = RN(args)
model_dirs = './model'
bs = args.batch_size
input_img = torch.FloatTensor(bs, 3, 75, 75)
input_qst = torch.FloatTensor(bs, 18)
label = torch.LongTensor(bs)
if args.cuda:
model.cuda()
input_img = input_img.cuda()
input_qst = input_qst.cuda()
label = label.cuda()
input_img = Variable(input_img)
input_qst = Variable(input_qst)
label = Variable(label)
def tensor_data(data, i):
img = torch.from_numpy(np.asarray(data[0][bs*i:bs*(i+1)]))
qst = torch.from_numpy(np.asarray(data[1][bs*i:bs*(i+1)]))
ans = torch.from_numpy(np.asarray(data[2][bs*i:bs*(i+1)]))
input_img.data.resize_(img.size()).copy_(img)
input_qst.data.resize_(qst.size()).copy_(qst)
label.data.resize_(ans.size()).copy_(ans)
def cvt_data_axis(data):
img = [e[0] for e in data]
qst = [e[1] for e in data]
ans = [e[2] for e in data]
return (img,qst,ans)
def train(epoch, ternary, rel, norel):
model.train()
if not len(rel[0]) == len(norel[0]):
print('Not equal length for relation dataset and non-relation dataset.')
return
random.shuffle(ternary)
random.shuffle(rel)
random.shuffle(norel)
ternary = cvt_data_axis(ternary)
rel = cvt_data_axis(rel)
norel = cvt_data_axis(norel)
acc_ternary = []
acc_rels = []
acc_norels = []
l_ternary = []
l_binary = []
l_unary = []
for batch_idx in range(len(rel[0]) // bs):
tensor_data(ternary, batch_idx)
accuracy_ternary, loss_ternary = model.train_(input_img, input_qst, label)
acc_ternary.append(accuracy_ternary.item())
l_ternary.append(loss_ternary.item())
tensor_data(rel, batch_idx)
accuracy_rel, loss_binary = model.train_(input_img, input_qst, label)
acc_rels.append(accuracy_rel.item())
l_binary.append(loss_binary.item())
tensor_data(norel, batch_idx)
accuracy_norel, loss_unary = model.train_(input_img, input_qst, label)
acc_norels.append(accuracy_norel.item())
l_unary.append(loss_unary.item())
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)] '
'Ternary accuracy: {:.0f}% | Relations accuracy: {:.0f}% | Non-relations accuracy: {:.0f}%'.format(
epoch,
batch_idx * bs * 2,
len(rel[0]) * 2,
100. * batch_idx * bs / len(rel[0]),
accuracy_ternary,
accuracy_rel,
accuracy_norel))
avg_acc_ternary = sum(acc_ternary) / len(acc_ternary)
avg_acc_binary = sum(acc_rels) / len(acc_rels)
avg_acc_unary = sum(acc_norels) / len(acc_norels)
summary_writer.add_scalars('Accuracy/train', {
'ternary': avg_acc_ternary,
'binary': avg_acc_binary,
'unary': avg_acc_unary
}, epoch)
avg_loss_ternary = sum(l_ternary) / len(l_ternary)
avg_loss_binary = sum(l_binary) / len(l_binary)
avg_loss_unary = sum(l_unary) / len(l_unary)
summary_writer.add_scalars('Loss/train', {
'ternary': avg_loss_ternary,
'binary': avg_loss_binary,
'unary': avg_loss_unary
}, epoch)
# return average accuracy
return avg_acc_ternary, avg_acc_binary, avg_acc_unary
def test(epoch, ternary, rel, norel):
model.eval()
if not len(rel[0]) == len(norel[0]):
print('Not equal length for relation dataset and non-relation dataset.')
return
ternary = cvt_data_axis(ternary)
rel = cvt_data_axis(rel)
norel = cvt_data_axis(norel)
accuracy_ternary = []
accuracy_rels = []
accuracy_norels = []
loss_ternary = []
loss_binary = []
loss_unary = []
for batch_idx in range(len(rel[0]) // bs):
tensor_data(ternary, batch_idx)
acc_ter, l_ter = model.test_(input_img, input_qst, label)
accuracy_ternary.append(acc_ter.item())
loss_ternary.append(l_ter.item())
tensor_data(rel, batch_idx)
acc_bin, l_bin = model.test_(input_img, input_qst, label)
accuracy_rels.append(acc_bin.item())
loss_binary.append(l_bin.item())
tensor_data(norel, batch_idx)
acc_un, l_un = model.test_(input_img, input_qst, label)
accuracy_norels.append(acc_un.item())
loss_unary.append(l_un.item())
accuracy_ternary = sum(accuracy_ternary) / len(accuracy_ternary)
accuracy_rel = sum(accuracy_rels) / len(accuracy_rels)
accuracy_norel = sum(accuracy_norels) / len(accuracy_norels)
print('\n Test set: Ternary accuracy: {:.0f}% Binary accuracy: {:.0f}% | Unary accuracy: {:.0f}%\n'.format(
accuracy_ternary, accuracy_rel, accuracy_norel))
summary_writer.add_scalars('Accuracy/test', {
'ternary': accuracy_ternary,
'binary': accuracy_rel,
'unary': accuracy_norel
}, epoch)
loss_ternary = sum(loss_ternary) / len(loss_ternary)
loss_binary = sum(loss_binary) / len(loss_binary)
loss_unary = sum(loss_unary) / len(loss_unary)
summary_writer.add_scalars('Loss/test', {
'ternary': loss_ternary,
'binary': loss_binary,
'unary': loss_unary
}, epoch)
return accuracy_ternary, accuracy_rel, accuracy_norel
def load_data():
print('loading data...')
dirs = './data'
filename = os.path.join(dirs,'sort-of-clevr.pickle')
with open(filename, 'rb') as f:
train_datasets, test_datasets = pickle.load(f)
ternary_train = []
ternary_test = []
rel_train = []
rel_test = []
norel_train = []
norel_test = []
print('processing data...')
for img, ternary, relations, norelations in train_datasets:
img = np.swapaxes(img, 0, 2)
for qst, ans in zip(ternary[0], ternary[1]):
ternary_train.append((img,qst,ans))
for qst,ans in zip(relations[0], relations[1]):
rel_train.append((img,qst,ans))
for qst,ans in zip(norelations[0], norelations[1]):
norel_train.append((img,qst,ans))
for img, ternary, relations, norelations in test_datasets:
img = np.swapaxes(img, 0, 2)
for qst, ans in zip(ternary[0], ternary[1]):
ternary_test.append((img, qst, ans))
for qst,ans in zip(relations[0], relations[1]):
rel_test.append((img,qst,ans))
for qst,ans in zip(norelations[0], norelations[1]):
norel_test.append((img,qst,ans))
return (ternary_train, ternary_test, rel_train, rel_test, norel_train, norel_test)
ternary_train, ternary_test, rel_train, rel_test, norel_train, norel_test = load_data()
try:
os.makedirs(model_dirs)
except:
print('directory {} already exists'.format(model_dirs))
if args.resume:
filename = os.path.join(model_dirs, args.resume)
if os.path.isfile(filename):
print('==> loading checkpoint {}'.format(filename))
checkpoint = torch.load(filename)
model.load_state_dict(checkpoint)
print('==> loaded checkpoint {}'.format(filename))
with open(f'./{args.model}_{args.seed}_log.csv', 'w') as log_file:
csv_writer = csv.writer(log_file, delimiter=',')
csv_writer.writerow(['epoch', 'train_acc_ternary', 'train_acc_rel',
'train_acc_norel', 'train_acc_ternary', 'test_acc_rel', 'test_acc_norel'])
print(f"Training {args.model} {f'({args.relation_type})' if args.model == 'RN' else ''} model...")
for epoch in range(1, args.epochs + 1):
train_acc_ternary, train_acc_binary, train_acc_unary = train(
epoch, ternary_train, rel_train, norel_train)
test_acc_ternary, test_acc_binary, test_acc_unary = test(
epoch, ternary_test, rel_test, norel_test)
csv_writer.writerow([epoch, train_acc_ternary, train_acc_binary,
train_acc_unary, test_acc_ternary, test_acc_binary, test_acc_unary])
model.save_model(epoch)