code: def device_string(
      n_devices=0, device_id=0, is_worker=True, is_training=True
  ):
      """
      assigning CPU/GPU based on user specifications
      """
      # pylint: disable=no-name-in-module
      from tensorflow.python.client import device_lib
  
      devices = device_lib.list_local_devices()
      n_local_gpus = sum([x.device_type == "GPU" for x in devices])
      if n_devices <= 0:  # user specified no gpu at all
          return "/cpu:{}".format(device_id)
      if is_training:
          # in training: use gpu only for workers whenever n_local_gpus
          device = "gpu" if (is_worker and n_local_gpus > 0) else "cpu"
          if device == "gpu" and device_id >= n_local_gpus:
              tf.logging.warning(
                  "trying to use gpu id %s, but only has %s GPU(s), "
                  "please set num_gpus to %s at most",
                  device_id,
                  n_local_gpus,
                  n_local_gpus,
              )
              # raise ValueError
          return "/{}:{}".format(device, device_id)
      # in inference: use gpu for everything whenever n_local_gpus
      return "/gpu:0" if n_local_gpus > 0 else "/cpu:0"
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import datetime
  import os
  import re
  from functools import partial
  import numpy
  import tensorflow
  from scipy import ndimage
  from six import string_types
label_desc: Use image node_id 16 for calling a global function with example usage: device_string(n_devices, device_id, is_worker, is_training) and returns: unknown, str, str