<?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 Roger Stark on Medium]]></title>
        <description><![CDATA[Stories by Roger Stark on Medium]]></description>
        <link>https://medium.com/@matriXanger?source=rss-2be06929ef78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*evurObU2I15_cpCuk3ZHGg.jpeg</url>
            <title>Stories by Roger Stark on Medium</title>
            <link>https://medium.com/@matriXanger?source=rss-2be06929ef78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 09:04:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matriXanger/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[Neural Style Transfer with Owl]]></title>
            <link>https://medium.com/@matriXanger/neural-style-transfer-with-owl-221813991381?source=rss-2be06929ef78------2</link>
            <guid isPermaLink="false">https://medium.com/p/221813991381</guid>
            <category><![CDATA[ocaml]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[owls]]></category>
            <category><![CDATA[art]]></category>
            <category><![CDATA[neural-networks]]></category>
            <dc:creator><![CDATA[Roger Stark]]></dc:creator>
            <pubDate>Fri, 26 Jan 2018 01:59:38 GMT</pubDate>
            <atom:updated>2018-02-20T12:42:44.801Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xap0Mey0k_MnaSSbkRavbQ.jpeg" /><figcaption>“Mona Lisa” with different art styles (source: <a href="http://genekogan.com/works/style-transfer/">http://genekogan.com/works/style-transfer/</a>)</figcaption></figure><h3>What is it?</h3><p>What is Neural Style Transfer (NST)? It is a pretty cool application of Deep Neural Networks (DNN), “the process of using DNN to migrate the semantic content of one image to different styles”.</p><p>Well it may sounds a little bit scary, but the idea is very simple, as the title image shows, this application takes two images A and B as input. Let’s say A is “Mona Lisa” of Da Vinci, and B is “The Starry Night” of Vincent van Gogh.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*Uaw1OPcSmS0PtrRqubsZJA.jpeg" /><figcaption>Mona Lisa</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ny5bxDjj6NDrUsxvfvRHYA.jpeg" /><figcaption>sStarry Night</figcaption></figure><p>We then specify A as the <em>content image</em> and B as the <em>style image</em>, then what a NST application can produce? Boom! A new Mona Lisa, but with the style of Van Gogh (see the middle of title image)! If you want another style, just replace image B and run the application again. Impressionism, abstractionism, classical art, you name it. Isn’t it amazing?</p><h3>How is it done?</h3><p>Without going into details, I will briefly introduce the math behind NST, so please feel free to ignore this no-so-interesting part.</p><p>The NST can be seen as an optimisation problem: given a content image c and a style image s , the target is to get a output image x so that it minimises:</p><pre>f(x) = content_distance(x, c) + style_distance(x, s)</pre><p>This equation can be easily translated as: I want to get such an image that its content is close to c , but its style similar to s .</p><p>DNNs, especially the ones that are used for computer vision tasks, are found to be an convenient tool to capture the content and style characteristics of an image. Then the euclidean distance of these characteristics are used to express the content_distance() and styel_distance() functions.</p><p>Finally, the optimisation techniques such as gradient descent are applied to f(x) to get a good enough x .</p><h3>So you want to do it… with Owl?</h3><p>Again, yes! I’ve implement an NST application with <a href="https://github.com/ryanrhymes/owl">Owl</a> (a powerful numerical computing library on OCaml; please refer to my <a href="https://medium.com/@matriXanger/image-recognition-with-owl-a5a6d0caef33">previous posts</a> if you are not familiar with it). All the code (about 180 lines) is included in this <a href="https://gist.github.com/jzstark/6f28d54e69d1a19c1819f52c5b16c1a1">Gist</a>. This application uses the <a href="https://arxiv.org/abs/1409.1556">VGG19</a> DNN, so the pre-trained network file is also included.</p><p>This application provides a simple interfaces to use. Here is an example showing how to use it with two lines of code:</p><pre>#zoo &quot;6f28d54e69d1a19c1819f52c5b16c1a1&quot;</pre><pre>Neural_transfer.run ~ckpt:50 ~src:&quot;path/to/content_img.jpg&quot; ~style:&quot;path/to/style_img.jpg&quot; ~dst:&quot;path/to/output_img.png&quot; 250.;;</pre><p>The first line download gist files and imported this gist as an OCaml module, and the second line uses the run function to produce an output image to your designated path. It’s syntax is quite straightforward, and you may only need to note the final parameter. It specifies how many iterations the optimisation algorithm runs. Normally 100 ~ 500 iterations is good enough.</p><p>This module also supports saving the intermediate images to the same directory as output image every N iterations (e.g. path/to/output_img_N.png). N is specified by the ckpt parameter, and its default value is 50 iterations. If users are already happy with the intermediate results, they can terminate the program without waiting for the final output image.</p><p>That’s all it takes! If you don’t have suitable input images, don’t worry, the gist already contains exemplar content and style images to get you started. I have to say I had a lot lot of fun playing with it — please allow me to introduce you one of my work using the exemplar images:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gQCCZ3_ic0MKVILE3Emv1w.png" /><figcaption>Starry… street view?</figcaption></figure><p>Here is a presentation of how the content image change gradually in style:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/224/1*nDpVciKsZezI6lEl_Eb7zQ.gif" /></figure><p>Now use the code, and most importantly, your imagination, to create your own art!</p><h3>Limit</h3><p>Alas, we still cannot claim that this is the best NST application out there in the Deep Learning market. There are some limit to this app for now. For one thing, it relies on the tool ImageMagick to manipulate image format conversion and resizing. Please make sure it is installed. For example, on Ubuntu you can run:</p><pre>sudo apt-get install imagemagick</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=221813991381" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inception as a Service]]></title>
            <link>https://medium.com/@matriXanger/inception-as-a-service-54d3afa1fed7?source=rss-2be06929ef78------2</link>
            <guid isPermaLink="false">https://medium.com/p/54d3afa1fed7</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[owls]]></category>
            <category><![CDATA[ocaml]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Roger Stark]]></dc:creator>
            <pubDate>Mon, 20 Nov 2017 00:03:59 GMT</pubDate>
            <atom:updated>2017-11-20T16:42:13.628Z</atom:updated>
            <content:encoded><![CDATA[<p>OK, I admit the title is a little bit cliche, but following the <a href="https://medium.com/@matriXanger/image-recognition-with-owl-a5a6d0caef33">previous blog</a> about image recognition with Owl, I would like to briefly introduce the API that the InceptionV3 service provides to programmers.</p><h3>Prerequisite</h3><p>Please refer to the <a href="https://medium.com/@matriXanger/image-recognition-with-owl-a5a6d0caef33">previous blog</a> for installation of OCaml, Owl, etc. If you have not tried Owl yet, I recommend using the <a href="https://hub.docker.com/r/ryanrhymes/owl/">Docker image</a> of Owl. As a prerequisite, please make sure that the tool ImageMagick is installed:</p><pre>sudo apt-get install imagemagick</pre><p>Besides, prepare one image on your computer. It can be of any common image format (jpg, png, gif, etc.) and size. If you’re not sure which image to use, here is one choice (the panda image from previous blog):</p><pre>wget -O panda.png <a href="https://goo.gl/dnyjh7">https://goo.gl/dnyjh7</a></pre><h3>API</h3><p><strong>infer</strong>: Service that performs image recognition tasks over client images. It accept a string that specify the location of a local image. Its return value is a 1x1000 N-dimension array, each element is a float number between 0 and 1, indicating the possibility that the image belongs to one of the <a href="http://image-net.org/challenges/LSVRC/2014/browse-synsets">1000 classes</a> from ImageNet.</p><p><strong>to_json</strong>: Convert the inferred result to a raw JSON string.</p><p>Parameter:</p><ul><li><em>top</em>: an int value to specify the top-N likeliest labels to return. Default value is 5.</li></ul><p><strong>to_tuples</strong>: Convert the inferred result to an array of tuples, each tuple contains label name (“class”, string) and the probability (“prop”, float, between 0 and 1) of target image being in that class.</p><p>Parameter:</p><ul><li><em>top</em>: an int value to specify the top-N likeliest labels to return. Default value is 5.</li></ul><h3>Example</h3><p>Here is a simple example to use this API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5da5b5d9e93155f77213112061cc4873/href">https://medium.com/media/5da5b5d9e93155f77213112061cc4873/href</a></iframe><p>The code is simple: 1) deploy InceptionV3 service to your machine with Zoo, 2) choose an image on your machine, and 3) image recognition.</p><p>To satisfy different user requirements, the service supports 3 kinds of returned values. By default, the returned value is a 1x1000 Ndarray in Owl:</p><pre>val labels : Owl_algodiff.S.arr =<br>  <br>            C0          C1               C998        C999 <br>R0 2.72124E-05 2.91834E-05  ...   3.38798E-05  5.04353E-05</pre><p>By using to_json , you can get the top-N inference results in a raw JSON string:</p><pre>[<br>  {<br>    &quot;class&quot;: &quot;giant panda,<br>     panda,<br>     panda bear,<br>     coon bear,<br>     Ailuropoda melanoleuca&quot;,<br>    &quot;prop&quot;:  0.961965441704<br>  },<br>  {<br>    &quot;class&quot;: &quot;lesser panda,<br>     red panda,<br>     panda,<br>     bear cat,<br>     cat bear,<br>     Ailurus fulgens&quot;,<br>    &quot;prop&quot;:  0.00117377145216<br>  },<br>  {<br>    &quot;class&quot;: &quot;space shuttle&quot;,<br>    &quot;prop&quot;:  0.000592212367337<br>  },<br>  {<br>    &quot;class&quot;: &quot;soccer ball&quot;,<br>    &quot;prop&quot;:  0.000403530168114<br>  },<br>  {<br>    &quot;class&quot;: &quot;indri,<br>     indris,<br>     Indri indri,<br>     Indri brevicaudatus&quot;,<br>    &quot;prop&quot;:  0.000263019668637<br>  } <br>]</pre><p>You can also get an array of tuple using to_tuples :</p><pre>val labels : (string * float) array =<br>[|<br>  (&quot;giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca&quot;, 0.961965441703796387);<br>  (&quot;lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens&quot;, 0.0011737714521586895);<br>  (&quot;space shuttle&quot;, 0.000592212367337197065);<br>  (&quot;soccer ball&quot;, 0.000403530168114230037);<br>  (&quot;indri, indris, Indri indri, Indri brevicaudatus&quot;, 0.000263019668636843562)<br>|]</pre><h3>Compare with Google API</h3><p>With this API, a user can write code that’s quite similar to that using Google Vision API:</p><pre>import io<br>import os<br><br># Imports the Google Cloud client library<br>from google.cloud import vision<br>from google.cloud.vision import types<br><br># Instantiates a client<br>client = vision.ImageAnnotatorClient()<br><br># The name of the image file to annotate<br>file_name = os.path.join(<br>    os.path.dirname(__file__),<br>    &#39;resources/panda.jpg&#39;)<br><br># Loads the image into memory<br>with io.open(file_name, &#39;rb&#39;) as image_file:<br>    content = image_file.read()<br><br>image = types.Image(content=content)<br><br># Performs label detection on the image file<br>response = client.label_detection(image=image)<br>labels = response.label_annotations<br><br>print(&#39;Labels:&#39;)<br>for label in labels:<br>    print(label.description)</pre><p>Of course, there are still a lot to be done to enhance the current API, such as support for more features and RESTful request/response (which will soon be introduced in further posts). Current service also relies on ImageMagick to convert the provided image to <a href="http://netpbm.sourceforge.net/doc/ppm.html">ppm format</a>, which will be stored in the same directory as the source image — it may not be an ideal solution in some cases. But I hope this example has already shown some of the design principles we keep in mind: expressiveness and usability for users.</p><p>The code of the InceptionV3 service is on the <a href="https://gist.github.com/jzstark/9428a62a31dbea75511882ab8218076f">Gist</a>. And <a href="http://138.68.155.178/">here</a> is a web-based demo of this image classification service powered by Owl. Please feel free to check them out!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54d3afa1fed7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Image Recognition with Owl]]></title>
            <link>https://medium.com/@matriXanger/image-recognition-with-owl-a5a6d0caef33?source=rss-2be06929ef78------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5a6d0caef33</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ocaml]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Roger Stark]]></dc:creator>
            <pubDate>Thu, 26 Oct 2017 18:02:50 GMT</pubDate>
            <atom:updated>2018-08-09T18:48:12.230Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PEyER6p6I8imMq81nkBFPA.png" /><figcaption>Network Architecture of InceptionV3</figcaption></figure><h3>Image Recognition</h3><p>How can a computer take an image and answer questions like “what is in this picture? A cat, dog, or something else?”</p><p>In the last few years the field of machine learning has made tremendous progress on addressing this difficult problem. In particular, Deep Neural Network (DNN) can achieve reasonable performance on visual recognition tasks — matching or exceeding human performance in some domains.</p><p><a href="https://arxiv.org/abs/1512.00567">InceptionV3</a> is one of Google’s latest effort to do image recognition. It is trained for the ImageNet Large Visual Recognition Challenge using the data from 2012. This is a standard task in computer vision, where models try to classify entire images into 1000 classes, like “Zebra”, “Dalmatian”, and “Dishwasher”. Compared with previous DNN models, InceptionV3 has one of the most complex networks architectures in computer vision.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*DUrloABX_r0juK86gYlOHQ.jpeg" /></figure><h3>So you want to do it… with OCaml?</h3><p>There exist many good deep learning frameworks that can be used to do image classification, such as TensorFlow, Caffe, Torch, etc. But what if your choice of language is Functional Programming Language such as OCaml? It has long been thought that OCaml is not suitable for advanced computation tasks like machine learning. And now we have <a href="https://github.com/ryanrhymes/owl">Owl</a>.</p><blockquote>Owl is an emerging numerical library for scientific computing and engineering. The library is developed in the OCaml language and inherits all its powerful features such as static type checking, powerful module system, and superior runtime efficiency. Owl allows you to write succinct type-safe numerical applications in functional language without sacrificing performance, significantly reduces the cost from prototype to production use.</blockquote><p>Owl provides fully functioning Neural network module for deep learning applications. Compared to existing deep learning platforms, Owl utilise some ideal properties of OCaml language: fast, and strong static typing with type inference. Moreover, as you’ll see soon, Owl code has great expressiveness. You can construct a running image classification application with short and elegant code.</p><h3>Preparation: Install Owl</h3><p>First, you need to <a href="https://ocaml.org/docs/install.html">install OCaml</a>. The most convenient way to do it is from your system package manager. However, Owl requires versions &gt;= 4.04.0 , which may not yet supported by your package manager. In that case, please try to compile the source. After installing OCaml, you can then <a href="https://opam.ocaml.org/doc/Install.html">install OPAM</a>, the package manager for OCaml. Again, the recommended way is to install from source. For your reference, here is a <a href="https://github.com/jzstark/owl-docker/blob/master/Dockerfile_ocaml">Dockerfile</a> for these two steps.</p><p>Installing Owl is easy. You can build from source, download with opam , or try all the up-to-date features with Docker. Please see the <a href="https://github.com/ryanrhymes/owl#installation">installation guide</a> for details. Note that Owl on opamlags behind the master branch and misses many new features, so I do not guarantee code from this article can run smoothly on it.</p><p>Before compiling Owl from source, you should install some external libraries and OCaml packages. Please note that one of these packages, eigen , also lags behind the master branch if installed from opam . Please install this package from the source if you can.</p><p>Of course, the most convenient methods to experiment with Owl is to use Docker. All you need to do is to pull the <a href="https://hub.docker.com/r/ryanrhymes/owl/">image</a>, start a container, and then play with it in utop !</p><h3>Let it roll!</h3><p>Enough of these boring installation steps. Forget any hello-world code. Let’s do the image classification, right here, right now!</p><pre>owl -run 6dfed11c521fb2cd286f2519fb88d3bf</pre><p>That’s it. This one-liner is all you need to do to see a image classification example in action. Here is the output:</p><pre>Top 5 Predictions:<br>Prediction #0 (96.20%) : giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca<br>Prediction #1 (0.12%) : lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens<br>Prediction #2 (0.06%) : space shuttle<br>Prediction #3 (0.04%) : soccer ball<br>Prediction #4 (0.03%) : indri, indris, Indri indri, Indri brevicaudatus</pre><p>That is the classification result of this image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*RTlnc94NC1EGKiK9mXCPZg.png" /></figure><p>The code and images used in this example are included in <a href="https://gist.github.com/jzstark/6dfed11c521fb2cd286f2519fb88d3bf">this Gist</a>. Let’s check this simple example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ed29a620283fe0479c5e51c854971c6/href">https://medium.com/media/7ed29a620283fe0479c5e51c854971c6/href</a></iframe><p>You need 5 steps to do image classification with InceptionV3:</p><ol><li>Import external code/libraries using Zoo in Owl. #Zoo “gist-id” enables you to use code modules defined in other Gists. Here we want to use <a href="https://gist.github.com/jzstark/9428a62a31dbea75511882ab8218076f">InceptionV3</a> modules. It defines the InceptionV3 network architecture and loads weights of the network. The downloaded code are cached in $HOME/.owl/zoo directory.</li><li>Load InceptionV3 model with one line of code</li><li>Designate an absolute path of your input image. Here we use the extend_zoo_path util function to automatically find the “panda.png” image contained in the Gist itself.</li><li>Run inference with the neural network model and the input image, and then decode the result, getting top-N (N defaults to 5) predictions in human-readable format. The output is an array of tuple, each tuple consists of a string for classified type description, and a float number ranging from 0 to 100 to represent the percentage probability of the input image actually being this type.</li></ol><p>5. If you want, you can pretty-print the result on your screen.</p><p>Of course, if you don’t want to use the owl command, you can always copy the example code from this gist and run it in your preferred way, such as utop .</p><h3>Online Demo</h3><p>If you are not interested in installing anything — No problem! <a href="http://138.68.155.178/">Here</a> is a web-based demo of this image classification application powered by Owl. Please feel free to play with it! And the server won’t store your image. Actually, if you are so keen to protect your personal data privacy — which I cannot agree with you more — then you definitely should try to pull the code here and fast build a local image processing service without worrying your images being seen by anybody else!</p><h3>Want to Know More?</h3><p>We do have more! I suggest you to read the code that constructing the whole InceptionV3 network from <a href="https://gist.github.com/jzstark/9428a62a31dbea75511882ab8218076f">this gist</a>. Even if you are not quite familiar with Owl or OCaml, it must still be quite surprising to see the network that contains 313 neuron nodes can be constructed using only ~150 lines of code. And we are talking about one of the most complex neural networks for computer vision. As to other smaller tasks, such as the most common h<a href="http://yann.lecun.com/exdb/mnist/">and-written digits recognition</a> task, you can construct a good deep neural network model with only 9 lines of code! Please check the Owl source code to get more <a href="https://github.com/ryanrhymes/owl/blob/master/examples/mnist_cnn.ml">examples</a> if you are interested.</p><p>The best starting point to learn to use Owl is from the <a href="https://github.com/ryanrhymes/owl#learning">tutorials</a>, with utop at hand, to learning by doing.</p><p><strong>Happy hunting!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5a6d0caef33" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>