halcon多层感知器MLP的使用

2020-05-18  本文已影响0人  卡拉肖克_潘

一、用于图像像素灰度级分类

step1:准备数据
在图像选择区域作为训练集,可以是矩形,也可以是任意形状的区域。

gen_rectangle1 (background, 95.4589, 5.20444, 132.872, 919.265)
gen_region_runs (foreground, [315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372], [834,834,831,831,831,830,828,828,828,827,826,824,824,824,823,823,823,821,821,821,820,820,819,819,819,819,819,817,817,817,817,816,816,815,815,815,815,815,815,813,813,813,813,813,813,813,813,812,812,812,812,812,812,813,816,817,820,820], [847,856,859,860,863,867,867,867,867,867,867,867,867,867,867,867,867,867,866,866,866,866,866,866,864,864,864,864,863,863,862,862,862,861,860,860,860,859,859,858,856,856,856,855,854,852,852,851,850,848,846,846,844,840,837,834,834,834])

step2:创建一个MLP

create_class_mlp (3, 3, 2, 'softmax', 'principal_components', 3, 42, MLPHandle)

step3:添加训练数据

gen_empty_obj (ClassSamples)
concat_obj (ClassSamples, background, ClassSamples)
concat_obj (ClassSamples, foreground, ClassSamples)
add_samples_image_class_mlp (trainImage, ClassSamples, MLPHandle)

step4:查看训练样本数量(非必须)

get_sample_num_class_mlp (MLPHandle, NumSamples)

step5:训练

train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)

step6:映射颜色表

create_class_lut_mlp (MLPHandle, [], [], ClassLUTHandle)

step7:使用

method1:颜色表分类
classify_image_class_lut (Image, ClassRegionsLUT, ClassLUTHandle)
method2:分类器直接分类,准确性会好一些,但会慢很多,用颜色表8ms的情况下,直接分类需要33ms。
classify_image_class_mlp (Image, ClassRegionsMLP, MLPHandle, 0.5)

step8:看效果

select_obj (ClassRegionsLUT, background, 1)
dev_set_color ('green')
dev_display (background)
select_obj (ClassRegionsLUT, foreground, 2)
dev_set_color ('red')
dev_display (foreground)

参考:classify_image_class_lut.hdev,texture_laws_mlp.hdev,segment_citrus_fruits.hdev

二、用于线性回归和数据分类

* Use the MLP for regression (function approximation)
create_class_mlp (1, NumHidden, 1, 'linear', 'none', 1, 42, MLPHandle)
* Generate the training data
* D = [...]
* T = [...]
* Add the training data
for J := 0 to NumData-1 by 1
    add_sample_class_mlp (MLPHandle, D[J], T[J])
endfor
* Train the MLP
train_class_mlp (MLPHandle, 200, 0.001, 0.001, Error, ErrorLog)
* Generate test data
* X = [...]
* Compute the output of the MLP on the test data
for J := 0 to N-1 by 1
    evaluate_class_mlp (MLPHandle, X[J], Y)
endfor

* Use the MLP for classification
create_class_mlp (NumIn, NumHidden, NumOut, 'softmax', \
                  'normalization', NumIn, 42, MLPHandle)
* Generate and add the training data
for J := 0 to NumData-1 by 1
    * Generate training features and classes
    * Data = [...]
    * Class = [...]
    add_sample_class_mlp (MLPHandle, Data, Class)
endfor
* Train the MLP
train_class_mlp (MLPHandle, 100, 1, 0.01, Error, ErrorLog)
* Use the MLP to classify unknown data
for J := 0 to N-1 by 1
    * Extract features
    * Features = [...]
    classify_class_mlp (MLPHandle, Features, 1, Class, Confidence)
endfor

参考:create_class_mlp (Operator)

三、其他

1、将MLP模型写入文件
write_class_mlp( : : MLPHandle, FileName : )

create_class_mlp (NumIn, NumHidden, NumOut, 'softmax', \
                  'normalization', 1, 42, MLPHandle)
read_samples_class_mlp (MLPHandle, 'samples.mtf')
train_class_mlp (MLPHandle, 100, 1, 0.01, Error, ErrorLog)
write_class_mlp (MLPHandle, 'classifier.mlp')

2、从文件中读入一个MLP模型
read_class_mlp( : : FileName : MLPHandle)
3、清除MLP
clear_class_mlp( : : MLPHandle : )

上一篇下一篇

猜你喜欢

热点阅读