forked from btgraham/SparseConvNet-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcifar10.cpp
More file actions
74 lines (64 loc) · 2.27 KB
/
cifar10.cpp
File metadata and controls
74 lines (64 loc) · 2.27 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
#include "SparseConvNet.h"
#include "NetworkArchitectures.h"
#include "SpatiallySparseDatasetCIFAR10.h"
#include <string>
int epoch=0;
int batchSize=50;
Picture* OpenCVPicture::distort(RNG& rng, batchType type) {
OpenCVPicture* pic=new OpenCVPicture(*this);
if (type==TRAINBATCH) {
float
c00=1, c01=0, //2x2 identity matrix---starting point for calculating affine distortion matrix
c10=0, c11=1;
c00*=1+rng.uniform(-0.2,0.2); // x stretch
c11*=1+rng.uniform(-0.2,0.2); // y stretch
if (rng.randint(2)==0) c00*=-1; //Horizontal flip
int r=rng.randint(3);
float alpha=rng.uniform(-0.2,0.2);
if (r==0) matrixMul2x2inPlace(c00,c01,c10,c11,1,0,alpha,1); //Slant
if (r==1) matrixMul2x2inPlace(c00,c01,c10,c11,1,alpha,0,1); //Slant other way
if (r==2) matrixMul2x2inPlace(c00,c01,c10,c11,cos(alpha),-sin(alpha),sin(alpha),cos(alpha)); //Rotate
transformImage(pic->mat, backgroundColor, c00, c01, c10, c11);
pic->jiggle(rng,16);
}
return pic;
}
class CNN : public SparseConvNet {
public:
CNN (int dimension, int l, int k, ActivationFunction fn, int nInputFeatures, int nClasses, float p=0.0f, int nTop=1);
};
CNN::CNN
(int dimension, int l, int k, ActivationFunction fn,
int nInputFeatures, int nClasses, float p, int nTop)
: SparseConvNet(dimension,nInputFeatures, nClasses, nTop) {
for (int i=0;i<=l;i++) {
addLeNetLayerMP((i+1)*k,2,1,1,1,fn,p*i*1.0f/l);
addLeNetLayerMP((i+1)*k,2,1,(i<l)?3:1,(i<l)?2:1,fn,p*i*1.0f/l);
}
addSoftmaxLayer();
}
int main( int argc, char *argv[] ) {
int max_epoch=2;
std::string baseName="weights/cifar10";
SpatiallySparseDataset trainSet=Cifar10TrainSet();
SpatiallySparseDataset testSet=Cifar10TestSet();
trainSet.summary();
testSet.summary();
CNN cnn(2,5,32,VLEAKYRELU,trainSet.nFeatures,trainSet.nClasses,0.0f);
if (epoch>0)
cnn.loadWeights(baseName,epoch);
if ( argc>1 )
max_epoch = std::atoi( argv[1] );
/** FOR TESTING PURPOSES
if ( max_epoch>2 )
max_epoch = 2;
**/
for ( epoch=1; epoch<max_epoch; epoch++ ) {
std::cout <<"epoch: " << epoch << " " << std::flush;
cnn.processDataset( trainSet, batchSize, 0.003*exp(-0.005 * epoch) );
if (epoch%50==0) {
cnn.saveWeights(baseName,epoch);
cnn.processDataset(testSet, batchSize);
}
}
}