下面是用來測試 tcl-opencv 新加入的 SVMSGD command,寫了一個簡單的測試程式來測試:
package require opencv
proc showImage {image columns name} {
set bigImage [cv::Mat::Mat 0 0 $::cv::CV_32F]
for {set i 0} {$i < [$image rows]} {incr i} {
set row [$image rowRange $i [expr $i + 1]]
set rs [$row reshape 0 $columns]
$bigImage push_back $rs
$row close
$rs close
}
set bigImageT [$bigImage transpose]
::cv::imshow $name $bigImageT
$bigImageT close
$bigImage close
}
#
# Download file from:
# https://github.com/opencv/opencv/tree/master/samples/data/data01.xml
#
set filename "data01.xml"
set f [::cv::FileStorage]
$f open $filename $::cv::FileStorage::READ
set dataMat [$f readMat datamat]
set labelsMat [$f readMat labelsmat]
$f close
# Notice: SVMSGD label type is CV_32F
set data [$dataMat convert $::cv::CV_32F]
set labels [$labelsMat convert $::cv::CV_32F]
puts "Loading training data... read [$data rows] rows of data"
$dataMat close
$labelsMat close
set data_train [cv::Mat::Mat 0 0 $::cv::CV_32F]
set data_test [cv::Mat::Mat 0 0 $::cv::CV_32F]
set labels_train [cv::Mat::Mat 0 0 $::cv::CV_32F]
set labels_test [cv::Mat::Mat 0 0 $::cv::CV_32F]
for {set i 0} {$i < [$data rows]} {incr i} {
if {[expr $i%2]==0} {
$data_train push_back [$data rowRange $i [expr $i + 1]]
$labels_train push_back [$labels rowRange $i [expr $i + 1]]
} else {
$data_test push_back [$data rowRange $i [expr $i + 1]]
$labels_test push_back [$labels rowRange $i [expr $i + 1]]
}
}
$data close
$labels close
# SVMSGD labels is -1 and 1, so update our labels
for {set i 0} {$i < [$labels_train rows]} {incr i} {
if {[$labels_train at [list $i 0] 0]==0} {
$labels_train at [list $i 0] 0 -1.0
}
puts [$labels_train at [list $i 0] 0]
}
for {set i 0} {$i < [$labels_test rows]} {incr i} {
if {[$labels_test at [list $i 0] 0]==0} {
$labels_test at [list $i 0] 0 -1.0
}
}
showImage $data_train 28 "train data"
showImage $data_test 28 "test data"
cv::waitKey 0
set term [::cv::TermCriteria [expr $::cv::EPS | $::cv::COUNT] 1000 0.001]
set svmsgd [::cv::ml::SVMSGD]
$svmsgd setSvmsgdType $::cv::ml::SVMSGD_ASGD
$svmsgd setMarginType $::cv::ml::SVMSGD_HARD_MARGIN
$svmsgd setMarginRegularization 0.00001
$svmsgd setInitialStepSize 0.05
$svmsgd setStepDecreasingPower 0.75
$svmsgd setTermCriteria $term
puts "Training..."
set trainData [::cv::ml::TrainData $data_train $::cv::ml::ROW_SAMPLE $labels_train]
$svmsgd train $trainData
$trainData close
$data_train close
$labels_train close
$term close
$svmsgd save "svmsgd.xml"
puts "Predicting..."
set response [$svmsgd predict $data_test]
set res [lindex $response 1]
$svmsgd close
puts ""
puts "Labels test: "
for {set i 0} {$i < [$labels_test rows]} {incr i} {
puts -nonewline "[$labels_test at [list $i 0] 0] "
}
puts ""
puts "Response: [$res size]"
for {set i 0} {$i < [$res rows]} {incr i} {
puts -nonewline "[$res at [list $i 0] 0] "
}
puts ""
set correct 0
for {set i 0} {$i < [$labels_test rows]} {incr i} {
if {[$res at [list $i 0] 0]==[$labels_test at [list $i 0] 0]} {
incr correct
}
}
puts "accuracy: [expr 100 * $correct/[$labels_test rows]]"
$res close
$data_test close
$labels_test close
沒有留言:
張貼留言