void SupportVectorMachine::predictSlidingWindow(const Feature &feat, CFloatImage &response) const { response.ReAllocate(CShape(feat.Shape().width, feat.Shape().height, 1)); response.ClearPixels(); /******** BEGIN TODO ********/ // Sliding window prediction. // // In this project we are using a linear SVM. This means that // it's classification function is very simple, consisting of a // dot product of the feature vector with a set of weights learned // during training, followed by a subtraction of a bias term // // pred <- dot(feat, weights) - bias term // // Now this is very simple to compute when we are dealing with // cropped images, our computed features have the same dimensions // as the SVM weights. Things get a little more tricky when you // want to evaluate this function over all possible subwindows of // a larger feature, one that we would get by running our feature // extraction on an entire image. // // Here you will evaluate the above expression by breaking // the dot product into a series of convolutions (remember that // a convolution can be though of as a point wise dot product with // the convolution kernel), each one with a different band. // // Convolve each band of the SVM weights with the corresponding // band in feat, and add the resulting score image. The final // step is to subtract the SVM bias term given by this->getBiasTerm(). // // Hint: you might need to set the origin for the convolution kernel // in order to get the result from convoltion to be correctly centered // // Useful functions: // Convolve, BandSelect, this->getWeights(), this->getBiasTerm() Feature weights = this->getWeights(); int nWtBands = weights.Shape().nBands; // Set the center of the window as the origin for the conv. kernel for (int band = 0; band < nWtBands; band++) { // Select a band CFloatImage featBand; CFloatImage weightBand; BandSelect(feat, featBand, band, 0); BandSelect(weights, weightBand, band, 0); // Set the origin of the kernel weightBand.origin[0] = weights.Shape().width / 2; weightBand.origin[1] = weights.Shape().height / 2; // Compute the dot product CFloatImage dotproduct; dotproduct.ClearPixels(); Convolve(featBand, dotproduct, weightBand); // Add the resulting score image for (int y = 0; y < feat.Shape().height; y++) { for (int x = 0; x < feat.Shape().width; x++) { response.Pixel(x, y, 0) += dotproduct.Pixel(x, y, 0); } // End of x loop } // End of y loop } // End of band loop // Substract the SVM bias term for (int y = 0; y < feat.Shape().height; y++) { for (int x = 0; x < feat.Shape().width; x++) { response.Pixel(x, y, 0) -= this->getBiasTerm(); } // End of x loop } // End of y loop /******** END TODO ********/ }
CFloatImage SupportVectorMachine::predictSlidingWindow(const Feature& feat) const { CFloatImage score(CShape(feat.Shape().width,feat.Shape().height,1)); score.ClearPixels(); /******** BEGIN TODO ********/ // Sliding window prediction. // // In this project we are using a linear SVM. This means that // it's classification function is very simple, consisting of a // dot product of the feature vector with a set of weights learned // during training, followed by a subtraction of a bias term // // pred <- dot(feat, weights) - bias term // // Now this is very simple to compute when we are dealing with // cropped images, our computed features have the same dimensions // as the SVM weights. Things get a little more tricky when you // want to evaluate this function over all possible subwindows of // a larger feature, one that we would get by running our feature // extraction on an entire image. // // Here you will evaluate the above expression by breaking // the dot product into a series of convolutions (remember that // a convolution can be though of as a point wise dot product with // the convolution kernel), each one with a different band. // // Convolve each band of the SVM weights with the corresponding // band in feat, and add the resulting score image. The final // step is to subtract the SVM bias term given by this->getBiasTerm(). // // Hint: you might need to set the origin for the convolution kernel // in order to get the result from convoltion to be correctly centered // // Useful functions: // Convolve, BandSelect, this->getWeights(), this->getBiasTerm() //printf("TODO: SupportVectorMachine.cpp:273\n"); exit(EXIT_FAILURE); Feature weights = getWeights(); for (int b=0; b<feat.Shape().nBands; b++){ CFloatImage currentBandWeights = CFloatImage(weights.Shape().width, weights.Shape().height, 1); CFloatImage currentBandFeatures = CFloatImage(feat.Shape().width, feat.Shape().height, 1); CFloatImage convolved = CFloatImage(CShape(feat.Shape().width, feat.Shape().height, 1)); CFloatImage final(CShape(feat.Shape().width, feat.Shape().height, 1)); BandSelect(weights, currentBandWeights, b, 0); BandSelect(feat, currentBandFeatures, b, 0); currentBandWeights.origin[0] = weights.origin[0]; currentBandWeights.origin[1] = weights.origin[1]; Convolve(feat, convolved, currentBandWeights); BandSelect(convolved, final, b, 0); try{ score += final; } catch (CError err) { printf("OH NOES: the final chapter!"); } } score-=getBiasTerm(); /******** END TODO ********/ return score; }