*************************************************halcon对象检测*********************************************
dev_close_window()
dev_get_window(WindowHandle)
dev_set_draw('margin')
**************************************************数据预处理**************************************************
***读取json文件
read_dict ('D:/Source/SourceMe/Halcon深度学习模型/Halcon对象检测模型/Dataset/pill_bag.json', [], [], DictJson)
***获取类别
get_dict_tuple (DictJson, 'categories', CategoryList)
get_dict_param (CategoryList, 'keys', [], CategoryKeys)
***获取图片列表
get_dict_tuple (DictJson, 'images', ImageList)
get_dict_param (ImageList, 'keys', [], ImageKeys)
***获取标注
get_dict_tuple (DictJson, 'annotations', AnnotationList)
get_dict_param (AnnotationList, 'keys', [], AnnotationKeys)
* 设置要检测类
for Index := 0 to |CategoryKeys| - 1 by 1
try
get_dict_tuple (CategoryList, CategoryKeys[Index], Category)
get_dict_tuple (Category, 'id', ID)
get_dict_tuple (Category, 'name', Name)
catch (ExceptionCategory)
throw ('COCO category number ' + Index + ' does not contain the entry id or name.')
endtry
ClassIDs[Index] := ID
ClassNames[Index] := Name
endfor
***创建模型对象检测参数
create_dict(DLModelDetectionParam)
set_dict_tuple(DLModelDetectionParam, 'class_ids', ClassIDs)
set_dict_tuple(DLModelDetectionParam, 'class_names', ClassNames)
set_dict_tuple(DLModelDetectionParam, 'instance_type','rectangle1')
set_dict_tuple(DLModelDetectionParam, 'image_width',512)
set_dict_tuple(DLModelDetectionParam, 'image_height',320)
set_dict_tuple(DLModelDetectionParam, 'max_overlap', 0.23)
set_dict_tuple(DLModelDetectionParam, 'max_overlap_class_agnostic', 0.27)
set_dict_tuple(DLModelDetectionParam, 'min_level', 2)
set_dict_tuple(DLModelDetectionParam, 'max_level', 4)
set_dict_tuple(DLModelDetectionParam, 'anchor_num_subscales', 3)
set_dict_tuple(DLModelDetectionParam, 'anchor_aspect_ratios', [0.534796,0.98496,1.48999,2.24198])
set_dict_tuple(DLModelDetectionParam, 'capacity', 'medium')
***创建模型
Backbone := 'pretrained_dl_classifier_compact.hdl'
* Create the model.
create_dl_model_detection (Backbone, 10, DLModelDetectionParam, DLModelHandle)
DLModelFileName := 'D:/Source/SourceMe/Halcon深度学习模型/Halcon对象检测模型/Dataset' + '/pretrained_dl_model_detection.hdl'
write_dl_model (DLModelHandle, DLModelFileName)
***创建预处理参数
create_dict(DLPreprocessParam)
set_dict_tuple(DLPreprocessParam, 'model_type', 'detection')
set_dict_tuple(DLPreprocessParam, 'image_width', 512)
set_dict_tuple(DLPreprocessParam, 'image_height',320)
set_dict_tuple(DLPreprocessParam, 'image_num_channels',3)
set_dict_tuple(DLPreprocessParam, 'image_range_min',-127)
set_dict_tuple(DLPreprocessParam, 'image_range_max',128)
set_dict_tuple(DLPreprocessParam, 'normalization_type','none')
set_dict_tuple(DLPreprocessParam, 'domain_handling','full_domain')
set_dict_tuple(DLPreprocessParam, 'augmentation', 'false')
set_dict_tuple(DLPreprocessParam, 'instance_type', 'rectangle1')
***创建samples
DLSamples:=[]
NumberImageKeys:=|ImageKeys|
IndexAnnotKeyStart:=0
IndexAnnotKeyEnd:=|AnnotationKeys|
DatasetImageDir:='D:/Source/SourceMe/Halcon深度学习模型/Halcon对象检测模型/Dataset'
DatasetPreprocessDir:='D:/Source/SourceMe/Halcon深度学习模型/Halcon对象检测模型/Dataset/DatasetPreprocess'
***间隔多小显示下
IntervalNumShow:=60
CurrentIntervalNum:=0
for Index := 0 to NumberImageKeys-1 by 1
***获取图片信息
get_dict_tuple (ImageList, ImageKeys[Index], ImageDict)
get_dict_tuple (ImageDict, 'file_name', ImageName)
get_dict_tuple (ImageDict, 'id', ImageID)
***读取图片
ImagePath:=DatasetImageDir+'/'+ImageName
read_image(Image, ImagePath)
***获取原来图片大小
get_image_size(Image, Width, Height)
zoom_image_size (Image, Image, 512, 320, 'constant')
convert_image_type (Image, Image, 'real')
RescaleRange := (128 -(-127) ) / 255.0
scale_image (Image, Image, RescaleRange, (-127))
* 计算缩放因子
FactorResampleWidth := real(512) / Width
FactorResampleHeight := real(320) / Height
create_dict (DLSample)
set_dict_tuple (DLSample, 'image_id', ImageID)
set_dict_tuple (DLSample, 'image_file_name', ImageName)
AnnotBboxRow1s:=[]
AnnotBboxColumn1s:=[]
AnnotBboxRow2s:=[]
AnnotBboxColumn2s:=[]
AnnotClassIDs:=[]
AnnotIndexClass:=0
Find:=1
while(Find==1)
if(IndexAnnotKeyStart<IndexAnnotKeyEnd)
get_dict_tuple(AnnotationList, AnnotationKeys[IndexAnnotKeyStart], Annotation)
get_dict_tuple(Annotation, 'image_id', image_id)
if(image_id==ImageID)
get_dict_tuple(Annotation, 'category_id', category_id)
AnnotClassIDs[AnnotIndexClass]:=category_id
get_dict_tuple(Annotation, 'bbox', bbox)
get_dict_tuple(bbox, 0, column)
get_dict_tuple(bbox, 1, row)
get_dict_tuple(bbox, 2, width)
get_dict_tuple(bbox, 3, height)
AnnotBboxColumn1s[AnnotIndexClass]:=column*FactorResampleWidth-0.5
AnnotBboxRow1s[AnnotIndexClass]:=row*FactorResampleHeight-0.5
AnnotBboxColumn2s[AnnotIndexClass]:=(column+width)*FactorResampleWidth-0.5
AnnotBboxRow2s[AnnotIndexClass]:=(row+height)*FactorResampleHeight-0.5
AnnotIndexClass:=AnnotIndexClass+1
else
Find:=0
***当没有放入
IndexAnnotKeyStart:=IndexAnnotKeyStart-1
endif
IndexAnnotKeyStart:=IndexAnnotKeyStart+1
else
Find:=0
endif
endwhile
if(CurrentIntervalNum==IntervalNumShow)
***显示下预处理的图片
dev_display(Image)
gen_rectangle1(Rectangle, AnnotBboxRow1s, AnnotBboxColumn1s, AnnotBboxRow2s, AnnotBboxColumn2s)
dev_display(Rectangle)
wait_seconds(1)
CurrentIntervalNum:=-1
endif
CurrentIntervalNum:=CurrentIntervalNum+1
*wait_seconds(1)
***设置box跟id
set_dict_tuple (DLSample, 'bbox_row1', AnnotBboxRow1s)
set_dict_tuple (DLSample, 'bbox_col1', AnnotBboxColumn1s)
set_dict_tuple (DLSample, 'bbox_row2', AnnotBboxRow2s)
set_dict_tuple (DLSample, 'bbox_col2', AnnotBboxColumn2s)
set_dict_tuple (DLSample, 'bbox_label_id', AnnotClassIDs)
set_dict_object(Image, DLSample, 'image')
* Generate the output file name.
FileNameOut := ImageID + '_dlsample.hdict'
RaiseErrorWriteDict := 'true'
write_dict (DLSample, DatasetPreprocessDir + '/' + FileNameOut, 'raise_error_if_content_not_serializable', RaiseErrorWriteDict)
* Add output path to DLDataset sample dictionary.
set_dict_tuple (DLSample, 'dlsample_file_name', FileNameOut)
*
DLSamples[Index]:=DLSample
***显示预处理进度
dev_disp_text('AllImageKeys:'+NumberImageKeys+' ImageKey:'+(Index+1), 'window', 10, 10, 'black', 'box', 'true')
endfor
stop()
***开始训练
query_available_dl_devices (['runtime', 'runtime'], ['gpu', 'cpu'], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
throw ('No supported device found to continue this example.')
endif
* Due to the filter used in query_available_dl_devices, the first device is a GPU, if available.
DLDevice := DLDeviceHandles[0]
get_dl_device_param (DLDevice, 'type', DLDeviceType)
if (DLDeviceType == 'cpu')
* The number of used threads may have an impact
* on the training duration.
NumThreadsTraining := 4
set_system ('thread_num', NumThreadsTraining)
endif
***读取初始模型
*read_dl_model (DLModelFileName, DLModelHandle)
BatchSize:=2
*设置批量
set_dl_model_param (DLModelHandle, 'batch_size', BatchSize)
*设置训练的设备
set_dl_model_param (DLModelHandle, 'device', DLDevice)
***初始化模型学习率
set_dl_model_param(DLModelHandle, 'learning_rate', 0.0001)
***模型训练周期次数
NumEpochs:=100
***数据批次的计数
DatasetSampleIndex:=0
***训练结果的集合
TrainResultsIndex:=0
TrainResults:=[]
***创建一个显示训练的窗口
dev_open_window(0, 0, 512, 512, 'black', WindowHandleTrain)
***创建一个显示信息的窗口
dev_open_window(0, 0, 512, 512, 'black', WindowHandleMessage)
SampleIndicesTrain := []
n:= ceil(NumEpochs)
for Index := 0 to ceil(NumEpochs) - 1 by 1
*tuple_shuffle (DatasetSamples, Shuffled)
n:=rand(|DLSamples|)
n:= sort_index(n)
ShuffleIndices := sort_index(rand(|DLSamples|))
Shuffled := DLSamples[ShuffleIndices]
SampleIndicesTrain := [SampleIndicesTrain,Shuffled]
endfor
NumIterationsPerEpoch := int(floor(|DLSamples| / real(BatchSize)))
NumIterations := int(NumIterationsPerEpoch * NumEpochs)
ComputeMeanLoss:=60
for NumEpoch:=0 to NumIterations by 1
***计算时间
count_seconds(Startime)
***显示训练信息
dev_set_window(WindowHandleMessage)
dev_disp_text('AllNumEpochs:'+NumIterations+' NumEpoch:'+NumEpoch, 'window', 10, 10, 'red', [], [])
***判断要不要更换学习率
**批量是BatchSize,读取多小个样品
DLSampleBatch:=[]
for DLSamplesProIndex:=0 to BatchSize-1 by 1
if ((DatasetSampleIndex+DLSamplesProIndex)>=|SampleIndicesTrain|)
DatasetSampleIndex:=0
else
DatasetSampleIndex:=DatasetSampleIndex+DLSamplesProIndex
endif
DLSamplesProc := SampleIndicesTrain[DatasetSampleIndex]
get_dict_tuple (DLSamplesProc, 'dlsample_file_name', DictPath)
read_dict (DatasetPreprocessDir + '/' + DictPath, [], [], DLSample)
DLSampleBatch[DLSamplesProIndex]:=DLSample
***显示训练图片
dev_set_window(WindowHandleTrain)
get_dict_object(train_pre_image, DLSampleBatch[DLSamplesProIndex], 'image')
dev_display(train_pre_image)
endfor
***当前批次训练模型
train_dl_model_batch (DLModelHandle, DLSampleBatch, TrainResult)
***记录训练的结果
TrainResults[TrainResultsIndex]:=TrainResult
get_dict_tuple (TrainResult, 'total_loss', LossValue)
***显示平均loss
dev_set_window(WindowHandleMessage)
***显示当前loss
dev_disp_text('total_loss:'+LossValue, 'window', 100, 10, 'red', [], [])
if(TrainResultsIndex==ComputeMeanLoss)
***计算平均loss
LossValues:=[]
for LossValuesIndex:=0 to TrainResultsIndex by 1
get_dict_tuple (TrainResults[LossValuesIndex], 'total_loss', LossValue)
LossValues[LossValuesIndex]:=LossValue
endfor
LossMean := mean(LossValues)
***显示平均loss
dev_set_window(WindowHandleMessage)
***显示当前loss
dev_disp_text('mean_loss:'+LossMean, 'window', 200, 10, 'red', [], [])
TrainResultsIndex:=0
else
***下一次结果加一
TrainResultsIndex:=TrainResultsIndex+1
endif
count_seconds(Endtime)
t:=Endtime-Startime
all_t:=t*NumIterations
use_t:=t*NumEpoch
***显示时间
dev_set_window(WindowHandleMessage)
***显示时间
dev_disp_text('AllTime:'+all_t+'SpeedTime'+use_t, 'window', 300, 10, 'red', [], [])
endfor
stop()
dev_close_window()
dev_close_window()
*优化内存消耗
set_dl_model_param (DLModelHandle, 'optimize_for_inference', 'true')
**优化模型的路径
path_optimize_model_:=DataDirectory+'/optimize_model.hdl'
*重新模型
write_dl_model (DLModelHandle, path_optimize_model_)
halcon对象检测训练流程
最新推荐文章于 2024-10-01 16:07:18 发布