文章目录
前言
利用halcon实现基于机器学习的MLP算法的分类器
一、halcon脚本
*************************************train***************************************
ImageDir:='../../DataSet/DataImage_Cls/DataImage'
ModelFile:='model_MLP.dat'
ClassFile:='classes.txt'
TrainNum:=2000
AddImage:=40
ImageDataList:=[]
ClassDataList:=[]
FeatureVector:=-1
list_files (ImageDir, 'directories', Dirs)
for Index1 := 0 to |Dirs|-1 by 1
eachDir:= Dirs[Index1]
list_files (eachDir, 'files', ImageFiles)
for Index2 := 0 to |ImageFiles|-1 by 1
eachfileName:= ImageFiles[Index2]
parse_filename (eachfileName, BaseName, Extension, Directory)
tuple_regexp_match (Directory, '.*/(.*?)/', Class_Name)
tuple_concat (ImageDataList, eachfileName, ImageDataList)
tuple_concat (ClassDataList, Class_Name, ClassDataList)
if(FeatureVector==-1)
read_image (Image, eachfileName)
gen_features (Image, FeatureVector)
endif
endfor
endfor
tuple_uniq (ClassDataList, Class_Names)
open_file (ClassFile, 'output', FileHandle)
for Index3 := 0 to |Class_Names|-1 by 1
fwrite_string (FileHandle, Class_Names[Index3])
fnew_line (FileHandle)
endfor
close_file (FileHandle)
create_class_mlp (|FeatureVector|, 150, |Dirs|, 'softmax', 'normalization', |FeatureVector|, 42, MLPHandle)
for Indexfile:=0 to |ImageDataList|-1 by 1
read_image (Image, ImageDataList[Indexfile])
for i:=0 to AddImage by 1
USEImage:=Image
tuple_rand (1, Rand)
if(Rand[0] >0.5)
tuple_rand (1, Rand)
rotate_image (USEImage, USEImage, (Rand[0]-0.5)*90, 'constant')
endif
tuple_rand (1, Rand)
if(Rand[0] >0.5)
emphasize (USEImage, USEImage, int(Rand[0]*7), int(Rand[0]*7), 1)
endif
tuple_rand (1, Rand)
if(Rand[0] >0.5)
illuminate (USEImage, USEImage, int(Rand[0]*255), int(Rand[0]*255), Rand[0])
endif
gen_features (USEImage, FeatureVector)
tuple_find (Class_Names, ClassDataList[Indexfile], Indices)
add_sample_class_mlp (MLPHandle, FeatureVector, Indices)
endfor
gen_features (Image, FeatureVector)
tuple_find (Class_Names, ClassDataList[Indexfile], Indices)
add_sample_class_mlp (MLPHandle, FeatureVector, Indices)
endfor
*训练
train_class_mlp (MLPHandle, TrainNum, 0.99, 0.0001, Error, ErrorLog)
write_class_mlp (MLPHandle, ModelFile)
clear_class_mlp (MLPHandle)
stop()
*************************************test***************************************
ModelFile:='model_MLP.dat'
ClassFile:='classes.txt'
TestImageDir:='../../DataSet/DataImage_Cls/test'
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'white', WindowID)
dev_set_color ('black')
dev_set_draw ('margin')
dev_clear_window ()
ClassNames:=[]
open_file (ClassFile, 'input', FileHandle)
repeat
fread_line(FileHandle, oneline, IsEOF)
if(IsEOF == 1)
break
endif
if(oneline == ' ' or oneline=='/n')
continue
endif
tuple_regexp_replace (oneline, '/n', '', oneline)
tuple_regexp_replace (oneline, '\n', '', oneline)
tuple_concat (ClassNames, oneline, ClassNames)
until (IsEOF)
close_file (FileHandle)
read_class_mlp (ModelFile, MLPHandle)
list_image_files (TestImageDir, 'default', [], ImageFiles)
for Index := 0 to |ImageFiles|-1 by 1
read_image (Image, ImageFiles[Index])
gen_features (Image, FeatureVector_test)
classify_class_mlp (MLPHandle, FeatureVector_test, 1, FoundClassIDs, Confidence)
classname:= ClassNames[FoundClassIDs]
stop()
endfor
总结
实现了基于mlp的分类器