设置首页收藏本站
开启左侧

大恒深度学习

 关闭 [复制链接]
 楼主| wenluderen 发表于 2020-5-26 10:52:11 | 显示全部楼层
* Get the network's image requirements
* from the handle of the classifier
* and use them as preprocessing parameters.
*
* Expected input image size:
get_dl_classifier_param (DLClassifierHandle, 'image_width', ImageWidth)获取图像的宽度
get_dl_classifier_param (DLClassifierHandle, 'image_height', ImageHeight)获取图像的高度
* Expected gray value range:
get_dl_classifier_param (DLClassifierHandle, 'image_range_min', ImageRangeMin)
get_dl_classifier_param (DLClassifierHandle, 'image_range_max', ImageRangeMax)
* Expected number of channels:
get_dl_classifier_param (DLClassifierHandle, 'image_num_channels', ImageNumChannels)获取图像的的通道数,此处应该是3


***
DLClassifierHandle 是用DLClassifierHandle----》DLModelHandle---》'pretrained_dl_classifier_compact.hdl'
这样层层传递进来的。
444444444.jpg

个人感觉,MVT的意思是说,预处理的参数,和神经网络的参数 要一致

所以 在预处理之前,先获取神经网络里面的参数, 然后用这个参数做预处理。


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 11:04:30 | 显示全部楼层
* Preprocess the images.
*
if (DomainHandling == 'full_domain')
    full_domain (Images, Images)
elseif (DomainHandling == 'crop_domain')
    crop_domain (Images, Images)
else
    throw ('Unsupported parameter value for \'domain_handling\'')
endif判断是否需要修改 区域
**
比如一张1000*1000的图像,我可以裁切里面的500*500 里面的图像 做预处理

********************************************************
* Zoom images only if they have a different size than the specified size
get_image_size (Images, ImageWidthInput, ImageHeightInput)//获取图像的尺寸

EqualWidth := ImageWidth [==] ImageWidthInput

EqualHeight := ImageHeight [==] ImageHeightInput

if (min(EqualWidth) == 0 or min(EqualHeight) == 0)

    zoom_image_size (Images, Images, ImageWidth, ImageHeight, 'constant')//做一个图像的额缩放

endif


1.jpg

此处是: [==]  符号第一次见到啊 是个判断语句吗? 不相等返回0 ,相等返回1
测试了ddd:=1[==]1 , ddd里面的值是1 ,看来猜的是对的。


***
那么这段代码就好理解了,不相等就在缩放了
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 11:08:51 | 显示全部楼层
if (ContrastNormalization == 'true')
    * Check the type of the input images.检查输入图像的类型。
    * Contrast normalization works here only for byte, integer and real images.  对比度归一化仅适用于字节,整数和实数图像。
    get_image_type (Images, Type)
    tuple_regexp_test (Type, 'byte|int|real', NumMatches)
    count_obj (Images, NumImages)
    if (NumMatches != NumImages)
        throw ('In case of contrast normalization, please provide only images of type \'byte\', \'int1\', \'int2\', \'uint2\', \'int4\', \'int8\', or \'real\'.')
    endif
    *
    * Perform contrast normalization
    if (Type == 'byte')
        * Scale the gray values to [0-255].
        scale_image_max (Images, Images)
    else
        * Scale the gray values to [ImageRangeMin-ImageRangeMax].
        * Scaling is performed separately for each image.
        gen_empty_obj (ImagesNew)
        for ImageIndex := 1 to NumImages by 1
            select_obj (Images, ImageSelected, ImageIndex)
            min_max_gray (ImageSelected, ImageSelected, 0, Min, Max, Range)
            Scale := (ImageRangeMax - ImageRangeMin) / (Max - Min)
            Shift := -Scale * Min + ImageRangeMin
            scale_image (ImageSelected, ImageSelected, Scale, Shift)
            concat_obj (ImagesNew, ImageSelected, ImagesNew)
        endfor
        Images := ImagesNew
        * Integer image convert to real image
        if (Type != 'real')
            convert_image_type (Images, Images, 'real')
        endif
    endif
elseif (ContrastNormalization != 'false')
    throw ('Unsupported parameter value for \'contrast_normalization\'')
endif

这段代码在原始程序里面 是直接跳过去的
大致看了下,这段代码应该是搞归一化的
既然原程序里面跳过去了,咱们也不管

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 11:15:35 | 显示全部楼层
* Check the type of the input images.检查输入图像的类型
* If the type is not 'byte', 如果不是byte
* the gray value scaling does not work correctly.灰度值缩放无法正常工作。

