<?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 Fangda Han on Medium]]></title>
        <description><![CDATA[Stories by Fangda Han on Medium]]></description>
        <link>https://medium.com/@fangdahan?source=rss-4e88899ac2df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*9zUmsiWPcE1fQlTdnHHEJA.png</url>
            <title>Stories by Fangda Han on Medium</title>
            <link>https://medium.com/@fangdahan?source=rss-4e88899ac2df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 12:27:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fangdahan/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[Install and Use TSNECUDA package]]></title>
            <link>https://fangdahan.medium.com/install-and-use-tsnecuda-package-a1355902eb90?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1355902eb90</guid>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Fri, 29 Jan 2021 16:17:58 GMT</pubDate>
            <atom:updated>2021-01-29T16:27:20.284Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2uNQzzPlWw95Nx_kVkIqbA.png" /><figcaption>from: <a href="https://github.com/CannyLab/tsne-cuda">https://github.com/CannyLab/tsne-cuda</a></figcaption></figure><p><a href="https://www.jmlr.org/papers/volume9/vandermaaten08a/vandermaaten08a.pdf?fbclid=IwAR0Bgg1eA5TFmqOZeCQXsIoL6PKrVXUFaskUKtg6yBhVXAFFvZA6yQiYx-M">T-SNE</a> is a great method to visualize high-dimension data by projecting them into three or two dimensions.</p><p>Sk-learn has an <a href="https://scikit-learn.org/stable/modules/generated/sklearn.manifold.TSNE.html">implementation</a> which works on CPU (so it has great compatibility), but the problem is it is too slow on large amount of data.</p><p>CannyLab also has <a href="https://github.com/CannyLab/tsne-cuda">an optimized version</a> that runs on Nvidia GPU (with CUDA), which they claims to be much faster than sk-learn and other implementations of t-SNE, although it can only project to two dimensions.</p><p>However, installation is not as easy as you might think, I encountered several problems during my installation so I want to write them down here in case I forget it.</p><h3>Introduction to My Computer</h3><p>Nvidia Tesla K80 GPU</p><h3>Find CUDA version</h3><p>The first step is to check which CUDA I’m using, after digging for awhile, I found <a href="https://varhowto.com/check-pytorch-cuda-version/">several ways </a>to do this. I tried all of them on my computer and here are what I got.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*ocD83c5fLYkETOUJThhrBQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GuADF9JBLjCzNzU9aSwybw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*KsGF7gZaQAMbhga3UvRLQQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*L_czGWdeE6VI6ssuzr9HXQ.jpeg" /></figure><p>After digging for awhile, I found <a href="https://stackoverflow.com/questions/53422407/different-cuda-versions-shown-by-nvcc-and-nvidia-smi">this page</a>, OK, I might follow nvidia-smi and believe it is <strong>CUDA 11.2</strong>, sounds good, let’s install cudatsne!</p><h3>Never Install cudatsne using pip</h3><p>On their <a href="https://github.com/CannyLab/tsne-cuda/wiki/Installation">Installation page</a>, they recommend using conda, however I found my CUDA version is too high, I tried to install the 10.1 version on my computer anyway, but the problem is after I type in `conda install tsnecuda cuda101 -c cannylab,<strong> I waited for 10 minutes and conda was still not able to install it for me</strong>.</p><p>I canceled it and tried pip install cudatsne because in my experience, pip always works faster than conda, I got it installed but when I use it complains</p><pre>cudatsne OSError: libfaiss.so: cannot open shared object file: No such file or directory</pre><p>I went back to their installation page and found this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WuSjc19lw4gTXSKnfo9rOQ.png" /></figure><p>OK, it seems the problems is the package itself, I check the version of the cudatsne I installed by running pip list|grep cudatsneand found it was 0.1.1, and was <a href="https://pypi.org/project/tsnecuda/#history">released on Aug 1 2018</a>. But on the Github page it displays a version of 2.1.</p><p>Finally, I decided to install it <strong>offline</strong> using conda.</p><h3>Install cudatsne offline using conda</h3><p>I first went to the <a href="https://anaconda.org/CannyLab/tsnecuda/files">conda tsne repository </a>to download the latest release with CUDA 10.1, I downloaded it in my computer and install it offlie.</p><pre>conda install --offline tsnecuda-2.1.0-cuda101.tar.bz2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Z3BpuIe478G1IhEcC7NDyA.jpeg" /></figure><h3>Compare speed with sk-learn</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CyBWc_zv6csQUuX_oww1JA.png" /><figcaption>SK-LEARN</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YamYXMzpxkxPSmH97Krn-g.png" /><figcaption>CUDATSNE</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*5H-c61l7Dlhf2z6geOCxuA.png" /></figure><h3>Conclusions</h3><ol><li>CUDATSNE is indeed much faster</li><li>CUDATSNE can work on higher CUDA version (at least CUDA 11.2) though its latest version is for CUDA 10.1</li><li>pip is sometimes too old</li><li>Offline installation could be a solution when pulling from conda repo directly is too slow</li></ol><h3>Run it on Google Colab</h3><p>If you are also interested in running it on Colab, you can follow these two links.</p><p><a href="https://github.com/CannyLab/tsne-cuda/issues/59">https://github.com/CannyLab/tsne-cuda/issues/59</a></p><p><a href="https://colab.research.google.com/drive/11YFkI_XA9DUE2lEnmGu08LZB7dv5-mbb">https://colab.research.google.com/drive/11YFkI_XA9DUE2lEnmGu08LZB7dv5-mbb</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1355902eb90" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading: Learning from Simulated and Unsupervised Images through Adversarial]]></title>
            <link>https://fangdahan.medium.com/reading-learning-from-simulated-and-unsupervised-images-through-adversarial-2aefd95415d1?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/2aefd95415d1</guid>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Tue, 05 May 2020 16:33:11 GMT</pubDate>
            <atom:updated>2020-05-08T14:13:39.259Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://arxiv.org/abs/1612.07828">https://arxiv.org/abs/1612.07828</a></h3><h3>Motivation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*5yH_Zc6Z8CNU-f4G8EWNjw.png" /></figure><blockquote>Learn a model that improves the realism of synthetic images from a simulator using unlabeled real data, while preserving the annotation information.</blockquote><h3>Methodology</h3><ul><li>Input:</li></ul><ol><li><strong>Synthetic</strong> images from simulator (which looks unreal)</li><li>Unlabeled real images</li></ol><ul><li>Output: <strong>Refined</strong> images (which looks real)</li><li>Structure: a Refiner R that is an autoencoder, a Discriminator D to help distinguish real and fake</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*vbdBEVIsoRlb0IoZJzZC1w.png" /></figure><ul><li>Loss</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*Ft-UnTDptuvJuwday0e8jg.png" /><figcaption>Two loss terms</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*3HcNGTK56-MVZDcWFgLUbw.png" /><figcaption>l_{real} is GAN loss</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*f2Dkb_fBd6uNCg7muCx_cg.png" /><figcaption>l_{reg} is L1 loss in image/feature level. If in the feature level, it is similar idea as perceptual loss¹.</figcaption></figure><ul><li>Tricks</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*W66TDE9Lcf4511zqY4d2xw.png" /><figcaption>Local Adversarial Loss, also used in Pix2Pix² later</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*jjcPUZkXUOPSSuKcUNV5gw.png" /><figcaption>Experience Replay, also widely used in reinforcement rearning.</figcaption></figure><h3>Datasets</h3><ol><li>MPIIGaze dataset</li><li>NYU hand pose dataset of depth images</li></ol><h3>Evaluation Metrics</h3><ol><li>Precision on pretrained-classifier</li></ol><p>[1] <a href="https://arxiv.org/pdf/1603.08155.pdf">https://arxiv.org/pdf/1603.08155.pdf</a></p><p>[2] <a href="https://arxiv.org/abs/1611.07004">https://arxiv.org/abs/1611.07004</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2aefd95415d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Few extra details about Transformer]]></title>
            <link>https://fangdahan.medium.com/a-few-extra-details-about-transformer-5df25a4b6ff9?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/5df25a4b6ff9</guid>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Wed, 04 Mar 2020 18:14:30 GMT</pubDate>
            <atom:updated>2020-03-04T18:14:30.233Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I read <a href="http://jalammar.github.io/illustrated-transformer/">a really nice article</a> about Transformer in NLP. I want to write a summary and some details about the post as well as Transformer itself. A lot of pictures are directly from that post, definitely read the original post pleaes!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j8Unpp-6WxyINEghwD71nA.png" /><figcaption>The big picture of Transformer, the input goes through 6 (hyerparameter) encoders and 6 decoders to generate the output. (ref: <a href="http://jalammar.github.io/illustrated-transformer/">http://jalammar.github.io/illustrated-transformer/</a>)</figcaption></figure><h3>Input</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vg8bHpJpvlMv7bi4gJf10A.png" /></figure><h3>Encoder (Self-Attention + Feed Forward Network)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uXXs7nqMDQtAeZ0o.png" /><figcaption>Big picture of one Encoder</figcaption></figure><h4>Self-Attention</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*UJhDD2RG8i69IYU_.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/0*hR-P5k__EQL3h_VB" /><figcaption>Weights of Transformer that transform Embedding to Queries, Keys and Values</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/120/0*s8Am6Pekl6xYW_Um" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/0*nHgD_pFawaV4nKW6" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/0*4TzxWd8O3VsX5RgO.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/190/0*CYPLkpbNP5ytm4eR" /></figure><p>In reality, we can compute all z_i in parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/0*A_NYd6MPPKlg4Jh_.png" /></figure><p>Multi-Head Self-Attention</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/0*vy1G0DisXOntDpg_.png" /><figcaption>Repeat the above self-attention module eight times</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Md0KHdffoMxTnx2d.png" /><figcaption>Combine eight heads by a weight matrix</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*t2HjzkwNoiD0NsVZ.png" /><figcaption>The big picture of self-attention module</figcaption></figure><h4>Positional Encoding (the hard-to-understand part)</h4><p>First of all, the intuition is that Transformer does not encode position information. So we need a way to tell the model the position of each word in one sentence. This paper adds a positional encoding to the input:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*95tHdokVXC3D6fdu.png" /></figure><p>Unfortunately, <a href="http://jalammar.github.io/illustrated-transformer/">the excellent post</a> explains this part in a wrong way. I found <a href="https://kazemnejad.com/blog/transformer_architecture_positional_encoding/">another post</a> that tries to explain the positional encoding correctly with more details. The idea is each position takes a specific place in the frequency domain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*h1SFZLL8WhjuIHV2ws6m-A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*K3e6PFinguz4nnjF.png" /><figcaption>ref: <a href="https://kazemnejad.com/blog/transformer_architecture_positional_encoding/">https://kazemnejad.com/blog/transformer_architecture_positional_encoding/</a></figcaption></figure><p>The proposed method has two immediate advantages:</p><ol><li>Each position vector is different and follows a specific trend.</li><li>All values stay between [-1,1] and <strong>the summation within each vector is similar, </strong>this a very good property because the contribution of each word will not affect be the positional encoding.</li></ol><h4>Residual Connection</h4><p>After every self-attention, the input and the output is added and layer-normalized.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/0*8UFwj18z0zT5sFxu.png" /></figure><h3>Decoder</h3><p>The output of the final encoder is transformed into a set of attention vectors K,V.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vVO85iwTDwPe0CDjy2M6Pg.png" /></figure><p>This is another part that I want to add details, let’s look at <a href="http://nlp.seas.harvard.edu/2018/04/03/attention.html">the code for PyTorch</a>, that the attention of the decoder is a combination of input and output embeddings, <strong>the target words are embedded as well to compute the attention</strong>. This is one key difference compared with previous RNN models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3oCdbcO1oq9d8gaQj1CXqA.png" /><figcaption>DecoderLayer: <a href="http://nlp.seas.harvard.edu/2018/04/03/attention.html">http://nlp.seas.harvard.edu/2018/04/03/attention.html</a></figcaption></figure><h3>Loss</h3><p>The output of the decoder is mapped back to the vocabulary space to compute the loss</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zdk2EdfeOicwn6jRmZPV-A.png" /><figcaption>SimpleLossCompute: <a href="http://nlp.seas.harvard.edu/2018/04/03/attention.html">http://nlp.seas.harvard.edu/2018/04/03/attention.html</a></figcaption></figure><h3>Summary</h3><p>The encoder, decoder and the loss cover the main thing about Transformer, to summrize the thing in one figure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SKi3GJ7jJr7wJ9J_rfPVjQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5df25a4b6ff9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Class Activation Mapping]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://fangdahan.medium.com/class-activation-mapping-8c3fd5149e36?source=rss-4e88899ac2df------2"><img src="https://cdn-images-1.medium.com/max/880/1*kQD0TyWUKdfrL_HjLI17Mw.png" width="880"></a></p><p class="medium-feed-snippet">Reference</p><p class="medium-feed-link"><a href="https://fangdahan.medium.com/class-activation-mapping-8c3fd5149e36?source=rss-4e88899ac2df------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://fangdahan.medium.com/class-activation-mapping-8c3fd5149e36?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c3fd5149e36</guid>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Tue, 28 Jan 2020 23:04:26 GMT</pubDate>
            <atom:updated>2020-01-29T03:51:09.170Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[A Survey on Food Computing]]></title>
            <link>https://fangdahan.medium.com/a-survey-on-food-computing-93916d399497?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/93916d399497</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Wed, 16 Jan 2019 02:31:02 GMT</pubDate>
            <atom:updated>2019-01-16T02:31:02.072Z</atom:updated>
            <content:encoded><![CDATA[<h3>Task</h3><p>They try to summary a few things.</p><ol><li>What is food computing in general?</li><li>How to acquire food data?</li><li>What are the current methods in each of the sub domain in food computing?</li><li>What are the challenges we still have now in food computing?</li><li>What are the future directions?</li></ol><h3>Method</h3><h4>Food computing in general</h4><ul><li>food computing mainly consists of five basic tasks, from perception, recognition, retrieval, recommendation to prediction and monitoring.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*WVADXMty74Qyxj3TfhtCzQ.png" /></figure><h4>How to acquire food data?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqgAKMtO74nFiG4eMax2Fg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dSw-ow27E5KU33T8NeW4Tw.png" /></figure><h4>Current methods in each of the sub domain</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w_7saw5l0ld4JLlgADicUQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LligNBEt7jQ0VFfBGWKCfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*1v-MEGSpeDUUwjSoTQXdIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AEVwwQMbj8K0HV2eaafPsQ.png" /></figure><h4>Challenges</h4><ul><li>Food Image Recognition</li><li>Vision based Dietary Management System</li><li>Multiple-Network oriented food data fusion and mining</li><li>Food Computing for Food Science</li></ul><h4>Future directions</h4><ul><li>Large-scale Standard Food Dataset Construction</li><li>Large-scale Robust Food Recognition System</li><li>Joint Deep and Broad Learning for Food Computing</li><li>Food-oriented Multimodal Knowledge Graph Construction and Inference</li><li>Food Computing for Personal Health</li><li>Food Computing for Human Behavior Understanding</li><li>Food Log-oriented Food Computing</li><li>Other Promising Applications in the Vertical Industry</li></ul><h3>Experiment</h3><p>No experiment.</p><h3>Limitation</h3><p>No limitation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93916d399497" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Understanding of Cooking Procedure for Cross-modal Recipe Retrieval]]></title>
            <link>https://fangdahan.medium.com/deep-understanding-of-cooking-procedure-for-cross-modal-recipe-retrieval-c8238b4b508?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8238b4b508</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Wed, 16 Jan 2019 00:52:06 GMT</pubDate>
            <atom:updated>2019-04-03T15:47:28.690Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*i0vy4dChDwKWjpwfPFTYuw.png" /></figure><h3>Link</h3><p><a href="https://dl.acm.org/citation.cfm?id=3240627">https://dl.acm.org/citation.cfm?id=3240627</a></p><h3>Task</h3><p>Given the recipe (title+instructions+ingredients), they try to retrieve the image corresponding to this recipe in the dataset and vice versa.</p><h3>Method</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LQpz1rWkK75WffTAY0nbnA.png" /></figure><p>During training, they have <strong>both recipe and image</strong>, for each component in the recipe, they first encode it with Bi-GRU, then they learn a weight matrix W_w and bias b_w to map each word(sentence) feature to a vector, which is the attention vector u_t.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*0T1mtOtA20mhRDYlAIL7kQ.png" /></figure><p>Attention means the weights of each word. Taking the title for example, each word in the title has its own attention, so each word should have <strong>a scalar</strong> to represent the attention.</p><p>In order to get the attention of each word(sentence), they need to change u_t to a scalar. There are a few ways to do this:</p><ol><li>Let W_w directly outputs a scalar instead of a vector, in other words, u_t is a scalar to be used as the attention.</li><li><strong>Learn</strong> a shared vector u_w (so-called context vector) which has the same length as u_t , then calculate the attention by dot production, which is what they do in the paper.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*-uNQC-3esB7nyTlU8Sok8g.png" /></figure><p>For method 2, context vector u_w is learned, so they are really hoping the vector could generally become a good representation of all the words in all the titles. Actually, I could not understand this part well.</p><p>The rest are easy, a classic ResNet-50 to extract image feature, and using cosine similarity as the loss</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*jo4DrCsXii2b58lTEESy4g.png" /></figure><h3>Experiment</h3><p>In Table 3, they got SotA on the Recipe1M dataset, however, they did not provide stds in their paper which makes the result not quite trustworthy. Taking the MedR in im2recipe as an example, if the std is more than 0.5 for attention+SR in the 1K experiment, then the direct compare between 5.2 (JNE+SR.) and 4.6(attention+SR.) does not make too much sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DSh1urGs4PTnqxlQO42uTQ.png" /></figure><h3>Limitation</h3><ol><li>In Sec.4.6, they mentioned using raw recipe instead of the processed recipe. My confusion is whether they also change the ingredients at the same time.</li><li>In their paper, the image feature is fixed during training, if we could fine-tune the image part or build image attention, it may further improve the result.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8238b4b508" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Calculate mean Average Precision (mAP) for multi-label classification]]></title>
            <link>https://fangdahan.medium.com/calculate-mean-average-precision-map-for-multi-label-classification-b082679d31be?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/b082679d31be</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Fri, 16 Nov 2018 18:59:39 GMT</pubDate>
            <atom:updated>2019-05-04T02:02:28.669Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*Y26jgddsD0MXyUJh.jpg" /><figcaption><a href="http://www.gdg-llc.com/food-ingredients/">http://www.gdg-llc.com/food-ingredients/</a></figcaption></figure><p>Suppose we want to train a model to recognize ingredients in a food image, one effective way to evaluate the performance is mean Average Precision(mAP), another is <a href="https://en.wikipedia.org/wiki/Receiver_operating_characteristic">ROC curve</a>. I’m always confused about how mAP is calculated actually and here I’ll use an example to learn it (hopefully).</p><p>Assume we have a toy data set with two samples (e.g. food images) and the corresponding target and predicted scores. In my case, we would have four possible food ingredients [a,b,c,d]. Sample 1 has ingredients b, c, d. Sample 2 only has ingredients c.</p><p>After a few epochs of training, we get the predicted scores shown below, which is far from correct. Let’s start from here. We first introduce precision and recall, then move to average precision, finally talk about mAP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7a1tJnvrqAPCTWBB9MuhCw.png" /><figcaption>Tab. 1 The toy data</figcaption></figure><h3>Precision and Recall</h3><blockquote>The precision and recall is in terms of each class, <strong>NOT</strong> each sample!</blockquote><p>The numerators of precision and recall are the same, that is the number of true samples in the retrieved(positive) samples, we call it true positive (or TP)</p><p>The denominator of precision is the number of retrieved(positive) samples, we call it P, so <strong>P keeps increasing as we retrieve more samples</strong>.</p><p>The denominator of recall is the number of true samples in the dataset, we call it T, so <strong>T is fixed.</strong></p><ul><li>precision = TP/P</li><li>recall = TP/T</li></ul><p>In our toy example, we have 4 classes(ingredients), let’s do two practices.</p><h4>Practice 1: class a</h4><p>For the first class, we have the target and predictions shown in dark red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pBETfH8s--DoRy9Vy9JgTg.png" /></figure><ul><li>If we just retrieve one sample for class a, we will choose sample 3, now precision=1/1=1.00, recall=1/1=1.00</li><li>If we retrieve two samples for class a, we will choose sample 3, then sample 2, now precision=1/2=0.50, recall=1/1=1.00</li><li>If we retrieve three samples for class a, we will choose sample 3, then sample 2, then sample 1, now precision=1/3=0.33, recall=1/1=1.00</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f4t8IW5RsLSaWFstNSUaXg.png" /></figure><p>We immediately see that <strong>as we retrieve more samples, recall always increases(at least keeps the same), however precision may raises and falls</strong>. What we really want is to choose a threshold that both precision and recall are high. That’s why people develop F1 score as a metric to combine them together:</p><pre>F1 = 2 * (precision * recall) / (precision + recall)</pre><p><strong>However, F1 just evaluates the model’s performance at a specific threshold</strong>, people continue to develop metrics like ROC (not covered here) and mAP to evaluate the performance <em>over all possible thresholds</em>. Before touching mAP, let’s have one more practice to make sure we understand precision and recall.</p><h4>Practice 2: class b</h4><p>For the second class, we have the target and predictions shown in dark red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1vjSNY21vEwoe2ZRLZGc_g.png" /></figure><ul><li>If we just retrieve one sample for class b, we will choose sample 2, now precision=0/1=0.00, recall=0/2=0.00</li><li>If we retrieve two samples for class b, we will choose sample 2, then sample 1, now precision=1/2=0.50, recall=1/2=0.50</li><li>If we retrieve three samples for class b, we will choose sample 2, then sample 1, then sample 3, now precision=2/3=0.66, recall=2/2=1.00</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*84srVc2wsBbfLqqU18I0Qg.png" /></figure><p>Ha, for class b the precision always raises! Why? If we take a close look at the data we are using, we see that the true samples have smaller probabilities, this means we need large threshold in order to get these true samples, not a good sign…</p><h4>All classes</h4><p>Hopefully I have compute all these values correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ED8_ZCEQtW8HpsWnlFBm2Q.png" /></figure><h3>Average Precision (AP)</h3><p>Now we want to know the performance of each class.</p><p>We need to take both precision and recall into account, one simple way is to just average the precisions of all possible recalls.</p><ul><li>class a, the possible recalls are T=1 (see <a href="https://medium.com/@jonathan_hui/map-mean-average-precision-for-object-detection-45c121a31173">here</a> for why we ignore T=2 and T=3)</li><li>class b, the possible recalls are T=2, T=3</li><li>…</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h41_FMICZkzqqnGBiDYyiA.png" /></figure><p>We see that class a and class d are better than class c, the worst class is class b, the conclusion makes sense if we check Tab .1 again.</p><p>In real world, <strong>for different classes, we choose different thresholds</strong>, <em>e.g.</em> for class a and class d T=1, is pretty good; for class b and class c, it depends on whether we take more care for precision or recall, if we treat them equally, then F1 would be a good metric to help us choose the threshold, in this case, we want both of them to use T=3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F-8eO6xAVRe4yfPFb2b8fw.png" /></figure><h3>mean Average Precision (mAP)</h3><p>Now we want to know the performance of the model over all classes.</p><p>mAP just moves one step further to average the APs over all classes. It measure how good the model is for the entire data set. In our case, mAP=0.81, not bad!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZgpsLBFPdmasFGCwvBLF4Q.png" /></figure><h3>References</h3><ul><li><a href="https://github.com/pytorch/tnt/blob/master/torchnet/meter/apmeter.py">https://github.com/pytorch/tnt/blob/master/torchnet/meter/apmeter.py</a></li><li><a href="https://medium.com/@jonathan_hui/map-mean-average-precision-for-object-detection-45c121a31173">https://medium.com/@jonathan_hui/map-mean-average-precision-for-object-detection-45c121a31173</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b082679d31be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[None-local Neural Networks]]></title>
            <link>https://fangdahan.medium.com/none-local-neural-networks-4ed463873771?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ed463873771</guid>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Thu, 11 Oct 2018 18:13:58 GMT</pubDate>
            <atom:updated>2019-05-04T15:54:02.743Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://arxiv.org/abs/1711.07971">Non-local Neural Networks</a></p><h3>Task</h3><p>Video classification and image classification</p><h3>Model</h3><p>Non-local operation computes the response at a position as a weighted sum of the features at all positions (spatial and temporal).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*PZljkvgm-ApYVLZMA6cyFg.png" /></figure><h4>None-local Block</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*Yo6diUph9fJjWFS1WKxLdg.png" /></figure><h3>Experiments</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-3GenkOraGhtx4Utknsn8Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ed463873771" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PyTorch Style Transfer Tutorial]]></title>
            <link>https://fangdahan.medium.com/pytorch-style-transfer-tutorial-91e58d6b1055?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/91e58d6b1055</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Wed, 22 Aug 2018 02:08:47 GMT</pubDate>
            <atom:updated>2018-08-22T02:14:30.647Z</atom:updated>
            <content:encoded><![CDATA[<p>In 2015, <a href="https://arxiv.org/abs/1508.06576">Leon A. Gatys et al.</a> published a paper using deep neural networks to transfer the style of one image to another. The beauty of this paper is to use DNN to extract content and style of pictures, which produces good results to simulate the masterpieces of previous artists.</p><p>I have read this paper long time ago, but I still wondered how the perform gradient descent on input image, luckily PyTorch provides a good <a href="https://pytorch.org/tutorials/advanced/neural_style_tutorial.html">tutorial</a> on how to do this. In this post I try to extract the core part that relates to the input image updating.</p><h3>Why `detach` target from graph</h3><pre>class ContentLoss(nn.Module):</pre><pre>  <br>  def __init__(self, target):</pre><pre>    super(ContentLoss, self).__init__()</pre><pre>    # we &#39;detach&#39; the target content from the tree used</pre><pre>    # to dynamically compute the gradient: this is a stated value,</pre><pre>    # not a variable. Otherwise the forward method of the criterion</pre><pre>    # will throw an error.</pre><pre>    self.target = target.detach()</pre><pre>  def forward(self, input):</pre><pre>    self.loss = F.mse_loss(input, self.target)</pre><pre>    return input<br>...</pre><pre>target = model(img)</pre><pre>content_loss = ContentLoss(target)</pre><pre>model.add_module(&quot;content_loss&quot;, content_loss)</pre><p>This is because later we just want to back propagate through the graph with input image, not the target image <strong>img</strong>. Otherwise PyTorch will try to back propagate the graph twice, however the classical error will appear:</p><pre>RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.</pre><h3>Why normalization?</h3><pre>cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)</pre><pre>cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)</pre><pre># create a module to normalize input image so we can easily put it in a nn.Sequential</pre><pre>class Normalization(nn.Module):</pre><pre>  def __init__(self, mean, std):</pre><pre>    super(Normalization, self).__init__()</pre><pre>    self.mean = torch.tensor(mean).view(-1, 1, 1)</pre><pre>    self.std = torch.tensor(std).view(-1, 1, 1)</pre><pre>  def forward(self, img):</pre><pre>    # normalize img</pre><pre>    return (img - self.mean) / self.std</pre><pre>normalization = Normalization(cnn_normalization_mean, cnn_normalization_std).to(device)</pre><pre>model = nn.Sequential(normalization)</pre><p>This is because the default VGG network in PyTorch will normalize the image with specific means and variances, which are shown above.</p><h3>How to update the INPUT IMAGE?</h3><pre>input_img = torch.randn(img.data.size(), device=device)</pre><pre>def get_input_optimizer(input_img):</pre><pre>  # this line to show that input is a parameter that requires a gradient</pre><pre>  optimizer = optim.LBFGS([input_img.requires_grad_()])</pre><pre>  return optimizer</pre><pre>optimizer = get_input_optimizer(input_img)</pre><ol><li>Make the input_img requires gradient computation by input_img.requires_grad_().</li><li>Set up the optimizer to optimize the input_img.</li></ol><h3>The result</h3><p>Here I just reconstruct the content of a specific layer. The result is consistent with the original paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vxXFZRiNugagbMA_LRcDWg.png" /><figcaption>Input image</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3aS4hwTJ8zD5M-cMClt7WQ.png" /><figcaption>Reconstruction of conv2 in VGG19</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ci1Ep-Bt2-3Hb0bsTc_2HQ.png" /><figcaption>Reconstruction of conv5 in VGG19</figcaption></figure><p>I think these are all the tricks that I did not understand when I read the tutorials, hope this post could help you better understand the tutorial.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91e58d6b1055" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GANimation: Anatomically-aware Facial Animation from a Single Image]]></title>
            <link>https://fangdahan.medium.com/ganimation-anatomically-aware-facial-animation-from-a-single-image-5b49160061c8?source=rss-4e88899ac2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b49160061c8</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Fangda Han]]></dc:creator>
            <pubDate>Tue, 31 Jul 2018 01:13:04 GMT</pubDate>
            <atom:updated>2018-07-31T01:13:04.304Z</atom:updated>
            <content:encoded><![CDATA[<h3>Task</h3><p>Continuous changing of AUs using unsupervised GAN.</p><p>Formally, it learns a model</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*PmhqDkLtx-j7QD5srhU4aA.png" /></figure><p>Given</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*uUQ883dgG6OK1zWteke6IA.png" /></figure><h3>Related Works</h3><p>DIAT, CycleGAN, IcGAN, StarGAN</p><h3>Method</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*crssruUaAihdnWuDD9NFHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eYcznq9UJwrnqZJIkYuQZg.png" /></figure><h4>Losses (four terms)</h4><ul><li>traditional GAN loss: try to make the generated image as real as possible</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DbQkc795o2fHcA8x9yvf-A.png" /></figure><ul><li>Attention loss: keep the mask smooth (TV) and force the mask to use the generated color map (L2)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*psSv_NDbR-CHBNAOZVDo9A.png" /></figure><ul><li>Conditional Expression Loss: training a separate AU regressor</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*XxDnNVWjO0MNgjPiGTt6YA.png" /></figure><ul><li>Identity Loss: nothing but a cycle loss</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*9NKG61nTSX3F9JVNw5bcMg.png" /></figure><h3>Results</h3><p>Dataset is EmotioNet dataset, result is amazing. The model could generate continuous AU changes, which is what I currently working on, SAD…</p><p>To tell the truth, this paper does not introduce really new stuff, WGAN-GP, TV loss, extra classifier/regressor, identity (cycle) loss, these are all techniques that have been used before, the good part is they combine them into a one single model and get outstanding performance! Excellent job!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/95/1*lItUY94ch524lBYAMJtzIQ.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MhvQ9o25yaNU7tVJHdEnVA.png" /></figure><h3>Reference</h3><p>[1] <a href="http://www.albertpumarola.com/research/GANimation/index.html">http://www.albertpumarola.com/research/GANimation/index.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b49160061c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>