code: # MIT License
  #
  # Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2020
  #
  # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
  # persons to whom the Software is furnished to do so, subject to the following conditions:
  #
  # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  # Software.
  #
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  # SOFTWARE.
  from __future__ import absolute_import, division, print_function, unicode_literals
  
  import logging
  import unittest
  import importlib
  
  import numpy as np
  import tensorflow as tf
  
  from tests.utils import TestBase, master_seed
  
  object_detection_spec = importlib.util.find_spec("object_detection")
  object_detection_found = object_detection_spec is not None
  
  logger = logging.getLogger(__name__)
  
  
  @unittest.skipIf(
      not object_detection_found,
      reason="Skip unittests if object detection module is not found because of pre-trained model.",
  )
  @unittest.skipIf(
      tf.__version__[0] == "2" or (tf.__version__[0] == "1" and tf.__version__.split(".")[1] != "15"),
      reason="Skip unittests if not TensorFlow v1.15 because of pre-trained model.",
  )
  class TestTensorFlowFasterRCNN(TestBase):
      """
      This class tests the TensorFlowFasterRCNN object detector.
      """
  
      @classmethod
      def setUpClass(cls):
          master_seed(seed=1234, set_tensorflow=True)
          super().setUpClass()
  
          cls.n_test = 10
          cls.x_test_mnist = cls.x_test_mnist[0 : cls.n_test]
          cls.y_test_mnist = cls.y_test_mnist[0 : cls.n_test]
  
          # Only import if object detection module is available
          from art.estimators.object_detection.tensorflow_faster_rcnn import TensorFlowFasterRCNN
  
          # Define object detector
          images = tf.placeholder(tf.float32, shape=[2, 28, 28, 1])
          cls.obj_dec = TensorFlowFasterRCNN(images=images)
  
      def test_predict(self):
          result = self.obj_dec.predict(self.x_test_mnist)
  
          self.assertTrue(
              list(result[0].keys())
              == [
                  "boxes",
                  "labels",
                  "scores",
              ]
          )
  
          self.assertTrue(result[0]["boxes"].shape == (300, 4))
          expected_detection_boxes = np.asarray([0.65566427, 0.0, 1.0, 0.9642794])
          np.testing.assert_array_almost_equal(result[0]["boxes"][2, :], expected_detection_boxes, decimal=6)
  
          self.assertTrue(result[0]["scores"].shape == (300,))
          expected_detection_scores = np.asarray(
              [
                  3.356745e-04,
                  3.190193e-04,
                  2.967696e-04,
                  2.128600e-04,
                  1.726381e-04,
                  1.472894e-04,
                  1.198768e-04,
                  1.109493e-04,
                  1.066341e-04,
                  8.560477e-05,
              ]
          )
          np.testing.assert_array_almost_equal(result[0]["scores"][:10], expected_detection_scores, decimal=6)
  
          self.assertTrue(result[0]["labels"].shape == (300,))
          expected_detection_classes = np.asarray([71.0, 81.0, 66.0, 15.0, 63.0, 66.0, 64.0, 84.0, 37.0, 2.0])
          np.testing.assert_array_almost_equal(result[0]["labels"][:10], expected_detection_classes, decimal=6)
  
      def test_loss_gradient(self):
          # Create labels
          result = self.obj_dec.predict(self.x_test_mnist[:2])
  
          y = [
              {
                  "boxes": result[0]["boxes"],
                  "labels": result[0]["labels"],
                  "scores": np.ones_like(result[0]["labels"]),
              },
              {
                  "boxes": result[1]["boxes"],
                  "labels": result[1]["labels"],
                  "scores": np.ones_like(result[1]["labels"]),
              },
          ]
  
          # Compute gradients
          grads = self.obj_dec.loss_gradient(self.x_test_mnist[:2], y)
  
          self.assertTrue(grads.shape == (2, 28, 28, 1))
  
          expected_gradients1 = np.asarray(
              [
                  [-6.1982083e-03],
                  [9.2188769e-04],
                  [2.2715484e-03],
                  [3.0439291e-03],
                  [3.9350586e-03],
                  [1.3214475e-03],
                  [-1.9790903e-03],
                  [-1.8616641e-03],
                  [-1.7762191e-03],
                  [-2.4208077e-03],
                  [-2.1795963e-03],
                  [-1.3475846e-03],
                  [-1.7141351e-04],
                  [5.3379539e-04],
                  [6.1705662e-04],
                  [9.1885449e-05],
                  [-2.4936342e-04],
                  [-7.8056828e-04],
                  [-2.4509570e-04],
                  [-1.3246380e-04],
                  [-6.9344416e-04],
                  [-2.8356430e-04],
                  [1.1605137e-03],
                  [2.7452575e-03],
                  [2.9905243e-03],
                  [2.2033940e-03],
                  [1.7121597e-03],
                  [8.4455572e-03],
              ]
          )
          np.testing.assert_array_almost_equal(grads[0, 0, :, :], expected_gradients1, decimal=2)
  
          expected_gradients2 = np.asarray(
              [
                  [-8.14103708e-03],
                  [-5.78497676e-03],
                  [-1.93702651e-03],
                  [-1.10854053e-04],
                  [-3.13712610e-03],
                  [-2.40660645e-03],
                  [-2.33814842e-03],
                  [-1.18874465e-04],
                  [-8.61960289e-05],
                  [-8.44302267e-05],
                  [1.16928865e-03],
                  [8.52172205e-04],
                  [1.50172669e-03],
                  [9.76039213e-04],
                  [6.99639553e-04],
                  [1.55441079e-03],
                  [1.99828879e-03],
                  [2.53868615e-03],
                  [3.47398920e-03],
                  [3.55495396e-03],
                  [3.40546807e-03],
                  [5.23657538e-03],
                  [9.50821862e-03],
                  [8.31787288e-03],
                  [4.75075701e-03],
                  [8.02019704e-03],
                  [1.00337435e-02],
                  [6.10247999e-03],
              ]
          )
          np.testing.assert_array_almost_equal(grads[1, :, 0, :], expected_gradients2, decimal=2)
  
      def test_loss_gradient_standard_format(self):
          # Create labels
          result_tf = self.obj_dec.predict(self.x_test_mnist[:2], standardise_output=False)
          result = self.obj_dec.predict(self.x_test_mnist[:2], standardise_output=True)
  
          from art.estimators.object_detection.utils import convert_tf_to_pt
  
          result_pt = convert_tf_to_pt(y=result_tf, height=self.x_test_mnist.shape[1], width=self.x_test_mnist.shape[2])
          for i in range(2):
              np.testing.assert_array_equal(result[i]["boxes"], result_pt[i]["boxes"])
              np.testing.assert_array_equal(result[i]["labels"], result_pt[i]["labels"])
              np.testing.assert_array_equal(result[i]["scores"], result_pt[i]["scores"])
  
          y = [
              {
                  "boxes": result[0]["boxes"],
                  "labels": result[0]["labels"],
                  "scores": np.ones_like(result[0]["labels"]),
              },
              {
                  "boxes": result[1]["boxes"],
                  "labels": result[1]["labels"],
                  "scores": np.ones_like(result[1]["labels"]),
              },
          ]
  
          # Compute gradients
          grads = self.obj_dec.loss_gradient(self.x_test_mnist[:2], y, standardise_output=True)
  
          self.assertTrue(grads.shape == (2, 28, 28, 1))
  
          expected_gradients1 = np.asarray(
              [
                  [-6.1982083e-03],
                  [9.2188769e-04],
                  [2.2715484e-03],
                  [3.0439291e-03],
                  [3.9350586e-03],
                  [1.3214475e-03],
                  [-1.9790903e-03],
                  [-1.8616641e-03],
                  [-1.7762191e-03],
                  [-2.4208077e-03],
                  [-2.1795963e-03],
                  [-1.3475846e-03],
                  [-1.7141351e-04],
                  [5.3379539e-04],
                  [6.1705662e-04],
                  [9.1885449e-05],
                  [-2.4936342e-04],
                  [-7.8056828e-04],
                  [-2.4509570e-04],
                  [-1.3246380e-04],
                  [-6.9344416e-04],
                  [-2.8356430e-04],
                  [1.1605137e-03],
                  [2.7452575e-03],
                  [2.9905243e-03],
                  [2.2033940e-03],
                  [1.7121597e-03],
                  [8.4455572e-03],
              ]
          )
          np.testing.assert_array_almost_equal(grads[0, 0, :, :], expected_gradients1, decimal=2)
  
          expected_gradients2 = np.asarray(
              [
                  [-8.14103708e-03],
                  [-5.78497676e-03],
                  [-1.93702651e-03],
                  [-1.10854053e-04],
                  [-3.13712610e-03],
                  [-2.40660645e-03],
                  [-2.33814842e-03],
                  [-1.18874465e-04],
                  [-8.61960289e-05],
                  [-8.44302267e-05],
                  [1.16928865e-03],
                  [8.52172205e-04],
                  [1.50172669e-03],
                  [9.76039213e-04],
                  [6.99639553e-04],
                  [1.55441079e-03],
                  [1.99828879e-03],
                  [2.53868615e-03],
                  [3.47398920e-03],
                  [3.55495396e-03],
                  [3.40546807e-03],
                  [5.23657538e-03],
                  [9.50821862e-03],
                  [8.31787288e-03],
                  [4.75075701e-03],
                  [8.02019704e-03],
                  [1.00337435e-02],
                  [6.10247999e-03],
              ]
          )
          np.testing.assert_array_almost_equal(grads[1, :, 0, :], expected_gradients2, decimal=2)
  
  
  if __name__ == "__main__":
      unittest.main()
  