get_image_type (Images, Type)
tuple_regexp_test (Type, 'byte|real', NumMatches)// Test if a string matches a regular expression.
count_obj (Images, NumImages)// Number of objects in a tuple.
if (NumMatches != NumImages)
    throw ('Please provide only images of type \'byte\' or \'real\'.')
endif
EqualByte := Type [==] 'byte'
if (max(EqualByte) == 1)
    if (min(EqualByte) == 0)
        throw ('Passing mixed type images is not supported.')
    endif
    * Convert the image type from byte to real,
    * because the classifier expects 'real' images.
    convert_image_type (Images, Images, 'real')
    * Scale/Shift the gray values from [0-255] to the expected range.
    RescaleRange := (ImageRangeMax - ImageRangeMin) / 255.0
    scale_image (Images, Images, RescaleRange, ImageRangeMin)
else
    * For real images it is assumed that the range is already correct
endif


***
不知道为啥要干这个灰度缩放,我猜估计MVT公司的深度学习算法不支持0-255 的灰度范围,所以调整为负的127 到正的127


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 11:18:46 | 显示全部楼层
* Check the number of channels.检查通道数目

count_obj (Images, NumImages)
for ImageIndex := 1 to NumImages by 1
    select_obj (Images, ObjectSelected, ImageIndex)
    count_channels (ObjectSelected, NumChannels)
    if (NumChannels != ImageNumChannels)
        *
        if (NumChannels == 1 and ImageNumChannels == 3)
            * If the image is a grayscale image, but the classifier expects a color image:
            * convert it to an image with three channels.
            compose3 (ObjectSelected, ObjectSelected, ObjectSelected, ThreeChannelImage)
            replace_obj (Images, ThreeChannelImage, Images, ImageIndex)
        elseif (NumChannels == 3 and ImageNumChannels == 1)
            * If the image is a color image, but the classifier expects a grayscale image:
            * convert it to an image with only one channel.
            rgb1_to_gray (ObjectSelected, SingleChannelImage)
            replace_obj (Images, SingleChannelImage, Images, ImageIndex)
        else
            throw ('Number of channels not supported. Please provide a grayscale or an RGB image.')
        endif
        *
    endif
endfor
ImagesPreprocessed := Images


这个里面是对通道数目做了处理
没有细看,不过看架势   
应该是将样本的通道数目  和神经网络里面的通道数目做了一个处理


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 12:34:19 | 显示全部楼层
*****************************************************
*上面的代码是预处理,下面的代码是训练
*****************************************************
*****************************************************
read_dl_classifier_data_set ('data', 'last_folder', ImageFiles, Labels, LabelsIndices, Classes)

Read the data set containing the images and their respective ground truth labels.读取包含图像及其各自的地面真相标签的数据集。

read_dl_classifier_data_set is obsolete and is only provided for reasons of backward compatibility.

New applications should use the general CNN-based procedure read_dl_dataset_classification.
read_dl_classifier_data_set已过时,仅出于向后兼容的原因提供。

新应用程序应使用基于CNN的常规过程read_dl_dataset_classification




*************
read_dl_classifier_data_set lists all ImageFiles located in ImageDirectory and its subdirectories, and returns the ground truth label of each image in GroundTruthLabels. LabelSource determines how the GroundTruthLabels are extracted. Additionally, indices are assigned to the labels, and the result is returned in LabelIndices. These indices can be used for the training instead of the string labels, which is more time efficient. The order of indices corresponds with the returned UniqueClasses.
read_dl_classifier_data_set列出位于ImageDirectory及其子目录中的所有ImageFile,并在GroundTruthLabels中返回每个图像的地面真相标签。 LabelSource确定如何提取GroundTruthLabel。 此外,将索引分配给标签,并在LabelIndices中返回结果。 这些索引可以代替字符串标签用于训练,这样可以节省时间。 索引的顺序与返回的UniqueClasses相对应。




***********
The images are listed using the procedure list_image_files. Note that subfolders and links are included as well. For more information on the used default directories, please have a look at its documentation.
使用过程list_image_files列出图像。 请注意,还包括子文件夹和链接。 有关使用的默认目录的更多信息,请查看其文档。






***********

For LabelSource, three modes are supported: For 'last_folder', the last folder name containing the image is used as label. For 'file_name', the file name of each image is used as label. For 'file_name_remove_index', all consecutive digits and underscores at the end of the file name (e.g., '01', '_20180101') are removed. If you want to configure your own LabelSource mode, you might find the procedure parse_filename helpful.


