forked from addtt/multi-object-datasets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_count.py
151 lines (119 loc) · 4.12 KB
/
demo_count.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
import argparse
import os
import torch
import torch.nn.functional as F
from torch import nn
from torch.optim.adamax import Adamax
from multiobject.pytorch import MultiObjectDataLoader, MultiObjectDataset
epochs = 100
batch_size = 64
lr = 3e-4
dataset_filename = os.path.join(
'dsprites',
'multi_dsprites_color_012.npz')
# dataset_filename = os.path.join(
# 'binary_mnist',
# 'multi_binary_mnist_012.npz')
class SimpleBlock(nn.Module):
def __init__(self, ch, kernel, stride=1, dropout=0.25):
super().__init__()
assert kernel % 2 == 1
padding = (kernel - 1) // 2
self.net = nn.Sequential(
nn.Conv2d(ch, ch, kernel, padding=padding, stride=stride),
nn.Dropout2d(dropout),
nn.LeakyReLU(),
nn.BatchNorm2d(ch),
)
def forward(self, x):
return self.net(x)
class Model(nn.Module):
def __init__(self, color_channels, n_classes):
super().__init__()
self.convnet = nn.Sequential(
nn.Conv2d(color_channels, 64, 5, padding=2, stride=2),
nn.LeakyReLU(),
SimpleBlock(64, 3, stride=2),
SimpleBlock(64, 3, stride=2),
SimpleBlock(64, 3, stride=2),
nn.Conv2d(64, 64, 3, padding=1, stride=2),
)
self.fcnet = nn.Sequential(
nn.Linear(64, 64),
nn.LeakyReLU(),
nn.Linear(64, n_classes),
)
def forward(self, x):
x = self.convnet(x) # output is 2x2 for 64x64 images
x = x.sum((2, 3)) # sum over spatial dimensions
x = self.fcnet(x)
return x
def main():
args = parse_args()
path = os.path.join('generated', args.dataset_path)
# Datasets and dataloaders
print("loading dataset...")
train_set = MultiObjectDataset(path, train=True)
test_set = MultiObjectDataset(path, train=False)
train_loader = MultiObjectDataLoader(
train_set, batch_size=batch_size, shuffle=True, drop_last=True)
test_loader = MultiObjectDataLoader(test_set, batch_size=100)
# Model and optimizer
print("initializing model...")
channels = train_set.x.shape[1]
n_classes = 3 # hardcoded for dataset with 0 to 2 objects
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Model(channels, n_classes).to(device)
optimizer = Adamax(model.parameters(), lr=lr)
# Training loop
print("training starts")
step = 0
model.train()
for e in range(1, epochs + 1):
for x, labels in train_loader:
# Run model and compute loss
loss, acc = forward(model, x, labels, device)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
step += 1
if step % 100 == 0:
print("[{}] loss: {:.2g} acc: {:.2g}".format(
step, loss.item(), acc))
# Test
with torch.no_grad():
model.eval()
loss = acc = 0.
for x, labels in test_loader:
loss_, acc_ = forward(model, x, labels, device)
k = len(x) / len(test_set)
loss += loss_.item() * k
acc += acc_ * k
model.train()
print("TEST [epoch {}] loss: {:.2g} acc: {:.2g}".format(
e, loss, acc))
def forward(model, x, labels, device):
# Forward pass through model
n = labels['n_obj'].to(device)
x = x.to(device)
logits = model(x)
# Loss
loss = F.cross_entropy(logits, n)
# Accuracy
pred = logits.max(1)[1]
accuracy = (n == pred).float().mean().item()
return loss, accuracy
def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
allow_abbrev=False)
parser.add_argument('--dataset',
type=str,
default=dataset_filename,
metavar='PATH',
dest='dataset_path',
help="relative path of the dataset")
return parser.parse_args()
if __name__ == '__main__':
main()