示例#1
0
        private static int GetThreePointShapeType(HTuple sortedIndex)
        {
            int index = -1;

            HTuple hvRet = new HTuple();

            HTuple[] hv_SortedIndex = new HTuple[6];
            //1th.Input Value is xiao zhong da
            hv_SortedIndex[0] = hv_SortedIndex[0].TupleConcat(0);
            hv_SortedIndex[0] = hv_SortedIndex[0].TupleConcat(1);
            hv_SortedIndex[0] = hv_SortedIndex[0].TupleConcat(2);
            //2th.Input Value is da zhong xiao
            hv_SortedIndex[1] = hv_SortedIndex[1].TupleConcat(2);
            hv_SortedIndex[1] = hv_SortedIndex[1].TupleConcat(1);
            hv_SortedIndex[1] = hv_SortedIndex[1].TupleConcat(0);
            //3th.Input Value is zhong da xiao
            hv_SortedIndex[2] = hv_SortedIndex[2].TupleConcat(2);
            hv_SortedIndex[2] = hv_SortedIndex[2].TupleConcat(0);
            hv_SortedIndex[2] = hv_SortedIndex[2].TupleConcat(1);
            //4th.Input Value is xiao da zhong
            hv_SortedIndex[3] = hv_SortedIndex[3].TupleConcat(0);
            hv_SortedIndex[3] = hv_SortedIndex[3].TupleConcat(2);
            hv_SortedIndex[3] = hv_SortedIndex[3].TupleConcat(1);
            //5th.Input Value is zhong xiao da
            hv_SortedIndex[4] = hv_SortedIndex[4].TupleConcat(1);
            hv_SortedIndex[4] = hv_SortedIndex[4].TupleConcat(0);
            hv_SortedIndex[4] = hv_SortedIndex[4].TupleConcat(2);
            //6th.Input Value is da xiao zhong
            hv_SortedIndex[5] = hv_SortedIndex[5].TupleConcat(1);
            hv_SortedIndex[5] = hv_SortedIndex[5].TupleConcat(2);
            hv_SortedIndex[5] = hv_SortedIndex[5].TupleConcat(0);
            for (int i = 0; i < 6; i++)
            {
                index = i;
                hvRet = sortedIndex.TupleEqualElem(hv_SortedIndex[i]);
                if (hvRet[0] == 1 && hvRet[1] == 1 && hvRet[2] == 1)
                {
                    break;
                }
            }

            return(index);
        }
        private void train_classifier(HTuple hv_DLClassifierHandle, HTuple hv_FileName,
                                      HTuple hv_NumEpochs, HTuple hv_TrainingImages, HTuple hv_TrainingLabels, HTuple hv_ValidationImages,
                                      HTuple hv_ValidationLabels, HTuple hv_LearningRateStepEveryNthEpoch, HTuple hv_LearningRateStepRatio,
                                      HTuple hv_PlotEveryNthEpoch, HTuple hv_WindowHandle)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_BatchImages = null;

            // Local control variables

            HTuple hv_TrainingErrors = new HTuple(), hv_ValidationErrors = new HTuple();
            HTuple hv_LearningRates = new HTuple(), hv_Epochs = new HTuple();
            HTuple hv_LossByIteration = new HTuple(), hv_BatchSize = new HTuple();
            HTuple hv_MinValidationError = new HTuple(), hv_NumBatchesInEpoch = new HTuple();
            HTuple hv_NumTotalIterations = new HTuple(), hv_PlottedIterations = new HTuple();
            HTuple hv_TrainSequence = new HTuple(), hv_SelectPercentageTrainingImages = new HTuple();
            HTuple hv_TrainingImagesSelected = new HTuple(), hv_TrainingLabelsSelected = new HTuple();
            HTuple hv_Epoch = new HTuple(), hv_Iteration = new HTuple();
            HTuple hv_BatchStart = new HTuple(), hv_BatchEnd = new HTuple();
            HTuple hv_BatchIndices = new HTuple(), hv_BatchImageFiles = new HTuple();
            HTuple hv_BatchLabels = new HTuple(), hv_GenParamName = new HTuple();
            HTuple hv_GenParamValue = new HTuple(), hv_DLClassifierTrainResultHandle = new HTuple();
            HTuple hv_Loss = new HTuple(), hv_CurrentIteration = new HTuple();
            HTuple hv_TrainingDLClassifierResultIDs = new HTuple();
            HTuple hv_ValidationDLClassifierResultIDs = new HTuple();
            HTuple hv_TrainingTop1Error = new HTuple(), hv_ValidationTop1Error = new HTuple();
            HTuple hv_LearningRate = new HTuple();

            HTupleVector hvec_TrainingPredictedLabels   = new HTupleVector(1);
            HTupleVector hvec_TrainingConfidences       = new HTupleVector(1);
            HTupleVector hvec_ValidationPredictedLabels = new HTupleVector(1);
            HTupleVector hvec_ValidationConfidences     = new HTupleVector(1);

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_BatchImages);
            try
            {
                //For the plot during training,
                //we need to concatenate some intermediate results.
                hv_TrainingErrors.Dispose();
                hv_TrainingErrors = new HTuple();
                hv_ValidationErrors.Dispose();
                hv_ValidationErrors = new HTuple();
                hv_LearningRates.Dispose();
                hv_LearningRates = new HTuple();
                hv_Epochs.Dispose();
                hv_Epochs = new HTuple();
                hv_LossByIteration.Dispose();
                hv_LossByIteration = new HTuple();
                hv_BatchSize.Dispose();
                HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "batch_size", out hv_BatchSize);
                hv_MinValidationError.Dispose();
                hv_MinValidationError = 1;
                //
                //Create a tuple that includes all the iterations
                //where the plot should be computed (including the last ieration).
                hv_NumBatchesInEpoch.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumBatchesInEpoch = (((((new HTuple(hv_TrainingImages.TupleLength()
                                                           )) / (hv_BatchSize.TupleReal()))).TupleFloor())).TupleInt();
                }
                hv_NumTotalIterations.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumTotalIterations = (hv_NumBatchesInEpoch * hv_NumEpochs) - 1;
                }
                hv_PlottedIterations.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_PlottedIterations = ((((hv_NumBatchesInEpoch * HTuple.TupleGenSequence(
                                                   0, hv_NumEpochs - 1, hv_PlotEveryNthEpoch))).TupleConcat(hv_NumTotalIterations))).TupleRound()
                    ;
                }
                //
                //TrainSequence is used for easier indexing of the training data.
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TrainSequence.Dispose();
                    HOperatorSet.TupleGenSequence(0, (new HTuple(hv_TrainingImages.TupleLength()
                                                                 )) - 1, 1, out hv_TrainSequence);
                }
                //
                //Select a subset of the training data set
                //in order to obtain a fast approximation
                //of the training error during training (plotting).
                hv_SelectPercentageTrainingImages.Dispose();
                hv_SelectPercentageTrainingImages = 100;
                hv_TrainingImagesSelected.Dispose(); hv_TrainingLabelsSelected.Dispose();
                select_percentage_dl_classifier_data(hv_TrainingImages, hv_TrainingLabels,
                                                     hv_SelectPercentageTrainingImages, out hv_TrainingImagesSelected, out hv_TrainingLabelsSelected);
                //
                HTuple end_val25  = hv_NumEpochs - 1;
                HTuple step_val25 = 1;
                #region
                for (hv_Epoch = 0; hv_Epoch.Continue(end_val25, step_val25); hv_Epoch = hv_Epoch.TupleAdd(step_val25))
                {
                    //In order to get randomness in each epoch,
                    //the training set is shuffled every epoch.
                    {
                        HTuple ExpTmpOutVar_0;
                        tuple_shuffle(hv_TrainSequence, out ExpTmpOutVar_0);
                        hv_TrainSequence.Dispose();
                        hv_TrainSequence = ExpTmpOutVar_0;
                    }
                    HTuple end_val29  = hv_NumBatchesInEpoch - 1;
                    HTuple step_val29 = 1;
                    for (hv_Iteration = 0; hv_Iteration.Continue(end_val29, step_val29); hv_Iteration = hv_Iteration.TupleAdd(step_val29))
                    {
                        //Select a batch from the training data set.
                        hv_BatchStart.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchStart = hv_Iteration * hv_BatchSize;
                        }
                        hv_BatchEnd.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchEnd = hv_BatchStart + (hv_BatchSize - 1);
                        }
                        hv_BatchIndices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchIndices = hv_TrainSequence.TupleSelectRange(
                                hv_BatchStart, hv_BatchEnd);
                        }
                        hv_BatchImageFiles.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchImageFiles = hv_TrainingImages.TupleSelect(
                                hv_BatchIndices);
                        }
                        hv_BatchLabels.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchLabels = hv_TrainingLabels.TupleSelect(
                                hv_BatchIndices);
                        }
                        //
                        //Read the image of the current batch.
                        ho_BatchImages.Dispose();
                        HOperatorSet.ReadImage(out ho_BatchImages, hv_BatchImageFiles);
                        //Augment the images to get a better variety of training images.
                        hv_GenParamName.Dispose();
                        hv_GenParamName = "mirror";
                        hv_GenParamValue.Dispose();
                        hv_GenParamValue = "rc";
                        {
                            HObject ExpTmpOutVar_0;
                            augment_images(ho_BatchImages, out ExpTmpOutVar_0, hv_GenParamName, hv_GenParamValue);
                            ho_BatchImages.Dispose();
                            ho_BatchImages = ExpTmpOutVar_0;
                        }
                        //
                        //Train the network with these images and ground truth labels.
                        hv_DLClassifierTrainResultHandle.Dispose();
                        HOperatorSet.TrainDlClassifierBatch(ho_BatchImages, hv_DLClassifierHandle,
                                                            hv_BatchLabels, out hv_DLClassifierTrainResultHandle);
                        //You can access the current value of the loss function,
                        //which should decrease during the training.
                        hv_Loss.Dispose();
                        HOperatorSet.GetDlClassifierTrainResult(hv_DLClassifierTrainResultHandle,
                                                                "loss", out hv_Loss);
                        //Store the loss in a tuple .
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                    ExpTmpLocalVar_LossByIteration = hv_LossByIteration.TupleConcat(
                                    hv_Loss);
                                hv_LossByIteration.Dispose();
                                hv_LossByIteration = ExpTmpLocalVar_LossByIteration;
                            }
                        }
                        //
                        //In regular intervals, we want to evaluate
                        //how well our classifier performs.
                        hv_CurrentIteration.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_CurrentIteration = ((hv_Iteration + (hv_NumBatchesInEpoch * hv_Epoch))).TupleInt()
                            ;
                        }
                        if ((int)(((hv_CurrentIteration.TupleEqualElem(hv_PlottedIterations))).TupleSum()
                                  ) != 0)
                        {
                            //Plot the progress regularly.
                            //Evaluate the current classifier on the training and validation set.
                            hv_TrainingDLClassifierResultIDs.Dispose(); hvec_TrainingPredictedLabels.Dispose(); hvec_TrainingConfidences.Dispose();
                            apply_dl_classifier_batchwise(hv_TrainingImagesSelected, hv_DLClassifierHandle,
                                                          out hv_TrainingDLClassifierResultIDs, out hvec_TrainingPredictedLabels,
                                                          out hvec_TrainingConfidences);
                            hv_ValidationDLClassifierResultIDs.Dispose(); hvec_ValidationPredictedLabels.Dispose(); hvec_ValidationConfidences.Dispose();
                            apply_dl_classifier_batchwise(hv_ValidationImages, hv_DLClassifierHandle,
                                                          out hv_ValidationDLClassifierResultIDs, out hvec_ValidationPredictedLabels,
                                                          out hvec_ValidationConfidences);
                            //Evaluate the top-1 error on each dataset.
                            hv_TrainingTop1Error.Dispose();
                            evaluate_dl_classifier(hv_TrainingLabelsSelected, hv_DLClassifierHandle,
                                                   hv_TrainingDLClassifierResultIDs, "top1_error", "global", out hv_TrainingTop1Error);
                            hv_ValidationTop1Error.Dispose();
                            evaluate_dl_classifier(hv_ValidationLabels, hv_DLClassifierHandle, hv_ValidationDLClassifierResultIDs,
                                                   "top1_error", "global", out hv_ValidationTop1Error);
                            //Concatenate the values for the plot.
                            hv_LearningRate.Dispose();
                            HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                              out hv_LearningRate);
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_TrainingErrors = hv_TrainingErrors.TupleConcat(
                                        hv_TrainingTop1Error);
                                    hv_TrainingErrors.Dispose();
                                    hv_TrainingErrors = ExpTmpLocalVar_TrainingErrors;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_ValidationErrors = hv_ValidationErrors.TupleConcat(
                                        hv_ValidationTop1Error);
                                    hv_ValidationErrors.Dispose();
                                    hv_ValidationErrors = ExpTmpLocalVar_ValidationErrors;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_LearningRates = hv_LearningRates.TupleConcat(
                                        hv_LearningRate);
                                    hv_LearningRates.Dispose();
                                    hv_LearningRates = ExpTmpLocalVar_LearningRates;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_Epochs = hv_Epochs.TupleConcat(
                                        (hv_PlottedIterations.TupleSelect(new HTuple(hv_Epochs.TupleLength()
                                                                                     ))) / (hv_NumBatchesInEpoch.TupleReal()));
                                    hv_Epochs.Dispose();
                                    hv_Epochs = ExpTmpLocalVar_Epochs;
                                }
                            }

                            //Plot validation and error against the epochs in order to
                            //observe the progress of the training.
                            plot_dl_classifier_training_progress(hv_TrainingErrors, hv_ValidationErrors, hv_LearningRates, hv_Epochs, hv_NumEpochs, hv_WindowHandle);
                            //如果上面绘制坐标系到窗体过程报错,搞不定的话就用下面这句代替,直接显示训练数据到窗体
                            //disp_training_progress_datas(hv_TrainingErrors, hv_ValidationErrors, hv_LearningRates, hv_Epochs, hv_NumEpochs, hv_WindowHandle);


                            if ((int)(new HTuple(hv_ValidationTop1Error.TupleLessEqual(hv_MinValidationError))) != 0)
                            {
                                HOperatorSet.WriteDlClassifier(hv_DLClassifierHandle, hv_FileName);
                                hv_MinValidationError.Dispose();
                                hv_MinValidationError = new HTuple(hv_ValidationTop1Error);
                            }
                        }
                    }
                    //Reduce the learning rate every nth epoch.
                    if ((int)(new HTuple((((hv_Epoch + 1) % hv_LearningRateStepEveryNthEpoch)).TupleEqual(
                                             0))) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HOperatorSet.SetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                              hv_LearningRate * hv_LearningRateStepRatio);
                        }
                        hv_LearningRate.Dispose();
                        HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                          out hv_LearningRate);
                    }
                }
                #endregion
                // stop(...); only in hdevelop
                ho_BatchImages.Dispose();

                hv_TrainingErrors.Dispose();
                hv_ValidationErrors.Dispose();
                hv_LearningRates.Dispose();
                hv_Epochs.Dispose();
                hv_LossByIteration.Dispose();
                hv_BatchSize.Dispose();
                hv_MinValidationError.Dispose();
                hv_NumBatchesInEpoch.Dispose();
                hv_NumTotalIterations.Dispose();
                hv_PlottedIterations.Dispose();
                hv_TrainSequence.Dispose();
                hv_SelectPercentageTrainingImages.Dispose();
                hv_TrainingImagesSelected.Dispose();
                hv_TrainingLabelsSelected.Dispose();
                hv_Epoch.Dispose();
                hv_Iteration.Dispose();
                hv_BatchStart.Dispose();
                hv_BatchEnd.Dispose();
                hv_BatchIndices.Dispose();
                hv_BatchImageFiles.Dispose();
                hv_BatchLabels.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
                hv_DLClassifierTrainResultHandle.Dispose();
                hv_Loss.Dispose();
                hv_CurrentIteration.Dispose();
                hv_TrainingDLClassifierResultIDs.Dispose();
                hv_ValidationDLClassifierResultIDs.Dispose();
                hv_TrainingTop1Error.Dispose();
                hv_ValidationTop1Error.Dispose();
                hv_LearningRate.Dispose();
                hvec_TrainingPredictedLabels.Dispose();
                hvec_TrainingConfidences.Dispose();
                hvec_ValidationPredictedLabels.Dispose();
                hvec_ValidationConfidences.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_BatchImages.Dispose();

                hv_TrainingErrors.Dispose();
                hv_ValidationErrors.Dispose();
                hv_LearningRates.Dispose();
                hv_Epochs.Dispose();
                hv_LossByIteration.Dispose();
                hv_BatchSize.Dispose();
                hv_MinValidationError.Dispose();
                hv_NumBatchesInEpoch.Dispose();
                hv_NumTotalIterations.Dispose();
                hv_PlottedIterations.Dispose();
                hv_TrainSequence.Dispose();
                hv_SelectPercentageTrainingImages.Dispose();
                hv_TrainingImagesSelected.Dispose();
                hv_TrainingLabelsSelected.Dispose();
                hv_Epoch.Dispose();
                hv_Iteration.Dispose();
                hv_BatchStart.Dispose();
                hv_BatchEnd.Dispose();
                hv_BatchIndices.Dispose();
                hv_BatchImageFiles.Dispose();
                hv_BatchLabels.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
                hv_DLClassifierTrainResultHandle.Dispose();
                hv_Loss.Dispose();
                hv_CurrentIteration.Dispose();
                hv_TrainingDLClassifierResultIDs.Dispose();
                hv_ValidationDLClassifierResultIDs.Dispose();
                hv_TrainingTop1Error.Dispose();
                hv_ValidationTop1Error.Dispose();
                hv_LearningRate.Dispose();
                hvec_TrainingPredictedLabels.Dispose();
                hvec_TrainingConfidences.Dispose();
                hvec_ValidationPredictedLabels.Dispose();
                hvec_ValidationConfidences.Dispose();

                throw HDevExpDefaultException;
            }
        }