title: Use Keras model with Flatten layer inside OpenCV 3
body: <p>I use Keras with TensorFlow as a backend to create and train a simple CNN.
  I am able to save the model and its weights in a .pb file, freeze it and optimize it for inference but when I try to load it into OpenCV 3.4.1 I get the error:</p>
  
  <pre><code>flatten/Shape:Shape(max_pooling2d/MaxPool)
  T:0
  out_type:[ ]
  OpenCV(3.4.1) Error: Unspecified error (Unknown layer type Shape in op flatten/Shape) in populateNet, file /home/dev/opencv-3.4.1/modules/dnn/src/tensorflow/tf_importer.cpp, line 1582
  Traceback (most recent call last):
    File "test.py", line 67, in &lt;module&gt;
      net = cv.dnn.readNetFromTensorflow('graph.pb')
  cv2.error: OpenCV(3.4.1) /home/dev/opencv-3.4.1/modules/dnn/src/tensorflow/tf_importer.cpp:1582: error: (-2) Unknown layer type Shape in op flatten/Shape in function populateNet
  </code></pre>
  
  <p>This is basically the same problem as the other question: <a href="https://stackoverflow.com/questions/48127178/how-to-import-tensorflow-model-with-flatten-layer-in-opencv">How to import TensorFlow model with flatten layer in OpenCV?</a>.</p>
  
  <p>The reason for the error is pretty well explained in this thread.
  The proposed workaround is to use directly <code>tf.reshape</code> instead of using the Keras API.</p>
  
  <p>However I don't know exactly how to do this.
  I tried to use the functional API and replace:</p>
  
  <pre><code>x = Flatten()(x)
  </code></pre>
  
  <p>by:</p>
  
  <pre><code>x = tf.reshape(x, [-1, some_value])
  </code></pre>
  
  <p>but this doesn't work and I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "test.py", line 57, in &lt;module&gt;
      tf_out = model.predict(inp)
    File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/models.py", line 965, in predict
      self.build()
    File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/models.py", line 578, in build
      self.model = Model(self.inputs, self.outputs[0], name=self.name + '_model')
    File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 678, in __init__
      super(Network, self).__init__(inputs, outputs, name=name)
    File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/network.py", line 341, in __init__
      '(thus holding past layer metadata). Found: ' + str(x))
  ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: Tensor("activation_4/Softmax:0", shape=(?, 10), dtype=float32)
  </code></pre>
  
  <p>Any idea of how I can export a TensorFlow model while still using Keras for most of the work?</p>