Esempio n. 1
0
    def testDeviceDetails(self):
        (cpu, ) = config.list_physical_devices('CPU')
        details = config.get_device_details(cpu)
        self.assertEqual(details, {})

        if not test_util.is_gpu_available():
            return

        gpus = config.list_physical_devices('GPU')
        details = config.get_device_details(gpus[0])
        self.assertIsInstance(details['device_name'], str)
        self.assertNotEmpty(details['device_name'])
        if test.is_built_with_rocm():
            # AMD GPUs do not have a compute capability
            self.assertNotIn('compute_capability', details)
        else:
            cc = details['compute_capability']
            self.assertIsInstance(cc, tuple)
            major, minor = cc
            self.assertGreater(major, 0)
            self.assertGreaterEqual(minor, 0)

        # Test GPU returned from get_visible_devices
        if len(gpus) > 2:
            config.set_visible_devices(gpus[1], 'GPU')
            (visible_gpu, ) = config.get_visible_devices('GPU')
            details = config.get_device_details(visible_gpu)
            self.assertIsInstance(details['device_name'], str)
Esempio n. 2
0
  def testGpuNone(self):
    gpus = config.list_physical_devices('GPU')
    self.assertGreater(len(gpus), 0)

    cpus = config.list_physical_devices('CPU')
    self.assertEqual(len(cpus), 1)

    self.assertEqual(len(config.get_visible_devices('CPU')), 1)
    self.assertGreater(len(config.get_visible_devices('GPU')), 0)
    config.set_visible_devices(cpus[0])
    self.assertEqual(len(config.get_visible_devices('CPU')), 1)
    self.assertEqual(len(config.get_visible_devices('GPU')), 0)

    with self.assertRaisesRegexp(RuntimeError, 'unknown device'):
      with ops.device('/device:GPU:0'):
        a = constant_op.constant(1.0)
        self.evaluate(a)
Esempio n. 3
0
  def testGpuNone(self):
    gpus = config.list_physical_devices('GPU')
    self.assertGreater(len(gpus), 0)

    cpus = config.list_physical_devices('CPU')
    self.assertEqual(len(cpus), 1)

    self.assertEqual(len(config.get_visible_devices('CPU')), 1)
    self.assertGreater(len(config.get_visible_devices('GPU')), 0)
    config.set_visible_devices(cpus[0])
    self.assertEqual(len(config.get_visible_devices('CPU')), 1)
    self.assertEqual(len(config.get_visible_devices('GPU')), 0)

    with self.assertRaisesRegexp(RuntimeError, 'unknown device'):
      with ops.device('/device:GPU:0'):
        a = constant_op.constant(1.0)
        self.evaluate(a)
Esempio n. 4
0
    def testGpuNone(self):
        config.set_soft_device_placement(False)
        gpus = config.list_physical_devices('GPU')
        self.assertGreater(len(gpus), 0)

        cpus = config.list_physical_devices('CPU')
        self.assertEqual(len(cpus), 1)

        self.assertEqual(len(config.get_visible_devices('CPU')), 1)
        self.assertGreater(len(config.get_visible_devices('GPU')), 0)

        self.assertEqual(len(config.get_visible_devices('XLA_GPU')), 0)

        config.set_visible_devices(cpus[0])
        self.assertEqual(len(config.get_visible_devices('CPU')), 1)
        self.assertEqual(len(config.get_visible_devices('GPU')), 0)
        self.assertEqual(len(config.list_logical_devices('XLA_GPU')), 0)

        with self.assertRaisesRegex(errors.InvalidArgumentError,
                                    'Could not satisfy'):
            with ops.device('/device:GPU:0'):
                a = array_ops.identity(1.0)
                self.evaluate(a)

        # Modifying the visible devices is not supported
        with self.assertRaisesRegex(RuntimeError, 'cannot be modified'):
            config.set_visible_devices(gpus)

        # Setting the same visible devices is fine
        config.set_visible_devices(cpus[0])
Esempio n. 5
0
    def testGpuInvalidConfig(self):
        gpus = config.list_physical_devices('GPU')
        self.assertNotEqual(len(gpus), 0)

        if len(gpus) > 1:
            # Assert if other GPUs were not configured
            config.set_memory_growth(gpus[0], True)
            with self.assertRaisesRegex(ValueError, 'cannot differ'):
                c = context.context().config

            # If we limit visibility to GPU 0, growth is fine
            config.set_visible_devices(gpus[0], 'GPU')
            c = context.context().config
            self.assertTrue(c.gpu_options.allow_growth)

            # Default setting for second GPU is False and works if we set visibility
            config.set_visible_devices(gpus[1], 'GPU')
            c = context.context().config
            self.assertFalse(c.gpu_options.allow_growth)

            # Growth now fails because all the GPUs are visible and not the same
            config.set_visible_devices(gpus, 'GPU')
            with self.assertRaisesRegex(ValueError, 'cannot differ'):
                c = context.context().config

        for gpu in gpus:
            config.set_memory_growth(gpu, True)

        c = context.context().config
        self.assertTrue(c.gpu_options.allow_growth)

        with self.assertRaisesRegex(ValueError, 'memory limit'):
            config.set_logical_device_configuration(gpus[-1], [
                context.LogicalDeviceConfiguration(),
                context.LogicalDeviceConfiguration()
            ])

        self.assertIsNone(config.get_logical_device_configuration(gpus[-1]))
        config.set_logical_device_configuration(gpus[-1], [
            context.LogicalDeviceConfiguration(memory_limit=10),
            context.LogicalDeviceConfiguration(memory_limit=10)
        ])

        c = context.context().config
        self.assertFalse(c.gpu_options.allow_growth)

        with self.assertRaisesRegex(ValueError, 'virtual devices'):
            config.set_memory_growth(gpus[-1], False)
