Exemplo n.º 1
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])
Exemplo n.º 2
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)
Exemplo n.º 3
0
 def _get_visible_devices():
     gpus = config.get_visible_devices('GPU')
     assert (len(gpus) > 0)
     visible_devices = []
     for i in range(len(gpus)):
         visible_devices.append(int(gpus[i].name.split(':')[-1]))
     return array_ops.constant(visible_devices, dtype=int32)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo 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])
Exemplo 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])
Exemplo 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])
Exemplo n.º 9
0
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',
    '--source',