Docker : How to export/save classifying results outside a Docker (tensorflow) box?
<p>I just followed this tutorial to easily train an image classifier with tensorflow :</p>

<p><a href="https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/" rel="nofollow">https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/</a></p>

<p>I now have a Docker box on my Ubuntu laptop, I managed to train the image classifier, and IT WORKS. Now I want to classify a batch of new images and <strong>store results (i.e. probabilities) on my laptop</strong>.</p>

<p>My objective is to save on my laptop the following results in an "output.csv" file like this:</p>

<pre><code>pic1    0.95   0.05
pic2    0.94   0.06
pic3    0.97   0.03
pic4    0.09   0.91
</code></pre>

<p>I put new images in a "tested" folder and I run the following python code, trying to write in an output file.</p>

<p>Inside Docker terminal I run:</p>

<pre><code>python /tf_files/label_batch_images.py /tf_files/tested
</code></pre>

<p>where label_batch_images.py code is:</p>

<pre><code>import tensorflow as tf, sys
import glob

folder_path = sys.argv[1]

# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line 
                   in tf.gfile.GFile("/tf_files/retrained_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
    out = open('output.csv', 'a')

    for image_path in glob.glob(folder_path+'/*'):

        # Read in the image_data
        image_data = tf.gfile.FastGFile(image_path, 'rb').read()

        # Feed the image_data as input to the graph and get first prediction
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

        predictions = sess.run(softmax_tensor, \
                 {'DecodeJpeg/contents:0': image_data})

        print("%s\t%s\t%s\n" % (image_path,predictions[0][1],predictions[0][0]))
        #THIS ACTUALLY WORKS, I see in my terminal "/tf_files/tested/pic1.jpg   0.00442768  0.995572"
        out.write("%s\t%s\t%s\n" % (image_path,predictions[0][1],predictions[0][0]))
        #This does not work, because output.csv is not modified

    out.close()
</code></pre>

<p>While it works - I see the expected results for each image in my Docker terminal - how to save it on a file on my laptop ?</p>

<h2>use volumes</h2>

<p>Share a so called host volume with your host, laptop, to write down the results directly on the host. e.g when starting your image</p>

<p><code>docker run -v /home/me/docker/results:/data/results &lt;image&gt;</code></p>

<p>In your container, when you write on /data/results, all files will be synced back / placed on the host under /home/me/docker/results</p>

<p>You can of course adjust both, the host or the container path</p>

<h2>user docker cp</h2>

<p>You can easily copy file from a docker container ( or to a docker container ) user <code>docker cp</code></p>

<p>so </p>

<pre><code>docker cp &lt;yourimageid&gt;:/path/to/the/file/on/the/container /home/me/docker/results
</code></pre>

<p>Do this, before you shutdown the container</p>

python csv docker tensorflow