-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainv5.cpp
More file actions
203 lines (179 loc) · 7.04 KB
/
Copy pathmainv5.cpp
File metadata and controls
203 lines (179 loc) · 7.04 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "DeepLearning.h"
// Setting Epochs, LearningRate and so on
bool quiet = false;
bool debugMode = true;
double LearningRate = 0.001;
double LearningRatio = 0.95;
int LearningChange = 1;
// we use adam to fasten training
// will use corrected error
bool adam = true;
double momCoff = 0.9;
double rmsCoff = 0.99;
double minStep = eps;
double maxStep = 1e5;
double regular = 1e-4;
int TrainData = 4000;
int TestData = 320;
int inputDepth = 1;
int inputWidth = 256;
int inputHeight = 256;
// number of choice each task
int outputWidth = 10;
// A epoch means running through whole training set
int maxEpoch = 10;
// Because of multiple thread, please go to DeepLearning.h
// to change batchSize
// int batchSize = 60;
// To decide how many iter between show changes
// must be no bigger than maxIter
int show = 1;
int type = SR;
double pic[maxWidth][maxHeight];
int label[maxNumLabel];
FILE* fpDebug = NULL;
FILE* fpResult = NULL;
ifstream* fpTrain = NULL;
ifstream* fpTest = NULL;
ifstream* fpTrainLabel = NULL;
ifstream* fpTestLabel = NULL;
vector<int> ran;
thread thr[batchSize];
const char strShow[2] = "+";
int main(){
readData(type);
InputLayer inp(fpTrain,fpTest,TrainData,TestData,1,inputHeight,inputWidth);
ConvLayer conv(inputDepth, inputHeight, inputWidth, inputDepth, 9, 9, 64, 1, 4);
ThirdReLULayer thiRel(64, inputHeight, inputWidth, 0);
ConvLayer conv1(64, inputHeight, inputWidth, 64, 1, 1, 32, 1, 0);
ThirdReLULayer thiRel1(32, inputHeight, inputWidth, 0);
ConvLayer conv2(32, inputHeight, inputWidth, 32, 5, 5, inputDepth, 1, 2);
ThirdReLULayer thiRel2(inputDepth, inputHeight, inputWidth, 1);
OutputPictureLayer out(fpTrainLabel,fpTestLabel,TrainData,TestData,inputDepth,inputHeight,inputWidth);
if(!quiet){
printf("Training...\n");
fprintf(fpResult, "Training...\n");
fflush(fpResult);
}
int tar = 0;
clock_t begin = clock();
double iterTime = 0;
double epochTime = 0;
for(int k = 0;k < maxEpoch;k++){
// Training
if(!quiet){
if(!quiet) printf("Epochs %d : ",k);
if(debugMode) printf("\n");
if(!quiet) fprintf(fpResult,"Epochs %d : ",k);
if(debugMode) fprintf(fpResult,"\n");
fflush(stdout);
}
DecreaseLearningRate(k);
inp.InitShuffle();
// Number of iters in an epoch
int maxIter = TrainData / batchSize;
double oldTotalLoss = 0;
double oldTotalRMSE = 0;
for(int i = 0;i < maxIter;i++){
if((!debugMode) && (!quiet) && (i % show == 0) && (i != 0)){
printf("%s",strShow);
fflush(stdout);
}
tar = i * batchSize;
inp.forward(tar);
conv.forward(inp.output);
thiRel.forward(conv.output);
conv1.forward(thiRel.output);
thiRel1.forward(conv1.output);
conv2.forward(thiRel1.output);
thiRel2.forward(conv2.output);
out.forward(tar,thiRel2.output);
out.backward(tar);
thiRel2.backward(out.error);
conv2.backward(thiRel2.error);
thiRel1.backward(conv2.error);
conv1.backward(thiRel1.error);
thiRel.backward(conv1.error);
conv.backward(thiRel.error);
if(debugMode && (i % show == (show - 1))){
double showLoss = (out.totalTrainLoss - oldTotalLoss) / show;
double showRMSE = (out.totalTrainRMSE - oldTotalRMSE) / show;
clock_t iter = clock();
double newIter = (double)(iter - begin) / CLOCKS_PER_SEC;
double usedTime = newIter - iterTime;
printf("in iteration %d: ",i);
printf("Training Loss = %llf\t",showLoss);
printf("Training RMSE = %llf\t",showRMSE);
printf("Used Time = %llf\n", usedTime);
fprintf(fpResult,"in iteration %d: ",i);
fprintf(fpResult,"Training Loss = %llf\t",showLoss);
fprintf(fpResult,"Training RMSE = %llf\t",showRMSE);
fprintf(fpResult,"Used Time = %llf\n", usedTime);
fprintf(fpDebug,"%llf, %llf, %llf; ", showLoss, showRMSE, usedTime);
fflush(fpDebug);
fflush(fpResult);
oldTotalLoss = out.totalTrainLoss;
oldTotalRMSE = out.totalTrainRMSE;
iterTime = newIter;
}
}
// Testing
int testIter = TestData / batchSize;
if(debugMode){
printf("Testing : ");
fflush(stdout);
}
for(int i = 0;i < testIter;i++){
if(i * batchSize == TestData){
break;
}
if((!quiet) && (i % show == 0) && (i != 0)){
printf("%s",strShow);
fflush(stdout);
}
tar = i * batchSize;
inp.StartTest(tar);
conv.forward(inp.output);
thiRel.forward(conv.output);
conv1.forward(thiRel.output);
thiRel1.forward(conv1.output);
conv2.forward(thiRel1.output);
thiRel2.forward(conv2.output);
out.TestForward(tar,thiRel2.output);
}
out.ComputeTestLoss();
if(!quiet){
clock_t epoch = clock();
double newEpoch = (double)(epoch - begin) / CLOCKS_PER_SEC;
double usedTime = newEpoch - epochTime;
printf("\nTraining Loss : %llf\n",out.totalTrainLoss / maxIter);
fprintf(fpResult, "Training Loss : %llf\n",out.totalTrainLoss / maxIter);
printf("Test Loss : %llf\n",out.totalTestLoss / testIter);
fprintf(fpResult, "Test Loss : %llf\n",out.totalTestLoss / testIter);
printf("Training RMSE : %llf\n",out.totalTrainRMSE / maxIter);
fprintf(fpResult, "Training RMSE : %llf\n",out.totalTrainRMSE / maxIter);
printf("Test RMSE : %llf\n",out.totalTestRMSE / testIter);
fprintf(fpResult, "Test RMSE : %llf\n",out.totalTestRMSE / testIter);
printf("Used Time = %llf\n", usedTime);
fprintf(fpResult, "Used Time = %llf\n", usedTime);
fflush(fpResult);
epochTime = newEpoch;
}
iterTime = epochTime;
out.totalTrainLoss = 0;
out.totalTrainRMSE = 0;
}
// Output valid data
FILE* valid = NULL;
fopen_s(&valid, "./sr/validResult/validResult.txt","w");
if(valid == NULL){
printf("Error: Unable to open valid file\n");
fflush(stdout);
exit(0);
}
out.testPredict[0].PrintMatlabArray(valid, string("boat"));
out.testPredict[1].PrintMatlabArray(valid, string("cameraman"));
out.testPredict[2].PrintMatlabArray(valid, string("fruits"));
out.testPredict[3].PrintMatlabArray(valid, string("testSet"));
return 0;
}