From 673252f7eadc3aa0cfae3d826e1c7bbe2400df17 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 9 Jul 2012 13:57:37 -0600 Subject: It generates pngs just like the java version. Maybe even prettier. --- src/tests/pic.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) (limited to 'src/tests') diff --git a/src/tests/pic.cpp b/src/tests/pic.cpp index 95bb523..4f4139f 100644 --- a/src/tests/pic.cpp +++ b/src/tests/pic.cpp @@ -3,8 +3,20 @@ #include "neural/row.h" #include "neural/neuron.h" +#include + +#include +#include +using namespace Bu; + +#include +#include +#include + int main( int argc, char *argv[] ) { + Bu::Random::seed( time( NULL ) ); + Neural::Column *c = new Neural::Column(); Neural::Row *r1 = new Neural::Row(); r1->addNode( new Neural::Neuron() ); @@ -27,6 +39,107 @@ int main( int argc, char *argv[] ) r3->addNode( new Neural::Neuron() ); r3->addNode( new Neural::Neuron() ); c->addNode( r3 ); + + c->finalize( 2 ); + sio << "Total weights: " << c->getNumWeights() << sio.nl; + sio << "Total biases: " << c->getNumBiases() << sio.nl; + sio << "Network inputs: " << c->getNumInputs() << sio.nl; + sio << "Network outputs: " << c->getNumOutputs() << sio.nl; + + float *pWeights = new float[c->getNumWeights()]; + float *pBiases = new float[c->getNumBiases()]; + + for( int j = 0; j < c->getNumWeights(); j++ ) + pWeights[j] = (Bu::Random::randNorm()*2.0)-1.0; + for( int j = 0; j < c->getNumBiases(); j++ ) + pBiases[j] = (Bu::Random::randNorm()*2.0)-1.0; + + c->setWeights( pWeights ); + c->setBiases( pBiases ); + delete pWeights; + delete pBiases; + + float *pIn = new float[c->getNumInputs()]; + float *pOut = new float[c->getNumOutputs()]; + + FILE *fp = fopen("test.png", "wb"); + + if (!fp) + return 1; + + png_structp png_ptr = png_create_write_struct + (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); + + if (!png_ptr) + return 1; + + png_infop info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, + (png_infopp)NULL); + return 1; + } + + png_set_IHDR(png_ptr, info_ptr, 500, 500, 8, PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT + ); + + /* Set the zlib compression level */ + png_set_compression_level(png_ptr, + Z_BEST_COMPRESSION); + + /* Set other zlib parameters for compressing IDAT */ + png_set_compression_mem_level(png_ptr, 8); + png_set_compression_strategy(png_ptr, + Z_DEFAULT_STRATEGY); + png_set_compression_window_bits(png_ptr, 15); + png_set_compression_method(png_ptr, 8); + png_set_compression_buffer_size(png_ptr, 8192); + + /* Set zlib parameters for text compression + * If you don't call these, the parameters + * fall back on those defined for IDAT chunks + */ + png_set_text_compression_mem_level(png_ptr, 8); + png_set_text_compression_strategy(png_ptr, + Z_DEFAULT_STRATEGY); + png_set_text_compression_window_bits(png_ptr, 15); + png_set_text_compression_method(png_ptr, 8); + + png_bytep *row_pointers; + row_pointers = new png_bytep[500]; + for( int y = 0; y < 500; y++ ) + { + row_pointers[y] = new png_byte[500*3]; + for( int x = 0; x < 500; x++ ) + { + pIn[0] = (x/499.0)*2.0-1.0; + pIn[1] = (y/499.0)*2.0-1.0; + c->process( pIn, pOut ); + row_pointers[y][x*3+0] = pOut[0]*127+127; + row_pointers[y][x*3+1] = pOut[1]*127+127; + row_pointers[y][x*3+2] = pOut[2]*127+127; + } + } + + png_init_io( png_ptr, fp ); + + png_set_rows( png_ptr, info_ptr, row_pointers ); + + png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); + + if (setjmp(png_jmpbuf(png_ptr))) + { + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fp); + return 1; + } + png_destroy_write_struct( &png_ptr, &info_ptr ); + fclose( fp ); + + delete c; return 0; } -- cgit v1.2.3