title: What is the way to use Tensor flow 2.0 object in open cv2 python and why is it so circuitous?
body: <p>I load an image using tensor flow api (2.0) like so : </p>
  
  <pre><code>def load(image_file):
    image = tf.io.read_file(image_file)
    image = tf.image.decode_jpeg(image)
  </code></pre>
  
  <p>Now that I have this object, I want to show this image, I can simply use matplotlib.pyplot, and this works. </p>
  
  <pre><code>plt.figure()
  plt.imshow(re/255.0)
  plt.show()
  </code></pre>
  
  <p>However attempting this with OpenCV2 is problematic from the start, most of the examples are from 1.0 with .eval() session based suggestion for numpy conversion. One way would be to first convert tensor flow object to numpy, here is the function to do that from API documentation :</p>
  
  <pre><code>TensorFlow
  API r2.0
  TensorFlow Core 2.0a
  Python
  tf.make_ndarray
  Create a numpy ndarray from a tensor.
  </code></pre>
  
  <p>I dont understand why this does not works and I get a number of errors while all I want is to do something simple and then use some open cv2 functions like remap, resize etc.:</p>
  
  <blockquote>
    <p>File
    "C:\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py",
    line 426, in <strong>call</strong>
        self._initialize(args, kwds, add_initializers_to=initializer_map)   File
    "C:\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py",
    line 370, in _initialize
        *args, **kwds))   File "C:\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py",
    line 1313, in _get_concrete_function_internal_garbage_collected
        graph_function, _, _ = self._maybe_define_function(args, kwargs)   File
    "C:\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py",
    line 1580, in _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)   File "C:\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py",
    line 1512, in _create_graph_function
        capture_by_value=self._capture_by_value),   File "C:\Python\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py",
    line 694, in func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)   File "C:\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py",
    line 317, in wrapped_fn
        return weak_wrapped_fn().<strong>wrapped</strong>(*args, **kwds)   File "C:\Python\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py",
    line 686, in wrapper
        ), args, kwargs)   File "C:\Python\Python37\lib\site-packages\tensorflow\python\autograph\impl\api.py",
    line 392, in converted_call
        result = converted_f(*effective_args, **kwargs)   File "C:\Users\syeda\AppData\Local\Temp\tmpnahp3og4.py", line 32, in
    tf__random_deform
        im2 = ag__.converted_call('make_ndarray', tf, ag__.ConversionOptions(recursive=True, verbose=0,
    strip_decorators=(tf.function, defun_9, ag__.convert,
    ag__.do_not_convert, ag__.converted_call), force_conversion=False,
    optional_features=(), internal_convert_user_code=True), (real_image,),
    {})   File
    "C:\Python\Python37\lib\site-packages\tensorflow\python\autograph\impl\api.py",
    line 267, in converted_call
        return _call_unconverted(f, args, kwargs)   File "C:\Python\Python37\lib\site-packages\tensorflow\python\autograph\impl\api.py",
    line 188, in _call_unconverted
        return f(*args, **kwargs)   File "C:\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_util.py",
    line 596, in MakeNdarray
        shape = [d.size for d in tensor.tensor_shape.dim] AttributeError: 'Tensor' object has no attribute 'tensor_shape'</p>
  </blockquote>
  
  <p><strong>Update 5/5/2018 :</strong> After searching more I found out that this has something to do with Tensorflow graph execution. 
  I have a function </p>
  
  <pre><code>def load_image_train(image_file):
    input_image, real_image = load(image_file)
   print(type(real_image))
    print(real_image.shape)
    some_image = Open CV operations like filtering, jitter etc performed on real_image
  return some_image
  </code></pre>
  
  <p>This works nicely when called eagerly with .numpy() attribute, however when called like following code and when you try to inspect what real_image is and its type returns</p>
  
  <blockquote>
    <p>class 'tensorflow.python.framework.ops.Tensor'   (None, None, None)</p>
  </blockquote>
  
  <p>Please advice.</p>
  
  <pre><code># Input pipeline
  train_dataset = tf.data.Dataset.list_files(PATH+'train/*.jpg')
  train_dataset = train_dataset.shuffle(BUFFER_SIZE)
  train_dataset = train_dataset.map(load_image_train,
                                 num_parallel_calls=tf.data.experimental.AUTOTUNE)
  train_dataset = train_dataset.batch(1)
  </code></pre>
  
  <p><strong>Update 5/5/2018 :</strong> I decided to do a preprocessing of the data so I don't have to worry about the using any opencv functionality during the load time of the data. However during training time I still want to do some openCV operations. Now as per the suggestion of @giser_yugang I tried using py_function, I wrap opencv operations in py_function and call that function in a wrapper tf.function. This wrapper tf.function I call in train step. However the output I get from this wrapper function is like so : </p>
  
  <pre><code>class 'tensorflow.python.framework.ops.Tensor'
  unknown
  </code></pre>
  
  <p>Then if I try to consume this tensor in the next train step operation I get a </p>
  
  <pre><code>incompatible with the layer: its rank is undefined, but the layer requires a defined rank.
  </code></pre>
  
  <p>If I don't use this py_function wrapper in my train step and directly try the numpy operations using opencv I get another error </p>
  
  <pre><code>AttributeError: 'Tensor' object has no attribute 'numpy'
  </code></pre>
  
  <p>I guess both ways you cant win !</p>