What is the loss function of YOLOv3TensorFlow: Implementing a class-wise weighted cross entropy loss?What is weight decay loss?YOLO Loss function decreasing accuracyPairwise Ranking Loss function in TensorflowKeras - custom loss function - chamfer distanceUnderstanding Cross Entropy LossWhat dataset is being used when Tensorflow Estimator prints the lossCustom Loss function Keras combining Cross entropy loss and mae loss for Ordinal ClassificationTensorFlow Regression Loss FunctionCalculating loss in YOLOv3 for all three scales

Why is template constructor preferred to copy constructor?

What is the missing number, can anyone solve this? My original puzzle

Why does the speed of sound decrease at high altitudes although the air density decreases?

What organs or modifications would be needed for a life biological creature not to require sleep?

My research paper filed as a patent in China by my Chinese supervisor without me as inventor

Should you only use colons and periods in dialogues?

How do EVA suits manage water excretion?

Does my opponent need to prove his creature has morph?

In what state are satellites left in when they are left in a graveyard orbit?

Can derivatives be defined as anti-integrals?

Which is the current decimal separator?

How do I say "quirky" in German without sounding derogatory?

Is the Dodge action perceptible to other characters?

What hard drive connector is this?

Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?

What is this gigantic dish at Ben Gurion airport?

Will the UK home office know about 5 previous visa rejections in other countries?

What is the derivative of an exponential function with another function as its base?

Karazuba Algorithm with arbitrary bases

Asked to Not Use Transactions and to Use A Workaround to Simulate One

Why do sellers care about down payments?

Why is my fire extinguisher emptied after one use?

How to stabilise the bicycle seatpost and saddle when it is all the way up?

What does a Light weapon mean mechanically?



What is the loss function of YOLOv3


TensorFlow: Implementing a class-wise weighted cross entropy loss?What is weight decay loss?YOLO Loss function decreasing accuracyPairwise Ranking Loss function in TensorflowKeras - custom loss function - chamfer distanceUnderstanding Cross Entropy LossWhat dataset is being used when Tensorflow Estimator prints the lossCustom Loss function Keras combining Cross entropy loss and mae loss for Ordinal ClassificationTensorFlow Regression Loss FunctionCalculating loss in YOLOv3 for all three scales






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I was going to write my own implementation of the YOLOv3 and coming up with some problem with the loss function. The original paper mention that he uses Binary Cross Entropy on the class prediction part, which is what I did.



I tried reading some code by the original darknet code, but I didn't find anything that that related to the BCE loss. And I read furthermore with some approach using Keras, Pytorch, and TensorFlow. Everyone seems to have their own opinion on the loss function. Some just take MSE for width and height estimation, and the rest with BCE, some take x,y,w,h with MSE and the rest with BCE.



Here's some of my code:



loss_x = self.mse_loss(x[mask], tx[mask])
loss_y = self.mse_loss(y[mask], ty[mask])
loss_w = self.mse_loss(w[mask], tw[mask])
loss_h = self.mse_loss(h[mask], th[mask])
loss_conf = self.bce_loss(pred_conf[conf_mask_false], tconf[conf_mask_false]) + self.bce_loss(pred_conf[conf_mask_true],tconf[conf_mask_true])
loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask],torch.argmax(tcls[mask], 1))
loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls


As the loss function plays an important role in the training. I wish someone could help me to figure it out.