对于LabelSource,支持三种模式:对于“ last_folder”,包含图像的最后一个文件夹名称用作标签。 对于“ file_name”,每个图像的文件名都用作标签。 对于“ file_name_remove_index”,文件名末尾的所有连续数字和下划线(例如“ 01”,“ _ 20180101”)将被删除。 如果要配置自己的LabelSource模式,则可能会发现过程parse_filename有用。


2020-05-26_123332.jpg


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 12:40:21 | 显示全部楼层
set_dl_classifier_param (DLModelHandle, 'classes', Classes) 设定参数
Classes里面的内容是是OK   NG  

***
split_dl_classifier_data_set (ImageFiles, Labels, 80, 20, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, TestImages, TestLabels)
Split and shuffle the images and ground truth labels into training, validation and test subsets.
将图像和地面真相标签分割并混排为训练,验证和测试子集。

split_dl_classifier_data_set is obsolete and is only provided for reasons of backward compatibility.
New applications should use the general CNN-based procedure split_dl_dataset.

split_dl_classifier_data_set已过时,仅出于向后兼容的原因提供。
新应用程序应使用基于CNN的常规过程split_dl_dataset。



split_dl_classifier_data_set divides the input data set (input images and ground truth labels) into three different subsets: training, validation, and test set. The number of images in each subset is defined by the given percentages TrainingPercent and ValidationPercent. Each subset contains randomly distributed data, whereby the original ratio of class sizes is kept.
split_dl_classifier_data_set将输入数据集(输入图像和地面真相标签)分为三个不同的子集:训练,验证和测试集。 每个子集中的图像数量由给定的百分比TrainingPercent和ValidationPercent定义。 每个子集包含随机分布的数据,从而保持了班级规模的原始比例。

TrainingPercent and ValidationPercent determine the percentage of the respective images and labels in the corresponding subsets, with a number between 0 and 100.
The remaining data constitute the test set. The sum of TrainingPercent and ValidationPercent must not be greater than 100.
Note that the actual percentages that are achieved can differ depending on the number of images and the number of images per class.

TrainingPercent和ValidationPercent确定相应图像和标签在相应子集中的百分比,其数字在0到100之间。其余数据构成测试集。
TrainingPercent和ValidationPercent的总和不得大于100。请注意,实际获得的百分比可能会有所不同,具体取决于图像数量和每个类的图像数量。

222222.jpg


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 12:42:03 | 显示全部楼层
set_dl_classifier_param (DLModelHandle, 'learning_rate', LearningRate) 学习效率参数设定

tuple_min2(2048, |TrainingImages|, BatchSize)//BatchSize的参数设定
Trying := true
while(Trying)
try
    set_dl_classifier_param (DLModelHandle, 'batch_size', BatchSize)
    set_dl_classifier_param (DLModelHandle, 'runtime_init', 'immediately')
    Trying := false
catch (Exception)
    BatchSize := BatchSize / 2
endtry
endwhile


*set_dl_classifier_param (DLModelHandle, 'weight_prior', 0.0005) 这个玩意 应该是和过拟合有关系的


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 12:49:36 | 显示全部楼层
*****************
***************正式训练的代码

