1-
2- #ifndef WIN32_LEAN_AND_MEAN
3- #define WIN32_LEAN_AND_MEAN
4- #endif
5- #include < windows.h>
6- #include < psapi.h>
7- #pragma comment(lib, "psapi.lib")
8- #include < crtdbg.h>
9- #include < algorithm>
1+ #include < algorithm>
102#include < chrono>
113#include < cmath>
124#include < filesystem>
179
1810#include " build.hpp"
1911
20- class MemoryLogger {
21- private:
22- std::chrono::steady_clock::time_point start_time;
23- size_t peak_memory = 0 ;
24- size_t initial_memory = 0 ;
25-
26- size_t getProcessMemory () {
27- HANDLE hProcess = GetCurrentProcess ();
28- PROCESS_MEMORY_COUNTERS pmc;
29- pmc.cb = sizeof (PROCESS_MEMORY_COUNTERS );
30-
31- if (GetProcessMemoryInfo (hProcess, &pmc, sizeof (pmc))) {
32- return pmc.WorkingSetSize / (1024 * 1024 );
33- }
34- return 0 ;
35- }
36-
37- public:
38- MemoryLogger () {
39- start_time = std::chrono::steady_clock::now ();
40- initial_memory = getProcessMemory ();
41- log (" START" );
42- }
43-
44- void log (const char * stage) {
45- auto now = std::chrono::steady_clock::now ();
46- auto elapsed =
47- std::chrono::duration_cast<std::chrono::seconds>(now - start_time)
48- .count ();
49-
50- size_t current = getProcessMemory ();
51- if (current > peak_memory) peak_memory = current;
52-
53- std::cout << " [" << std::setw (4 ) << elapsed << " s] " << std::setw (30 )
54- << stage << " | "
55- << " PROCESS MEM: " << std::setw (6 ) << current << " MB"
56- << " (PEAK: " << std::setw (6 ) << peak_memory << " MB)"
57- << " (DELTA: " << std::setw (4 ) << (current - initial_memory)
58- << " MB)\n " ;
59- }
60-
61- ~MemoryLogger () {
62- log (" END" );
63- std::cout << " ====================================\n " ;
64- std::cout << " PEAK PROCESS MEMORY: " << peak_memory << " MB\n " ;
65- std::cout << " INITIAL PROCESS MEMORY: " << initial_memory << " MB\n " ;
66- std::cout << " FINAL PROCESS MEMORY: " << getProcessMemory () << " MB\n " ;
67- if (getProcessMemory () > initial_memory + 10 ) {
68- std::cout << " WARNING: Process memory growth! (+"
69- << (getProcessMemory () - initial_memory) << " MB)\n " ;
70- } else {
71- std::cout << " OK: No significant process memory growth\n " ;
72- }
73- }
74- };
75-
76- MemoryLogger g_memLogger;
77-
78- #define LOG_MEM (stage ) g_memLogger.log(stage)
79-
8012namespace fs = std::filesystem;
8113using namespace it_lab_ai ;
8214
8315int main (int argc, char * argv[]) {
84- LOG_MEM (" Program start" );
85-
8616 std::string model_name = " alexnet_mnist" ;
8717 RuntimeOptions options;
8818 size_t num_photo = 1000 ;
@@ -139,8 +69,6 @@ int main(int argc, char* argv[]) {
13969 }
14070 }
14171
142- LOG_MEM (" After args parsing" );
143-
14472 std::string dataset_path;
14573 if (model_name == " alexnet_mnist" ) {
14674 dataset_path = MNIST_PATH ;
@@ -154,8 +82,6 @@ int main(int argc, char* argv[]) {
15482 std::cout << ' \n ' ;
15583
15684 if (model_name == " alexnet_mnist" ) {
157- LOG_MEM (" MNIST start" );
158-
15985 std::vector<size_t > counts = {979 , 1134 , 1031 , 1009 , 981 ,
16086 891 , 957 , 1027 , 973 , 1008 };
16187 int stat = 0 ;
@@ -189,7 +115,7 @@ int main(int argc, char* argv[]) {
189115 for (int j = 0 ; j < 28 ; ++j) {
190116 size_t a = ind;
191117 for (size_t n = 0 ; n < name; n++) a += counts[n] + 1 ;
192- res[(a)* 28 * 28 + i * 28 + j] = channels[0 ].at <uchar>(j, i);
118+ res[(a) * 28 * 28 + i * 28 + j] = channels[0 ].at <uchar>(j, i);
193119 }
194120 }
195121 }
@@ -223,20 +149,15 @@ int main(int argc, char* argv[]) {
223149 (static_cast <double >(stat) / static_cast <double >(sum + 10 )) * 100 ;
224150 std::cout << " Stat: " << std::fixed << std::setprecision (2 ) << percentage
225151 << " %" << ' \n ' ;
226-
227- LOG_MEM (" MNIST end" );
228152 return 0 ;
229153 }
230154
231- LOG_MEM (" ImageNet start" );
232-
233155 std::vector<size_t > counts (1000 , 0 );
234156 std::vector<std::string> image_paths;
235157 std::vector<int > true_labels;
236158 std::vector<float > all_image_data;
237159 size_t total_images = 0 ;
238160
239- LOG_MEM (" Counting classes" );
240161 for (int class_id = 0 ; class_id < 1000 ; ++class_id) {
241162 std::ostringstream folder_oss;
242163 folder_oss << std::setw (5 ) << std::setfill (' 0' ) << class_id;
@@ -262,14 +183,12 @@ int main(int argc, char* argv[]) {
262183 size_t image_size = channels * height * width;
263184 size_t output_classes = 1000 ;
264185
265- LOG_MEM (" Reserving memory" );
266186 all_image_data.reserve (num_photo * image_size);
267187 image_paths.reserve (num_photo);
268188 true_labels.reserve (num_photo);
269189
270190 total_images = 0 ;
271191
272- LOG_MEM (" Loading images start" );
273192 for (int class_id = 0 ; class_id < 1000 ; ++class_id) {
274193 size_t need_from_class = images_per_class_base;
275194 if (remaining > 0 ) {
@@ -317,16 +236,8 @@ int main(int argc, char* argv[]) {
317236 std::cout << " Warning: Class " << class_id << " has only " << taken
318237 << " images (needed " << need_from_class << " )" << ' \n ' ;
319238 }
320-
321- if (class_id % 100 == 0 && class_id > 0 ) {
322- char buf[50 ];
323- sprintf (buf, " Class %d" , class_id);
324- LOG_MEM (buf);
325- }
326239 }
327240
328- LOG_MEM (" Images loaded" );
329-
330241 if (total_images != num_photo) {
331242 std::cout << " Warning: Requested " << num_photo << " images but loaded "
332243 << total_images << " due to insufficient data" << ' \n ' ;
@@ -336,8 +247,6 @@ int main(int argc, char* argv[]) {
336247 int correct_predictions_top1 = 0 ;
337248 int correct_predictions_top5 = 0 ;
338249
339- LOG_MEM (" Building master graph" );
340-
341250 it_lab_ai::Shape full_shape ({num_photo, static_cast <size_t >(channels),
342251 static_cast <size_t >(height),
343252 static_cast <size_t >(width)});
@@ -348,7 +257,6 @@ int main(int argc, char* argv[]) {
348257
349258 Graph graph;
350259 build_graph (graph, dummy_input, dummy_output, json_path, options, false );
351- LOG_MEM (" Master graph built" );
352260
353261 std::shared_ptr<Layer> input_layer = nullptr ;
354262 std::shared_ptr<Layer> output_layer = nullptr ;
@@ -368,7 +276,6 @@ int main(int argc, char* argv[]) {
368276 return 1 ;
369277 }
370278
371- LOG_MEM (" Starting batch processing" );
372279 auto total_start_time = std::chrono::high_resolution_clock::now ();
373280 int total_inference_time = 0 ;
374281 int batch_count = 0 ;
@@ -378,11 +285,6 @@ int main(int argc, char* argv[]) {
378285 size_t batch_end = std::min (batch_start + batch_size, num_photo);
379286 size_t current_batch_size = batch_end - batch_start;
380287
381- char batch_log[100 ];
382- sprintf (batch_log, " Batch %zu/%zu (size %zu)" , batch_start / batch_size + 1 ,
383- (num_photo + batch_size - 1 ) / batch_size, current_batch_size);
384- LOG_MEM (batch_log);
385-
386288 std::vector<float > batch_data;
387289 batch_data.reserve (current_batch_size * image_size);
388290
@@ -402,7 +304,6 @@ int main(int argc, char* argv[]) {
402304 graph.setInput (input_layer, batch_input);
403305 graph.setOutput (output_layer, batch_output);
404306
405- LOG_MEM (" Batch inference" );
406307 auto batch_start_time = std::chrono::high_resolution_clock::now ();
407308 graph.inference (options);
408309 auto batch_end_time = std::chrono::high_resolution_clock::now ();
@@ -493,13 +394,5 @@ int main(int argc, char* argv[]) {
493394 std::cout << " Top-5 Accuracy: " << std::fixed << std::setprecision (2 )
494395 << final_accuracy_top5 << " %" << ' \n ' ;
495396
496- all_image_data.clear ();
497- all_image_data.shrink_to_fit ();
498- image_paths.clear ();
499- image_paths.shrink_to_fit ();
500- true_labels.clear ();
501- true_labels.shrink_to_fit ();
502-
503- LOG_MEM (" Program end" );
504397 return 0 ;
505398}
0 commit comments