From 94d966b50ab1a09d8650b7c693e9223273a44acf Mon Sep 17 00:00:00 2001 From: Georg Holzmann Date: Tue, 12 Jul 2005 14:39:36 +0000 Subject: initial commit of pix_linNN svn path=/trunk/externals/grh/; revision=3319 --- pix_linNN/LinNeuralNet.cpp | 147 ++++++++++++ pix_linNN/LinNeuralNet.h | 154 +++++++++++++ pix_linNN/Makefile | 44 ++++ pix_linNN/gpl.txt | 346 ++++++++++++++++++++++++++++ pix_linNN/help-pix_linNN.pd | 135 +++++++++++ pix_linNN/pix_linNN.cpp | 541 ++++++++++++++++++++++++++++++++++++++++++++ pix_linNN/pix_linNN.h | 188 +++++++++++++++ pix_linNN/readme.txt | 26 +++ 8 files changed, 1581 insertions(+) create mode 100755 pix_linNN/LinNeuralNet.cpp create mode 100755 pix_linNN/LinNeuralNet.h create mode 100755 pix_linNN/Makefile create mode 100755 pix_linNN/gpl.txt create mode 100755 pix_linNN/help-pix_linNN.pd create mode 100755 pix_linNN/pix_linNN.cpp create mode 100755 pix_linNN/pix_linNN.h create mode 100755 pix_linNN/readme.txt diff --git a/pix_linNN/LinNeuralNet.cpp b/pix_linNN/LinNeuralNet.cpp new file mode 100755 index 0000000..87318a0 --- /dev/null +++ b/pix_linNN/LinNeuralNet.cpp @@ -0,0 +1,147 @@ +///////////////////////////////////////////////////////////////////////////// +// +// class LinNeuralNet +// +// source file +// +// Copyright (c) 2004 Georg Holzmann +// +// For information on usage and redistribution, and for a DISCLAIMER OF ALL +// WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution. +// +///////////////////////////////////////////////////////////////////////////// + +#include "LinNeuralNet.h" + +//-------------------------------------------------- +/* Constructor + */ +LinNeuralNet::LinNeuralNet(int netsize) : learn_rate_(0), range_(1), IW_(NULL), b1_(0) +{ + // set random seed: + srand( (unsigned)time(NULL) ); + + netsize_ = (netsize<1) ? 1 : netsize; +} + +//-------------------------------------------------- +/* Destructor + */ +LinNeuralNet::~LinNeuralNet() +{ + if(IW_) + delete[] IW_; +} + +//-------------------------------------------------- +/* creates a new IW-matrix (size: netsize_) and + * b1-vector + * ATTENTION: if they exist they'll be deleted + */ +bool LinNeuralNet::createNeurons() +{ + // delete if they exist + if(IW_) + delete[] IW_; + + IW_ = new float[netsize_]; + if(!IW_) + return false; + + return true; +} + +//-------------------------------------------------- +/* inits the weight matrix and the bias vector of + * the network with random values between [min|max] + */ +bool LinNeuralNet::initNetworkRand(const int &min, const int &max) +{ + if(!IW_) + return false; + + // make randomvalue between 0 and 1 + // then map it to the bounds + b1_ = ((float)rand()/(float)RAND_MAX)*(max-min) + min; + + for(int i=0; i +// +// For information on usage and redistribution, and for a DISCLAIMER OF ALL +// WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution. +// +///////////////////////////////////////////////////////////////////////////// + + +#ifndef _INCLUDE_LIN_NEURAL_NET__ +#define _INCLUDE_LIN_NEURAL_NET__ + +#include +#include +//#include "m_pd.h" // for debug + +class LinNeuralNet +{ + protected: + + /* this is the number of input values, which is + * automatically the netsize and the size of IW + */ + int netsize_; + + /* the input weight matrix IW + * (size: netsize ) + */ + float *IW_; + + /* the bias vector b1 + */ + float b1_; + + /* the learning rate of the net + */ + float learn_rate_; + + /* the range of the input values should be from 0 + * to range_ + * outputvalues are from -1 to 1 + */ + float range_; + + + public: + + /* Constructor + */ + LinNeuralNet(int netsize); + + /* Destructor + */ + virtual ~LinNeuralNet(); + + + //----------------------------------------------------- + + /* Set/Get learning rate + */ + virtual void setLearningRate(float learn_rate) + { learn_rate_=learn_rate; } + virtual float getLearningRate() const + { return learn_rate_; } + + /* Set/Get range + */ + virtual void setRange(float range) + { range_=range; } + virtual float getRange() const + { return range_; } + + /* some more get/set methods + */ + virtual int getNetsize() const + { return netsize_; } + virtual float *getIW() const + { return IW_; } + virtual void setIW(const float *IW) + { for(int i=0; i + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + diff --git a/pix_linNN/help-pix_linNN.pd b/pix_linNN/help-pix_linNN.pd new file mode 100755 index 0000000..932116f --- /dev/null +++ b/pix_linNN/help-pix_linNN.pd @@ -0,0 +1,135 @@ +#N canvas 871 74 498 738 10; +#X obj 28 237 gemwin; +#X msg 28 211 create \, 1; +#N canvas 463 0 765 790 pix2sig_stuff~ 0; +#X obj 120 35 gemhead; +#X obj 120 132 pix_texture; +#X obj 119 274 outlet~; +#X obj 139 185 square 4; +#X obj 139 163 separator; +#X obj 61 165 separator; +#X obj 120 101 pix_video; +#X msg 186 64 dimen 640 480; +#X obj 26 36 block~ 2048; +#X msg 186 38 dimen 320 240; +#X msg 76 535 getprecision; +#X msg 93 696 getlearnrate; +#X msg 65 671 learnrate 0.2; +#X msg 424 459 getneurons; +#X msg 404 206 train; +#X obj 31 227 inlet~; +#X msg 65 647 learnrate 0.05; +#X text 296 49 <- input dimension; +#X msg 76 498 precision \$1; +#X floatatom 76 481 5 0 0 0 - - -; +#X text 42 335 precision:; +#X text 53 358 1: means every pixel is used in calculation; +#X text 53 372 2: only every second pixel; +#X text 53 386 ...; +#X obj 62 411 loadbang; +#X msg 407 401 neurons 2048; +#X msg 407 422 neurons 64; +#X text 403 336 neurons:; +#X text 416 357 nr. of neurons used in the calculation; +#X text 415 370 (_MUST_ be the same as the buffersize !!!); +#X text 43 615 learnrate:; +#X msg 62 456 precision 1; +#X msg 62 436 precision 4; +#X text 397 126 train:; +#X text 417 152 trains the neural net; +#X text 418 166 (the current video frame to; +#X text 425 178 the current audio block); +#X obj 61 252 pix_linNN; +#X text 346 592 save/load; +#X text 359 614 saves/load the actual trained net to/from a file; +#X msg 440 684 load net.dat; +#X msg 440 664 save net.dat; +#X obj 78 226 r \$0-linNN; +#X obj 404 233 s \$0-linNN; +#X obj 62 564 s \$0-linNN; +#X obj 407 492 s \$0-linNN; +#X obj 65 725 s \$0-linNN; +#X obj 440 723 s \$0-linNN; +#X connect 0 0 6 0; +#X connect 1 0 4 0; +#X connect 1 0 5 0; +#X connect 4 0 3 0; +#X connect 5 0 37 0; +#X connect 6 0 1 0; +#X connect 7 0 6 0; +#X connect 9 0 6 0; +#X connect 10 0 44 0; +#X connect 11 0 46 0; +#X connect 12 0 46 0; +#X connect 13 0 45 0; +#X connect 14 0 43 0; +#X connect 15 0 37 0; +#X connect 16 0 46 0; +#X connect 18 0 44 0; +#X connect 19 0 18 0; +#X connect 24 0 32 0; +#X connect 25 0 45 0; +#X connect 26 0 45 0; +#X connect 31 0 44 0; +#X connect 32 0 44 0; +#X connect 37 1 2 0; +#X connect 40 0 47 0; +#X connect 41 0 47 0; +#X connect 42 0 37 0; +#X restore 87 492 pd pix2sig_stuff~; +#X msg 102 212 0 \, destroy; +#X obj 114 537 unsig~; +#X obj 204 382 osc~ 440; +#X obj 203 406 *~; +#X obj 235 406 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 205 446 sig~ 0; +#X floatatom 115 558 8 0 0 0 - - -; +#X text 199 230 <- create gemwin; +#X obj 39 392 readsf~; +#X obj 39 351 openpanel; +#X msg 39 371 open \$1; +#X obj 39 330 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 +-1; +#X text 65 329 <- load sample for training; +#X obj 120 367 tgl 25 0 empty empty empty 0 -6 0 8 -195568 -1 -1 0 +1; +#X floatatom 204 364 5 0 0 0 - - -; +#X text 270 381 <- simple osc for training; +#X text 260 447 <- to train silence; +#X obj 83 413 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 +-1; +#X text 214 491 <- audio/video work; +#X obj 88 634 dac~; +#X obj 88 609 *~; +#X obj 116 609 dbtorms; +#X floatatom 116 591 5 0 0 0 - - -; +#X text 166 588 <- outvol in dB; +#X text 110 703 Georg Holzmann \, 2004; +#X text 24 23 pix_linNN:; +#X text 22 58 (see also pix_recNN !!!); +#X text 24 90 pix_linNN~ calculates an audio signal out of a video +frame with a linear neural network \, which can be trained.; +#X text 24 124 The network has one neuron per audio sample: this neuron +has three inputs (a RGB-signal) \, a weight vector for each of the +inputs \, a bias value and a linear output function.; +#X connect 1 0 0 0; +#X connect 2 0 4 0; +#X connect 2 0 23 0; +#X connect 3 0 0 0; +#X connect 4 0 9 0; +#X connect 5 0 6 0; +#X connect 6 0 2 0; +#X connect 7 0 6 1; +#X connect 8 0 2 0; +#X connect 11 0 2 0; +#X connect 11 1 20 0; +#X connect 12 0 13 0; +#X connect 13 0 11 0; +#X connect 14 0 12 0; +#X connect 16 0 11 0; +#X connect 17 0 5 0; +#X connect 23 0 22 0; +#X connect 23 0 22 1; +#X connect 24 0 23 1; +#X connect 25 0 24 0; diff --git a/pix_linNN/pix_linNN.cpp b/pix_linNN/pix_linNN.cpp new file mode 100755 index 0000000..515a6a2 --- /dev/null +++ b/pix_linNN/pix_linNN.cpp @@ -0,0 +1,541 @@ +///////////////////////////////////////////////////////////////////////////// +// +// GEM - Graphics Environment for Multimedia +// +// pix_linNN +// +// Implementation file +// +// Copyright (c) 2004 Georg Holzmann +// (and of course lot's of other developers for PD and GEM) +// +// For information on usage and redistribution, and for a DISCLAIMER OF ALL +// WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution. +// +///////////////////////////////////////////////////////////////////////////// + +#include "pix_linNN.h" + +CPPEXTERN_NEW_WITH_TWO_ARGS(pix_linNN, t_floatarg, A_DEFFLOAT, t_floatarg, A_DEFFLOAT) + +//---------------------------------------------------------- +/* Constructor + */ + pix_linNN::pix_linNN(t_floatarg arg0=64, t_floatarg arg1=1) : + m_data_(NULL), m_xsize_(0), m_ysize_(0), m_csize_(0), + train_on_(false), net_(NULL) +{ + // init args ????????????????????????????????? + neuron_nr_=2048; //static_cast((arg0<0)?2:arg0); + precision_=2; //static_cast((arg1<1)?1:arg1); + //post("arg0: %d, arg1: %d",arg0,arg1); + + // generate the in- and outlet: + out0_ = outlet_new(this->x_obj, &s_signal); + inlet_new(this->x_obj, &this->x_obj->ob_pd, &s_signal, &s_signal); + + // set random seed: + srand( (unsigned)time(NULL) ); + + // creates the nets + net_ = new LinNeuralNet[neuron_nr_](3); + if(!net_) + { + post("pix_linNN~: no memory for neural nets!"); + return; + } + + for(int i=0; ineuron_nr_) + { + post("pix_linNN~: neurons and buffersize are different! You MUST have the same neuron nr as the buffersize !!!"); + post("neurons: %d, buffersize: %d", x->neuron_nr_, blocksize); + return (w+5); + } + + + // some needed data + long int pix_size = x->m_xsize_ * x->m_ysize_; + int pix_blocksize = (blocksizem_xsize_ / nr; // x size of a slice in pixels + float y_slice = x->m_ysize_ / nr; // x size of a slice in pixels + int x_slice_int = static_cast( x_slice ); + int y_slice_int = static_cast( y_slice ); + + // the number of slices on one axis (is the float nr + // from above rounded up) + int slice_nr = static_cast(nr) + 1; + + if (x->m_data_) + { + switch(x->m_format_) + { + case GL_RGBA: + for(int n=0; n( (n % slice_nr) * x_slice ); + int lu_pix_y = static_cast( static_cast(n / slice_nr) * y_slice ); + + //post("lu_pix: %d, %d", lu_pix_x, lu_pix_y); + + // now sum up all the pixels of one slice and then divide through the + // number of pixels + unsigned long int temp_data[3] = { 0, 0, 0 }; // the storage to sum the pixels + t_float average_pix[3] = { 0, 0, 0 }; // the average of the pixels + + // only for optimization: + int helper1 = x->m_xsize_ * x->m_csize_; + int add_count = 0; + + for(int i=0; iprecision_) + { + for(int j=0; jprecision_) + { + // the way to access the pixels: (C=chRed, chBlue, ...) + //data[Y * xsize * csize + X * csize + C] + + //post("current pixel: %d %d", + // ((lu_pix_x+i)%x->m_xsize), ((lu_pix_y+j)%x->m_ysize) ); + + temp_data[0] += x->m_data_[ + (lu_pix_y+j) * helper1 + + (lu_pix_x+i) * x->m_csize_ + chRed ]; + + temp_data[1] += x->m_data_[ + ((lu_pix_y+j)) * helper1 + + ((lu_pix_x+i)) * x->m_csize_ + chGreen ]; + + temp_data[2] += x->m_data_[ + ((lu_pix_y+j)%x->m_ysize_) * helper1 + + ((lu_pix_x+i)%x->m_xsize_) * x->m_csize_ + chBlue ]; + + add_count++; + } + } + average_pix[0] = temp_data[0] / add_count; + average_pix[1] = temp_data[1] / add_count; + average_pix[2] = temp_data[2] / add_count; + + // the calculation of the network: + *out_signal = x->net_[n].calculateNet(average_pix); + + //post("%d: RGBav: %f %f %f, out_signal: %f", + //n,average_pix[0],average_pix[1],average_pix[2],*out_signal); + + // learning: + if(x->train_on_) + x->net_[n].trainNet(average_pix, *out_signal, *in_signal); + + out_signal++; + in_signal++; + } + break; + default: + post("RGB only for now"); + } + } + else + { + pix_blocksize=blocksize; + while (pix_blocksize--) *out_signal++=0; + } + + x->train_on_=false; + return (w+5); +} + +//---------------------------------------------------------- +/* DSP-Message + */ +void pix_linNN::dspMess(void *data, t_signal** sp) +{ + dsp_add(perform, 4, data, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n); +} + +//---------------------------------------------------------- +/* saves the contents of the current net to file + * (it saves the neuron_nr_, learning rate + * IW-matrix and b1-vector of the net) + */ +void pix_linNN::saveNet(string filename) +{ + // open and check outfile + ofstream outfile; + outfile.open(filename.c_str()); + if(!outfile) + { + post("pix_linNN~: failed to open output-file!"); + return; + } + + // write XML-header + outfile << "" << endl; + + // start-tag + outfile << "" << endl; + + // neuron_nr_(=size) and learning rate + outfile << "\t " << neuron_nr_ << " " << endl; + outfile << "\t " << net_[0].getLearningRate() + << " " << endl; + + // now the IW-matrix of the neural net + outfile << "\t" << endl; + for(int i=0; i" << endl; + + // and the b1-vector + outfile << "\t" << endl << "\t\t"; + for(int i=0; i" << endl; + + // end-tag + outfile << "" << endl; + + + outfile.close(); + post("pix_linNN~: saved to output-file %s", filename.c_str()); + return; +} + +//---------------------------------------------------------- +/* loads the parameters of the net from file + * (it loads the neuron_nr_, learning rate + * IW-matrix and b1-vector of the net) + */ +void pix_linNN::loadNet(string filename) +{ + // temp variables + float IW[3]; + float b1, learnrate; + + ifstream infile; + infile.open(filename.c_str()); + + if(!infile) + { + post("pix_linNN~: cannot open input-file!"); + return; + } + + post("pix_linNN~: loading input-file %s",filename.c_str()); + + int state = 0, IWcount = 0, b1count = 0; + bool tag=false; + string line, temp; + + while (getline(infile, line)) + { + istringstream instream(line); + instream >> temp; + + // specify the tags + //post("input: %s",temp.c_str()); + if( temp == "" ) + {state=1; } + if( temp == "" ) + {state=2; } + if( temp == "" ) + {state=3; } + if( temp == "" ) + {state=4; } + if( !strncmp(temp.c_str(),"> temp; // if theres a tag, stream it + + + bool go_on=false; + while(!go_on) + { + // end of a line + if(instream.eof() || !state) + { + go_on=true; + break; + } + + + // + if(state == 1) + { + instream >> neuron_nr_; + if(!net_) + { + // creates new nets + net_ = new LinNeuralNet[neuron_nr_](3); + if(!net_) + { + post("pix_linNN~: no memory for neural nets!"); + break; + } + } + for(int i=0; i + if(state == 2) + { + instream >> learnrate; + + for(int i=0; i + if(state == 3) + { + instream >> IW[0]; + instream >> IW[1]; + instream >> IW[2]; + + if(IWcount + if(state == 4) + { + for(int i=0; i> b1; + net_[b1count++].setb1(b1); + } + + go_on = false; + break; + } + + //else: + go_on=false; + break; + } + } + + infile.close(); + return; +} + +//---------------------------------------------------------- +/* setup callback + */ +void pix_linNN::obj_setupCallback(t_class *classPtr) +{ + class_addcreator((t_newmethod)_classpix_linNN, gensym("pix_linNN~"), A_NULL); + + class_addmethod(classPtr, (t_method)pix_linNN::setNeurons, + gensym("neurons"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::getNeurons, + gensym("getneurons"), A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::setPrecision, + gensym("precision"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::getPrecision, + gensym("getprecision"), A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::setTrainOn, + gensym("train"), A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::setLearnrate, + gensym("learnrate"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::getLearnrate, + gensym("getlearnrate"), A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::saveToFile, + gensym("save"), A_SYMBOL, A_NULL); + class_addmethod(classPtr, (t_method)pix_linNN::loadFromFile, + gensym("load"), A_SYMBOL, A_NULL); + + class_addmethod(classPtr, (t_method)pix_linNN::dspMessCallback, + gensym("dsp"), A_NULL); + class_addmethod(classPtr, nullfn, gensym("signal"), A_NULL); +} + +//---------------------------------------------------------- +/* DSP callback + */ +void pix_linNN::dspMessCallback(void *data, t_signal** sp) +{ + GetMyClass(data)->dspMess(data, sp); +} + +//---------------------------------------------------------- +/* sets the precision + */ +void pix_linNN::setPrecision(void *data, t_floatarg precision) +{ + GetMyClass(data)->precision_ = + (precision<1) ? 1 : static_cast(precision); +} +void pix_linNN::getPrecision(void *data) +{ + post("pix_linNN~: precision: %d",GetMyClass(data)->precision_); +} + +//---------------------------------------------------------- +/* method to train the network + */ +void pix_linNN::setTrainOn(void *data) +{ + GetMyClass(data)->train_on_ = true; +} + +//---------------------------------------------------------- +/* changes the number of neurons + * (which should be the same as the audio buffer) + * ATTENTION: a new IW-matrix and b1-vector will be initialized + */ +void pix_linNN::setNeurons(void *data, t_floatarg neurons) +{ + GetMyClass(data)->neuron_nr_ = + (neurons<1) ? 1 : static_cast(neurons); + + if(GetMyClass(data)->net_) + delete[] GetMyClass(data)->net_; + + // creates the nets + GetMyClass(data)->net_ = new LinNeuralNet[GetMyClass(data)->neuron_nr_](3); + if(!GetMyClass(data)->net_) + { + post("pix_linNN~: no memory for neural nets!"); + return; + } + + for(int i=0; ineuron_nr_; i++) + { + if( !GetMyClass(data)->net_[i].createNeurons() ) + { + post("pix_linNN~: error in creating the net!"); + return; + } + if( !GetMyClass(data)->net_[i].initNetworkRand(-1,1) ) + { + post("pix_linNN~: error in initializing the net!"); + return; + } + } +} +void pix_linNN::getNeurons(void *data) +{ + post("pix_linNN~: nr of neurons: %d (MUST be the same as buffersize!)", + GetMyClass(data)->neuron_nr_); +} + +//---------------------------------------------------------- +/* sets the learnrate of the net + */ +void pix_linNN::setLearnrate(void *data, t_floatarg learn_rate) +{ + for(int i=0; ineuron_nr_; i++) + GetMyClass(data)->net_[i].setLearningRate(learn_rate); +} +void pix_linNN::getLearnrate(void *data) +{ + post("pix_linNN~: learning rate: %f",GetMyClass(data)->net_[0].getLearningRate()); +} + +//---------------------------------------------------------- +/* FileIO-stuff + */ +void pix_linNN::saveToFile(void *data, t_symbol *filename) +{ + GetMyClass(data)->saveNet(filename->s_name); +} +void pix_linNN::loadFromFile(void *data, t_symbol *filename) +{ + GetMyClass(data)->loadNet(filename->s_name); +} diff --git a/pix_linNN/pix_linNN.h b/pix_linNN/pix_linNN.h new file mode 100755 index 0000000..4ebc10c --- /dev/null +++ b/pix_linNN/pix_linNN.h @@ -0,0 +1,188 @@ +///////////////////////////////////////////////////////////////////////////// +// +// GEM - Graphics Environment for Multimedia +// +// pix_linNN~ +// Calculates an audio signal out of a video frame +// with a linear neural network, which can be trained +// +// the network has one neuron per audio sample: this neuron has +// three inputs (a RGB-signal), a weight vector for each of the inputs, +// a bias value and a linear output function +// (see LinNeuralNet.h for more info) +// +// header file +// +// Copyright (c) 2004 Georg Holzmann +// (and of course lot's of other developers for PD and GEM) +// +// For information on usage and redistribution, and for a DISCLAIMER OF ALL +// WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution. +// +///////////////////////////////////////////////////////////////////////////// + + +#ifndef _INCLUDE_PIX_LINNN_H__ +#define _INCLUDE_PIX_LINNN_H__ + +#include +#include +#include +#include "Base/GemPixObj.h" +#include "LinNeuralNet.h" + + +using std::string; +using std::endl; +using std::ifstream; +using std::ofstream; +using std::istringstream; + + +/*----------------------------------------------------------------- + * CLASS + * pix_linNN~ + * + * calculates an audio signal out of a video frame with + * a linear neural network + * + * KEYWORDS + * pix audio + * + * DESCRIPTION + * 1 signal-outlet + */ +class GEM_EXTERN pix_linNN : public GemPixObj +{ + CPPEXTERN_HEADER(pix_linNN, GemPixObj) + + public: + + /* Constructor + */ + pix_linNN(t_floatarg arg0, t_floatarg arg1); + + protected: + + /* Destructor + */ + virtual ~pix_linNN(); + + + //----------------------------------- + /* Image STUFF: + */ + + /* The pixBlock with the current image + * pixBlock m_pixBlock; + */ + unsigned char *m_data_; + int m_xsize_; + int m_ysize_; + int m_csize_; + int m_format_; + + /* precision of the image: + * 1 means every pixel is taken for the calculation, + * 2 every second pixel, 3 every third, ... + */ + int precision_; + + /* processImage + */ + virtual void processImage(imageStruct &image); + + + //----------------------------------- + /* Neural Network STUFF: + */ + + /* the linear neural nets + * (size: buffsize) + */ + LinNeuralNet *net_; + + /* training modus on + * (will only be on for one audio buffer) + */ + bool train_on_; + + /* the number of neurons, which should be + * (= size of the array nets_) + * THE SAME as the audio buffer size + */ + int neuron_nr_; + + + //----------------------------------- + /* Audio STUFF: + */ + + /* the outlet + */ + t_outlet *out0_; + + /* DSP perform + */ + static t_int* perform(t_int* w); + + /* DSP-Message + */ + virtual void dspMess(void *data, t_signal** sp); + + + //----------------------------------- + /* File IO: + */ + + /* saves the contents of the current net to file + * (it saves the neuron_nr_, learning rate + * IW-matrix and b1-vector of the net) + */ + virtual void saveNet(string filename); + + /* loads the parameters of the net from file + * (it loads the neuron_nr_, learning rate + * IW-matrix and b1-vector of the net) + */ + virtual void loadNet(string filename); + + private: + + //----------------------------------- + /* static members + * (interface to the PD world) + */ + + /* set/get the precision of the image calculation + */ + static void setPrecision(void *data, t_floatarg precision); + static void getPrecision(void *data); + + /* method to train the network + */ + static void setTrainOn(void *data); + + /* changes the number of neurons + * (which should be the same as the audio buffer) + * ATTENTION: a new IW-matrix and b1-vector will be initialized + */ + static void setNeurons(void *data, t_floatarg neurons); + static void getNeurons(void *data); + + /* sets the learnrate of the net + */ + static void setLearnrate(void *data, t_floatarg learn_rate); + static void getLearnrate(void *data); + + /* DSP callback + */ + static void dspMessCallback(void* data, t_signal** sp); + + /* File IO: + */ + static void saveToFile(void *data, t_symbol *filename); + static void loadFromFile(void *data, t_symbol *filename); +}; + +#endif // for header file diff --git a/pix_linNN/readme.txt b/pix_linNN/readme.txt new file mode 100755 index 0000000..aa3ce84 --- /dev/null +++ b/pix_linNN/readme.txt @@ -0,0 +1,26 @@ +pix_linNN - by Georg Holzmann , 2004 +look at: http://grh.mur.at/software/thebrain.html + +--------------------------------license--------------------------------------- + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +In the official pix_recNN distribution, the GNU General Public License is +in the file gpl.txt + + +-------------------------------information----------------------------------- + +see the PD help patch \ No newline at end of file -- cgit v1.2.1