dev_open_window(0, 0, 800, 600, 'black', WindowHandle)
set_font(WindowHandle, 'default-16')
TrainingErrors := []
ValidationErrors := []
LearningRates := []
Epochs := []
LossByIteration := []
MinValidationError := 1
NumBatchesInEpoch := int(floor(|TrainingImages| / real(BatchSize)))
PlottedIterations := round([NumBatchesInEpoch * [0:1:NumEpochs - 1], (NumBatchesInEpoch * NumEpochs) - 1])
select_percentage_dl_classifier_data (TrainingImages, TrainingLabels, 20, TrainingImagesSelected, TrainingLabelsSelected)
tuple_gen_sequence (0, |TrainingImages| - 1, 1, TrainSequence)
for Epoch := 0 to NumEpochs - 1 by 1
    tuple_shuffle (TrainSequence, TrainSequence)
    for Iteration := 0 to NumBatchesInEpoch - 1 by 1
        * Select a shuffled batch every epoch
        BatchStart := Iteration * BatchSize
        BatchEnd := BatchStart + (BatchSize - 1)
        BatchIndices := TrainSequence[BatchStart:BatchEnd]
        BatchImageFiles := TrainingImages[BatchIndices]
        BatchLabels := TrainingLabels[BatchIndices]
        read_image (BatchImages, BatchImageFiles)
        GenParamName := 'mirror'
        GenParamValue := 'rc'
        augment_images (BatchImages, BatchImages, GenParamName, GenParamValue)
        train_dl_classifier_batch (BatchImages, DLModelHandle, BatchLabels, DLClassifierTrainResultHandle)
        get_dl_classifier_train_result (DLClassifierTrainResultHandle, 'loss', Loss)
        LossByIteration := [LossByIteration,Loss]
        CurrentIteration := int(Iteration + (NumBatchesInEpoch * Epoch))
        if (sum(CurrentIteration [==] PlottedIterations))
            apply_dl_classifier_batchwise (TrainingImagesSelected, DLModelHandle, TrainingDLClassifierResultIDs, TrainingPredictedLabels, TrainingConfidences)
            apply_dl_classifier_batchwise (ValidationImages, DLModelHandle, ValidationDLClassifierResultIDs, ValidationPredictedLabels, ValidationConfidences)
            evaluate_dl_classifier (TrainingLabelsSelected, DLModelHandle, TrainingDLClassifierResultIDs, 'top1_error', 'global', TrainingTop1Error)
            evaluate_dl_classifier (ValidationLabels, DLModelHandle, ValidationDLClassifierResultIDs, 'top1_error', 'global', ValidationTop1Error)
            get_dl_classifier_param (DLModelHandle, 'learning_rate', LearningRate)
            TrainingErrors := [TrainingErrors,TrainingTop1Error]
            ValidationErrors := [ValidationErrors,ValidationTop1Error]
            LearningRates := [LearningRates,LearningRate]
            Epochs := [Epochs,PlottedIterations[|Epochs|] / real(NumBatchesInEpoch)]
            plot_dl_classifier_training_progress (TrainingErrors, ValidationErrors, LearningRates, Epochs, NumEpochs, WindowHandle)
                if (ValidationTop1Error <= MinValidationError)
                    write_dl_classifier (DLModelHandle, 'model_best.hdl')
                    MinValidationError := ValidationTop1Error
                endif
        endif
    endfor
    * Reduce the learning rate every nth epoch.
    if ((Epoch + 1) % LearningRateStepEveryNthEpoch == 0)
*         get_dl_classifier_param (DLModelHandle, 'learning_rate', LearningRate)
        set_dl_classifier_param (DLModelHandle, 'learning_rate', LearningRate * LearningRateStepRatio)
        get_dl_classifier_param (DLModelHandle, 'learning_rate', LearningRate)
    endif
endfor
dump_window(WindowHandle, 'png', 'train')

3333333333.jpg

PS:正常来说应该用的  损失函数来表示 什么训练结束,但是这个例子当中 是其他的
诡异

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| wenluderen 发表于 2020-5-26 12:53:44 | 显示全部楼层
下面是评估环节
* Evaluate
read_dl_classifier ('model_best.hdl', DLModelHandle)

apply_dl_classifier_batchwise (TrainingImages, DLModelHandle, DLClassifierResultIDsTraining, PredictedClassesTraining, Confidences)
evaluate_dl_classifier (TrainingLabels, DLModelHandle, DLClassifierResultIDsTraining, 'top1_error', 'global', Top1ErrorTraining)
apply_dl_classifier_batchwise (ValidationImages, DLModelHandle, DLClassifierResultIDsValidation, PredictedClassesValidation, Confidences)
evaluate_dl_classifier (ValidationLabels, DLModelHandle, DLClassifierResultIDsValidation, 'top1_error', 'global', Top1ErrorValidation)
Top1ClassValidation := []
for Index := 0 to PredictedClassesValidation.length() - 1 by 1
    Top1ClassValidation := [Top1ClassValidation,PredictedClassesValidation.at(Index)[0]]
endfor
dev_clear_window ()
gen_confusion_matrix (ValidationLabels, Top1ClassValidation, [], [], WindowHandle, ConfusionMatrix)
Text         := 'Top-1 error (training    images): ' + (Top1ErrorTraining * 100)$'.1f' + ' %'
Text[|Text|] := 'Top-1 error (validation images): ' + (Top1ErrorValidation * 100)$'.1f' + ' %'
Text[|Text|] := 'Press Run (F5) to display incorrectly classified images.'
dev_disp_text (Text, 'window', 'bottom', 'left', 'white', 'box', 'false')
dump_window(WindowHandle, 'png', 'matrix')
get_dl_classifier_image_results (ImagesErroneouslyClassified, ValidationImages, ValidationLabels, Top1ClassValidation, 'global_selection', 'erroneously_classified', WindowHandle)

44444444444.jpg

只有一个混淆矩阵
个人感觉李东生的这个例子 应该是遭遇19.11 版本

所以很多功能不全



奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表