OpenCV - Trainings lead to different result when using TrainData_create





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm using MLP ANN provided by OpenCV 3.4 with python. I noticed that when training data is prepared via cv2.ml.TrainData_create the ANN performs well, in case this is not used but same samples and parameters are used, ANN is not correctly trained.



Here I don't mean trainings differ even if using the same data (which can be expected due to random starting points), because what I see here is working-training VS not-working-training and this occurs always.



The following code uses cv2.ml.TrainData_create



import cv2
import numpy as np

ann = cv2.ml.ANN_MLP_create()
ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
ann.setLayerSizes(np.array([3, 8, 4]))
ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

input_array = np.array([ [1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
], dtype=np.float32)

output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
], dtype=np.float32)

td = cv2.ml.TrainData_create(input_array, cv2.ml.ROW_SAMPLE, output_array)
ann.train(td, cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)

SAMPLES = 5000
for x in range(0, SAMPLES):
ann.train(td, cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)


and this works well:



print(ann.predict(input_array))

(0.0, array([[ 1.0000000e+00, 0.0000000e+00, 4.7625793e-16, 2.8575474e-16],
[ 1.9050316e-16, 10000000e+00, 5.7150949e-16, 9.5251581e-17],
[ 9.5251581e-17, 0.0000000e+00, 1.0000000e+00, -1.9050316e-16],
[-1.9050316e-16, -1.9050316e-16, 0.0000000e+00, 1.0000000e+00]],
dtype=float32))


The following code doesn't use cv2.ml.TrainData_create but apparently use the same data and parameters:



import cv2
import numpy as np

ann = cv2.ml.ANN_MLP_create()
ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)
ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
ann.setLayerSizes(np.array([3, 8, 4]))
ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

input_array = np.array([ [1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
], dtype=np.float32)

output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
], dtype=np.float32)