apis: tensorflow.__version__.split numpy.ones_like numpy.asarray tensorflow.placeholder numpy.testing.assert_array_equal numpy.testing.assert_array_almost_equal
api_extract: [(29, 'importlib.util.find_spec', 'importlib.util.find_spec', (['"""object_detection"""'], {}), False, 'import importlib\n'), (32, 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), False, 'import logging\n'), (35, 'unittest.skipIf', 'unittest.skipIf', (['(not object_detection_found)'], {'reason': '"""Skip unittests if object detection module is not found because of pre-trained model."""'}), False, 'import unittest\n'), (292, 'unittest.main', 'unittest.main', ([], {}), False, 'import unittest\n'), (50, 'tests.utils.master_seed', 'master_seed', ([], {'seed': '(1234)', 'set_tensorflow': '(True)'}), False, 'from tests.utils import TestBase, master_seed\n'), (61, 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32'], {'shape': '[2, 28, 28, 1]'}), True, 'import tensorflow as tf\n'), (62, 'art.estimators.object_detection.tensorflow_faster_rcnn.TensorFlowFasterRCNN', 'TensorFlowFasterRCNN', ([], {'images': 'images'}), False, 'from art.estimators.object_detection.tensorflow_faster_rcnn import TensorFlowFasterRCNN\n'), (77, 'numpy.asarray', 'np.asarray', (['[0.65566427, 0.0, 1.0, 0.9642794]'], {}), True, 'import numpy as np\n'), (78, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (["result[0]['boxes'][(2), :]", 'expected_detection_boxes'], {'decimal': '(6)'}), True, 'import numpy as np\n'), (81, 'numpy.asarray', 'np.asarray', (['[0.0003356745, 0.0003190193, 0.0002967696, 0.00021286, 0.0001726381, \n    0.0001472894, 0.0001198768, 0.0001109493, 0.0001066341, 8.560477e-05]'], {}), True, 'import numpy as np\n'), (95, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (["result[0]['scores'][:10]", 'expected_detection_scores'], {'decimal': '(6)'}), True, 'import numpy as np\n'), (98, 'numpy.asarray', 'np.asarray', (['[71.0, 81.0, 66.0, 15.0, 63.0, 66.0, 64.0, 84.0, 37.0, 2.0]'], {}), True, 'import numpy as np\n'), (99, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (["result[0]['labels'][:10]", 'expected_detection_classes'], {'decimal': '(6)'}), True, 'import numpy as np\n'), (123, 'numpy.asarray', 'np.asarray', (['[[-0.0061982083], [0.00092188769], [0.0022715484], [0.0030439291], [\n    0.0039350586], [0.0013214475], [-0.0019790903], [-0.0018616641], [-\n    0.0017762191], [-0.0024208077], [-0.0021795963], [-0.0013475846], [-\n    0.00017141351], [0.00053379539], [0.00061705662], [9.1885449e-05], [-\n    0.00024936342], [-0.00078056828], [-0.0002450957], [-0.0001324638], [-\n    0.00069344416], [-0.0002835643], [0.0011605137], [0.0027452575], [\n    0.0029905243], [0.002203394], [0.0017121597], [0.0084455572]]'], {}), True, 'import numpy as np\n'), (155, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['grads[(0), (0), :, :]', 'expected_gradients1'], {'decimal': '(2)'}), True, 'import numpy as np\n'), (157, 'numpy.asarray', 'np.asarray', (['[[-0.00814103708], [-0.00578497676], [-0.00193702651], [-0.000110854053], [\n    -0.0031371261], [-0.00240660645], [-0.00233814842], [-0.000118874465],\n    [-8.61960289e-05], [-8.44302267e-05], [0.00116928865], [0.000852172205],\n    [0.00150172669], [0.000976039213], [0.000699639553], [0.00155441079], [\n    0.00199828879], [0.00253868615], [0.0034739892], [0.00355495396], [\n    0.00340546807], [0.00523657538], [0.00950821862], [0.00831787288], [\n    0.00475075701], [0.00802019704], [0.0100337435], [0.00610247999]]'], {}), True, 'import numpy as np\n'), (189, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['grads[(1), :, (0), :]', 'expected_gradients2'], {'decimal': '(2)'}), True, 'import numpy as np\n'), (198, 'art.estimators.object_detection.utils.convert_tf_to_pt', 'convert_tf_to_pt', ([], {'y': 'result_tf', 'height': 'self.x_test_mnist.shape[1]', 'width': 'self.x_test_mnist.shape[2]'}), False, 'from art.estimators.object_detection.utils import convert_tf_to_pt\n'), (222, 'numpy.asarray', 'np.asarray', (['[[-0.0061982083], [0.00092188769], [0.0022715484], [0.0030439291], [\n    0.0039350586], [0.0013214475], [-0.0019790903], [-0.0018616641], [-\n    0.0017762191], [-0.0024208077], [-0.0021795963], [-0.0013475846], [-\n    0.00017141351], [0.00053379539], [0.00061705662], [9.1885449e-05], [-\n    0.00024936342], [-0.00078056828], [-0.0002450957], [-0.0001324638], [-\n    0.00069344416], [-0.0002835643], [0.0011605137], [0.0027452575], [\n    0.0029905243], [0.002203394], [0.0017121597], [0.0084455572]]'], {}), True, 'import numpy as np\n'), (254, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['grads[(0), (0), :, :]', 'expected_gradients1'], {'decimal': '(2)'}), True, 'import numpy as np\n'), (256, 'numpy.asarray', 'np.asarray', (['[[-0.00814103708], [-0.00578497676], [-0.00193702651], [-0.000110854053], [\n    -0.0031371261], [-0.00240660645], [-0.00233814842], [-0.000118874465],\n    [-8.61960289e-05], [-8.44302267e-05], [0.00116928865], [0.000852172205],\n    [0.00150172669], [0.000976039213], [0.000699639553], [0.00155441079], [\n    0.00199828879], [0.00253868615], [0.0034739892], [0.00355495396], [\n    0.00340546807], [0.00523657538], [0.00950821862], [0.00831787288], [\n    0.00475075701], [0.00802019704], [0.0100337435], [0.00610247999]]'], {}), True, 'import numpy as np\n'), (288, 'numpy.testing.assert_array_almost_equal', 'np.testing.assert_array_almost_equal', (['grads[(1), :, (0), :]', 'expected_gradients2'], {'decimal': '(2)'}), True, 'import numpy as np\n'), (200, 'numpy.testing.assert_array_equal', 'np.testing.assert_array_equal', (["result[i]['boxes']", "result_pt[i]['boxes']"], {}), True, 'import numpy as np\n'), (201, 'numpy.testing.assert_array_equal', 'np.testing.assert_array_equal', (["result[i]['labels']", "result_pt[i]['labels']"], {}), True, 'import numpy as np\n'), (202, 'numpy.testing.assert_array_equal', 'np.testing.assert_array_equal', (["result[i]['scores']", "result_pt[i]['scores']"], {}), True, 'import numpy as np\n'), (109, 'numpy.ones_like', 'np.ones_like', (["result[0]['labels']"], {}), True, 'import numpy as np\n'), (114, 'numpy.ones_like', 'np.ones_like', (["result[1]['labels']"], {}), True, 'import numpy as np\n'), (208, 'numpy.ones_like', 'np.ones_like', (["result[0]['labels']"], {}), True, 'import numpy as np\n'), (213, 'numpy.ones_like', 'np.ones_like', (["result[1]['labels']"], {}), True, 'import numpy as np\n'), (40, 'tensorflow.__version__.split', 'tf.__version__.split', (['"""."""'], {}), True, 'import tensorflow as tf\n')]