share|improve this question






























    3















    I was going to write my own implementation of the YOLOv3 and coming up with some problem with the loss function. The original paper mention that he uses Binary Cross Entropy on the class prediction part, which is what I did.



    I tried reading some code by the original darknet code, but I didn't find anything that that related to the BCE loss. And I read furthermore with some approach using Keras, Pytorch, and TensorFlow. Everyone seems to have their own opinion on the loss function. Some just take MSE for width and height estimation, and the rest with BCE, some take x,y,w,h with MSE and the rest with BCE.



    Here's some of my code:



    loss_x = self.mse_loss(x[mask], tx[mask])
    loss_y = self.mse_loss(y[mask], ty[mask])
    loss_w = self.mse_loss(w[mask], tw[mask])
    loss_h = self.mse_loss(h[mask], th[mask])
    loss_conf = self.bce_loss(pred_conf[conf_mask_false], tconf[conf_mask_false]) + self.bce_loss(pred_conf[conf_mask_true],tconf[conf_mask_true])
    loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask],torch.argmax(tcls[mask], 1))
    loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls


    As the loss function plays an important role in the training. I wish someone could help me to figure it out.










    share|improve this question


























      3












      3








      3








      I was going to write my own implementation of the YOLOv3 and coming up with some problem with the loss function. The original paper mention that he uses Binary Cross Entropy on the class prediction part, which is what I did.



      I tried reading some code by the original darknet code, but I didn't find anything that that related to the BCE loss. And I read furthermore with some approach using Keras, Pytorch, and TensorFlow. Everyone seems to have their own opinion on the loss function. Some just take MSE for width and height estimation, and the rest with BCE, some take x,y,w,h with MSE and the rest with BCE.



      Here's some of my code:



      loss_x = self.mse_loss(x[mask], tx[mask])
      loss_y = self.mse_loss(y[mask], ty[mask])
      loss_w = self.mse_loss(w[mask], tw[mask])
      loss_h = self.mse_loss(h[mask], th[mask])
      loss_conf = self.bce_loss(pred_conf[conf_mask_false], tconf[conf_mask_false]) + self.bce_loss(pred_conf[conf_mask_true],tconf[conf_mask_true])
      loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask],torch.argmax(tcls[mask], 1))
      loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls


      As the loss function plays an important role in the training. I wish someone could help me to figure it out.










      share|improve this question














      I was going to write my own implementation of the YOLOv3 and coming up with some problem with the loss function. The original paper mention that he uses Binary Cross Entropy on the class prediction part, which is what I did.



      I tried reading some code by the original darknet code, but I didn't find anything that that related to the BCE loss. And I read furthermore with some approach using Keras, Pytorch, and TensorFlow. Everyone seems to have their own opinion on the loss function. Some just take MSE for width and height estimation, and the rest with BCE, some take x,y,w,h with MSE and the rest with BCE.



      Here's some of my code:



      loss_x = self.mse_loss(x[mask], tx[mask])
      loss_y = self.mse_loss(y[mask], ty[mask])
      loss_w = self.mse_loss(w[mask], tw[mask])
      loss_h = self.mse_loss(h[mask], th[mask])
      loss_conf = self.bce_loss(pred_conf[conf_mask_false], tconf[conf_mask_false]) + self.bce_loss(pred_conf[conf_mask_true],tconf[conf_mask_true])
      loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask],torch.argmax(tcls[mask], 1))
      loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls


      As the loss function plays an important role in the training. I wish someone could help me to figure it out.







      machine-learning deep-learning computer-vision object-detection yolo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 10:22









      Paul GuoPaul Guo

      214 bronze badges




      214 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1
















          Loss function of Yolo v3, look at src/yolo_layer.c



          delta for box, line 93



          float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride)

          box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
          float iou = box_iou(pred, truth);

          float tx = (truth.x*lw - i);
          float ty = (truth.y*lh - j);
          float tw = log(truth.w*w / biases[2*n]);
          float th = log(truth.h*h / biases[2*n + 1]);

          delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
          delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
          delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
          delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
          return iou;



          delta for class, line 111



          void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)

          int n;
          if (delta[index])
          delta[index + stride*class] = 1 - output[index + stride*class];
          if(avg_cat) *avg_cat += output[index + stride*class];
          return;

          for(n = 0; n < classes; ++n)
          delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
          if(n == class && avg_cat) *avg_cat += output[index + stride*n];




          delta for objectness, line 178



          l.delta[obj_index] = 0 - l.output[obj_index];
          if (best_iou > l.ignore_thresh) {
          l.delta[obj_index] = 0;


          and



          l.delta[obj_index] = 1 - l.output[obj_index];


          Loss = sum of square



          *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);


          Anyway I just give you a glimpse about loss function in Yolo V3. For detail explanation you should follow this github discussion :
          https://github.com/AlexeyAB/darknet/issues/1695#issuecomment-426016524
          and
          https://github.com/AlexeyAB/darknet/issues/1845#issuecomment-434079752






          share|improve this answer


























            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/4.0/"u003ecc by-sa 4.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%2f55395205%2fwhat-is-the-loss-function-of-yolov3%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1
















            Loss function of Yolo v3, look at src/yolo_layer.c



            delta for box, line 93



            float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride)

            box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
            float iou = box_iou(pred, truth);

            float tx = (truth.x*lw - i);
            float ty = (truth.y*lh - j);
            float tw = log(truth.w*w / biases[2*n]);
            float th = log(truth.h*h / biases[2*n + 1]);

            delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
            delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
            delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
            delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
            return iou;



            delta for class, line 111



            void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)

            int n;
            if (delta[index])
            delta[index + stride*class] = 1 - output[index + stride*class];
            if(avg_cat) *avg_cat += output[index + stride*class];
            return;

            for(n = 0; n < classes; ++n)
            delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
            if(n == class && avg_cat) *avg_cat += output[index + stride*n];




            delta for objectness, line 178



            l.delta[obj_index] = 0 - l.output[obj_index];
            if (best_iou > l.ignore_thresh) {
            l.delta[obj_index] = 0;


            and



            l.delta[obj_index] = 1 - l.output[obj_index];


            Loss = sum of square



            *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);


            Anyway I just give you a glimpse about loss function in Yolo V3. For detail explanation you should follow this github discussion :
            https://github.com/AlexeyAB/darknet/issues/1695#issuecomment-426016524
            and
            https://github.com/AlexeyAB/darknet/issues/1845#issuecomment-434079752






            share|improve this answer































              1
















              Loss function of Yolo v3, look at src/yolo_layer.c



              delta for box, line 93



              float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride)

              box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
              float iou = box_iou(pred, truth);

              float tx = (truth.x*lw - i);
              float ty = (truth.y*lh - j);
              float tw = log(truth.w*w / biases[2*n]);
              float th = log(truth.h*h / biases[2*n + 1]);

              delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
              delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
              delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
              delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
              return iou;



              delta for class, line 111



              void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)

              int n;
              if (delta[index])
              delta[index + stride*class] = 1 - output[index + stride*class];
              if(avg_cat) *avg_cat += output[index + stride*class];
              return;

              for(n = 0; n < classes; ++n)
              delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
              if(n == class && avg_cat) *avg_cat += output[index + stride*n];




              delta for objectness, line 178



              l.delta[obj_index] = 0 - l.output[obj_index];
              if (best_iou > l.ignore_thresh) {
              l.delta[obj_index] = 0;


              and



              l.delta[obj_index] = 1 - l.output[obj_index];


              Loss = sum of square



              *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);


              Anyway I just give you a glimpse about loss function in Yolo V3. For detail explanation you should follow this github discussion :
              https://github.com/AlexeyAB/darknet/issues/1695#issuecomment-426016524
              and
              https://github.com/AlexeyAB/darknet/issues/1845#issuecomment-434079752






              share|improve this answer





























                1














                1










                1









                Loss function of Yolo v3, look at src/yolo_layer.c



                delta for box, line 93



                float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride)

                box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
                float iou = box_iou(pred, truth);

                float tx = (truth.x*lw - i);
                float ty = (truth.y*lh - j);
                float tw = log(truth.w*w / biases[2*n]);
                float th = log(truth.h*h / biases[2*n + 1]);

                delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
                delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
                delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
                delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
                return iou;



                delta for class, line 111



                void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)

                int n;
                if (delta[index])
                delta[index + stride*class] = 1 - output[index + stride*class];
                if(avg_cat) *avg_cat += output[index + stride*class];
                return;

                for(n = 0; n < classes; ++n)
                delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
                if(n == class && avg_cat) *avg_cat += output[index + stride*n];




                delta for objectness, line 178



                l.delta[obj_index] = 0 - l.output[obj_index];
                if (best_iou > l.ignore_thresh) {
                l.delta[obj_index] = 0;


                and



                l.delta[obj_index] = 1 - l.output[obj_index];


                Loss = sum of square



                *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);


                Anyway I just give you a glimpse about loss function in Yolo V3. For detail explanation you should follow this github discussion :
                https://github.com/AlexeyAB/darknet/issues/1695#issuecomment-426016524
                and
                https://github.com/AlexeyAB/darknet/issues/1845#issuecomment-434079752






                share|improve this answer















                Loss function of Yolo v3, look at src/yolo_layer.c



                delta for box, line 93



                float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride)

                box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
                float iou = box_iou(pred, truth);

                float tx = (truth.x*lw - i);
                float ty = (truth.y*lh - j);
                float tw = log(truth.w*w / biases[2*n]);
                float th = log(truth.h*h / biases[2*n + 1]);

                delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
                delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
                delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
                delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
                return iou;



                delta for class, line 111



                void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)

                int n;
                if (delta[index])
                delta[index + stride*class] = 1 - output[index + stride*class];
                if(avg_cat) *avg_cat += output[index + stride*class];
                return;

                for(n = 0; n < classes; ++n)
                delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
                if(n == class && avg_cat) *avg_cat += output[index + stride*n];




                delta for objectness, line 178



                l.delta[obj_index] = 0 - l.output[obj_index];
                if (best_iou > l.ignore_thresh) {
                l.delta[obj_index] = 0;


                and



                l.delta[obj_index] = 1 - l.output[obj_index];


                Loss = sum of square



                *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);


                Anyway I just give you a glimpse about loss function in Yolo V3. For detail explanation you should follow this github discussion :
                https://github.com/AlexeyAB/darknet/issues/1695#issuecomment-426016524
                and
                https://github.com/AlexeyAB/darknet/issues/1845#issuecomment-434079752







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 29 at 8:15

























                answered Mar 29 at 0:07









                gameon67gameon67

                1,58712 silver badges29 bronze badges




                1,58712 silver badges29 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















                    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%2f55395205%2fwhat-is-the-loss-function-of-yolov3%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript