-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·179 lines (141 loc) · 5.92 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
"""""""""
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 torch
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')
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)
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, 11)
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, 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(rel)
random.shuffle(norel)
rel = cvt_data_axis(rel)
norel = cvt_data_axis(norel)
for batch_idx in range(len(rel[0]) // bs):
tensor_data(rel, batch_idx)
accuracy_rel = model.train_(input_img, input_qst, label)
tensor_data(norel, batch_idx)
accuracy_norel = model.train_(input_img, input_qst, label)
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.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_rel, accuracy_norel))
def test(epoch, rel, norel):
model.eval()
if not len(rel[0]) == len(norel[0]):
print('Not equal length for relation dataset and non-relation dataset.')
return
rel = cvt_data_axis(rel)
norel = cvt_data_axis(norel)
accuracy_rels = []
accuracy_norels = []
for batch_idx in range(len(rel[0]) // bs):
tensor_data(rel, batch_idx)
accuracy_rels.append(model.test_(input_img, input_qst, label))
tensor_data(norel, batch_idx)
accuracy_norels.append(model.test_(input_img, input_qst, label))
accuracy_rel = sum(accuracy_rels) / len(accuracy_rels)
accuracy_norel = sum(accuracy_norels) / len(accuracy_norels)
print('\n Test set: Relation accuracy: {:.0f}% | Non-relation accuracy: {:.0f}%\n'.format(
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)
rel_train = []
rel_test = []
norel_train = []
norel_test = []
print('processing data...')
for img, relations, norelations in train_datasets:
img = np.swapaxes(img,0,2)
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, relations, norelations in test_datasets:
img = np.swapaxes(img,0,2)
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 (rel_train, rel_test, norel_train, norel_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))
for epoch in range(1, args.epochs + 1):
train(epoch, rel_train, norel_train)
test(epoch, rel_test, norel_test)
model.save_model(epoch)