GitHub #557 wrong result in int8 model

 * Added support for qasymms8 (int8) to ImageTensorGenerator
 * Added qasymmu8 as alias for qasymm8 in ImageTensorGenerator
 * Added support for qasymms8 (int8) to ExecuteNetwork
 * Added qasymmu8 as alias for qasymm8 in ExecuteNetwork
 * Set tflite to be the default model format in ImageTensorGenerator as
   it's the only supported model format.

Signed-off-by: Mike Kelly <mike.kelly@arm.com>
Change-Id: Ieda7b78e668ea390e3565cd65a41fe0a9c8a5b83
diff --git a/tests/ImageTensorGenerator/ImageTensorGenerator.cpp b/tests/ImageTensorGenerator/ImageTensorGenerator.cpp
index a2110f9..b443255 100644
--- a/tests/ImageTensorGenerator/ImageTensorGenerator.cpp
+++ b/tests/ImageTensorGenerator/ImageTensorGenerator.cpp
@@ -164,15 +164,16 @@
                 ("f,model-format",
                     "Format of the intended model file that uses the images."
                     "Different formats have different image normalization styles."
+                    "If unset, defaults to tflite."
                     "Accepted value (tflite)",
-                    cxxopts::value<std::string>(m_ModelFormat))
+                    cxxopts::value<std::string>(m_ModelFormat)->default_value("tflite"))
                 ("o,outfile",
                     "Output raw tensor file path",
                     cxxopts::value<std::string>(m_OutputFileName))
                 ("z,output-type",
                     "The data type of the output tensors."
                     "If unset, defaults to \"float\" for all defined inputs. "
-                    "Accepted values (float, int or qasymm8)",
+                    "Accepted values (float, int, qasymms8 or qasymmu8)",
                     cxxopts::value<std::string>(m_OutputType)->default_value("float"))
                 ("new-width",
                     "Resize image to new width. Keep original width if unspecified",
@@ -254,10 +255,14 @@
         {
             return armnn::DataType::Signed32;
         }
-        else if (m_OutputType == "qasymm8")
+        else if (m_OutputType == "qasymm8" || m_OutputType == "qasymmu8")
         {
             return armnn::DataType::QAsymmU8;
         }
+        else if (m_OutputType == "qasymms8")
+        {
+            return armnn::DataType::QAsymmS8;
+        }
         else
         {
             throw armnn::Exception("Unsupported input type" + m_OutputType);
@@ -292,7 +297,8 @@
     const unsigned int batchSize = 1;
     const armnn::DataLayout outputLayout(cmdline.GetLayout());
 
-    using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>>;
+    using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>,
+                                             std::vector<int8_t>>;
     std::vector<TContainer> imageDataContainers;
     const NormalizationParameters& normParams = GetNormalizationParameters(modelFormat, outputType);
     try
@@ -307,6 +313,10 @@
                 imageDataContainers.push_back(PrepareImageTensor<uint8_t>(
                     imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
                 break;
+            case armnn::DataType::QAsymmS8:
+                imageDataContainers.push_back(PrepareImageTensor<int8_t>(
+                        imagePath, newWidth, newHeight, normParams, batchSize, outputLayout));
+                break;
             case armnn::DataType::Float32:
             default:
                 imageDataContainers.push_back(PrepareImageTensor<float>(
diff --git a/tests/ImageTensorGenerator/ImageTensorGenerator.hpp b/tests/ImageTensorGenerator/ImageTensorGenerator.hpp
index 5aa2ca8..6d2e549 100644
--- a/tests/ImageTensorGenerator/ImageTensorGenerator.hpp
+++ b/tests/ImageTensorGenerator/ImageTensorGenerator.hpp
@@ -56,6 +56,10 @@
                     normParams.mean = { 128.0, 128.0, 128.0 };
                     break;
                 case armnn::DataType::QAsymmU8:
+                    break;
+                case armnn::DataType::QAsymmS8:
+                    normParams.mean = { 128.0, 128.0, 128.0 };
+                    break;
                 default:
                     break;
             }
@@ -138,7 +142,7 @@
     return imageDataInt;
 }
 
-// Prepare qasymm8 image tensor
+// Prepare qasymmu8 image tensor
 template <>
 std::vector<uint8_t> PrepareImageTensor<uint8_t>(const std::string& imagePath,
                                                  unsigned int newWidth,
@@ -158,6 +162,26 @@
     return imageDataQasymm8;
 }
 
+// Prepare qasymms8 image tensor
+template <>
+std::vector<int8_t> PrepareImageTensor<int8_t>(const std::string& imagePath,
+                                               unsigned int newWidth,
+                                               unsigned int newHeight,
+                                               const NormalizationParameters& normParams,
+                                               unsigned int batchSize,
+                                               const armnn::DataLayout& outputLayout)
+{
+    // Get float32 image tensor
+    std::vector<float> imageDataFloat =
+            PrepareImageTensor<float>(imagePath, newWidth, newHeight, normParams, batchSize, outputLayout);
+    std::vector<int8_t> imageDataQasymms8;
+    imageDataQasymms8.reserve(imageDataFloat.size());
+    // Convert to uint8 image tensor with static cast
+    std::transform(imageDataFloat.begin(), imageDataFloat.end(), std::back_inserter(imageDataQasymms8),
+                   [](float val) { return static_cast<uint8_t>(val); });
+    return imageDataQasymms8;
+}
+
 /** Write image tensor to ofstream
  *
  * @param[in] imageData         Image tensor data
@@ -176,3 +200,11 @@
 {
     std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<int>(imageTensorFile, " "));
 }
+
+// For int8_t image tensor, cast it to int before writing it to prevent writing data as characters instead of
+// numerical values
+template <>
+void WriteImageTensorImpl<int8_t>(const std::vector<int8_t>& imageData, std::ofstream& imageTensorFile)
+{
+    std::copy(imageData.begin(), imageData.end(), std::ostream_iterator<int>(imageTensorFile, " "));
+}
\ No newline at end of file