Changeset 972 for cpp/frams/_demos/neuro_layout_test.cpp
- Timestamp:
- 07/03/20 00:32:23 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/_demos/neuro_layout_test.cpp
r408 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 21 21 22 22 // stl is fun? ;-) ForwardIterator implementation for element coordinates (required by min_element/max_element) 23 template <int MEMBER> struct NNIter : public std::iterator<std::forward_iterator_tag,int> //MEMBER: 0..3=x/y/w/h23 template <int MEMBER> struct NNIter : public std::iterator<std::forward_iterator_tag, int> //MEMBER: 0..3=x/y/w/h 24 24 { 25 NNLayoutState *nn; int index;26 NNIter() {}27 NNIter(NNLayoutState *_nn, int _index):nn(_nn),index(_index) {}28 int operator*() {return nn->GetXYWH(index)[MEMBER];}29 NNIter& operator++() {index++; return *this;}30 bool operator!=(const NNIter& it) {return index!=it.index;}31 bool operator==(const NNIter& it) {return index==it.index;}25 NNLayoutState *nn; int index; 26 NNIter() {} 27 NNIter(NNLayoutState *_nn, int _index) :nn(_nn), index(_index) {} 28 int operator*() { return nn->GetXYWH(index)[MEMBER]; } 29 NNIter& operator++() { index++; return *this; } 30 bool operator!=(const NNIter& it) { return index != it.index; } 31 bool operator==(const NNIter& it) { return index == it.index; } 32 32 33 static NNIter begin(NNLayoutState *_nn) {return NNIter(_nn,0);}34 static NNIter end(NNLayoutState *_nn) {return NNIter(_nn,_nn->GetElements());}33 static NNIter begin(NNLayoutState *_nn) { return NNIter(_nn, 0); } 34 static NNIter end(NNLayoutState *_nn) { return NNIter(_nn, _nn->GetElements()); } 35 35 }; 36 36 37 37 class Screen 38 38 { 39 int min_x,max_x,min_y,max_y,scale_x,scale_y;40 int rows,columns;41 char* screen;39 int min_x, max_x, min_y, max_y, scale_x, scale_y; 40 int rows, columns; 41 char* screen; 42 42 43 43 public: 44 44 45 Screen(int _min_x,int _max_x,int _min_y,int _max_y,int _scale_x,int _scale_y) 46 :min_x(_min_x),max_x(_max_x),min_y(_min_y),max_y(_max_y),scale_x(_scale_x),scale_y(_scale_y) 45 Screen(int _min_x, int _max_x, int _min_y, int _max_y, int _scale_x, int _scale_y) 46 :min_x(_min_x), max_x(_max_x), min_y(_min_y), max_y(_max_y), scale_x(_scale_x), scale_y(_scale_y) 47 { 48 columns = (max_x - min_x + scale_x - 1) / scale_x; 49 rows = (max_y - min_y + scale_y - 1) / scale_y; 50 screen = new char[rows * columns]; 51 memset(screen, ' ', rows * columns); 52 } 53 54 ~Screen() 55 { 56 delete[] screen; 57 } 58 59 void put(int x, int y, const char *str) 60 { 61 x = (x - min_x) / scale_x; 62 y = (y - min_y) / scale_y; 63 if (x < 0) return; 64 if (y < 0) return; 65 if (y >= rows) return; 66 for (; *str; str++, x++) 47 67 { 48 columns=(max_x-min_x+scale_x-1)/scale_x; 49 rows=(max_y-min_y+scale_y-1)/scale_y; 50 screen=new char[rows*columns]; 51 memset(screen,' ',rows*columns); 52 } 53 54 ~Screen() 55 { 56 delete[] screen; 57 } 58 59 void put(int x,int y,const char *str) 60 { 61 x=(x-min_x)/scale_x; 62 y=(y-min_y)/scale_y; 63 if (x<0) return; 64 if (y<0) return; 65 if (y>=rows) return; 66 for(;*str;str++,x++) 67 { 68 if (x>=columns) return; 69 screen[columns*y+x]=*str; 68 if (x >= columns) return; 69 screen[columns * y + x] = *str; 70 70 } 71 71 } 72 72 73 void print()73 void print() 74 74 { 75 for(int y=0;y<rows;y++)75 for (int y = 0; y < rows; y++) 76 76 { 77 fwrite(&screen[columns*y],1,columns,stdout);78 printf("\n");77 fwrite(&screen[columns * y], 1, columns, stdout); 78 printf("\n"); 79 79 } 80 80 } 81 81 }; 82 82 83 int main(int argc, char*argv[])83 int main(int argc, char*argv[]) 84 84 { 85 LoggerToStdout messages_to_stdout(LoggerBase::Enable);86 PreconfiguredGenetics genetics;85 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 86 PreconfiguredGenetics genetics; 87 87 88 if (argc<=1)88 if (argc <= 1) 89 89 { 90 90 puts("Parameters:\n" 91 92 93 91 " 1. Genotype (or - character indicating the genotype will be read from stdin)\n" 92 " 2. (Optional) layout type (the only useful layout is 2, which is the default, see nn_simple_layout.cpp"); 93 return 10; 94 94 } 95 SString gen(argv[1]);96 if (!strcmp(gen.c_str(),"-"))95 SString gen(argv[1]); 96 if (!strcmp(gen.c_str(), "-")) 97 97 { 98 gen=0;99 StdioFILEDontClose in(stdin);100 loadSString(&in,gen);98 gen = 0; 99 StdioFILEDontClose in(stdin); 100 loadSString(&in, gen); 101 101 } 102 int layout_type=2;103 if (argc>2) layout_type=atol(argv[2]);104 Geno g(gen);105 if (!g.isValid()) {puts("invalid genotype");return 5;}106 Model m(g);107 if (!m.getNeuroCount()) {puts("no neural network");return 1;}108 printf("%d neurons,",m.getNeuroCount());102 int layout_type = 2; 103 if (argc > 2) layout_type = atol(argv[2]); 104 Geno g(gen); 105 if (!g.isValid()) { puts("invalid genotype"); return 5; } 106 Model m(g, Model::SHAPE_UNKNOWN); 107 if (!m.getNeuroCount()) { puts("no neural network"); return 1; } 108 printf("%d neurons,", m.getNeuroCount()); 109 109 110 NNLayoutState_Model nn_layout(&m);111 struct NNLayoutFunction &nnfun=nn_layout_functions[layout_type];112 printf(" using layout type=%d (%s)\n",layout_type,nnfun.name);113 nnfun.doLayout(&nn_layout);110 NNLayoutState_Model nn_layout(&m); 111 struct NNLayoutFunction &nnfun = nn_layout_functions[layout_type]; 112 printf(" using layout type=%d (%s)\n", layout_type, nnfun.name); 113 nnfun.doLayout(&nn_layout); 114 114 115 for(int i=0;i<nn_layout.GetElements();i++)115 for (int i = 0; i < nn_layout.GetElements(); i++) 116 116 { 117 int *xywh=nn_layout.GetXYWH(i);118 printf("#%-3d %s\t%d,%d\t%dx%d\n",i,m.getNeuro(i)->getClassName().c_str(),119 xywh[0],xywh[1],xywh[2],xywh[3]);117 int *xywh = nn_layout.GetXYWH(i); 118 printf("#%-3d %s\t%d,%d\t%dx%d\n", i, m.getNeuro(i)->getClassName().c_str(), 119 xywh[0], xywh[1], xywh[2], xywh[3]); 120 120 } 121 121 122 Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))-30,123 *std::max_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))+70,124 *std::min_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout)),125 *std::max_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout))+30,126 10,35);122 Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout), NNIter<0>::end(&nn_layout)) - 30, 123 *std::max_element(NNIter<0>::begin(&nn_layout), NNIter<0>::end(&nn_layout)) + 70, 124 *std::min_element(NNIter<1>::begin(&nn_layout), NNIter<1>::end(&nn_layout)), 125 *std::max_element(NNIter<1>::begin(&nn_layout), NNIter<1>::end(&nn_layout)) + 30, 126 10, 35); 127 127 128 printf("===========================================\n");129 for(int i=0;i<nn_layout.GetElements();i++)128 printf("===========================================\n"); 129 for (int i = 0; i < nn_layout.GetElements(); i++) 130 130 { 131 int *xywh=nn_layout.GetXYWH(i);132 SString label=SString::sprintf("%d:%s",i,m.getNeuro(i)->getClassName().c_str());133 screen.put(xywh[0],xywh[1],label.c_str());131 int *xywh = nn_layout.GetXYWH(i); 132 SString label = SString::sprintf("%d:%s", i, m.getNeuro(i)->getClassName().c_str()); 133 screen.put(xywh[0], xywh[1], label.c_str()); 134 134 } 135 screen.print();136 printf("===========================================\n");135 screen.print(); 136 printf("===========================================\n"); 137 137 138 138 }
Note: See TracChangeset
for help on using the changeset viewer.