{"action":"opened","issue":{"url":"https://api.github.com/repos/mshabunin/test2/issues/353","labels_url":"https://api.github.com/repos/mshabunin/test2/issues/353/labels{/name}","comments_url":"https://api.github.com/repos/mshabunin/test2/issues/353/comments","events_url":"https://api.github.com/repos/mshabunin/test2/issues/353/events","html_url":"https://github.com/mshabunin/test2/issues/353","id":95412496,"number":353,"title":"bug, race conditions in updateTrainingSet/fillPassedSamples/NegReader::get","user":{"login":"mshabunin","id":3304494,"avatar_url":"https://avatars.githubusercontent.com/u/3304494?v=3","gravatar_id":"","url":"https://api.github.com/users/mshabunin","html_url":"https://github.com/mshabunin","followers_url":"https://api.github.com/users/mshabunin/followers","following_url":"https://api.github.com/users/mshabunin/following{/other_user}","gists_url":"https://api.github.com/users/mshabunin/gists{/gist_id}","starred_url":"https://api.github.com/users/mshabunin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshabunin/subscriptions","organizations_url":"https://api.github.com/users/mshabunin/orgs","repos_url":"https://api.github.com/users/mshabunin/repos","events_url":"https://api.github.com/users/mshabunin/events{/privacy}","received_events_url":"https://api.github.com/users/mshabunin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-16T11:24:16Z","updated_at":"2015-07-16T11:24:16Z","closed_at":null,"body":"Transferred from http://code.opencv.org/issues/3147\r\n```\n|| Alex Leverington on 2013-07-09 18:19\n|| Priority: Normal\n|| Affected: 2.4.6 (latest release)\n|| Category: objdetect\n|| Tracker: Bug\n|| Difficulty: Hard\n|| PR: \n|| Platform: Any / Any\n```\n\nbug, race conditions in updateTrainingSet/fillPassedSamples/NegReader::get\n-----------\n```\nThere is a common (and major) symptom which is caused by the issues discussed below. Basically, the issues below causes the training process to take an excessive amount of time. The current strategy I've seen from SO posts and such is \"well, training takes a long time, so just let it run for a week\".\r\n\r\nHowever after using the cascade trainer a lot, I've found there are indeed bugs, and I've found fixes for them!\r\n\r\nI've described the issues in more (excruciating) detail below, but let's look at the math. Based on my analysis of the code surrounding these bugs, my changes are based on the math/logic below. I've personally validated that the training now runs much faster (read: O(1) time as compared to previous O(currStage*nStages*0.5)) and works properly.\r\n\r\n\r\nBelow is psuedo code of the workflow for train() and updateTrainingSet(). Is this accurate with regard to acceptanceRatio, requiredLeafRate, and reaching target FA rate?\r\n\r\n\r\nrequiredLeafRate = pow(maxFA, numStages) / maxDepth\r\n\r\nupdateTrainingSet(&acceptanceRatio)\r\n   posCount = fillPassedSamples(&posConsumed) // fills from 0 .. numPos + rejectPosCount (until out of pos; if out, training fails)\r\n   negCount = fillPassedSamples(&negConsumed) // cycles through tiling/scaling every image in -bg parameter\r\n\r\n   _curNumSamples = posCount + negCount;\r\n   acceptanceRatio = negConsumed == 0 ? 0 : ( (double)negCount/(double)(int64)negConsumed );\r\n   return\r\n\r\nif acceptanceRatio <= requiredLeafRate\r\n  done: \"Required leaf false alarm rate achieved.\"\r\n\r\n\r\n\r\n\r\n\r\n\r\nBug #1: infinite loop occurs if a given stage reaches a 0 FA rate (such as when no negatives are detected as FAs)\r\nTo reproduce\r\n1) create positive image sample set with clear and consistent features\r\n2) create negative image samples which contain no features (gradients/gauss blur will do this)\r\n3) negative images have same size as positive images\r\n4) negative images are square\r\n\r\nThe result of these conditions is that fillPassedSamples will endlessly loop pulling background images (until \"rounds\" integer overflows).\r\n\r\n\r\n#2: if trained classifier cannot detect a given positive image, and there are no extra image within vector, training fails\r\nTo reproduce:\r\nCreating some poor/blurred positives would yield this result. Although this can easily be discerned by reading current fillPassedSamples method. Quite simply, if there are 100 positive samples in a vector, numPos=100, and one of the positive images isn't detected within predix() of fillPassedSamples, the training will end with an error that there are no more images in the vector file.\r\n\r\nQuestion:\r\nHow should this function?\r\n\r\nIt's quite possible that stage 1 could eliminate 75% of positives but then all remaining stages detect every positive. Moreover, as long as there at at least n=(positive - eliminated) \"extra\" negative images, there will still be enough samples for adaboost.\r\n\r\nSo there are two options. Either 1, recommend to users they provide a vector file which has numPos*numStages*0.5 samples. Or, option 2, allow positive image reader to return false if it reads all images and have fillPassedSamples determine whether there are not enough images. In the latter case\r\n\r\nIn either case, documentation or logic should be updated to correct this issue.\r\n\r\n\r\n#3: (related to #1) If number of negative FA < numNeg, fillPassedSamples will perform excessive FA detections.\r\nThis is really a math issue. So, either the \"supporting\" code around the current logic is flawed, or the current logic is flawed. (math is above)\n```\n\nHistory\n-------\n##### Nikita Manovich on 2013-07-11 12:36\n```\nAlex,\r\n\r\nThank you for the bug report with a lot of details. If you have a fix for the problem please prepare a pull request. It will help us significantly. We will be glad to accept your contribution. On the page http://www.code.opencv.org/projects/opencv/wiki/How_to_contribute you will find information how to do that.\n-   Description changed from There is a common (and major) symptom which\n    is caused by the issues discusse... to There is a common (and major)\n    symptom which is caused by the issues discussed... More\n-   Assignee set to Alex Leverington\n-   Status changed from New to Open\n```"}}