<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>:<br>
Yes</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>:<br>
Mac OS X 10.13.6</li>
<li><strong>Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device</strong>:<br>
N/A</li>
<li><strong>TensorFlow installed from (source or binary)</strong>:<br>
binary</li>
<li><strong>TensorFlow version (use command below)</strong>:<br>
v1.10.0-rc1-19-g656e7a2b34 1.10.0</li>
<li><strong>Python version</strong>:<br>
Python 3.6.5</li>
<li><strong>Bazel version (if compiling from source)</strong>:<br>
N/A</li>
<li><strong>GCC/Compiler version (if compiling from source)</strong>:<br>
N/A</li>
<li><strong>CUDA/cuDNN version</strong>:<br>
N/A</li>
<li><strong>GPU model and memory</strong>:<br>
N/A</li>
<li><strong>Exact command to reproduce</strong>:<br>
N/A</li>
</ul>
<h3 dir="auto">Describe the problem</h3>
<p dir="auto">Placeholders with shape <code class="notranslate">()</code> gets shape <code class="notranslate">&lt;unknown&gt;</code> after <code class="notranslate">tf.graph_util.convert_variables_to_constants</code> has been run on a graph def.</p>
<p dir="auto">The expected result is that the shape is kept, as it is for tensors with higher rank.</p>
<h3 dir="auto">Source code / logs</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    placeholder = tf.placeholder(tf.float32, shape=(), name='placeholder')
    output = tf.identity(placeholder)

    graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, [output.op.name])
    print('Before:', placeholder.shape)


with tf.Graph().as_default() as g, tf.Session() as sess:
    tf.import_graph_def(graph_def, name='')
    placeholder = g.get_tensor_by_name('placeholder:0')
    print('After:', placeholder.shape)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">tensorflow</span> <span class="pl-k">as</span> <span class="pl-s1">tf</span>

<span class="pl-k">with</span> <span class="pl-s1">tf</span>.<span class="pl-v">Graph</span>().<span class="pl-en">as_default</span>(), <span class="pl-s1">tf</span>.<span class="pl-v">Session</span>() <span class="pl-k">as</span> <span class="pl-s1">sess</span>:
    <span class="pl-s1">placeholder</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-en">placeholder</span>(<span class="pl-s1">tf</span>.<span class="pl-s1">float32</span>, <span class="pl-s1">shape</span><span class="pl-c1">=</span>(), <span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'placeholder'</span>)
    <span class="pl-s1">output</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-en">identity</span>(<span class="pl-s1">placeholder</span>)

    <span class="pl-s1">graph_def</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-s1">graph_util</span>.<span class="pl-en">convert_variables_to_constants</span>(<span class="pl-s1">sess</span>, <span class="pl-s1">sess</span>.<span class="pl-s1">graph_def</span>, [<span class="pl-s1">output</span>.<span class="pl-s1">op</span>.<span class="pl-s1">name</span>])
    <span class="pl-en">print</span>(<span class="pl-s">'Before:'</span>, <span class="pl-s1">placeholder</span>.<span class="pl-s1">shape</span>)


<span class="pl-k">with</span> <span class="pl-s1">tf</span>.<span class="pl-v">Graph</span>().<span class="pl-en">as_default</span>() <span class="pl-k">as</span> <span class="pl-s1">g</span>, <span class="pl-s1">tf</span>.<span class="pl-v">Session</span>() <span class="pl-k">as</span> <span class="pl-s1">sess</span>:
    <span class="pl-s1">tf</span>.<span class="pl-en">import_graph_def</span>(<span class="pl-s1">graph_def</span>, <span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">''</span>)
    <span class="pl-s1">placeholder</span> <span class="pl-c1">=</span> <span class="pl-s1">g</span>.<span class="pl-en">get_tensor_by_name</span>(<span class="pl-s">'placeholder:0'</span>)
    <span class="pl-en">print</span>(<span class="pl-s">'After:'</span>, <span class="pl-s1">placeholder</span>.<span class="pl-s1">shape</span>)</pre></div>
<p dir="auto">Output</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Before: ()
After: &lt;unknown&gt;"><pre class="notranslate"><code class="notranslate">Before: ()
After: &lt;unknown&gt;
</code></pre></div>
<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>: yes</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>: Ubuntu 16.04</li>
<li><strong>Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device</strong>: no</li>
<li><strong>TensorFlow installed from (source or binary)</strong>: <code class="notranslate">pip install tensorflow</code></li>
<li><strong>TensorFlow version (use command below)</strong>: v1.10.0-0-g656e7a2b34 1.10.0</li>
<li><strong>Python version</strong>: <code class="notranslate">Python 3.6.6 :: Anaconda, Inc.</code></li>
<li><strong>Bazel version (if compiling from source)</strong>: N/A</li>
<li><strong>GCC/Compiler version (if compiling from source)</strong>: N/A</li>
<li><strong>CUDA/cuDNN version</strong>: N/A</li>
<li><strong>GPU model and memory</strong>: N/A</li>
<li><strong>Exact command to reproduce</strong>: Run the program below</li>
</ul>
<h3 dir="auto">Describe the problem</h3>
<p dir="auto">Usually, <code class="notranslate">tf.placeholder(tf.float32, shape=[]).get_shape()</code> is an empty shape, <code class="notranslate">()</code>. However, if you import a specific frozen graph first, the result of <code class="notranslate">get_shape()</code> will be <code class="notranslate">unknown</code>.</p>
<p dir="auto">Here is an example of a script that reproduces the issue and prints <code class="notranslate">unknown</code>:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import tensorflow as tf

IN_OUT = ['images_ph:0', 'inception_v3/logits/flatten/Reshape:0']

with open('inception-v3.pb', 'rb') as in_file:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(in_file.read())
    tf.import_graph_def(graph_def, name='', return_elements=IN_OUT)

print('shape result', tf.placeholder(tf.float32, shape=[]).get_shape())"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">tensorflow</span> <span class="pl-k">as</span> <span class="pl-s1">tf</span>

<span class="pl-v">IN_OUT</span> <span class="pl-c1">=</span> [<span class="pl-s">'images_ph:0'</span>, <span class="pl-s">'inception_v3/logits/flatten/Reshape:0'</span>]

<span class="pl-k">with</span> <span class="pl-en">open</span>(<span class="pl-s">'inception-v3.pb'</span>, <span class="pl-s">'rb'</span>) <span class="pl-k">as</span> <span class="pl-s1">in_file</span>:
    <span class="pl-s1">graph_def</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-v">GraphDef</span>()
    <span class="pl-s1">graph_def</span>.<span class="pl-v">ParseFromString</span>(<span class="pl-s1">in_file</span>.<span class="pl-en">read</span>())
    <span class="pl-s1">tf</span>.<span class="pl-en">import_graph_def</span>(<span class="pl-s1">graph_def</span>, <span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">''</span>, <span class="pl-s1">return_elements</span><span class="pl-c1">=</span><span class="pl-v">IN_OUT</span>)

<span class="pl-en">print</span>(<span class="pl-s">'shape result'</span>, <span class="pl-s1">tf</span>.<span class="pl-en">placeholder</span>(<span class="pl-s1">tf</span>.<span class="pl-s1">float32</span>, <span class="pl-s1">shape</span><span class="pl-c1">=</span>[]).<span class="pl-en">get_shape</span>())</pre></div>
<p dir="auto">Where <code class="notranslate">inception-v3.pb</code> can be downloaded at: <a href="https://storage.googleapis.com/agi-data/models/inception-v3.pb" rel="nofollow">https://storage.googleapis.com/agi-data/models/inception-v3.pb</a></p>