SAMPLES = 5000
for x in range(0, SAMPLES):
ann.train(np.array([[1.0, 0.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[1.0, 0.0, 0.0, 0.0]], dtype=np.float32))
ann.train(np.array([[0.0, 1.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 1.0, 0.0, 0.0]], dtype=np.float32))
ann.train(np.array([[0.0, 0.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 1.0, 0.0]], dtype=np.float32))
ann.train(np.array([[1.0, 1.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 0.0, 1.0]], dtype=np.float32))


But this one simply doesn't work:



print(ann.predict(input_array))

(0.0, array([[ 1.2886142 , 0.51306236, -1.0352006 , -0.19007786],
[ 1.2194023 , 0.7686653 , -1.097198 , -0.03246666],
[ 0.99483347, 0.40380374, -0.917998 , 0.08949649],
[ 0.7475754 , 0.12770385, -0.81321925, 0.37416443]],
dtype=float32))


What's wrong with the second code snippet?










share|improve this question





























    0















    I'm using MLP ANN provided by OpenCV 3.4 with python. I noticed that when training data is prepared via cv2.ml.TrainData_create the ANN performs well, in case this is not used but same samples and parameters are used, ANN is not correctly trained.



    Here I don't mean trainings differ even if using the same data (which can be expected due to random starting points), because what I see here is working-training VS not-working-training and this occurs always.



    The following code uses cv2.ml.TrainData_create



    import cv2
    import numpy as np

    ann = cv2.ml.ANN_MLP_create()
    ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
    ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
    ann.setLayerSizes(np.array([3, 8, 4]))
    ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

    input_array = np.array([ [1.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    [1.0, 1.0, 1.0],
    ], dtype=np.float32)

    output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0, 0.0],
    [0.0, 0.0, 1.0, 0.0],
    [0.0, 0.0, 0.0, 1.0],
    ], dtype=np.float32)

    td = cv2.ml.TrainData_create(input_array, cv2.ml.ROW_SAMPLE, output_array)
    ann.train(td, cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)

    SAMPLES = 5000
    for x in range(0, SAMPLES):
    ann.train(td, cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)


    and this works well:



    print(ann.predict(input_array))

    (0.0, array([[ 1.0000000e+00, 0.0000000e+00, 4.7625793e-16, 2.8575474e-16],
    [ 1.9050316e-16, 10000000e+00, 5.7150949e-16, 9.5251581e-17],
    [ 9.5251581e-17, 0.0000000e+00, 1.0000000e+00, -1.9050316e-16],
    [-1.9050316e-16, -1.9050316e-16, 0.0000000e+00, 1.0000000e+00]],
    dtype=float32))


    The following code doesn't use cv2.ml.TrainData_create but apparently use the same data and parameters:



    import cv2
    import numpy as np

    ann = cv2.ml.ANN_MLP_create()
    ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)
    ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
    ann.setLayerSizes(np.array([3, 8, 4]))
    ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

    input_array = np.array([ [1.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    [1.0, 1.0, 1.0],
    ], dtype=np.float32)

    output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0, 0.0],
    [0.0, 0.0, 1.0, 0.0],
    [0.0, 0.0, 0.0, 1.0],
    ], dtype=np.float32)

    SAMPLES = 5000
    for x in range(0, SAMPLES):
    ann.train(np.array([[1.0, 0.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[1.0, 0.0, 0.0, 0.0]], dtype=np.float32))
    ann.train(np.array([[0.0, 1.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 1.0, 0.0, 0.0]], dtype=np.float32))
    ann.train(np.array([[0.0, 0.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 1.0, 0.0]], dtype=np.float32))
    ann.train(np.array([[1.0, 1.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 0.0, 1.0]], dtype=np.float32))


    But this one simply doesn't work:



    print(ann.predict(input_array))

    (0.0, array([[ 1.2886142 , 0.51306236, -1.0352006 , -0.19007786],
    [ 1.2194023 , 0.7686653 , -1.097198 , -0.03246666],
    [ 0.99483347, 0.40380374, -0.917998 , 0.08949649],
    [ 0.7475754 , 0.12770385, -0.81321925, 0.37416443]],
    dtype=float32))


    What's wrong with the second code snippet?










    share|improve this question

























      0












      0








      0








      I'm using MLP ANN provided by OpenCV 3.4 with python. I noticed that when training data is prepared via cv2.ml.TrainData_create the ANN performs well, in case this is not used but same samples and parameters are used, ANN is not correctly trained.



      Here I don't mean trainings differ even if using the same data (which can be expected due to random starting points), because what I see here is working-training VS not-working-training and this occurs always.



      The following code uses cv2.ml.TrainData_create



      import cv2
      import numpy as np

      ann = cv2.ml.ANN_MLP_create()
      ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
      ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
      ann.setLayerSizes(np.array([3, 8, 4]))
      ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

      input_array = np.array([ [1.0, 0.0, 0.0],
      [0.0, 1.0, 0.0],
      [0.0, 0.0, 1.0],
      [1.0, 1.0, 1.0],
      ], dtype=np.float32)

      output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
      [0.0, 1.0, 0.0, 0.0],
      [0.0, 0.0, 1.0, 0.0],
      [0.0, 0.0, 0.0, 1.0],
      ], dtype=np.float32)

      td = cv2.ml.TrainData_create(input_array, cv2.ml.ROW_SAMPLE, output_array)
      ann.train(td, cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)

      SAMPLES = 5000
      for x in range(0, SAMPLES):
      ann.train(td, cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)


      and this works well:



      print(ann.predict(input_array))

      (0.0, array([[ 1.0000000e+00, 0.0000000e+00, 4.7625793e-16, 2.8575474e-16],
      [ 1.9050316e-16, 10000000e+00, 5.7150949e-16, 9.5251581e-17],
      [ 9.5251581e-17, 0.0000000e+00, 1.0000000e+00, -1.9050316e-16],
      [-1.9050316e-16, -1.9050316e-16, 0.0000000e+00, 1.0000000e+00]],
      dtype=float32))


      The following code doesn't use cv2.ml.TrainData_create but apparently use the same data and parameters:



      import cv2
      import numpy as np

      ann = cv2.ml.ANN_MLP_create()
      ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)
      ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
      ann.setLayerSizes(np.array([3, 8, 4]))
      ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

      input_array = np.array([ [1.0, 0.0, 0.0],
      [0.0, 1.0, 0.0],
      [0.0, 0.0, 1.0],
      [1.0, 1.0, 1.0],
      ], dtype=np.float32)

      output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
      [0.0, 1.0, 0.0, 0.0],
      [0.0, 0.0, 1.0, 0.0],
      [0.0, 0.0, 0.0, 1.0],
      ], dtype=np.float32)

      SAMPLES = 5000
      for x in range(0, SAMPLES):
      ann.train(np.array([[1.0, 0.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[1.0, 0.0, 0.0, 0.0]], dtype=np.float32))
      ann.train(np.array([[0.0, 1.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 1.0, 0.0, 0.0]], dtype=np.float32))
      ann.train(np.array([[0.0, 0.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 1.0, 0.0]], dtype=np.float32))
      ann.train(np.array([[1.0, 1.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 0.0, 1.0]], dtype=np.float32))


      But this one simply doesn't work:



      print(ann.predict(input_array))

      (0.0, array([[ 1.2886142 , 0.51306236, -1.0352006 , -0.19007786],
      [ 1.2194023 , 0.7686653 , -1.097198 , -0.03246666],
      [ 0.99483347, 0.40380374, -0.917998 , 0.08949649],
      [ 0.7475754 , 0.12770385, -0.81321925, 0.37416443]],
      dtype=float32))


      What's wrong with the second code snippet?










      share|improve this question














      I'm using MLP ANN provided by OpenCV 3.4 with python. I noticed that when training data is prepared via cv2.ml.TrainData_create the ANN performs well, in case this is not used but same samples and parameters are used, ANN is not correctly trained.



      Here I don't mean trainings differ even if using the same data (which can be expected due to random starting points), because what I see here is working-training VS not-working-training and this occurs always.



      The following code uses cv2.ml.TrainData_create



      import cv2
      import numpy as np

      ann = cv2.ml.ANN_MLP_create()
      ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
      ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
      ann.setLayerSizes(np.array([3, 8, 4]))
      ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

      input_array = np.array([ [1.0, 0.0, 0.0],
      [0.0, 1.0, 0.0],
      [0.0, 0.0, 1.0],
      [1.0, 1.0, 1.0],
      ], dtype=np.float32)

      output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
      [0.0, 1.0, 0.0, 0.0],
      [0.0, 0.0, 1.0, 0.0],
      [0.0, 0.0, 0.0, 1.0],
      ], dtype=np.float32)

      td = cv2.ml.TrainData_create(input_array, cv2.ml.ROW_SAMPLE, output_array)
      ann.train(td, cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)

      SAMPLES = 5000
      for x in range(0, SAMPLES):
      ann.train(td, cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)


      and this works well:



      print(ann.predict(input_array))

      (0.0, array([[ 1.0000000e+00, 0.0000000e+00, 4.7625793e-16, 2.8575474e-16],
      [ 1.9050316e-16, 10000000e+00, 5.7150949e-16, 9.5251581e-17],
      [ 9.5251581e-17, 0.0000000e+00, 1.0000000e+00, -1.9050316e-16],
      [-1.9050316e-16, -1.9050316e-16, 0.0000000e+00, 1.0000000e+00]],
      dtype=float32))


      The following code doesn't use cv2.ml.TrainData_create but apparently use the same data and parameters:



      import cv2
      import numpy as np

      ann = cv2.ml.ANN_MLP_create()
      ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS | cv2.ml.ANN_MLP_NO_INPUT_SCALE | cv2.ml.ANN_MLP_NO_OUTPUT_SCALE)
      ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
      ann.setLayerSizes(np.array([3, 8, 4]))
      ann.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))

      input_array = np.array([ [1.0, 0.0, 0.0],
      [0.0, 1.0, 0.0],
      [0.0, 0.0, 1.0],
      [1.0, 1.0, 1.0],
      ], dtype=np.float32)

      output_array = np.array([ [1.0, 0.0, 0.0, 0.0],
      [0.0, 1.0, 0.0, 0.0],
      [0.0, 0.0, 1.0, 0.0],
      [0.0, 0.0, 0.0, 1.0],
      ], dtype=np.float32)

      SAMPLES = 5000
      for x in range(0, SAMPLES):
      ann.train(np.array([[1.0, 0.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[1.0, 0.0, 0.0, 0.0]], dtype=np.float32))
      ann.train(np.array([[0.0, 1.0, 0.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 1.0, 0.0, 0.0]], dtype=np.float32))
      ann.train(np.array([[0.0, 0.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 1.0, 0.0]], dtype=np.float32))
      ann.train(np.array([[1.0, 1.0, 1.0]], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([[0.0, 0.0, 0.0, 1.0]], dtype=np.float32))


      But this one simply doesn't work:



      print(ann.predict(input_array))

      (0.0, array([[ 1.2886142 , 0.51306236, -1.0352006 , -0.19007786],
      [ 1.2194023 , 0.7686653 , -1.097198 , -0.03246666],
      [ 0.99483347, 0.40380374, -0.917998 , 0.08949649],
      [ 0.7475754 , 0.12770385, -0.81321925, 0.37416443]],
      dtype=float32))


      What's wrong with the second code snippet?







      python opencv machine-learning neural-network training-data






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 8:21









      kumakuma

      13




      13
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442938%2fopencv-trainings-lead-to-different-result-when-using-traindata-create%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442938%2fopencv-trainings-lead-to-different-result-when-using-traindata-create%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?