prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Upsample TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
  Hi,  
  I have a problem with the upsample op when trying to prepare an onnx model converted from keras. Any idea of the solution ? Thx
  
  > 
  >   File "C:\Users\vthibault\anaconda3\envs\tensorflow\lib\site-packages\onnx_tf\backend.py", line 56, in prepare
  >     return cls.onnx_model_to_tensorflow_rep(model, strict)
  >   File "C:\Users\vthibault\anaconda3\envs\tensorflow\lib\site-packages\onnx_tf\backend.py", line 76, in onnx_model_to_tensorflow_rep
  >     return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict)
  >   File "C:\Users\vthibault\anaconda3\envs\tensorflow\lib\site-packages\onnx_tf\backend.py", line 133, in _onnx_graph_to_tensorflow_rep
  >     onnx_node, tensor_dict, handlers, opset=opset, strict=strict)
  >   File "C:\Users\vthibault\anaconda3\envs\tensorflow\lib\site-packages\onnx_tf\backend.py", line 228, in _onnx_node_to_tensorflow_op
  >     return handler.handle(node, tensor_dict=tensor_dict, strict=strict)
  >   File "C:\Users\vthibault\anaconda3\envs\tensorflow\lib\site-packages\onnx_tf\handlers\handler.py", line 59, in handle
  >     return ver_handle(node, **kwargs)
  >   File "C:\Users\vthibault\anaconda3\envs\tensorflow\lib\site-packages\onnx_tf\handlers\backend\upsample.py", line 33, in version_7
  >     new_height = np.floor(x_shape[2] * scales[2])
  > TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `onnx_tf/handlers/backend/upsample.py`
  Content:
  ```
  1 import copy
  2 
  3 import numpy as np
  4 import tensorflow as tf
  5 
  6 from onnx_tf.common import exception
  7 from onnx_tf.handlers.backend_handler import BackendHandler
  8 from onnx_tf.handlers.handler import onnx_op
  9 from onnx_tf.handlers.handler import partial_support
  10 from onnx_tf.handlers.handler import ps_description
  11 from onnx_tf.handlers.handler import tf_func
  12 from onnx_tf.common.tf_helper import tf_shape
  13 
  14 
  15 @onnx_op("Upsample")
  16 @tf_func(tf.image.resize)
  17 @partial_support(True)
  18 @ps_description("Upsample required 4D input in Tensorflow.")
  19 class Upsample(BackendHandler):
  20 
  21   @classmethod
  22   def args_check(cls, node, **kwargs):
  23     x = kwargs["tensor_dict"][node.inputs[0]]
  24     x_shape = x.get_shape().as_list()
  25     if len(x_shape) != 4:
  26       exception.OP_UNSUPPORTED_EXCEPT("Upsample without 4D input", "Tensorflow")
  27 
  28     if node.attrs.get(
  29         "mode", "nearest").lower() not in ["nearest", "bilinear", "linear"]:
  30       exception.OP_UNSUPPORTED_EXCEPT("Upsample without nearest or bilinear",
  31                                       "Tensorflow")
  32 
  33   @classmethod
  34   def version_7(cls, node, **kwargs):
  35     x = kwargs["tensor_dict"][node.inputs[0]]
  36     x_shape = x.get_shape().as_list()
  37     attrs = copy.deepcopy(node.attrs)
  38     scales = attrs["scales"]
  39     new_height = np.floor(x_shape[2] * scales[2])
  40     new_weight = np.floor(x_shape[3] * scales[3])
  41 
  42     mode = attrs.get("mode", "nearest")
  43     if mode.lower() == "bilinear" or mode.lower() == "linear":
  44       mode = tf.image.ResizeMethod.BILINEAR
  45     else:
  46       mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR
  47 
  48     attrs["size"] = np.array((new_height, new_weight), dtype=np.int32)
  49     attrs["method"] = mode
  50 
  51     return [
  52         cls.make_tensor_from_onnx_node(
  53             node, attrs=attrs, c_last_only=True, **kwargs)
  54     ]
  55 
  56   @classmethod
  57   def version_9(cls, node, **kwargs):
  58     x = kwargs["tensor_dict"][node.inputs[0]]
  59     x_shape = tf_shape(x)
  60     attrs = copy.deepcopy(node.attrs)
  61     scales = kwargs["tensor_dict"][node.inputs[1]]
  62 
  63     assert_n_c_scale_is_one = tf.Assert(
  64         tf.logical_and(tf.equal(scales[0], 1), tf.equal(scales[1], 1)),
  65         [scales])
  66 
  67     with tf.control_dependencies([assert_n_c_scale_is_one]):
  68       h_w_scale = scales[2:]
  69       h_w_shape = x_shape[2:]
  70       new_h_w_shape = tf.cast(h_w_scale * tf.cast(h_w_shape, scales.dtype),
  71                               tf.int32)
  72 
  73       mode = attrs.get("mode", "nearest")
  74       if mode.lower() == "bilinear" or mode.lower() == "linear":
  75         mode = tf.image.ResizeMethod.BILINEAR
  76       else:
  77         mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR
  78 
  79       attrs["size"] = new_h_w_shape
  80       attrs["method"] = mode
  81 
  82       # Remove scale.
  83       upsample_node = copy.deepcopy(node)
  84       del upsample_node.inputs[1]
  85       return [
  86           cls.make_tensor_from_onnx_node(
  87               upsample_node, attrs=attrs, c_last_only=True, **kwargs)
  88       ]
  89
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/onnx_tf/handlers/backend/upsample.py b/onnx_tf/handlers/backend/upsample.py
  --- a/onnx_tf/handlers/backend/upsample.py
  +++ b/onnx_tf/handlers/backend/upsample.py
  @@ -1,6 +1,5 @@
   import copy
   
  -import numpy as np
   import tensorflow as tf
   
   from onnx_tf.common import exception
  @@ -33,20 +32,28 @@
     @classmethod
     def version_7(cls, node, **kwargs):
       x = kwargs["tensor_dict"][node.inputs[0]]
  -    x_shape = x.get_shape().as_list()
  +    x_shape = tf_shape(x)
       attrs = copy.deepcopy(node.attrs)
       scales = attrs["scales"]
  -    new_height = np.floor(x_shape[2] * scales[2])
  -    new_weight = np.floor(x_shape[3] * scales[3])
   
  -    mode = attrs.get("mode", "nearest")
  -    if mode.lower() == "bilinear" or mode.lower() == "linear":
  -      mode = tf.image.ResizeMethod.BILINEAR
  -    else:
  -      mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR
  +    assert_n_c_scale_is_one = tf.Assert(
  +        tf.logical_and(tf.equal(scales[0], 1), tf.equal(scales[1], 1)),
  +        [scales])
  +
  +    with tf.control_dependencies([assert_n_c_scale_is_one]):
  +      h_w_scale = scales[2:]
  +      h_w_shape = x_shape[2:]
  +      new_h_w_shape = tf.cast(h_w_scale * tf.cast(h_w_shape, type(h_w_scale[0])),
  +                              tf.int32)
  +
  +      mode = attrs.get("mode", "nearest")
  +      if mode.lower() == "bilinear" or mode.lower() == "linear":
  +        mode = tf.image.ResizeMethod.BILINEAR
  +      else:
  +        mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR
   
  -    attrs["size"] = np.array((new_height, new_weight), dtype=np.int32)
  -    attrs["method"] = mode
  +      attrs["size"] = new_h_w_shape
  +      attrs["method"] = mode
   
       return [
           cls.make_tensor_from_onnx_node(
  
verification_info: {"golden_diff": "diff --git a/onnx_tf/handlers/backend/upsample.py b/onnx_tf/handlers/backend/upsample.py\n--- a/onnx_tf/handlers/backend/upsample.py\n+++ b/onnx_tf/handlers/backend/upsample.py\n@@ -1,6 +1,5 @@\n import copy\n \n-import numpy as np\n import tensorflow as tf\n \n from onnx_tf.common import exception\n@@ -33,20 +32,28 @@\n   @classmethod\n   def version_7(cls, node, **kwargs):\n     x = kwargs[\"tensor_dict\"][node.inputs[0]]\n-    x_shape = x.get_shape().as_list()\n+    x_shape = tf_shape(x)\n     attrs = copy.deepcopy(node.attrs)\n     scales = attrs[\"scales\"]\n-    new_height = np.floor(x_shape[2] * scales[2])\n-    new_weight = np.floor(x_shape[3] * scales[3])\n \n-    mode = attrs.get(\"mode\", \"nearest\")\n-    if mode.lower() == \"bilinear\" or mode.lower() == \"linear\":\n-      mode = tf.image.ResizeMethod.BILINEAR\n-    else:\n-      mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR\n+    assert_n_c_scale_is_one = tf.Assert(\n+        tf.logical_and(tf.equal(scales[0], 1), tf.equal(scales[1], 1)),\n+        [scales])\n+\n+    with tf.control_dependencies([assert_n_c_scale_is_one]):\n+      h_w_scale = scales[2:]\n+      h_w_shape = x_shape[2:]\n+      new_h_w_shape = tf.cast(h_w_scale * tf.cast(h_w_shape, type(h_w_scale[0])),\n+                              tf.int32)\n+\n+      mode = attrs.get(\"mode\", \"nearest\")\n+      if mode.lower() == \"bilinear\" or mode.lower() == \"linear\":\n+        mode = tf.image.ResizeMethod.BILINEAR\n+      else:\n+        mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR\n \n-    attrs[\"size\"] = np.array((new_height, new_weight), dtype=np.int32)\n-    attrs[\"method\"] = mode\n+      attrs[\"size\"] = new_h_w_shape\n+      attrs[\"method\"] = mode\n \n     return [\n         cls.make_tensor_from_onnx_node(\n", "issue": "Upsample TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'\nHi,  \r\nI have a problem with the upsample op when trying to prepare an onnx model converted from keras. Any idea of the solution ? Thx\r\n\r\n> \r\n>   File \"C:\\Users\\vthibault\\anaconda3\\envs\\tensorflow\\lib\\site-packages\\onnx_tf\\backend.py\", line 56, in prepare\r\n>     return cls.onnx_model_to_tensorflow_rep(model, strict)\r\n>   File \"C:\\Users\\vthibault\\anaconda3\\envs\\tensorflow\\lib\\site-packages\\onnx_tf\\backend.py\", line 76, in onnx_model_to_tensorflow_rep\r\n>     return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict)\r\n>   File \"C:\\Users\\vthibault\\anaconda3\\envs\\tensorflow\\lib\\site-packages\\onnx_tf\\backend.py\", line 133, in _onnx_graph_to_tensorflow_rep\r\n>     onnx_node, tensor_dict, handlers, opset=opset, strict=strict)\r\n>   File \"C:\\Users\\vthibault\\anaconda3\\envs\\tensorflow\\lib\\site-packages\\onnx_tf\\backend.py\", line 228, in _onnx_node_to_tensorflow_op\r\n>     return handler.handle(node, tensor_dict=tensor_dict, strict=strict)\r\n>   File \"C:\\Users\\vthibault\\anaconda3\\envs\\tensorflow\\lib\\site-packages\\onnx_tf\\handlers\\handler.py\", line 59, in handle\r\n>     return ver_handle(node, **kwargs)\r\n>   File \"C:\\Users\\vthibault\\anaconda3\\envs\\tensorflow\\lib\\site-packages\\onnx_tf\\handlers\\backend\\upsample.py\", line 33, in version_7\r\n>     new_height = np.floor(x_shape[2] * scales[2])\r\n> TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'\n", "before_files": [{"content": "import copy\n\nimport numpy as np\nimport tensorflow as tf\n\nfrom onnx_tf.common import exception\nfrom onnx_tf.handlers.backend_handler import BackendHandler\nfrom onnx_tf.handlers.handler import onnx_op\nfrom onnx_tf.handlers.handler import partial_support\nfrom onnx_tf.handlers.handler import ps_description\nfrom onnx_tf.handlers.handler import tf_func\nfrom onnx_tf.common.tf_helper import tf_shape\n\n\n@onnx_op(\"Upsample\")\n@tf_func(tf.image.resize)\n@partial_support(True)\n@ps_description(\"Upsample required 4D input in Tensorflow.\")\nclass Upsample(BackendHandler):\n\n  @classmethod\n  def args_check(cls, node, **kwargs):\n    x = kwargs[\"tensor_dict\"][node.inputs[0]]\n    x_shape = x.get_shape().as_list()\n    if len(x_shape) != 4:\n      exception.OP_UNSUPPORTED_EXCEPT(\"Upsample without 4D input\", \"Tensorflow\")\n\n    if node.attrs.get(\n        \"mode\", \"nearest\").lower() not in [\"nearest\", \"bilinear\", \"linear\"]:\n      exception.OP_UNSUPPORTED_EXCEPT(\"Upsample without nearest or bilinear\",\n                                      \"Tensorflow\")\n\n  @classmethod\n  def version_7(cls, node, **kwargs):\n    x = kwargs[\"tensor_dict\"][node.inputs[0]]\n    x_shape = x.get_shape().as_list()\n    attrs = copy.deepcopy(node.attrs)\n    scales = attrs[\"scales\"]\n    new_height = np.floor(x_shape[2] * scales[2])\n    new_weight = np.floor(x_shape[3] * scales[3])\n\n    mode = attrs.get(\"mode\", \"nearest\")\n    if mode.lower() == \"bilinear\" or mode.lower() == \"linear\":\n      mode = tf.image.ResizeMethod.BILINEAR\n    else:\n      mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR\n\n    attrs[\"size\"] = np.array((new_height, new_weight), dtype=np.int32)\n    attrs[\"method\"] = mode\n\n    return [\n        cls.make_tensor_from_onnx_node(\n            node, attrs=attrs, c_last_only=True, **kwargs)\n    ]\n\n  @classmethod\n  def version_9(cls, node, **kwargs):\n    x = kwargs[\"tensor_dict\"][node.inputs[0]]\n    x_shape = tf_shape(x)\n    attrs = copy.deepcopy(node.attrs)\n    scales = kwargs[\"tensor_dict\"][node.inputs[1]]\n\n    assert_n_c_scale_is_one = tf.Assert(\n        tf.logical_and(tf.equal(scales[0], 1), tf.equal(scales[1], 1)),\n        [scales])\n\n    with tf.control_dependencies([assert_n_c_scale_is_one]):\n      h_w_scale = scales[2:]\n      h_w_shape = x_shape[2:]\n      new_h_w_shape = tf.cast(h_w_scale * tf.cast(h_w_shape, scales.dtype),\n                              tf.int32)\n\n      mode = attrs.get(\"mode\", \"nearest\")\n      if mode.lower() == \"bilinear\" or mode.lower() == \"linear\":\n        mode = tf.image.ResizeMethod.BILINEAR\n      else:\n        mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR\n\n      attrs[\"size\"] = new_h_w_shape\n      attrs[\"method\"] = mode\n\n      # Remove scale.\n      upsample_node = copy.deepcopy(node)\n      del upsample_node.inputs[1]\n      return [\n          cls.make_tensor_from_onnx_node(\n              upsample_node, attrs=attrs, c_last_only=True, **kwargs)\n      ]\n", "path": "onnx_tf/handlers/backend/upsample.py"}], "after_files": [{"content": "import copy\n\nimport tensorflow as tf\n\nfrom onnx_tf.common import exception\nfrom onnx_tf.handlers.backend_handler import BackendHandler\nfrom onnx_tf.handlers.handler import onnx_op\nfrom onnx_tf.handlers.handler import partial_support\nfrom onnx_tf.handlers.handler import ps_description\nfrom onnx_tf.handlers.handler import tf_func\nfrom onnx_tf.common.tf_helper import tf_shape\n\n\n@onnx_op(\"Upsample\")\n@tf_func(tf.image.resize)\n@partial_support(True)\n@ps_description(\"Upsample required 4D input in Tensorflow.\")\nclass Upsample(BackendHandler):\n\n  @classmethod\n  def args_check(cls, node, **kwargs):\n    x = kwargs[\"tensor_dict\"][node.inputs[0]]\n    x_shape = x.get_shape().as_list()\n    if len(x_shape) != 4:\n      exception.OP_UNSUPPORTED_EXCEPT(\"Upsample without 4D input\", \"Tensorflow\")\n\n    if node.attrs.get(\n        \"mode\", \"nearest\").lower() not in [\"nearest\", \"bilinear\", \"linear\"]:\n      exception.OP_UNSUPPORTED_EXCEPT(\"Upsample without nearest or bilinear\",\n                                      \"Tensorflow\")\n\n  @classmethod\n  def version_7(cls, node, **kwargs):\n    x = kwargs[\"tensor_dict\"][node.inputs[0]]\n    x_shape = tf_shape(x)\n    attrs = copy.deepcopy(node.attrs)\n    scales = attrs[\"scales\"]\n\n    assert_n_c_scale_is_one = tf.Assert(\n        tf.logical_and(tf.equal(scales[0], 1), tf.equal(scales[1], 1)),\n        [scales])\n\n    with tf.control_dependencies([assert_n_c_scale_is_one]):\n      h_w_scale = scales[2:]\n      h_w_shape = x_shape[2:]\n      new_h_w_shape = tf.cast(h_w_scale * tf.cast(h_w_shape, type(h_w_scale[0])),\n                              tf.int32)\n\n      mode = attrs.get(\"mode\", \"nearest\")\n      if mode.lower() == \"bilinear\" or mode.lower() == \"linear\":\n        mode = tf.image.ResizeMethod.BILINEAR\n      else:\n        mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR\n\n      attrs[\"size\"] = new_h_w_shape\n      attrs[\"method\"] = mode\n\n    return [\n        cls.make_tensor_from_onnx_node(\n            node, attrs=attrs, c_last_only=True, **kwargs)\n    ]\n\n  @classmethod\n  def version_9(cls, node, **kwargs):\n    x = kwargs[\"tensor_dict\"][node.inputs[0]]\n    x_shape = tf_shape(x)\n    attrs = copy.deepcopy(node.attrs)\n    scales = kwargs[\"tensor_dict\"][node.inputs[1]]\n\n    assert_n_c_scale_is_one = tf.Assert(\n        tf.logical_and(tf.equal(scales[0], 1), tf.equal(scales[1], 1)),\n        [scales])\n\n    with tf.control_dependencies([assert_n_c_scale_is_one]):\n      h_w_scale = scales[2:]\n      h_w_shape = x_shape[2:]\n      new_h_w_shape = tf.cast(h_w_scale * tf.cast(h_w_shape, scales.dtype),\n                              tf.int32)\n\n      mode = attrs.get(\"mode\", \"nearest\")\n      if mode.lower() == \"bilinear\" or mode.lower() == \"linear\":\n        mode = tf.image.ResizeMethod.BILINEAR\n      else:\n        mode = tf.image.ResizeMethod.NEAREST_NEIGHBOR\n\n      attrs[\"size\"] = new_h_w_shape\n      attrs[\"method\"] = mode\n\n      # Remove scale.\n      upsample_node = copy.deepcopy(node)\n      del upsample_node.inputs[1]\n      return [\n          cls.make_tensor_from_onnx_node(\n              upsample_node, attrs=attrs, c_last_only=True, **kwargs)\n      ]\n", "path": "onnx_tf/handlers/backend/upsample.py"}]}