下面是用來測試 tcl-opencv 新加入的 Boost 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
set data [$dataMat convert $::cv::CV_32F]
set labels [$labelsMat convert $::cv::CV_32S]
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_32S]
set labels_test [cv::Mat::Mat 0 0 $::cv::CV_32S]
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
showImage $data_train 28 "train data"
showImage $data_test 28 "test data"
cv::waitKey 0
set boost [::cv::ml::Boost]
# It is necessary to setup max
$boost setMaxDepth 10
$boost setUseSurrogates 0
$boost setBoostType $::cv::ml::BOOST_REAL
$boost setWeakCount 100
$boost setWeightTrimRate 0.95
puts "Training..."
set trainData [::cv::ml::TrainData $data_train $::cv::ml::ROW_SAMPLE $labels_train]
$boost train $trainData
$trainData close
$data_train close
$labels_train close
$boost save "boost.xml"
$boost close
set boost [::cv::ml::Boost::load "boost.xml"]
puts "Predicting..."
set response [$boost predict $data_test]
set res [lindex $response 1]
$boost 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: "
set res2 [$res convert $::cv::CV_32S]
for {set i 0} {$i < [$res2 rows]} {incr i} {
puts -nonewline "[$res2 at [list $i 0] 0] "
}
puts ""
set correct 0
for {set i 0} {$i < [$labels_test rows]} {incr i} {
if {[$res2 at [list $i 0] 0]==[$labels_test at [list $i 0] 0]} {
incr correct
}
}
puts "accuracy: [expr 100 * $correct/[$labels_test rows]]"
$res close
$res2 close
$data_test close
$labels_test close
沒有留言:
張貼留言