Esempio n. 6
0
    def testGpuNone(self):
        config.set_soft_device_placement(False)
        gpus = config.list_physical_devices('GPU')
        self.assertGreater(len(gpus), 0)

        cpus = config.list_physical_devices('CPU')
        self.assertEqual(len(cpus), 1)

        self.assertEqual(len(config.get_visible_devices('CPU')), 1)
        self.assertGreater(len(config.get_visible_devices('GPU')), 0)

        # get_visible_devices filters out XLA_* devices.  list_logical_devices does
        # not, but we can't call it here because it initializes the devices and
        # calling set_visible_devices after that is disallowed.
        self.assertEqual(len(config.get_visible_devices('XLA_GPU')), 0)

        config.set_visible_devices(cpus[0])
        self.assertEqual(len(config.get_visible_devices('CPU')), 1)
        self.assertEqual(len(config.get_visible_devices('GPU')), 0)
        self.assertEqual(len(config.list_logical_devices('XLA_GPU')), 0)

        with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                     'Could not satisfy'):
            with ops.device('/device:GPU:0'):
                a = array_ops.identity(1.0)
                self.evaluate(a)

        with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                     'Could not satisfy'):
            with ops.device('/device:XLA_GPU:0'):
                a = array_ops.identity(1.0)
                self.evaluate(a)

        # Modifying the visible devices is not supported
        with self.assertRaisesRegexp(RuntimeError, 'cannot be modified'):
            config.set_visible_devices(gpus)

        # Setting the same visible devices is fine
        config.set_visible_devices(cpus[0])
Esempio n. 7
0
    def testGpuNone(self):
        gpus = config.list_physical_devices('GPU')
        self.assertGreater(len(gpus), 0)

        cpus = config.list_physical_devices('CPU')
        self.assertEqual(len(cpus), 1)

        self.assertEqual(len(config.get_visible_devices('CPU')), 1)
        self.assertGreater(len(config.get_visible_devices('GPU')), 0)

        # get_visible_devices filters out XLA_* devices.  list_logical_devices does
        # not, but we can't call it here because it initializes the devices and
        # calling set_visible_devices after that is disallowed.
        self.assertEqual(len(config.get_visible_devices('XLA_GPU')), 0)

        config.set_visible_devices(cpus[0])
        self.assertEqual(len(config.get_visible_devices('CPU')), 1)
        self.assertEqual(len(config.get_visible_devices('GPU')), 0)
        self.assertEqual(len(config.list_logical_devices('XLA_GPU')), 0)

        with self.assertRaisesRegexp(RuntimeError, 'unknown device'):
            with ops.device('/device:GPU:0'):
                a = constant_op.constant(1.0)
                self.evaluate(a)

        with self.assertRaisesRegexp(RuntimeError, 'unknown device'):
            with ops.device('/device:XLA_GPU:0'):
                a = constant_op.constant(1.0)
                self.evaluate(a)

        # Modifying the visible devices is not supported
        with self.assertRaisesRegexp(RuntimeError, 'cannot be modified'):
            config.set_visible_devices(gpus)

        # Setting the same visible devices is fine
        config.set_visible_devices(cpus[0])
Esempio n. 8
0
  def testGpuNone(self):
    gpus = config.list_physical_devices('GPU')
    self.assertGreater(len(gpus), 0)

    cpus = config.list_physical_devices('CPU')
    self.assertEqual(len(cpus), 1)

    self.assertEqual(len(config.get_visible_devices('CPU')), 1)
    self.assertGreater(len(config.get_visible_devices('GPU')), 0)
    config.set_visible_devices(cpus[0])
    self.assertEqual(len(config.get_visible_devices('CPU')), 1)
    self.assertEqual(len(config.get_visible_devices('GPU')), 0)

    with self.assertRaisesRegexp(RuntimeError, 'unknown device'):
      with ops.device('/device:GPU:0'):
        a = constant_op.constant(1.0)
        self.evaluate(a)

    # Modifying the visible devices is not supported
    with self.assertRaisesRegexp(RuntimeError, 'cannot be modified'):
      config.set_visible_devices(gpus)

    # Setting the same visible devices is fine
    config.set_visible_devices(cpus[0])
Esempio n. 9
0
from PIL import Image
from sys import exit


def predict(inter, image):
    inter.set_tensor(input_details[0]['index'],
                     expand_dims(asarray(image).astype('float32'), axis=0))
    inter.invoke()
    return softmax(inter.get_tensor(output_details[0]['index']))


DESIRED_RESULT = 0.9
CLASS_NAMES = ['hogweed', 'cetera', 'other']

try:
    set_visible_devices([], 'GPU')
    visible_devices = get_visible_devices()
    for device in visible_devices:
        assert device.device_type != 'GPU'
except:
    print("Failed to disable GPUs, testing may be inaccurate")

# Prepare argument parser
parser = ArgumentParser(
    description="Test created detector '.tflite' neural network")
parser.add_argument('-n',
                    '--network',
                    required=True,
                    help="Network, saved in '.tflite' file")
parser.add_argument(
    '-s',