-
Notifications
You must be signed in to change notification settings - Fork 0
/
torch_dataloader_example.py
145 lines (125 loc) · 5.31 KB
/
torch_dataloader_example.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
import torch
import numpy as np
import torch.nn.functional as functional
import cv2
from torch.utils.data import DataLoader, Dataset, TensorDataset
from torch.autograd import Variable
from mnist_read import read_mnist
import matplotlib.pyplot as plt
batch_size = 3000
class TrainDataset(Dataset):
def __init__(self, mnist_data):
self.train_data = np.uint8(mnist_data.train_data)
self.train_label = np.reshape(mnist_data.train_label, [len(mnist_data.train_label), ])
pass
def __getitem__(self, item):
out_data = np.reshape(self.train_data[item, :], [28, 28])
ret, out_data = cv2.threshold(out_data, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
out_data = cv2.bitwise_not(out_data)
out_data = np.array([out_data / 255.0])
out_label = np.array(self.train_label[item])
return Variable(torch.from_numpy(np.float32(out_data))), Variable(torch.from_numpy(out_label))
def __len__(self):
return self.train_data.shape[0]
class TestDataset(Dataset):
def __init__(self, mnist_data):
self.test_data = np.uint8(mnist_data.test_data)
self.test_label = np.reshape(mnist_data.test_label, [len(mnist_data.test_label), ])
def __getitem__(self, item):
out_data = np.reshape(self.test_data[item, :], [28, 28])
ret, out_data = cv2.threshold(out_data, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
out_data = cv2.bitwise_not(out_data)
out_data = np.array([out_data / 255.0])
out_label = np.array(self.test_label[item])
return Variable(torch.from_numpy(np.float32(out_data))), Variable(torch.from_numpy(out_label))
def __len__(self):
return self.test_data.shape[0]
class KaggleCNN(torch.nn.Module):
def __init__(self):
super(KaggleCNN, self).__init__()
self.conv_l1 = torch.nn.Sequential(
# output: 64 * 28 *28
torch.nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, padding=2),
torch.nn.BatchNorm2d(num_features=16),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=16, out_channels=16, kernel_size=5, padding=2),
torch.nn.BatchNorm2d(num_features=16),
torch.nn.ReLU(),
# output: 64 * 14 * 14
torch.nn.MaxPool2d(kernel_size=2)
)
self.conv_l2 = torch.nn.Sequential(
# output: 128 * 14 * 14
torch.nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, padding=2),
torch.nn.BatchNorm2d(num_features=32),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=2),
torch.nn.BatchNorm2d(num_features=32),
torch.nn.ReLU(),
# output: 128 * 7 * 7
torch.nn.MaxPool2d(kernel_size=2)
)
self.linear_l1 = torch.nn.Linear(32 * 7 * 7, 10)
self.log_softmax = torch.nn.LogSoftmax(dim=1)
def forward(self, x):
x = self.conv_l1(x)
x = self.conv_l2(x)
x = x.view(x.size(0), -1)
x = self.linear_l1(x)
x = self.log_softmax(x)
return x
def build_first_nn(device):
net = KaggleCNN().to(device)
return net
def main():
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
mnist_data = read_mnist("data")
train_dataset = TrainDataset(mnist_data)
train_dataloader = DataLoader(dataset=train_dataset, batch_size=3000, shuffle=True)
test_dataset = TestDataset(mnist_data)
test_dataloader = DataLoader(dataset=test_dataset, batch_size=3000, shuffle=True)
net = build_first_nn(device)
optimizer = torch.optim.Adam(net.parameters(), lr=0.01)
print(net)
loss_record = []
early_stop_count = 0
for i in range(100000):
net.train()
loss_val = 0
for j, data in enumerate(train_dataloader):
train_x, train_y = data
optimizer.zero_grad()
output = net(train_x.to(device))
loss = functional.nll_loss(output, train_y.to(device))
loss_val += loss.data.cpu()
# print("{}:{}:{}".format(i, j, loss.data.cpu()))
loss.backward()
optimizer.step()
print("{}:{}".format(i, loss_val))
if i > 10:
loss_record.append(loss_val)
if len(loss_record) > 10:
del (loss_record[0])
if loss_val > (sum(loss_record) / len(loss_record)):
early_stop_count += 1
print("Early stop ready: count=%d, current_loss=%.8f, average=%.8f" %
(early_stop_count, loss_val, (sum(loss_record) / len(loss_record))))
if early_stop_count > 1 and loss_val < (sum(loss_record) / len(loss_record)):
print("Early stop done: current_loss=%.8f, average=%.8f" %
(loss_val, (sum(loss_record) / len(loss_record))))
break
net.eval()
correct = 0
total = 0
for i, data in enumerate(test_dataloader):
test_x, test_y = data
tensor_x = test_x.to(device)
predict_y = net(tensor_x).cpu().detach().argmax(dim=1)
correct += predict_y.eq(test_y.view_as(predict_y)).sum().item()
total += test_y.shape[0]
print("Testing {}/{}".format(correct, total))
if __name__ == "__main__":
main()