-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
158 lines (137 loc) · 5.92 KB
/
main.py
File metadata and controls
158 lines (137 loc) · 5.92 KB
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
import argparse
import os
import cupy as cpy
from cross_entropy_loss import CrossEntropyLoss
from optimizer import Adam
from vit import ViT
import tqdm
cpy.cuda.Device(0).use()
class ViTNumPy:
"""VIT implementation Wrapper."""
def __init__(self, path_to_mnist: str, batch_size: int, epochs: int, test_epoch_interval: int,
hidden_dimension: int, heads: int, layers: int, output: int) -> None:
"""Initialize.
Args:
path_to_mnist: path to folder containing mnist.
batch_size: batch size.
epochs: number of epochs.
test_epoch_interval: test epoch run interval.
"""
self.path_to_mnist = path_to_mnist
self.batch_size = batch_size
self.epochs = epochs
self.test_epoch_interval = test_epoch_interval
self.hidden_dimension = hidden_dimension
self.heads = heads
self.layers = layers
self.output = output
self.load_dataset_from_file(path_to_mnist)
def datafeeder(self, x: cpy.ndarray, y: cpy.ndarray, shuffle:bool = False):
"""Datafeeder for train test.
Args:
x: input images.
y: label.
shuffle: shuffle data.
Yields:
a batch of data
"""
if shuffle:
randomize = cpy.arange(len(y))
cpy.random.shuffle(randomize)
x = x[:,randomize]
y = y[randomize]
for i in range(0, len(y), self.batch_size):
yield x[:, i : i + self.batch_size], y[i : i + self.batch_size]
def load_dataset_from_file(self, path_to_mnist: str) -> None:
"""Load dataset from file.
Args:
path_to_mnist: path to folder containing mnist.
"""
with open(os.path.join(path_to_mnist, "mnist_train.npy"), "rb") as f:
self.x_train = cpy.load(f)
self.y_train = cpy.load(f)
with open(os.path.join(path_to_mnist, "mnist_test.npy"), "rb") as f:
self.x_test = cpy.load(f)
self.y_test = cpy.load(f)
def train_iter(self) -> None:
"""Train model for one epoch."""
dataloader = self.datafeeder(self.x_train, self.y_train, True)
train_error = []
total_len = len(self.y_train)//self.batch_size
for batch in tqdm.tqdm(dataloader, total = total_len):
x, y = batch
x = x.transpose(1, 0)
x = x.reshape(self.batch_size, 1, 28, 28)
y_hat = self.model.forward(x)
loss = self.loss_function.forward(y_hat, y)
error = self.loss_function.backward()
self.model.backward(error)
self.model.update_weights()
train_error.append(loss)
print("Train-error:"+str(cpy.mean(cpy.asarray(train_error))))
def test_iter(self) -> None:
"""Test model."""
test_dataloader = self.datafeeder(self.x_test, self.y_test)
test_error = []
epoch_tp = 0
epoch_total = 0
total_len = len(self.y_test)//self.batch_size
for batch in tqdm.tqdm(test_dataloader, total = total_len):
x, y = batch
x = x.transpose(1, 0)
batch_size = x.shape[0]
x = x.reshape(batch_size, 1, 28, 28)
y_hat = self.model.forward(x)
loss = self.loss_function.forward(y_hat, y)
y_pred = cpy.argmax(y_hat, axis=-1)
correct = cpy.sum(y_pred == y)
total = cpy.size(y)
epoch_tp += correct
epoch_total += total
test_error.append(loss)
print("test error", cpy.mean(cpy.asarray(test_error)))
print("test acc", epoch_tp / epoch_total)
def train_model(self) -> None:
"""Train model."""
self.model = ViT(chw=(1, 28, 28), n_patches=7, hidden_d=self.hidden_dimension,
n_heads=self.heads, num_blocks=self.layers, out_classses=self.output)
self.loss_function = CrossEntropyLoss()
self.optimizer = Adam() # SGD()
self.model.set_optimizer(self.optimizer)
for epoch in range(self.epochs):
self.train_iter()
if epoch % self.test_epoch_interval == 0:
self.test_iter()
self.model.save_weights("vit_weights.npy")
def load_and_test_model(self, weights_file_path="vit_weights.npy") -> None:
"""Load model weights and validate"""
self.model = ViT(chw=(1, 28, 28), n_patches=7, hidden_d=self.hidden_dimension,
n_heads=self.heads, num_blocks=self.layers, out_classses=self.output)
self.loss_function = CrossEntropyLoss()
self.model.load_weights(weights_file_path)
self.test_iter()
def parse_args():
"""Parse the arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--path_to_mnist",
dest="path_to_mnist",
required=True,
)
parser.add_argument('--hidden_dimension', required=False, default= 256)
parser.add_argument('--heads', required=False, default= 8)
parser.add_argument('--layers', required=False, default= 6)
parser.add_argument('--output', required=False, default=10)
parser.add_argument("--batch_size", dest="batch_size", required=False, default=32)
parser.add_argument("--epochs", dest="epochs", required=False, default=10)
parser.add_argument("--test_epoch_interval", dest="test_epoch_interval", required=False, default=2)
args = parser.parse_args()
return (args.path_to_mnist, args.batch_size, args.epochs, args.test_epoch_interval,
args.hidden_dimension, args.heads, args.layers, args.output)
if __name__ == "__main__":
path_to_mnist, batch_size, epochs, test_epoch_interval, \
hidden_dimension, heads, layers, output = parse_args()
vit_mnist = ViTNumPy(path_to_mnist, batch_size, epochs, test_epoch_interval,
hidden_dimension, heads, layers, output)
#vit_mnist.train_model()
vit_mnist.load_and_test_model()