<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yanfeng Liu on Medium]]></title>
        <description><![CDATA[Stories by Yanfeng Liu on Medium]]></description>
        <link>https://medium.com/@yanfengliux?source=rss-30f5166c3ac2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4duP_vjHEce6cGYOxlOxZQ.jpeg</url>
            <title>Stories by Yanfeng Liu on Medium</title>
            <link>https://medium.com/@yanfengliux?source=rss-30f5166c3ac2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 17:47:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yanfengliux/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Automatically run Cypress tests on every code push with GitHub Action and Docker]]></title>
            <link>https://medium.com/geekculture/automatically-run-cypress-tests-on-every-code-push-with-github-action-and-docker-f6f305632fc5?source=rss-30f5166c3ac2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6f305632fc5</guid>
            <category><![CDATA[cypress]]></category>
            <category><![CDATA[system-testing]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Yanfeng Liu]]></dc:creator>
            <pubDate>Thu, 29 Jul 2021 02:16:56 GMT</pubDate>
            <atom:updated>2021-08-03T10:47:30.724Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*W0JB93S_pClp6MRv.png" /></figure><p>Ever since I published the Turkey <a href="https://github.com/yanfengliu/turkey">repository</a> for crowd-sourcing image segmentation annotation on Amazon Mechanical Turk, I have been thinking about how to keep improving it using the best practices of software engineering. One key improvement that I implemented recently is getting the GitHub repo to run full system tests on every code push. It is fully automated, consistent, environment agnostic, and free.</p><h3>1. Turkey</h3><p>The product I am testing is called Turkey. It is a HTML file that you can paste into Amazon Mechanical Turk as a custom job. It allows crowd sourcing image annotation data, such as instance segmentation, bounding boxes, and lines, in the same job. It supports undo, redo, zooming in, zooming out, drag, and reset. I made this when I was in college because there wasn&#39;t a good and free solution for online image annotation crowd sourcing, and we needed one for a research project.</p><h3>2. Cypress</h3><p>Cypress is great for system tests. I plan to write a separate article on writing good Cypress tests. For the scope of this article, I will only talk about what I use it for. As the functionalities of Turkey expands, it is hard to keep track of all the things it does. It is even harder to manually test all the features reliably. Cypress programmatically simulates clicking, dragging, and typing. All the things that a user would typically do when using my product. Due to the nature of the image annotation tool, it is not enough to just know that Cypress found the rendered components and interacted with them successfully. I also need to make sure that the graphical user interface (GUI) looks exactly the way I expect it to. So I use a Cypress plugin package called cypress-image-snapshot(<a href="https://www.npmjs.com/package/cypress-image-snapshot">npm link</a>) to take screenshots and compare them with the stored version. A typical test for Turkey looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f38afb0080025e0de0da2b1cb921e529/href">https://medium.com/media/f38afb0080025e0de0da2b1cb921e529/href</a></iframe><p>the command matchImageSnapshot would take a screenshot of only what cy.get(&quot;canvas&quot;) returned, compare it with the stored screenshot of the same name, and check of percentage of different pixels. It fails if the percentage is above a customizable threshold. If there is not a stored screenshot by that name, then it stores the freshly captured one as the correct answer.</p><p>To run the tests, first you need to serve the website on localhost, and then run cypress run. It will automatically run all the tests found within the cypress folder. You can also use cypress open to watch it in action, but be aware that this mode could be using a different viewport than headless mode unless overridden. The resolution difference will cause snapshot comparison to fail.</p><h3>3. GitHub Actions</h3><p>GitHub released their Actions feature in 2018. It is great to see native support of CI/CD on GitHub. By specifying a \\.github\\workflows\\github-actions.ymlfile in your repo, GitHub automatically picks up the action definitions. The action then gets triggered on every pull request or push. Cypress now has their official GitHub action, and the options are <a href="https://github.com/cypress-io/github-action">quite impressive</a>. So I started experimenting with the most basic version:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b382932024eadae509fb6f4b6dc4581/href">https://medium.com/media/1b382932024eadae509fb6f4b6dc4581/href</a></iframe><p>Let’s go through the file line by line:</p><ul><li>name: Turkey end-to-end tests chrome headless: name of the action. Doesn&#39;t affect any functionality other than helping you remember what it does</li><li>on: [push]: this action will be triggered on every git push</li><li>jobs:defines what runs when action is triggered.</li><li>cypress-run:: this is also just a name for the job. You can have multiple jobs defined. Jobs run in parallel by default, but they can also run in sequence. So in our case, we can have multiple jobs running, one for each supported browser, or a slightly different variation in settings.</li><li>runs-on: ubuntu-20.04: operating system that the action runs on. As it turns out, this will have an impact on our system tests results. More on that later.</li><li>steps: steps of the job. These run in sequence.</li><li>- uses: actions/checkout@v2: uses version 2 of a community action that downloads your repo to the host.</li><li>- uses: cypress-io/github-action@v2: uses the cypress action that runs the job. It runs cypress run. Note that, for this to work, there must be a package.json file in the repo and has cypress as a dependency in it.</li><li>with:extra settings that we pass into the cypress action</li><li>start: npm start: before running cypress run, we will run npm start to start hosting the website first</li><li>wait-on: &quot;&lt;http://localhost:5000&gt;&quot;: wait until the website is up</li><li>browser: chrome: we run the tests on chrome, whatever version that comes with the cypress package.</li><li>headless: true: we use headless mode to stay consistent with what we do locally, and also because we can&#39;t see it anyway</li></ul><p>I pushed the code, saw the action running, cheered loudly, and saw that tests failed. What went wrong? To be able to debug this, I need GitHub to save the artifacts so that I can download the screenshot diffs and see for myself. So a little addition to the yml file at the end:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e7de7b1e69b2fcd59eef554b41830c7/href">https://medium.com/media/5e7de7b1e69b2fcd59eef554b41830c7/href</a></iframe><p>This tells GitHub to make whatever is in cypress-screenshots folder available if a test fails. It will also always provide cypress videos, which gets saved on every test run. So I downloaded the diffs, and they look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-t-EAJlI-cYkB3z8R8UVgg.png" /></figure><p>The before and after look identical to the human eyes, so what is going on? Turns out, rendering is very different between operating systems. When the tests ran locally, it was on my Windows 10 machine, but on GitHub Actions, it was on Ubuntu. The website also looks different on different browsers or machines, even if they are running on the same operating system. The physical monitor could also impact the color profile used for rendering. So the only way to be absolutely sure is to run the tests on a consistent, isolated environment. You guessed it — we need Docker.</p><h3>4. Docker</h3><p>The Cypress GitHub action supports Docker natively, and Cypress also has their official Docker images hosted on Docker Hub, so with one line of code, we can get Docker version of Cypress tests running very easily:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b217ad9f242e895093845d7d2c28fda0/href">https://medium.com/media/b217ad9f242e895093845d7d2c28fda0/href</a></iframe><p>This works surprisingly well with the rest of the definitions. Artifacts are still uploaded, even though they are generated by the test running from Docker. Now the focus is getting the same set up working locally.</p><p>It turns out, we don’t have to use the exact environment that the Action runner was on. We just need the Linux environment that Docker uses by default. We write our own Dockerfile that builds on top of cypress/included:8.0.0 and adds the other dependencies that we use locally to host the website:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a7866e9ab73987df35f3c63fc2208bb/href">https://medium.com/media/5a7866e9ab73987df35f3c63fc2208bb/href</a></iframe><p>where the scripts in package.json looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/416e78625887d89bb970f1b5182096f9/href">https://medium.com/media/416e78625887d89bb970f1b5182096f9/href</a></iframe><p>so npm run test:chrome starts the website, waits for it to be ready on port 5000, and then runs tests on chrome. We clear the cypress/screenshots folder so that the first run would generate ground truth screenshots, and then we push them onto the repo. This time, the tests pass. It is a very satisfying feeling when everything works in the end.</p><h3>References</h3><ul><li><a href="https://github.com/yanfengliu/turkey">https://github.com/yanfengliu/turkey</a></li><li><a href="https://docs.cypress.io/guides/continuous-integration/github-actions">https://docs.cypress.io/guides/continuous-integration/github-actions</a></li><li><a href="https://docs.github.com/en/actions">https://docs.github.com/en/actions</a></li></ul><p>(Here’s my original Notion <a href="https://yanfeng.notion.site/Automatically-run-Cypress-tests-on-every-code-push-with-GitHub-Action-and-Docker-6283469aa9414540b982ff25156a213f">link</a> for a better reading experience)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6f305632fc5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/automatically-run-cypress-tests-on-every-code-push-with-github-action-and-docker-f6f305632fc5">Automatically run Cypress tests on every code push with GitHub Action and Docker</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing turkey: an Amazon Mechanical Turk turn-key segment tool]]></title>
            <link>https://yanfengliux.medium.com/introducing-turkey-an-amazon-mechanical-turk-turn-key-segment-tool-c9f4cc6f7dd0?source=rss-30f5166c3ac2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9f4cc6f7dd0</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[image-annotation]]></category>
            <category><![CDATA[image-segmentation]]></category>
            <category><![CDATA[amazon-mechanical-turk]]></category>
            <dc:creator><![CDATA[Yanfeng Liu]]></dc:creator>
            <pubDate>Fri, 28 Jun 2019 19:52:47 GMT</pubDate>
            <atom:updated>2021-04-21T23:31:48.417Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*JnwuCcPbtPANwO2z7AyV8A.jpeg" /></figure><p>Crowd-sourcing image instance segmentation data can be cumbersome, especially when you have to write the tool from scratch. That’s why I created turkey. The <a href="https://github.com/yanfengliu/turkey">code</a> is designed to be directly copied and pasted onto <a href="https://www.mturk.com/">Amazon Mechanical Turk</a> for instance segmentation. Its current functions include:</p><ul><li>Customize the annotation modes (dot, line, polygon) and class labels on per-image basis</li><li>Import previous annotations generated by either another human or an algorithm (such as a neural network)</li><li>Zoom, delete, undo, reset</li></ul><p>The repository also includes a Matlab script that reads the downloadable csv result and reconstructs the annotations.</p><p>I’m aware that Amazon has recently released their own image segmentation tool. There are 3 advantages of my tool compared to theirs:</p><ul><li>It seems to be a semantic segmentation tool, which is different from instance segmentation. For semantic segmentation you basically label which pixel belongs to which class, but if there are multiple instances of the same class, semantic segmentation doesn’t differentiate them.</li><li>Their tool also seemed a little buggy. When I tried it, only 1 of the 2 images got loaded correctly, and the annotation results in the downloaded csv was confusing (it looked somewhat like “B8gzS96nSL7Xnp0”).</li><li>The most import thing is that their tool is encapsulated into their internal API, which we have no direct access to. That means if you want to change something or add something that was not included in the official example, you would have to write a new tool from scratch. My tool is written in plain JavaScript, with a little help from jQuery and bootstrap. Feel free to adapt it to your needs.</li></ul><p>Code is tested on Chrome and Microsoft Edge. Development is still in progress. Ideas and suggestions are welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9f4cc6f7dd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Writing Custom Loss Functions in Keras]]></title>
            <link>https://yanfengliux.medium.com/on-writing-custom-loss-functions-in-keras-e04290dd7a96?source=rss-30f5166c3ac2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e04290dd7a96</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[loss-function]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[inverse-kinematics]]></category>
            <dc:creator><![CDATA[Yanfeng Liu]]></dc:creator>
            <pubDate>Tue, 01 Jan 2019 06:56:00 GMT</pubDate>
            <atom:updated>2019-01-02T22:25:49.748Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are doing research in deep learning, chances are that you have to write your own loss functions pretty often. I was playing with a toy problem of solving inverse kinematics with neural networks, and I came across something that at first was very strange to me.</p><p>The inverse kinematics problem is formulated as the following: given a robot arm of a certain length (or maybe a robot arm consisting of several segments with different lengths) and a target location in either the 2D coordinate system or 3D, produce the angles that the robot arm needs to rotate its segments to, so that the end tip of the arm is at that location.</p><p>Consider a robot arm with 2 segments, with length <em>L1 </em>and <em>L2</em>. It needs to rotate these two segments to relative angles of <em>q1</em> and <em>q2</em>. The end tip <em>(p1, p2)</em> will then be at:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*7l8w1VEXJCbKHiTimUBZlA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*4c-ARLx7b6IVv_g89AYqsA.png" /><figcaption>Illustration borrowed from [1]</figcaption></figure><p>I decided to try the simplest version of the problem first, which involves only L1 and q1. The end tip calculation is then as easy as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/197/1*7fRxAn0xnBTk_T9OUSb52g.png" /></figure><p>This should be pretty easy to learn for a neural network. To train it, we provide the end tip location and … hold on, <em>how </em>should we train it?</p><p>You see, the problem is that robot arms can freely rotate to any degree. It might not be directly obvious when we only have 1 segment, but when we have 2 or more segments, there are exponentially more ways to reach the same location. In other words, there is no definite, fixed solution to our problem, even though it is pretty easy to solve. If we train the neural network in a strictly supervised, i.e. feeding the target location and expected angle outputs, the neural network will be biased towards one type of solutions over the others. If the bias in the ground truth is not consistent (solutions are picked randomly), then it is even worse: the network will be confused and try to mix different solutions into one. Consequently, the angles it predicts will land the end tip <em>around </em>the target but not <em>on </em>it.</p><p>A better way to formulate the problem is let the network predict the angles, but then convert angles to the <em>xy </em>location that the angles will take us to, and check the distance between the result <em>xy </em>and target <em>xy</em>. This part is the loss function’s job, which is the main focus of this blog post.</p><p>We basically need to write transform matrix in TensorFlow and then perform matrix multiplication between the location vector and the transform matrix. The rest is as simple as a mean squared error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b7f97e5ff81236aa54d871e5da1f2c8/href">https://medium.com/media/2b7f97e5ff81236aa54d871e5da1f2c8/href</a></iframe><p>We added a little regularization <em>loss2 </em>in the end just to make sure it doesn’t go outside the boundary too far, because otherwise the network could easily add 2*pi to any solution and it will be still valid. In this case we set the boundary of the first angle to be [-pi, pi], and the second angle to be [-0.5*pi, 0.5*pi].</p><p>The only thing we need in order to train the network would be a way to generate data, such as the following block:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa93af1fd73b2e778b99889c608d8fdc/href">https://medium.com/media/fa93af1fd73b2e778b99889c608d8fdc/href</a></iframe><p>The network is a fully connected network. I added many layers just to make sure it is complex enough, but fewer layers will probably suffice.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41afe75a29cac17e49bfe8c2a97930b4/href">https://medium.com/media/41afe75a29cac17e49bfe8c2a97930b4/href</a></iframe><p>The training starts, and the loss value starts to go down, as does the mean squared error on the test set. Everything works and life is great. I smile a little, and decide to increase the batch size so that the training is more stable and it converges faster. BOOM! Loss function goes up all of a sudden, leaving me utterly confused.</p><p>How is it possible that an already trained network suddenly gets destroyed simply because I increase the batch size? If anything, bigger batch sizes should help with the training.</p><p>And then it occurred to me that I wrote my own loss function, which at this point does not support batch size &gt; 1 yet. The loss was simply not calculated correctly. This is the key. When you write your custom design loss function, please keep in mind that it won’t handle batch training unless you specifically tell it how to. Basically, you have to take the average loss over each example in the batch.</p><p>The tricky part is how to write such a loss function. In my case, all I really need it to do in batches is the matrix multiplication between the location vector and the transform matrix. The following block should do the trick:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42aad1c88e63704286b23e9b07d754e1/href">https://medium.com/media/42aad1c88e63704286b23e9b07d754e1/href</a></iframe><p>Let’s test it with batch size = 32:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*FMYKrGXJ1YCgDL_vkqrc8w.png" /><figcaption>Results after 58 mini-batches</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*2C2unEL8RqJk5WP17wexoA.png" /><figcaption>Results after 400 mini-batches</figcaption></figure><p>Below is the full source code for this blog post, ran in jupyter lab.</p><p>Library versions: <strong>tensorflow-gpu</strong>=1.10.0; <strong>keras</strong>=2.2.2; <strong>Python</strong>=3.6.6</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09c521f5486d3235c116fc85cb5b2fc7/href">https://medium.com/media/09c521f5486d3235c116fc85cb5b2fc7/href</a></iframe><p>[1] Tejomurtula, Sreenivas, and Subhash Kak. “Inverse kinematics in robotics using neural networks.” <em>Information sciences</em> 116.2–4 (1999): 147–164.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e04290dd7a96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Confusing Metrics of AP and mAP for Object Detection]]></title>
            <link>https://yanfengliux.medium.com/the-confusing-metrics-of-ap-and-map-for-object-detection-3113ba0386ef?source=rss-30f5166c3ac2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3113ba0386ef</guid>
            <category><![CDATA[instance-segmentation]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[average-precision]]></category>
            <category><![CDATA[object-detection]]></category>
            <dc:creator><![CDATA[Yanfeng Liu]]></dc:creator>
            <pubDate>Thu, 25 Oct 2018 21:37:06 GMT</pubDate>
            <atom:updated>2020-05-18T00:10:23.652Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Confusing Metrics of AP and mAP for Object Detection / Instance Segmentation</h3><p>If you are working on an object detection or instance segmentation algorithm, you have probably come across the messy pile of different kinds of performance metrics. There are AP, AP50, AP75, mAP, AP@[0.5:0.95], and all kinds of fun stuff. Researchers are actually inventing even more new metrics as we speak. Pretty soon you will be pulling your hair out and ask yourself what the **** is all this. “I have an algorithm, and I have my dataset. All I want to do is to evaluate it. Why is it so hard?” Well, I have gone through the same thing and probably spent a ridiculously stupid amount of time trying to figure out what they are. Now that I have done that, I would like to make it easier for you.</p><p>Instance segmentation is kind of a new field. Until the age of deep learning, there weren’t a lot of datasets for it because the algorithms were simply not good enough. These days you mainly see <a href="http://cocodataset.org/#home">MSCOCO</a>, <a href="http://host.robots.ox.ac.uk/pascal/VOC/">PASCAL</a>, <a href="https://www.cityscapes-dataset.com/">CityScape</a>, and all sorts of datasets that include a task called instance segmentation. The main idea of instance segmentation is that we have to segment out each instance of each category, so that, for example, two people in an image will have the same category label as “person” but different instance labels like “person 1” and “person 2”, as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q7L9L7Nb6-c3VbHvs3auXQ.png" /><figcaption>Example for different visual tasks</figcaption></figure><p>To evaluate a certain algorithm on instance segmentation, usually you will see the terms mentioned above, namely <strong>AP</strong> (Average Precision) and <strong>mAP</strong> (Mean Average Precision). Let’s start with the definition of precision and recall.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*lzyCEgSRM-RaOPhmnnu7Wg.png" /></figure><p>where tp = true positive, fp = false positive, fn = false negative. What are they? The table below should make things clear:</p><figure><img alt="Image result for true positive false positive" src="https://cdn-images-1.medium.com/proxy/1*CPnO_bcdbE8FXTejQiV2dg.png" /></figure><p>So basically <strong>precision </strong>is measuring the percentage of correct positive predictions among all predictions made; and <strong>recall </strong>is measuring the percentage of correct positive predictions among all positive cases in reality. There is always a trade-off between the two metrics. Imagine if we label everything as positive, then recall will be 1 because we do not have false negatives, but precision will be horrible because only a small percentage of our positive predictions are actually correct. In the other extreme case, we can be very careful about the selection of positive prediction, so prediction will be very good, but we might have labelled many positive cases as negative and consequently lowered recall.</p><p>Now that we understand precision and recall, we are well equipped to understand <strong>average precision</strong>. It is defined as the area under the precision-recall curve (PR curve). The x-axis is recall, and y-axis is precision. How do we get multiple precision-recall value pairs? In the case of objection detection or instance segmentation, this is done by changing the <strong>score </strong>cutoff. Any detection with a score lower than the cutoff is treated as a false positive. At each unique level of classification score that is present in the detection results, we calculate the precision and recall at that point. After we have gone through all the precision-recall value pairs corresponding to each unique score cutoff, we have a precision-recall curve. It might look like the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*IIP5_lTXigXMViiu.png" /></figure><p>Note that it is monotonically decreasing, which is what we want. And it makes sense: it is always a trade-off between precision and recall. Increasing one will decrease the other. Sometimes it is not monotonically decreasing. When that happens, we make the curve monotonically decreasing by setting P for R equal to the maximum P obtained for any R’&gt;R. So the green curve will look like the red one after we are done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*5raTlICR4mO6hqdyCOAyZw.jpeg" /></figure><p>Now the mystery of AP is solved. But what about the numbers after it? What is AP50, AP75, or AP@[0.5:0.95]? First we have to understand what IoU (Intersection over Union) is. The image below is my favorite explanation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*VnvOCo9NkWG705F3.png" /></figure><p><strong>IoU </strong>is a good way of measuring the amount of overlap between two bounding boxes or segmentation masks. If the prediction is perfect, IoU = 1, and if it completely misses, IoU = 0. A degree of overlap will produce a IoU value between those two. We have to make a decision about when to call it a good enough prediction, i.e. true positive. 50% IoU is a good place to start, but what about 60%? 78.1%? 99%? 100%? Good question. It would introduce a bias if we favor a certain threshold and ignore the others. One way is to change the IoU threshold over a range. If we do that, the precision and recall values will change, and if we draw the precision-recall pairs on a coordinate system, they form a curve. This is the precision-recall curve. In COCO they change the IoU values from 50% to 95%, at a step of 5%. So we end up with 10 precision-recall pairs. If we take the average of those 10 values, we get AP@[0.5:0.95].</p><p>Sometimes the IoU threshold is fixed, for example, at 50% or 75%, which are called AP50 and AP75, respectively. When this is the case, it is simply the AP value with the IoU threshold at that value. Remember, again, we still have to calculate the precision-recall pairs at different score cutoffs.</p><p>Mean average precision (mAP) is much easier to understand once we understand AP. mAP is simply all the AP values averaged over different classes/categories. However, researchers do not like to make things easy. On the COCO website, it confusingly says:</p><blockquote>AP is averaged over all categories. Traditionally, this is called “mean average precision” (mAP). We make no distinction between AP and mAP (and likewise AR and mAR) and assume the difference is clear from context.</blockquote><p>One more thing before we get ready for some evaluation:</p><p>Usually in an object detection/instance segmentation algorithm, there are multiple categories. In fact, even if there are only one class, we still have make the distinction between that class and the background. To make that distinction, our algorithm has to output a classification score (or confidence score). We will just call it score for short. This score is needed for most instance segmentation/object detection datasets out there. If you are using a <strong>top-down</strong> approach, this is no big deal. Your network naturally generates a score for the image enclosed by the bounding box. This score is actually very helpful in the non-max suppression stage because it can filter out duplicate results. However, if you are working on a <strong>bottom-up</strong> approach, meaning that you do not use bounding boxes at all and instead assign a class label and an instance label for each pixel, then you have to use some kind of clustering algorithm for a mask. The score associated with that mask is not immediately obvious. I see two ways of computing it: either calculating the average score over all pixels in that mask, or training a separate branch in the network to specifically predict a single score for that mask.</p><p>There is some slight difference among different datasets’ evaluation mechanisms. For example:</p><p>In COCO, if you look at their source code, they rank all the detections based on the scores from high to low, and then cut off the results at the maximum number of detections allowed. For each detection, the algorithm iterates through all ground truth, and the previously unmatched ground truth with the highest IoU is matched with the detection.</p><p>In CityScapes, for each ground truth, the algorithm iterates through all predictions that have non-zero intersection with it. When there are more than one predictions matched with the same ground truth, the ones with the lower score are automatically set as false positive. IoU is only used to decide if it passes the threshold.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3113ba0386ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>