Commit d4848938 authored by Marina Noskova's avatar Marina Noskova

Deleted functions makeTrainData() and makeTestData() in test_svmsgd.cpp.

Added function makeData() in test_svmsgd.cpp.
parent 74c87a26
...@@ -1626,10 +1626,10 @@ public: ...@@ -1626,10 +1626,10 @@ public:
* stepDecreasingPower = 1; * stepDecreasingPower = 1;
* termCrit.maxCount = 100000; * termCrit.maxCount = 100000;
* termCrit.epsilon = 0.00001; * termCrit.epsilon = 0.00001;
* @param svmsgdType is the type of SVMSGD classifier. Legal values are SvmsgdType::SGD and SvmsgdType::ASGD. * @param svmsgdType is the type of SVMSGD classifier. Legal values are SVMSGD::SvmsgdType::SGD and SVMSGD::SvmsgdType::ASGD.
* Recommended value is SvmsgdType::ASGD (by default). * Recommended value is SVMSGD::SvmsgdType::ASGD (by default).
* @param marginType is the type of margin constraint. Legal values are MarginType::SOFT_MARGIN and MarginType::HARD_MARGIN. * @param marginType is the type of margin constraint. Legal values are SVMSGD::MarginType::SOFT_MARGIN and SVMSGD::MarginType::HARD_MARGIN.
* Default value is MarginType::SOFT_MARGIN. * Default value is SVMSGD::MarginType::SOFT_MARGIN.
*/ */
CV_WRAP virtual void setOptimalParameters(int svmsgdType = SVMSGD::ASGD, int marginType = SVMSGD::SOFT_MARGIN) = 0; CV_WRAP virtual void setOptimalParameters(int svmsgdType = SVMSGD::ASGD, int marginType = SVMSGD::SOFT_MARGIN) = 0;
......
...@@ -142,6 +142,7 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier) ...@@ -142,6 +142,7 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
int samplesCount = samples.rows; int samplesCount = samples.rows;
average = Mat(1, featuresCount, samples.type()); average = Mat(1, featuresCount, samples.type());
CV_Assert(average.type() == CV_32FC1);
for (int featureIndex = 0; featureIndex < featuresCount; featureIndex++) for (int featureIndex = 0; featureIndex < featuresCount; featureIndex++)
{ {
average.at<float>(featureIndex) = static_cast<float>(mean(samples.col(featureIndex))[0]); average.at<float>(featureIndex) = static_cast<float>(mean(samples.col(featureIndex))[0]);
...@@ -170,11 +171,11 @@ void SVMSGDImpl::makeExtendedTrainSamples(const Mat &trainSamples, Mat &extended ...@@ -170,11 +171,11 @@ void SVMSGDImpl::makeExtendedTrainSamples(const Mat &trainSamples, Mat &extended
cv::hconcat(normalizedTrainSamples, onesCol, extendedTrainSamples); cv::hconcat(normalizedTrainSamples, onesCol, extendedTrainSamples);
} }
void SVMSGDImpl::updateWeights(InputArray _sample, bool firstClass, float stepSize, Mat& weights) void SVMSGDImpl::updateWeights(InputArray _sample, bool positive, float stepSize, Mat& weights)
{ {
Mat sample = _sample.getMat(); Mat sample = _sample.getMat();
int response = firstClass ? 1 : -1; // ensure that trainResponses are -1 or 1 int response = positive ? 1 : -1; // ensure that trainResponses are -1 or 1
if ( sample.dot(weights) * response > 1) if ( sample.dot(weights) * response > 1)
{ {
...@@ -197,6 +198,7 @@ float SVMSGDImpl::calcShift(InputArray _samples, InputArray _responses) const ...@@ -197,6 +198,7 @@ float SVMSGDImpl::calcShift(InputArray _samples, InputArray _responses) const
Mat trainResponses = _responses.getMat(); Mat trainResponses = _responses.getMat();
CV_Assert(trainResponses.type() == CV_32FC1);
for (int samplesIndex = 0; samplesIndex < trainSamplesCount; samplesIndex++) for (int samplesIndex = 0; samplesIndex < trainSamplesCount; samplesIndex++)
{ {
Mat currentSample = trainSamples.row(samplesIndex); Mat currentSample = trainSamples.row(samplesIndex);
...@@ -261,7 +263,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int) ...@@ -261,7 +263,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
RNG rng(0); RNG rng(0);
CV_Assert (params.termCrit.type & TermCriteria::COUNT || params.termCrit.type & TermCriteria::EPS); CV_Assert ((params.termCrit.type & TermCriteria::COUNT || params.termCrit.type & TermCriteria::EPS) && (trainResponses.type() == CV_32FC1));
int maxCount = (params.termCrit.type & TermCriteria::COUNT) ? params.termCrit.maxCount : INT_MAX; int maxCount = (params.termCrit.type & TermCriteria::COUNT) ? params.termCrit.maxCount : INT_MAX;
double epsilon = (params.termCrit.type & TermCriteria::EPS) ? params.termCrit.epsilon : 0; double epsilon = (params.termCrit.type & TermCriteria::EPS) ? params.termCrit.epsilon : 0;
...@@ -300,7 +302,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int) ...@@ -300,7 +302,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
weights_ = extendedWeights(roi); weights_ = extendedWeights(roi);
weights_ *= multiplier; weights_ *= multiplier;
CV_Assert(params.marginType == SOFT_MARGIN || params.marginType == HARD_MARGIN); CV_Assert((params.marginType == SOFT_MARGIN || params.marginType == HARD_MARGIN) && (extendedWeights.type() == CV_32FC1));
if (params.marginType == SOFT_MARGIN) if (params.marginType == SOFT_MARGIN)
{ {
...@@ -332,7 +334,7 @@ float SVMSGDImpl::predict( InputArray _samples, OutputArray _results, int ) cons ...@@ -332,7 +334,7 @@ float SVMSGDImpl::predict( InputArray _samples, OutputArray _results, int ) cons
else else
{ {
CV_Assert( nSamples == 1 ); CV_Assert( nSamples == 1 );
results = Mat(1, 1, CV_32F, &result); results = Mat(1, 1, CV_32FC1, &result);
} }
for (int sampleIndex = 0; sampleIndex < nSamples; sampleIndex++) for (int sampleIndex = 0; sampleIndex < nSamples; sampleIndex++)
......
...@@ -62,8 +62,7 @@ public: ...@@ -62,8 +62,7 @@ public:
private: private:
virtual void run( int start_from ); virtual void run( int start_from );
static float decisionFunction(const Mat &sample, const Mat &weights, float shift); static float decisionFunction(const Mat &sample, const Mat &weights, float shift);
void makeTrainData(Mat weights, float shift); void makeData(int samplesCount, Mat weights, float shift, RNG rng, Mat &samples, Mat & responses);
void makeTestData(Mat weights, float shift);
void generateSameBorders(int featureCount); void generateSameBorders(int featureCount);
void generateDifferentBorders(int featureCount); void generateDifferentBorders(int featureCount);
...@@ -108,46 +107,28 @@ void CV_SVMSGDTrainTest::generateDifferentBorders(int featureCount) ...@@ -108,46 +107,28 @@ void CV_SVMSGDTrainTest::generateDifferentBorders(int featureCount)
} }
} }
void CV_SVMSGDTrainTest::makeTrainData(Mat weights, float shift) float CV_SVMSGDTrainTest::decisionFunction(const Mat &sample, const Mat &weights, float shift)
{ {
int datasize = 100000; return static_cast<float>(sample.dot(weights)) + shift;
int featureCount = weights.cols;
RNG rng(0);
cv::Mat samples = cv::Mat::zeros(datasize, featureCount, CV_32FC1);
for (int featureIndex = 0; featureIndex < featureCount; featureIndex++)
{
rng.fill(samples.col(featureIndex), RNG::UNIFORM, borders[featureIndex].first, borders[featureIndex].second);
}
cv::Mat responses = cv::Mat::zeros(datasize, 1, CV_32FC1);
for (int sampleIndex = 0; sampleIndex < datasize; sampleIndex++)
{
responses.at<float>(sampleIndex) = decisionFunction(samples.row(sampleIndex), weights, shift) > 0 ? 1.f : -1.f;
}
data = TrainData::create(samples, cv::ml::ROW_SAMPLE, responses);
} }
void CV_SVMSGDTrainTest::makeTestData(Mat weights, float shift) void CV_SVMSGDTrainTest::makeData(int samplesCount, Mat weights, float shift, RNG rng, Mat &samples, Mat & responses)
{ {
int testSamplesCount = 100000;
int featureCount = weights.cols; int featureCount = weights.cols;
cv::RNG rng(42);
testSamples.create(testSamplesCount, featureCount, CV_32FC1); samples.create(samplesCount, featureCount, CV_32FC1);
for (int featureIndex = 0; featureIndex < featureCount; featureIndex++) for (int featureIndex = 0; featureIndex < featureCount; featureIndex++)
{ {
rng.fill(testSamples.col(featureIndex), RNG::UNIFORM, borders[featureIndex].first, borders[featureIndex].second); rng.fill(samples.col(featureIndex), RNG::UNIFORM, borders[featureIndex].first, borders[featureIndex].second);
} }
testResponses.create(testSamplesCount, 1, CV_32FC1); responses.create(samplesCount, 1, CV_32FC1);
for (int i = 0 ; i < testSamplesCount; i++) for (int i = 0 ; i < samplesCount; i++)
{ {
testResponses.at<float>(i) = decisionFunction(testSamples.row(i), weights, shift) > 0 ? 1.f : -1.f; responses.at<float>(i) = decisionFunction(samples.row(i), weights, shift) > 0 ? 1.f : -1.f;
} }
} }
CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDataType _type, double _precision) CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDataType _type, double _precision)
...@@ -169,13 +150,16 @@ CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDat ...@@ -169,13 +150,16 @@ CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDat
CV_Error(CV_StsBadArg, "Unknown train data type"); CV_Error(CV_StsBadArg, "Unknown train data type");
} }
makeTrainData(weights, shift); RNG rng(0);
makeTestData(weights, shift);
}
float CV_SVMSGDTrainTest::decisionFunction(const Mat &sample, const Mat &weights, float shift) Mat trainSamples;
{ Mat trainResponses;
return static_cast<float>(sample.dot(weights)) + shift; int trainSamplesCount = 10000;
makeData(trainSamplesCount, weights, shift, rng, trainSamples, trainResponses);
data = TrainData::create(trainSamples, cv::ml::ROW_SAMPLE, trainResponses);
int testSamplesCount = 100000;
makeData(testSamplesCount, weights, shift, rng, testSamples, testResponses);
} }
void CV_SVMSGDTrainTest::run( int /*start_from*/ ) void CV_SVMSGDTrainTest::run( int /*start_from*/ )
...@@ -205,7 +189,6 @@ void CV_SVMSGDTrainTest::run( int /*start_from*/ ) ...@@ -205,7 +189,6 @@ void CV_SVMSGDTrainTest::run( int /*start_from*/ )
} }
} }
void makeWeightsAndShift(int featureCount, Mat &weights, float &shift) void makeWeightsAndShift(int featureCount, Mat &weights, float &shift)
{ {
weights.create(1, featureCount, CV_32FC1); weights.create(1, featureCount, CV_32FC1);
...@@ -253,7 +236,7 @@ TEST(ML_SVMSGD, trainSameScale100) ...@@ -253,7 +236,7 @@ TEST(ML_SVMSGD, trainSameScale100)
float shift = 0; float shift = 0;
makeWeightsAndShift(featureCount, weights, shift); makeWeightsAndShift(featureCount, weights, shift);
CV_SVMSGDTrainTest test(weights, shift, CV_SVMSGDTrainTest::UNIFORM_SAME_SCALE); CV_SVMSGDTrainTest test(weights, shift, CV_SVMSGDTrainTest::UNIFORM_SAME_SCALE, 0.02);
test.safe_run(); test.safe_run();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment