-
Notifications
You must be signed in to change notification settings - Fork 10
/
4_mnist_mlp_da.cpp
143 lines (110 loc) · 3.68 KB
/
4_mnist_mlp_da.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
/*
* EDDL Library - European Distributed Deep Learning Library.
* Version: 0.7
* copyright (c) 2020, Universidad Politécnica de Valencia (UPV), PRHLT Research Centre
* Date: April 2020
* Author: PRHLT Research Centre, UPV, ([email protected]), ([email protected])
* All rights reserved
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "eddl/apis/eddl.h"
#include "eddl/serialization/onnx/eddl_onnx.h" // Not allowed
using namespace eddl;
//////////////////////////////////
// mnist_mlp_da.cpp:
// A very basic MLP for mnist
// Playing with Data Augmentation
// Using fit for training
//////////////////////////////////
int main(int argc, char **argv) {
// Download mnist
download_mnist();
// Settings
int epochs = 5;
int batch_size = 100;
int num_classes = 10;
// Define network
layer in = Input({784});
layer l = in; // Aux var
// Data augmentation assumes 3D tensors... images:
l=Reshape(l,{1,28,28});
// Data augmentation
l = RandomCropScale(l, {0.9f, 1.0f});
// Come back to 1D tensor for fully connected:
l=Reshape(l,{-1});
l = ReLu(GaussianNoise(BatchNormalization(Dense(l, 1024)),0.3));
l = ReLu(GaussianNoise(BatchNormalization(Dense(l, 1024)),0.3));
l = ReLu(GaussianNoise(BatchNormalization(Dense(l, 1024)),0.3));
//l = ReLu(Dense(l, 1024));
//l = ReLu(Dense(l, 1024));
layer out = Activation(Dense(l, num_classes), "softmax");
model net = Model({in}, {out});
net->verbosity_level = 0;
// dot from graphviz should be installed:
plot(net, "model.pdf");
// Build model
build(net,
sgd(0.01, 0.9), // Optimizer
{"soft_cross_entropy"}, // Losses
{"categorical_accuracy"}, // Metrics
CS_GPU({1}) // one GPU
//CS_GPU({1,1},100) // two GPU with weight sync every 100 batches
//CS_CPU()
//CS_FPGA({1})
);
// View model
summary(net);
setlogfile(net,"mnist_bn_da_lra");
// Load dataset
Tensor* x_train = Tensor::load("mnist_trX.bin");
Tensor* y_train = Tensor::load("mnist_trY.bin");
Tensor* x_test = Tensor::load("mnist_tsX.bin");
Tensor* y_test = Tensor::load("mnist_tsY.bin");
// Preprocessing
x_train->div_(255.0f);
x_test->div_(255.0f);
// Train model
fit(net, {x_train}, {y_train}, batch_size, epochs);
// Evaluate
printf("Evaluate:\n");
evaluate(net, {x_test}, {y_test});
// LR annealing
setlr(net,{0.005,0.9});
// Train model
fit(net, {x_train}, {y_train}, batch_size, epochs/2);
// Evaluate
printf("Evaluate:\n");
evaluate(net, {x_test}, {y_test});
// LR annealing
setlr(net,{0.001,0.9});
// Train model
fit(net, {x_train}, {y_train}, batch_size, epochs/2);
// Evaluate
printf("Evaluate:\n");
evaluate(net, {x_test}, {y_test});
// LR annealing
setlr(net,{0.0001,0.9});
// Train model
fit(net, {x_train}, {y_train}, batch_size, epochs/4);
// Evaluate
printf("Evaluate:\n");
evaluate(net, {x_test}, {y_test});
// Export
string path("trained_model.onnx");
save_net_to_onnx_file(net, path);
cout << "Saved net to onnx file" << endl;
// Import
Net* imported_net = import_net_from_onnx_file(path, DEV_CPU);
build(imported_net,
sgd(0.001, 0.9), // Optimizer
{"soft_cross_entropy"}, // Losses
{"categorical_accuracy"}, // Metrics
CS_GPU({1}, "low_mem"), // one GPU
//CS_CPU(), // CPU with maximum threads availables
false // Parameter that indicates that the weights of the net must be initialized to random values.
);
cout << "Evaluating with imported net" << endl;
evaluate(imported_net, {x_test}, {y_test});
}