IVGCVSW-3522 Support dynamic output shape in hal_1_2::HalPolicy::ConvertResize

Signed-off-by: Aron Virginas-Tar <Aron.Virginas-Tar@arm.com>
Change-Id: I962f9759679f539566f7bc3aa75ed3e0bffe7c9f
diff --git a/1.2/HalPolicy.cpp b/1.2/HalPolicy.cpp
index 0c57636..5b501db 100644
--- a/1.2/HalPolicy.cpp
+++ b/1.2/HalPolicy.cpp
@@ -727,7 +727,7 @@
     }
 
     const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
-    const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
+    armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*output);
 
     armnn::ResizeDescriptor descriptor;
     descriptor.m_Method     = resizeMethod;
@@ -795,6 +795,19 @@
         return false;
     }
 
+    if (IsDynamicOutput(outputInfo))
+    {
+        try
+        {
+            ALOGD("Output shape not set, will infer from inputs");
+            outputInfo.SetShape(InferResizeOutputShape(inputInfo.GetShape(), descriptor));
+        }
+        catch (armnn::Exception& e)
+        {
+            return Fail("%s: Could not infer dynamic output shape: %s", __func__, e.what());
+        }
+    }
+
     bool isSupported = false;
     FORWARD_LAYER_SUPPORT_FUNC(__func__,
                                IsResizeSupported,
@@ -803,6 +816,7 @@
                                inputInfo,
                                outputInfo,
                                descriptor);
+
     if (!isSupported)
     {
         return false;
@@ -815,7 +829,12 @@
     layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
     input.Connect(layer->GetInputSlot(0));
 
-    return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
+    return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation,
+                                                            0,
+                                                            *layer,
+                                                            model,
+                                                            data,
+                                                            armnn::Optional<armnn::TensorInfo>(outputInfo));
 }
 
 bool HalPolicy::ConvertSpaceToDepth(const Operation& operation, const Model& model, ConversionData& data)
diff --git a/OutputShapeUtils.cpp b/OutputShapeUtils.cpp
index 285e25f..6c936ee 100644
--- a/OutputShapeUtils.cpp
+++ b/OutputShapeUtils.cpp
@@ -144,6 +144,28 @@
     return CalculateMaxShape(inputShape, alphaShape);
 }
 
+TensorShape InferResizeOutputShape(const TensorShape& inputShape, const ResizeDescriptor& descriptor)
+{
+    if (inputShape.GetNumDimensions() != 4)
+    {
+        throw InvalidArgumentException("Input shape for Resize must be 4D");
+    }
+
+    armnnUtils::DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout);
+
+    const unsigned int cIndex = dataLayoutIndexed.GetChannelsIndex();
+    const unsigned int wIndex = dataLayoutIndexed.GetWidthIndex();
+    const unsigned int hIndex = dataLayoutIndexed.GetHeightIndex();
+
+    TensorShape outputShape(4);
+    outputShape[0]      = inputShape[0];
+    outputShape[cIndex] = inputShape[cIndex];
+    outputShape[wIndex] = descriptor.m_TargetWidth;
+    outputShape[hIndex] = descriptor.m_TargetHeight;
+
+    return outputShape;
+}
+
 TensorShape InferSubOutputShape(const TensorShape& input0Shape, const TensorShape& input1Shape)
 {
     return CalculateMaxShape(input0Shape, input1Shape);
diff --git a/OutputShapeUtils.hpp b/OutputShapeUtils.hpp
index bcb4347..2a83261 100644
--- a/OutputShapeUtils.hpp
+++ b/OutputShapeUtils.hpp
@@ -28,6 +28,9 @@
 
 armnn::TensorShape InferPreluOutputShape(const armnn::TensorShape& inputShape, const armnn::TensorShape& alphaShape);
 
+armnn::TensorShape InferResizeOutputShape(const armnn::TensorShape& inputShape,
+                                          const armnn::ResizeDescriptor& descriptor);
+
 armnn::TensorShape InferSubOutputShape(const armnn::TensorShape& input0Shape, const armnn::TensorShape& input1Shape);
 
 } // namespace armnn_driver