-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathn2d2.cpp
More file actions
executable file
·394 lines (340 loc) · 13.6 KB
/
Copy pathn2d2.cpp
File metadata and controls
executable file
·394 lines (340 loc) · 13.6 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
(C) Copyright 2013 CEA LIST. All Rights Reserved.
Contributor(s): Olivier BICHLER (olivier.bichler@cea.fr)
This software is governed by the CeCILL-C license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL-C
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL-C license and that you accept its terms.
*/
/** @file
* This program simulates the learning of static images with a convolutional NN.
* Do not forget to run ./tools/install/install_dataset.py to automatically download and
*install the stimuli databases.
*
* The 101_ObjectCategories database
* ---------------------------------
* ./n2d2 $N2D2_MODELS/caltech101-Scherer2010.ini 101_ObjectCategories -pc 30 -test-pc 0
*-learn 1000000 -log 10000
*
* The German Traffic Sign Recognition Benchmark (GTSRB)
* -----------------------------------------------------
* ./n2d2 $N2D2_MODELS/GTSRB-Ciresan2012.ini GTSRB -pc 0 -test-pc 0 -learn 1000000 -log
*10000
*
* MNIST
* -----
* ./n2d2 $N2D2_MODELS/mnist-29x29-6(5x5)-12(5x5)-100-10.ini mnist -learn 6000000 -log
*10000
* or
* ./n2d2 $N2D2_MODELS/mnist-Scherer2010.ini mnist -learn 6000000 -log 10000
*
* MNIST RBF
* ---------
* ./n2d2 $N2D2_MODELS/mnist-28x28-rbf.ini mnist -learn 6000000 -log 100000
*/
#include <future>
#include "N2D2.hpp"
#include "DeepNet.hpp"
#include "DeepNetQuantization.hpp"
#include "Quantizer/QAT/Optimization/DeepNetQAT.hpp"
#include "DrawNet.hpp"
#include "CEnvironment.hpp"
#include "Xnet/Environment.hpp"
#include "Histogram.hpp"
#include "Xnet/NodeEnv.hpp"
#include "RangeStats.hpp"
#include "ScalingMode.hpp"
#include "StimuliProvider.hpp"
#include "Activation/LogisticActivation.hpp"
#include "Cell/Cell_Frame_Top.hpp"
#include "Cell/SoftmaxCell.hpp"
#include "Cell/FcCell_Spike.hpp"
#include "Cell/NodeIn.hpp"
#include "Cell/NodeOut.hpp"
#include "Export/CellExport.hpp"
#include "Export/DeepNetExport.hpp"
#include "Export/StimuliProviderExport.hpp"
#include "Generator/DeepNetGenerator.hpp"
#include "Solver/SGDSolver.hpp"
#include "Target/TargetROIs.hpp"
#include "Target/TargetBBox.hpp"
#include "Target/TargetScore.hpp"
#include "Target/TargetMatching.hpp"
#include "Transformation/RangeAffineTransformation.hpp"
#include "utils/ProgramOptions.hpp"
#include "Adversarial.hpp"
#include "utils/Helper.hpp"
#ifdef CUDA
#include <cudnn.h>
#include "CudaContext.hpp"
#endif
using namespace N2D2;
using namespace N2D2_HELPER;
int main(int argc, char* argv[]) try
{
#if defined(__GNUC__) && !defined(NDEBUG) && defined(GPROF_INTERRUPT)
signal(SIGINT, sigUsr1Handler);
#endif
const Options opt(argc, argv);
#ifdef CUDA
CudaContext::setDevice(cudaDevice);
#endif
Network net(opt.seed);
std::shared_ptr<DeepNet> deepNet
= DeepNetGenerator::generate(net, opt.iniConfig);
deepNet->initialize();
if (opt.genConfig) {
deepNet->saveNetworkParameters();
std::exit(0);
}
#ifdef CUDA
#ifdef NVML
if (opt.banMultiDevice && opt.learnEpoch > 0)
deepNet->setBanAllowed(opt.banMultiDevice);
#endif
#endif
Database& database = *deepNet->getDatabase();
std::cout << "Learning database size: "
<< database.getNbStimuli(Database::Learn) << " images"
<< std::endl;
std::cout << "Validation database size: "
<< database.getNbStimuli(Database::Validation) << " images"
<< std::endl;
std::cout << "Testing database size: "
<< database.getNbStimuli(Database::Test) << " images"
<< std::endl;
// Network topology construction
SGDSolver::mMaxSteps = (opt.learnEpoch > 0)
? opt.learnEpoch * database.getNbStimuli(Database::Learn)
: opt.learn;
SGDSolver::mLogSteps = (opt.logEpoch > 0)
? opt.logEpoch * database.getNbStimuli(Database::Learn)
: opt.log;
if (opt.logDbStats) {
// Log stats
Utils::createDirectories("dbstats");
// Stimuli stats
database.logStats("dbstats/database-size.dat",
"dbstats/database-label.dat");
database.logStats("dbstats/learnset-size.dat",
"dbstats/learnset-label.dat", Database::LearnOnly);
database.logStats("dbstats/validationset-size.dat",
"dbstats/validationset-label.dat",
Database::ValidationOnly);
database.logStats("dbstats/testset-size.dat",
"dbstats/testset-label.dat", Database::TestOnly);
// ROIs stats
database.logROIsStats("dbstats/database-roi-size.dat",
"dbstats/database-roi-label.dat");
database.logROIsStats("dbstats/learnset-roi-size.dat",
"dbstats/learnset-roi-label.dat",
Database::LearnOnly);
database.logROIsStats("dbstats/validationset-roi-size.dat",
"dbstats/validationset-roi-label.dat",
Database::ValidationOnly);
database.logROIsStats("dbstats/testset-roi-size.dat",
"dbstats/testset-roi-label.dat",
Database::TestOnly);
// Multi-channels
if (!database.getParameter<std::string>("MultiChannelMatch").empty()) {
database.logMultiChannelStats("dbstats/database-multi-stats.dat");
database.logMultiChannelStats("dbstats/learnset-multi-stats.dat",
Database::LearnOnly);
database.logMultiChannelStats("dbstats/validationset-multi-stats.dat",
Database::ValidationOnly);
database.logMultiChannelStats("dbstats/testset-multi-stats.dat",
Database::TestOnly);
}
}
/**
* Historically N2D2 normalized integers stimuli in the [0.0;1.0] or [-1.0;1.0] range,
* depending on the signess, when loading integer stimuli inside a floating-point Tensor.
*
* Keep this implicit conversion for backward compatibility.
* TODO Make plans to eventually remove it.
*/
if(deepNet->getDatabase() != nullptr && !deepNet->getDatabase()->empty()) {
const int stimuliCvDepth = deepNet->getDatabase()->getStimuliDepth();
if(deepNet->getStimuliProvider()->normalizeIntegersStimuli(stimuliCvDepth)) {
std::cout << Utils::cnotice << "Notice: normalizing the stimuli in the [0;1] range."
<< Utils::cdef << std::endl;
}
}
if (!opt.saveTestSet.empty()) {
StimuliProvider& sp = *deepNet->getStimuliProvider();
CompositeTransformation trans;
trans.push_back(sp.getTransformation(Database::Test));
trans.push_back(sp.getOnTheFlyTransformation(Database::Test));
database.save(opt.saveTestSet, Database::TestOnly, trans);
}
if (!opt.load.empty())
deepNet->load(opt.load);
bool afterCalibration = false;
if (!opt.genExport.empty()) {
afterCalibration = generateExport(opt, deepNet);
if (!afterCalibration)
std::exit(0);
}
if(opt.calibOnly>0){
afterCalibration = calibNetwork(opt, deepNet);
if (!afterCalibration)
std::exit(0);
}
if (!afterCalibration) {
logStats(opt, deepNet);
}
if (opt.findLr > 0) {
findLearningRate(opt, deepNet);
std::exit(0);
}
if (opt.learnEpoch > 0) {
learn_epoch(opt, deepNet);
}
else if (opt.learn > 0) {
learn(opt, deepNet);
}
// Pruning testing section
if (!opt.pruningMethod.empty()) {
Pruning prune = Pruning(opt.pruningMethod);
// Options for pruning
std::vector<float> pruneOpt;
// Add threshold
pruneOpt.push_back(opt.pruningThreshold);
// Apply pruning algorithm to deepNet
prune.apply(deepNet, pruneOpt);
// Save network parameters
deepNet->exportNetworkFreeParameters("weights_pruned");
}
if (!afterCalibration) {
if (opt.learn > 0) {
// Reload best state after learning
if (database.getNbStimuli(Database::Validation) > 0)
deepNet->load("net_state_validation");
else
deepNet->load("net_state");
}
else if (opt.learnEpoch > 0)
{
if (database.getNbStimuli(Database::Validation) > 0){
deepNet->importNetworkFreeParameters("weights_validation", opt.ignoreNoExist);
}
else
deepNet->importNetworkFreeParameters("weights", opt.ignoreNoExist);
}
else if (opt.learnStdp == 0 && opt.load.empty() && opt.weights.empty())
{
if (database.getNbStimuli(Database::Validation) > 0){
if(!opt.pruningMethod.empty()){
deepNet->importNetworkFreeParameters("weights_pruned", opt.ignoreNoExist);
}
else{
deepNet->importNetworkFreeParameters("weights_validation", opt.ignoreNoExist);
}
}
else
deepNet->importNetworkFreeParameters("weights", opt.ignoreNoExist);
}
if (opt.fuse)
deepNet->fuseBatchNorm();
}
else if (opt.nbBits > 0) {
// afterCalibration means that we are trying to simulate export result.
// In this case, if nbBits > 0, set EstimatedLabelsValueDisplay to false
// for non-softmax, non-logistic targets
for (std::vector<std::shared_ptr<Target> >::const_iterator itTargets
= deepNet->getTargets().begin(),
itTargetsEnd = deepNet->getTargets().end();
itTargets != itTargetsEnd;
++itTargets)
{
std::shared_ptr<Cell> cell = (*itTargets)->getCell();
std::shared_ptr<Cell_Frame_Top> cellFrame
= std::dynamic_pointer_cast<Cell_Frame_Top>(cell);
if (cell->getType() != SoftmaxCell::Type
&& (!cellFrame || !cellFrame->getActivation()
|| cellFrame->getActivation()->getType()
!= std::string("Logistic")))
{
(*itTargets)->setParameter<bool>("EstimatedLabelsValueDisplay",
false);
}
}
}
if (opt.testIndex >= 0 || opt.testId >= 0) {
const int label = (opt.testId >= 0)
? database.getStimulusLabel(opt.testId)
: database.getStimulusLabel(Database::Test, opt.testIndex);
std::cout << "Pattern label ID = " << label << std::endl;
for (std::vector<std::shared_ptr<Target> >::const_iterator itTargets
= deepNet->getTargets().begin(),
itTargetsEnd = deepNet->getTargets().end();
itTargets != itTargetsEnd;
++itTargets) {
std::cout << "Output target = "
<< (*itTargets)->getLabelTarget(label) << std::endl;
}
}
try
{
std::shared_ptr<Cell_Frame_Top> cellFrame = deepNet->getTargetCell<Cell_Frame_Top>();
if (cellFrame && (opt.learn > 0 || opt.test || opt.learnEpoch > 0)) {
test(opt, deepNet, afterCalibration);
}
}
catch (const std::exception& e)
{
std::cout << "Error for testing: " << e.what() << std::endl;
std::cout << "Continue..." << std::endl;
}
std::shared_ptr<CEnvironment> cEnv = std::dynamic_pointer_cast
<CEnvironment>(deepNet->getStimuliProvider());
if (cEnv) {
testCStdp(opt, deepNet);
}
// Adversararial testing section
if (!opt.testAdv.empty()) {
std::shared_ptr<StimuliProvider> sp = deepNet->getStimuliProvider();
if (sp->getAdversarialAttack()->getAttackName() == Adversarial::Attack_T::None) {
std::stringstream msgStr;
msgStr << "Please precise the name of your attack "
<< "in the [sp.Adversarial] section of the ini file";
throw std::runtime_error(msgStr.str());
}
std::ostringstream dirName;
dirName << "testAdversarial";
Utils::createDirectories(dirName.str());
if (opt.testAdv == "Multi")
sp->getAdversarialAttack()->multiTestAdv(deepNet, dirName.str());
else if (opt.testAdv == "Solo")
sp->getAdversarialAttack()->singleTestAdv(deepNet, dirName.str());
else
throw std::runtime_error("Unknown adversarial option");
}
std::shared_ptr<Environment> env = std::dynamic_pointer_cast
<Environment>(deepNet->getStimuliProvider());
if (!env)
return 0;
// Spike-based testing
Monitor monitorEnv(net);
monitorEnv.add(env->getNodes());
Monitor& monitorOut =
*deepNet->getMonitor(deepNet->getTargetCell()->getName());
if (opt.learnStdp > 0) {
learnStdp(opt, deepNet, env, net, monitorEnv, monitorOut);
}
//testStdp(opt, deepNet, env, net, monitorEnv, monitorOut);
return 0;
}
catch (const std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
return 0;
}