-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdemo_mnist.cpp
180 lines (148 loc) · 4.78 KB
/
demo_mnist.cpp
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
/*
* TinyTorch
* @author : keith@robot9.me
*
*/
#include "Torch.h"
using namespace TinyTorch;
// https://github.com/pytorch/examples/blob/main/mnist/main.py
class Net : public nn::Module {
public:
Net() { registerModules({conv1, conv2, dropout1, dropout2, fc1, fc2}); }
Tensor forward(Tensor &x) override {
x = conv1(x);
x = Function::relu(x);
x = conv2(x);
x = Function::relu(x);
x = Function::maxPool2d(x, 2);
x = dropout1(x);
x = Tensor::flatten(x, 1);
x = fc1(x);
x = Function::relu(x);
x = dropout2(x);
x = fc2(x);
x = Function::logSoftmax(x, 1);
return x;
}
private:
nn::Conv2D conv1{1, 32, 3, 1};
nn::Conv2D conv2{32, 64, 3, 1};
nn::Dropout dropout1{0.25};
nn::Dropout dropout2{0.5};
nn::Linear fc1{9216, 128};
nn::Linear fc2{128, 10};
};
// Training settings
struct TrainArgs {
// input batch size for training (default: 64)
int32_t batchSize = 64;
// input batch size for testing (default: 1000)
int32_t testBatchSize = 1000;
// number of epochs to train (default: 1)
int32_t epochs = 1;
// learning rate (default: 1.0)
float lr = 1.f;
// Learning rate step gamma (default: 0.7)
float gamma = 0.7f;
// disables CUDA training
bool noCuda = false;
// quickly check a single pass
bool dryRun = false;
// random seed (default: 1)
unsigned long seed = 1;
// how many batches to wait before logging training status
int32_t logInterval = 10;
// For Saving the current Model
bool saveModel = false;
};
void train(TrainArgs &args, nn::Module &model, Device device,
data::DataLoader &dataLoader, optim::Optimizer &optimizer,
int32_t epoch) {
model.train();
Timer timer;
timer.start();
for (auto [batchIdx, batch] : dataLoader) {
auto &data = batch[0].to(device);
auto &target = batch[1].to(device);
optimizer.zeroGrad();
auto output = model(data);
auto loss = Function::nllloss(output, target);
loss.backward();
optimizer.step();
if (batchIdx % args.logInterval == 0) {
timer.mark();
auto currDataCnt = batchIdx * dataLoader.batchSize();
auto totalDataCnt = dataLoader.dataset().size();
auto elapsed = (float)timer.elapseMillis() / 1000.f; // seconds
LOGD("Train Epoch: %d [%d/%d (%.0f%%)] Loss: %.6f, Elapsed: %.2fs", epoch,
currDataCnt, totalDataCnt, 100.f * currDataCnt / (float)totalDataCnt,
loss.item(), elapsed);
if (args.dryRun) {
break;
}
}
}
}
void test(nn::Module &model, Device device, data::DataLoader &dataLoader) {
model.eval();
Timer timer;
timer.start();
auto testLoss = 0.f;
auto correct = 0;
withNoGrad {
for (auto [batchIdx, batch] : dataLoader) {
auto &data = batch[0].to(device);
auto &target = batch[1].to(device);
auto output = model(data);
testLoss += Function::nllloss(output, target, SUM).item();
auto pred = output.data().argmax(1, true);
correct +=
(int32_t)(pred == target.data().view(pred.shape())).sum().item();
}
}
auto total = dataLoader.dataset().size();
testLoss /= (float)total;
timer.mark();
auto elapsed = (float)timer.elapseMillis() / 1000.f; // seconds
LOGD(
"Test set: Average loss: %.4f, Accuracy: %d/%d (%.0f%%), Elapsed: "
"%.2fs",
testLoss, correct, total, 100. * correct / (float)total, elapsed);
}
void demo_mnist() {
LOGD("demo_mnist ...");
Timer timer;
timer.start();
TrainArgs args;
manualSeed(args.seed);
auto useCuda = (!args.noCuda) && Tensor::deviceAvailable(Device::CUDA);
Device device = useCuda ? Device::CUDA : Device::CPU;
LOGD("Train with device: %s", useCuda ? "CUDA" : "CPU");
auto transform = std::make_shared<data::transforms::Compose>(
data::transforms::Normalize(0.1307f, 0.3081f));
auto dataDir = "./data/";
auto trainDataset = std::make_shared<data::DatasetMNIST>(
dataDir, data::DatasetMNIST::TRAIN, transform);
auto testDataset = std::make_shared<data::DatasetMNIST>(
dataDir, data::DatasetMNIST::TEST, transform);
if (trainDataset->size() == 0 || testDataset->size() == 0) {
LOGE("Dataset invalid.");
return;
}
auto trainDataloader = data::DataLoader(trainDataset, args.batchSize, true);
auto testDataloader = data::DataLoader(testDataset, args.testBatchSize, true);
auto model = Net();
model.to(device);
auto optimizer = optim::AdaDelta(model.parameters(), args.lr);
auto scheduler = optim::lr_scheduler::StepLR(optimizer, 1, args.gamma);
for (auto epoch = 1; epoch < args.epochs + 1; epoch++) {
train(args, model, device, trainDataloader, optimizer, epoch);
test(model, device, testDataloader);
scheduler.step();
}
if (args.saveModel) {
save(model, "mnist_cnn.model");
}
timer.mark();
LOGD("Total Time cost: %lld ms", timer.elapseMillis());
}