title: Limit GPU devices in Tensorflow
body: <p>I am developing in Python an application which uses Tensorflow and another model which with GPUs.
  I have a PC with many GPUs (3xNVIDIA GTX1080), due to the fact that all models try to use all available GPUs, resulting in OUT_OF_MEMORY_ERROR, I have found that you can assign a specific GPU to a Python script with</p>
  
  <pre><code>os.environ['CUDA_VISIBLE_DEVICES'] = '1'
  </code></pre>
  
  <p>Here I attach a snippet of my FCN class</p>
  
  <pre><code>class FCN:
    def __init__(self):
      os.environ['CUDA_VISIBLE_DEVICES'] = '1'
      self.keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
      self.image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
      self.annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")
  
      self.pred_annotation, logits = inference(self.image, self.keep_probability)
      tf.summary.image("input_image", self.image, max_outputs=2)
      tf.summary.image("ground_truth", tf.cast(self.annotation, tf.uint8), max_outputs=2)
      tf.summary.image("pred_annotation", tf.cast(self.pred_annotation, tf.uint8), max_outputs=2)
      self.loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                                                 labels=tf.squeeze(self.annotation,
                                                                                                   squeeze_dims=[3]),
                                                                                 name="entropy")))
      tf.summary.scalar("entropy", self.loss)
  
  ...
  </code></pre>
  
  <p>Inside the same file <code>FCN.py</code>, I have a little main which uses the class and when Tensorflow prints the output I can see that only 1 GPU is used, as I expect.</p>
  
  <pre><code>if __name__ == "__main__":
    fcn = FCN()
    fcn.train_model()
  
    images_dir = '/home/super/datasets/MeterDataset/full-dataset-gas-images/'
    for img_file in os.listdir(images_dir):
      fcn.segment(os.path.join(images_dir, img_file))
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>2018-01-09 11:31:57.351029: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 0 with properties: 
  name: GeForce GTX 1080
  major: 6 minor: 1 memoryClockRate (GHz) 1.7335
  pciBusID 0000:09:00.0
  Total memory: 7.92GiB
  Free memory: 7.60GiB
  2018-01-09 11:31:57.351047: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0 
  2018-01-09 11:31:57.351051: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y 
  2018-01-09 11:31:57.351057: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -&gt; (device: 0, name: GeForce GTX 1080, pci bus id: 0000:09:00.0)
  </code></pre>
  
  <p>The problem arises when I try to instantiate the FCN object from another script.</p>
  
  <pre><code>def main(args):
    start_time = datetime.now()
  
    font = cv2.FONT_HERSHEY_SIMPLEX
  
    results_file = "../results.txt"
    if os.path.exists(results_file):
      os.remove(results_file)
  
    results_file = open(results_file, "a")
  
    fcn = FCN()
  </code></pre>
  
  <p>Here the creation of the object always uses all 3 GPUs instead of using the only assigned into the <code>__init__()</code> method.</p>
  
  <p>Here the undesired output:</p>
  
  <pre><code>2018-01-09 11:41:02.537548: I 
  
  tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0 1 2 
  2018-01-09 11:41:02.537555: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y Y Y 
  2018-01-09 11:41:02.537558: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 1:   Y Y Y 
  2018-01-09 11:41:02.537561: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 2:   Y Y Y 
  2018-01-09 11:41:02.537567: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -&gt; (device: 0, name: GeForce GTX 1080, pci bus id: 0000:0b:00.0)
  2018-01-09 11:41:02.537571: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:1) -&gt; (device: 1, name: GeForce GTX 1080, pci bus id: 0000:09:00.0)
  2018-01-09 11:41:02.537574: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:2) -&gt; (device: 2, name: GeForce GTX 1080, pci bus id: 0000:05:00.0)
  </code></pre>