forked from btgraham/SparseConvNet-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
casia.cpp
64 lines (57 loc) · 2 KB
/
casia.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
#include "SparseConvNet.h"
#include "NetworkArchitectures.h"
#include "SpatiallySparseDatasetCasiaOLHWDB.h"
#include "OnlineHandwritingPicture.h"
int epoch = 0;
int cudaDevice = -1; // PCI bus ID, -1 for default GPU
int batchSize = 100;
Picture *OnlineHandwritingPicture::distort(RNG &rng, batchType type) {
OnlineHandwritingPicture *pic = new OnlineHandwritingPicture(*this);
if (type == TRAINBATCH) {
arma::mat aff = arma::eye(2, 2);
aff(0, 0) += rng.uniform(-0.3, 0.3); // x stretch
aff(1, 1) += rng.uniform(-0.3, 0.3); // y stretch
int r = rng.randint(3);
float alpha = rng.uniform(-0.3, 0.3);
arma::mat x = arma::eye(2, 2);
if (r == 0)
x(0, 1) = alpha;
if (r == 1)
x(1, 0) = alpha;
if (r == 2) {
x(0, 0) = cos(alpha);
x(0, 1) = -sin(alpha);
x(1, 0) = sin(alpha);
x(1, 1) = cos(alpha);
}
aff = aff * x;
arma::mat y(1, 2);
y(0, 0) = renderSize * rng.uniform(-0.1875, 0.1875);
y(0, 1) = renderSize * rng.uniform(-0.1875, 0.1875);
for (unsigned int i = 0; i < ops.size(); ++i)
pic->ops[i] = pic->ops[i] * aff + arma::repmat(y, pic->ops[i].n_rows, 1);
pic->offset3d = rng.uniform(-0.1, 0.1);
}
return pic;
}
int main() {
std::string baseName = "weights/casia";
SpatiallySparseDataset trainSet = CasiaOLHWDB11TrainSet(64, Octogram);
SpatiallySparseDataset testSet = CasiaOLHWDB11TestSet(64, Octogram);
trainSet.summary();
testSet.summary();
DeepC3C3Valid cnn(2, 5, 96, VLEAKYRELU, trainSet.nFeatures, trainSet.nClasses,
0.0f, cudaDevice);
cnn.calculateInputRegularizingConstants(trainSet);
if (epoch > 0)
cnn.loadWeights(baseName, epoch / 100);
for (epoch++;; epoch++) {
std::cout << "mini-epoch:" << epoch << ": " << std::flush;
auto trainSubset = trainSet.subset(50000);
cnn.processDataset(trainSubset, batchSize, 0.002 * exp(-epoch * 0.0025));
if (epoch % 18 == 0) {
cnn.processDataset(testSet, batchSize);
cnn.saveWeights(baseName, epoch / 100);
}
}
}