<?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 Alexander Knyazev on Medium]]></title>
        <description><![CDATA[Stories by Alexander Knyazev on Medium]]></description>
        <link>https://medium.com/@alexkn15?source=rss-d23dc24c6a5b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DUhQywqeZ__JA7Bi.</url>
            <title>Stories by Alexander Knyazev on Medium</title>
            <link>https://medium.com/@alexkn15?source=rss-d23dc24c6a5b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:55:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexkn15/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[Tensorflow and JavaCV (OpenCV)]]></title>
            <link>https://medium.com/@alexkn15/tensorflow-and-javacv-591c1b9443a3?source=rss-d23dc24c6a5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/591c1b9443a3</guid>
            <category><![CDATA[javacv]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[opencv]]></category>
            <dc:creator><![CDATA[Alexander Knyazev]]></dc:creator>
            <pubDate>Tue, 27 Feb 2018 16:26:55 GMT</pubDate>
            <atom:updated>2018-04-03T18:03:22.960Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*Oa_0launoc-w3GmHyVSZMQ.png" /></figure><p>Some time ago I decided to transfer my Tensorflow model to Java. I had to do some heavy operations after using a neural net, and Java is faster than Python in many ways. Also, I have more than 10 years of experience in Java and I’m more familiar with it than with Python.</p><p>The reason why I decided to write this article is a lack of good examples on how to use Tensorflow in Java, and I want to share my experience. Using Tensorflow is easy in Python, you just pass a Numpy array of the required shape to the input layer of a Tensorflow model and get the result. In Java it is almost the same, but you don’t know about that yet. Usually you search for an example on how to feed a picture to a Tensorflow model and find <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java"><strong>this one</strong></a>, and after looking at it your main thought is “WTF is going on here? Why can’t I just use some library to resize an image and pass it to a Tensorflow model as a multidimensional array, as I do it in Python?”. Actually, you can!</p><p>If you’ve read my <a href="https://medium.com/@alexkn15/tensorflow-save-model-for-use-in-java-or-c-ab351a708ee4"><strong>previous article</strong></a>, you already know how to feed data to a Tensorflow model in Java. There is a little left to do, just convert a picture to a multidimensional array. But for those who didn’t or don’t want to read it, I’ll explain briefly how to do this.</p><h4>Feed data to a Model</h4><p>In Python it is as simple as this:</p><pre>sess.run(Z, feed_dict={X: <strong>[4, 3, 2, 1]</strong>})</pre><p>In Java you have to create a Tensor object from your data before passing it to a Tensorflow Session. It’s done like this:</p><pre>float[] inputData = {4, 3, 2, 1};<br>Tensor inputTensor = Tensor.<em>create</em>(inputData, Float.class);</pre><p>Session runs a bit differently than in Python. In Java it has a special DSL for running a model.</p><pre>Tensor result = sess.runner()<br>        .feed(<strong>&quot;input&quot;</strong>, inputTensor)<br>        .fetch(<strong>&quot;output&quot;</strong>).run().get(0);</pre><p>Then you can create an array buffer of desired output shape and use result.copyTo(myBuffer) method to acquire the data from a Tensor.</p><h4>Let’s go to a concrete example with JavaCV and Tensorflow!</h4><p>We will use <strong>Inception5h </strong>model from example I mentioned above. It accepts 4-dimensional float array [?][224][224][3] as an input, and outputs a 2-dimensional float array [?][1008]. It can classify 1008 objects as you might guess.</p><p>First, you have to add these dependencies to your Gradle or Maven project</p><pre>dependencies {<br>    compile(&#39;org.tensorflow:tensorflow:1.5.0&#39;)<br>    compile(&#39;org.bytedeco:javacv-platform:1.4&#39;)<br>}</pre><h4>Done? Let’s go further!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*QvfShVmR6xEUPt3GnCCm7g.png" /><figcaption>This is my project structure</figcaption></figure><p>I wrote two helper classes. <strong>ImageProcessor </strong>reads and normalizes images and returns them as a 4-dimensional array to feed them to the model. <strong>Classificator </strong>accepts image data as 4-dimensional float array and predicts what is on the pictures.</p><p>Here is an example on how to use them</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e77d7dd3a721c2a898db5e8115f3507/href">https://medium.com/media/4e77d7dd3a721c2a898db5e8115f3507/href</a></iframe><p><strong>ImageProcessor </strong>returns float[?][224][224][3].<br>The first dimension of the array is a batch for a neural network. You may pass multiple image paths to <strong>ImageProcessor</strong>. Then you just pass returned array from <strong>ImageProcessor </strong>to <strong>Classificator </strong>and, in its turn, it returns a list of predicted labels. I’ll explain how each class works, starting from <strong>ImageProcessor</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2746edaab64e28e7b2d99abf03c3af13/href">https://medium.com/media/2746edaab64e28e7b2d99abf03c3af13/href</a></iframe><p>As you see, it is very simple. <strong>ImageProcessor </strong>has two important methods:</p><p><strong>loadAndNormalizeImage</strong> is the only public method that is used outside the class. It accepts multiple image paths, loads each image to <strong>IplImage </strong>object, resizes it using <strong>cvResize </strong>and passes resulting <strong>IplImage </strong>to <strong>getRGBArray. </strong>Of course, you can use other powerful <strong>JavaCV </strong>features like <strong>Face Detection</strong> etc. For simplicity, I just resize images to the appropriate size that this model accepts.</p><p><strong>getRGBArray </strong>is quite straightforward, it just goes through each pixel and fills a 3-dimensional image array with RGB values.</p><h4><strong>Let’s move to Classificator class.</strong></h4><p>I’ll explain it part by part, starting with the constructor</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b193a96f599a287736345916fda8cd4/href">https://medium.com/media/1b193a96f599a287736345916fda8cd4/href</a></iframe><p>First, I get the path to the <strong>Tensorflow model</strong> and the path to its labels. Then I import model data to a <strong>Graph </strong>object and create a <strong>Session </strong>object with it. <strong>“labels” </strong>is just a list with 1008 elements, it is used to print predicted values.</p><h4>Now let’s see the methods</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d9ab5dd17a079a5708779dcf4fcea96/href">https://medium.com/media/5d9ab5dd17a079a5708779dcf4fcea96/href</a></iframe><p>As I explained earlier, Tensorflow in Java cannot work with raw arrays, they must be covered with Tensor objects.</p><p><strong>classify </strong>is the only public method, it accepts a 4-dimensional float array of image data and covers it with <strong>Tensor </strong>object. Then it calls <strong>predict </strong>method and uses an acquired result to find the predicted labels.</p><p>In <strong>predict </strong>method I use <strong>session.runner()</strong> to feed an image tensor to the network.</p><pre>Tensor result = sess.runner()<br>        .feed(<strong>&quot;input&quot;</strong>, imageTensor)<br>        .fetch(<strong>&quot;output&quot;</strong>).run().get(0);</pre><p><strong>.run()</strong> method returns a <strong>List</strong> of operations, and no matter if you pass an index to an operation like “output<strong>:0</strong>”, it will always return a <strong>List</strong>, but always with one element in this case. So we just call <strong>.get(0)</strong> to get our result.</p><p><strong>result.shape()[0] </strong>returns the size of the first dimension of an array, which happens to be a batch size. Then I create a buffer of required shape.</p><pre>float[][] prediction = new float[batchSize][1008];</pre><p><strong>Tensor.copyTo(Object obj)</strong> fills a buffer array with data.</p><p>In <strong>findPredictedLabel </strong>method I find maximum value index in each row of predicted data. Then I use these indices to return appropriate labels + their probabilities.</p><p><strong>Let’s run our Main class with two images and see the output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*NooueXVPh4nXs-4mGzL8Zg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*ETGy1REp7_CSm35OwbSpVg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*F8JqcVbmp9sYbUOCHJVBig.png" /><figcaption>Output</figcaption></figure><p>That’s all. Thank you for your attention.</p><p>Full source code is here: <a href="https://github.com/MrKnyaz/Tensorflow-and-JavaCV">https://github.com/MrKnyaz/Tensorflow-and-JavaCV</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=591c1b9443a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tensorflow: save model for use in Java or C++]]></title>
            <link>https://medium.com/@alexkn15/tensorflow-save-model-for-use-in-java-or-c-ab351a708ee4?source=rss-d23dc24c6a5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab351a708ee4</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Alexander Knyazev]]></dc:creator>
            <pubDate>Sat, 17 Feb 2018 16:48:27 GMT</pubDate>
            <atom:updated>2018-02-18T09:26:09.215Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AFhNbx_PnLB1KnEANNRrJA.png" /></figure><p>Recently, I searched how to save a Tensorflow model to a single *.pb file. Unfortunately, there is not enough information about that. Usually you can find tutorials that save the model to *.pb file and create a “variables” folder, where your model’s variables are stored. Then, you can’t just create a <strong>Graph</strong> object in Java or C++ and pass data from *.pb file to its <strong>.importGraphDef()</strong> method (hint: you have to use <strong>SavedModelBundle</strong> in this case). In fact, there is no need in <strong>“variables”</strong>, you need <strong>“constants”, </strong>i.e. a frozen graph, because usually you want to use an already trained model and not to train it in Java or C++.</p><p>In this tutorial we will create a simple Tensorflow model and save it as a single *.pb file. After that we will load and use it in Java. Let’s start!</p><p>First, import required libraries and create a simple model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d29cbb2f572f3aed833b99d839185c4/href">https://medium.com/media/8d29cbb2f572f3aed833b99d839185c4/href</a></iframe><p>Ok, now we have a model, let’s check it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/244a6995f014080c7515d0ec7b48e684/href">https://medium.com/media/244a6995f014080c7515d0ec7b48e684/href</a></iframe><p>For simplicity, we’ve assigned some data to our variables instead of training them. <strong>“not_activated_output”</strong> is easily calculated, so we use it in <strong>sess.run</strong> to check that our model is working as expected.</p><h4><strong>Let’s save the model!</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ff05af0cc6f89dd475d52fbd47732e/href">https://medium.com/media/01ff05af0cc6f89dd475d52fbd47732e/href</a></iframe><p>We have to convert all variables to constants using function <strong>graph_util.convert_variables_to_constants</strong>. All model’s variables become <strong>constants</strong>, “variables” folder won’t be created after saving the model.</p><p>Before we pass to Java code, I’ll show how to load the model back to python.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f69e96a5bc895bb04c0c13fe6993d62b/href">https://medium.com/media/f69e96a5bc895bb04c0c13fe6993d62b/href</a></iframe><p>Now we need to acquire required tensors (“input:0” and “not_activated_output:0”) and create a Session object to run our model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf1a88ff178c70c520af42b61dbee2ab/href">https://medium.com/media/bf1a88ff178c70c520af42b61dbee2ab/href</a></iframe><p>As I mentioned in the beginning, we <strong>cannot train</strong> our model, because we do not have any <strong>variables</strong>, they are loaded as <strong>constants</strong>. If you want to train a model after loading, use standard <a href="https://www.tensorflow.org/programmers_guide/saved_model"><strong>Tensorflow tutorial</strong></a> on how to save <strong>checkpoints</strong> or <strong>*.pb file with variables</strong>.</p><h4><strong>Let’s go to Java now!</strong></h4><p>To use Tensorflow in Java you have to add its libraries to your classpath. If you use Maven or Gradle it is as simple as adding one dependency.</p><pre>dependencies {<br>    compile(&#39;org.tensorflow:tensorflow:1.5.0&#39;)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*LII4kwxgaLEPE3836FuHvg.png" /><figcaption>This is my project structure</figcaption></figure><p>The code is pretty simple</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1eda85e6c259097de9a6db6891f3629/href">https://medium.com/media/c1eda85e6c259097de9a6db6891f3629/href</a></iframe><p>First, we get an absolute path to our model using getResource().toURI() method. Then we just read our model’s data to byte array, create an empty Graph object and import our data to it. To create a Session object you have to use recently created Graph object. Now we can use our model.</p><p><strong>Run the Session</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d89036b3c168926ce1695ac7344492c5/href">https://medium.com/media/d89036b3c168926ce1695ac7344492c5/href</a></iframe><p>In Python, to pass an input data to a <strong>session</strong>,<strong> </strong>we simply use Numpy arrays.</p><pre>X=tf.get_default_graph().get_tensor_by_name(&quot;input:0&quot;)<br>Z=tf.get_default_graph().get_tensor_by_name(&quot;not_activated_output:0&quot;)<br>sess.run([Z], feed_dict={X: <strong>[[4, 3, 2, 1]]</strong>})</pre><p>In Java, to run a session, we need to create a Tensor object using an array.</p><pre>float[][] inputData = {{4, 3, 2, 1}};<br>Tensor inputTensor = Tensor.<em>create</em>(inputData, Float.class);</pre><p>Also, there is no need to get required operations like in Python before we run a session. We just pass their names to <strong>.feed</strong> and <strong>.fetch</strong> methods.</p><pre>Tensor result = sess.runner()<br>        .feed(<strong>&quot;input&quot;</strong>, inputTensor)<br>        .fetch(<strong>&quot;not_activated_output&quot;</strong>).run().get(0);</pre><p><strong>.run()</strong> method returns a <strong>List</strong> of operations, and no matter if you pass an index to an operation like “not_activated_output<strong>:0</strong>”, it will always return a <strong>List</strong>, but always with one element in this case. So we just call <strong>.get(0)</strong> to get our result.</p><p>To acquire an array of data from our Tensor there is a <strong>.copyTo(Object obj)</strong> method, where <strong>obj</strong> is a buffer of an array type with required shape. In our case it is <strong>float[1][3]. </strong>The result is copied to this array.</p><p>That’s all, pretty easy, huh?:)</p><p>Thanks for your attention!</p><p>Full source code is here: <a href="https://github.com/MrKnyaz/Tensorflow-to-PB">https://github.com/MrKnyaz/Tensorflow-to-PB</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab351a708ee4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>