void transform( qiv_image *q, enum Orientation orientation) { switch (orientation) { default: return; case HFLIP: flipH(q); snprintf(infotext, sizeof infotext, "(Flipped horizontally)"); break; case VFLIP: flipV(q); snprintf(infotext, sizeof infotext, "(Flipped vertically)"); break; case ROT_180: rot180(q); snprintf(infotext, sizeof infotext, "(Turned upside down)"); break; case TRANSPOSE: transpose(q); swapWH(q); snprintf(infotext, sizeof infotext, "(Transposed)"); break; case ROT_90: rot90(q); swapWH(q); snprintf(infotext, sizeof infotext, "(Rotated left)"); break; case TRANSVERSE: transpose(q); rot180(q); swapWH(q); snprintf(infotext, sizeof infotext, "(Transversed)"); break; case ROT_270: rot270(q); swapWH(q); snprintf(infotext, sizeof infotext, "(Rotated left)"); break; } }
void ConvolutionalLayer::feedBackward( vector<mat>& errors, const vector<mat>& deltas) { // Since nInputs == nOutputs for subsampling layer, I just use N. size_t nInputs = getNumInputMaps(), nOutputs = getNumOutputMaps(); SIZE s = this->get_input_img_size(); size_t batch_size = deltas[0].getCols(); vector<vector<mat> > oImgs(nOutputs), iImgs(nInputs); for (size_t j=0; j<nOutputs; ++j) oImgs[j] = reshapeVectors2Images(deltas[j], this->get_output_img_size()); for (size_t i=0; i<nInputs; ++i) iImgs[i].resize(batch_size); for (size_t k=0; k<batch_size; ++k) { for (size_t i=0; i<nInputs; ++i) { iImgs[i][k].resize(s.m, s.n, 0); for (size_t j=0; j<nOutputs; ++j) iImgs[i][k] += convn(oImgs[j][k], rot180(_kernels[i][j]), "full"); } } if (errors.size() != nInputs) errors.resize(nInputs); for (size_t i=0; i<nInputs; ++i) errors[i] = reshapeImages2Vectors(iImgs[i]); }
inline T Image2D<T>::rotN90 (int row, int col, int N) // Generalazed rotation by N*90 degree { if (N==0) return rot000(row,col); else if (N==1) return rot090(row,col); else if (N==2) return rot180(row,col); else if (N==3) return rot270(row,col); else return rot000(row,col); }
void ConvolutionalLayer::backPropagate(vector<mat>& errors, const vector<mat>& fins, const vector<mat>& fouts, float learning_rate) { size_t nInputs = getNumInputMaps(), nOutputs = getNumOutputMaps(); size_t batch_size = fins[0].getCols(); // In the following codes, the iteration index i and j stands for // i : # of input features. i = 0 ~ nInputs - 1 // j : # of output features. j = 0 ~ nOutputs - 1 vector<mat> deltas(nOutputs); for (size_t j=0; j<nOutputs; ++j) deltas[j] = fouts[j] & ( 1.0f - fouts[j] ) & errors[j]; this->feedBackward(errors, deltas); assert(learning_rate > 0); float lr = learning_rate / batch_size; // iImgs represents the input images. // oImgs represents the output images. (Before sigmoid or any other activation function) vector<vector<mat> > iImgs(nInputs), oImgs(nOutputs); for (size_t i=0; i<nInputs; ++i) iImgs[i] = reshapeVectors2Images(fins[i], _input_img_size); for (size_t j=0; j<nOutputs; ++j) oImgs[j] = reshapeVectors2Images(deltas[j], this->get_output_img_size()); // Update kernels with learning rate for (size_t k=0; k<batch_size; ++k) { for (size_t j=0; j<nOutputs; ++j) { for (size_t i=0; i<nInputs; ++i) _kernels[i][j] -= convn(rot180(iImgs[i][k]), oImgs[j][k], "valid_shm") * lr; _bias[j] -= sum_all(oImgs[j][k]) * lr; } } }
/* In the constructor, we just pass the standard parameters on to QWidget. The menu uses a single slot to simplify the process of adding more items to the options menu. */ ImageViewer::ImageViewer( QWidget *parent, const char *name, int wFlags ) : QWidget( parent, name, wFlags ), conversion_flags( PreferDither ), helpmsg( 0 ) { pickx = -1; picky = -1; clickx = -1; clicky = -1; alloc_context = 0; menubar = new QMenuBar(this); menubar->setSeparator( QMenuBar::InWindowsStyle ); QStrList fmt = QImage::outputFormats(); saveimage = new QPopupMenu( menubar ); savepixmap = new QPopupMenu( menubar ); for (const char* f = fmt.first(); f; f = fmt.next()) { saveimage->insertItem( f ); savepixmap->insertItem( f ); } connect( saveimage, SIGNAL(activated(int)), this, SLOT(saveImage(int)) ); connect( savepixmap, SIGNAL(activated(int)), this, SLOT(savePixmap(int)) ); file = new QPopupMenu( menubar ); menubar->insertItem( "&File", file ); file->insertItem( "&New window", this, SLOT(newWindow()), CTRL+Key_N ); file->insertItem( "&Open...", this, SLOT(openFile()), CTRL+Key_O ); si = file->insertItem( "Save image", saveimage ); sp = file->insertItem( "Save pixmap", savepixmap ); file->insertSeparator(); file->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q ); edit = new QPopupMenu( menubar ); menubar->insertItem( "&Edit", edit ); edit->insertItem("&Copy", this, SLOT(copy()), CTRL+Key_C); edit->insertItem("&Paste", this, SLOT(paste()), CTRL+Key_V); edit->insertSeparator(); edit->insertItem("&Horizontal flip", this, SLOT(hFlip()), ALT+Key_H); edit->insertItem("&Vertical flip", this, SLOT(vFlip()), ALT+Key_V); edit->insertItem("&Rotate 180", this, SLOT(rot180()), ALT+Key_R); edit->insertSeparator(); edit->insertItem("&Text...", this, SLOT(editText())); edit->insertSeparator(); t1 = edit->insertItem( "Convert to &1 bit", this, SLOT(to1Bit()) ); t8 = edit->insertItem( "Convert to &8 bit", this, SLOT(to8Bit()) ); t32 = edit->insertItem( "Convert to &32 bit", this, SLOT(to32Bit()) ); options = new QPopupMenu( menubar ); menubar->insertItem( "&Options", options ); ac = options->insertItem( "AutoColor" ); co = options->insertItem( "ColorOnly" ); mo = options->insertItem( "MonoOnly" ); options->insertSeparator(); fd = options->insertItem( "DiffuseDither" ); bd = options->insertItem( "OrderedDither" ); td = options->insertItem( "ThresholdDither" ); options->insertSeparator(); ta = options->insertItem( "ThresholdAlphaDither" ); ba = options->insertItem( "OrderedAlphaDither" ); fa = options->insertItem( "DiffuseAlphaDither" ); options->insertSeparator(); ad = options->insertItem( "PreferDither" ); dd = options->insertItem( "AvoidDither" ); options->insertSeparator(); ss = options->insertItem( "Smooth scaling" ); cc = options->insertItem( "Use color context" ); if ( QApplication::colorSpec() == QApplication::ManyColor ) options->setItemEnabled( cc, FALSE ); options->setCheckable( TRUE ); setMenuItemFlags(); menubar->insertSeparator(); QPopupMenu* help = new QPopupMenu( menubar ); menubar->insertItem( "&Help", help ); help->insertItem( "Help!", this, SLOT(giveHelp()), CTRL+Key_H ); connect( options, SIGNAL(activated(int)), this, SLOT(doOption(int)) ); status = new QLabel(this); status->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); status->setFixedHeight( fontMetrics().height() + 4 ); setMouseTracking( TRUE ); }
static Image* _cachedpage(Document *doc, int angle, int page, char *ra) { int i; Cached *c, old; Image *im, *tmp; if((page < 0 || page >= doc->npage) && !doc->fwdonly) return nil; Again: for(i=0; i<nelem(cache); i++){ c = &cache[i]; if(c->doc == doc && c->angle == angle && c->page == page){ if(chatty) fprint(2, "cache%s hit %d\n", ra, page); goto Found; } if(c->doc == nil) break; } if(i >= nelem(cache)) i = nelem(cache)-1; c = &cache[i]; if(c->im) freeimage(c->im); c->im = nil; c->doc = nil; c->page = -1; if(chatty) fprint(2, "cache%s load %d\n", ra, page); im = doc->drawpage(doc, page); if(im == nil){ if(doc->fwdonly) /* end of file */ wexits(0); im = questionmark(); if(im == nil){ Flush: if(i > 0){ cacheflush(); goto Again; } fprint(2, "out of memory: %r\n"); wexits("memory"); } return im; } if(im->r.min.x != 0 || im->r.min.y != 0){ /* translate to 0,0 */ tmp = xallocimage(display, Rect(0, 0, Dx(im->r), Dy(im->r)), im->chan, 0, DNofill); if(tmp == nil){ freeimage(im); goto Flush; } drawop(tmp, tmp->r, im, nil, im->r.min, S); freeimage(im); im = tmp; } switch(angle){ case 90: im = rot90(im); break; case 180: rot180(im); break; case 270: im = rot270(im); break; } if(im == nil) goto Flush; c->doc = doc; c->page = page; c->angle = angle; c->im = im; Found: if(chatty) fprint(2, "cache%s mtf %d @%d:", ra, c->page, i); old = *c; memmove(cache+1, cache, (c-cache)*sizeof cache[0]); cache[0] = old; if(chatty){ for(i=0; i<nelem(cache); i++) fprint(2, " %d", cache[i].page); fprint(2, "\n"); } if(chatty) fprint(2, "cache%s return %d %p\n", ra, old.page, old.im); return old.im; }
/* In the constructor, we just pass the standard parameters on to QWidget. The menu uses a single slot to simplify the process of adding more items to the options menu. */ FLImageViewer::FLImageViewer( QWidget *parent, const char *name, WFlags fl ) : QWidget( parent, name, fl | Qt::WDestructiveClose | Qt::WType_Dialog | Qt::WShowModal | Qt::WStyle_Maximize | Qt::WStyle_SysMenu ), conversion_flags( PreferDither ), helpmsg( 0 ) { pickx = -1; picky = -1; clickx = -1; clicky = -1; alloc_context = 0; menubar = new QMenuBar( this ); menubar->setSeparator( QMenuBar::InWindowsStyle ); QStrList fmt = QImage::outputFormats(); saveimage = new QPopupMenu( menubar ); savepixmap = new QPopupMenu( menubar ); for ( const char* f = fmt.first(); f; f = fmt.next() ) { saveimage->insertItem( f ); savepixmap->insertItem( f ); } connect( saveimage, SIGNAL( activated( int ) ), this, SLOT( saveImage( int ) ) ); connect( savepixmap, SIGNAL( activated( int ) ), this, SLOT( savePixmap( int ) ) ); file = new QPopupMenu( menubar ); menubar->insertItem( tr( "&Archivo" ), file ); //file->insertItem( "&New window", this, SLOT(newWindow()), CTRL+Key_N ); file->insertItem( tr( "&Abrir..." ), this, SLOT( openFile() ), CTRL + Key_O ); si = file->insertItem( tr( "Guardar imagen" ), saveimage ); sp = file->insertItem( tr( "Guardar pixmap" ), savepixmap ); //file->insertSeparator(); //file->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q ); edit = new QPopupMenu( menubar ); menubar->insertItem( tr( "&Edición" ), edit ); edit->insertItem( tr( "&Copiar" ), this, SLOT( copy() ), CTRL + Key_C ); edit->insertItem( tr( "&Pegar" ), this, SLOT( paste() ), CTRL + Key_V ); edit->insertSeparator(); edit->insertItem( tr( "Volteo &Horizontal" ), this, SLOT( hFlip() ), ALT + Key_H ); edit->insertItem( tr( "Volteo &Vertical" ), this, SLOT( vFlip() ), ALT + Key_V ); edit->insertItem( tr( "&Rotar 180" ), this, SLOT( rot180() ), ALT + Key_R ); edit->insertSeparator(); //edit->insertItem("&Text...", this, SLOT(editText())); //edit->insertSeparator(); t1 = edit->insertItem( tr( "Convertir a &1 bit" ), this, SLOT( to1Bit() ) ); t8 = edit->insertItem( tr( "Convertir a &8 bit" ), this, SLOT( to8Bit() ) ); t32 = edit->insertItem( tr( "Convertir a &32 bit" ), this, SLOT( to32Bit() ) ); options = new QPopupMenu( menubar ); menubar->insertItem( tr( "&Opciones" ), options ); ac = options->insertItem( "AutoColor" ); co = options->insertItem( "ColorOnly" ); mo = options->insertItem( "MonoOnly" ); options->insertSeparator(); fd = options->insertItem( "DiffuseDither" ); bd = options->insertItem( "OrderedDither" ); td = options->insertItem( "ThresholdDither" ); options->insertSeparator(); ta = options->insertItem( "ThresholdAlphaDither" ); ba = options->insertItem( "OrderedAlphaDither" ); fa = options->insertItem( "DiffuseAlphaDither" ); options->insertSeparator(); ad = options->insertItem( "PreferDither" ); dd = options->insertItem( "AvoidDither" ); options->insertSeparator(); ss = options->insertItem( tr( "Escalado suavizado" ) ); cc = options->insertItem( tr( "Usar contexto de color" ) ); if ( QApplication::colorSpec() == QApplication::ManyColor ) options->setItemEnabled( cc, FALSE ); options->setCheckable( TRUE ); setMenuItemFlags(); //menubar->insertSeparator(); //QPopupMenu* help = new QPopupMenu( menubar ); //menubar->insertItem( "&Help", help ); //help->insertItem( "Help!", this, SLOT(giveHelp()), CTRL+Key_H ); connect( options, SIGNAL( activated( int ) ), this, SLOT( doOption( int ) ) ); status = new QLabel( this ); status->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); status->setFixedHeight( fontMetrics().height() + 4 ); setMouseTracking( TRUE ); }