text: """
   Copyright (c) 2018 Intel Corporation
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
  """
  
  import logging as log
  import os.path as osp
  
  from openvino.inference_engine import IEPlugin
  
  class InferenceContext:
      def __init__(self):
          self.plugins = {}
  
      def load_plugins(self, devices, cpu_ext="", gpu_ext=""):
          log.info("Loading plugins for devices: %s" % (devices))
  
          plugins = { d: IEPlugin(d) for d in devices }
          if 'CPU' in plugins and not len(cpu_ext) == 0:
              log.info("Using CPU extensions library '%s'" % (cpu_ext))
              assert osp.isfile(cpu_ext), "Failed to open CPU extensions library"
              plugins['CPU'].add_cpu_extension(cpu_ext)
  
          if 'GPU' in plugins and not len(gpu_ext) == 0:
              assert osp.isfile(gpu_ext), "Failed to open GPU definitions file"
              plugins['GPU'].set_config({"CONFIG_FILE": gpu_ext})
  
          self.plugins = plugins
  
          log.info("Plugins are loaded")
  
      def get_plugin(self, device):
          return self.plugins.get(device, None)
  
      def check_model_support(self, net, device):
          plugin = self.plugins[device]
  
          if plugin.device == "CPU":
              supported_layers = plugin.get_supported_layers(net)
              not_supported_layers = [l for l in net.layers.keys() \
                                      if l not in supported_layers]
              if len(not_supported_layers) != 0:
                  log.error("The following layers are not supported " \
                      "by the plugin for the specified device {}:\n {}" \
                      .format(plugin.device, ', '.join(not_supported_layers)))
                  log.error("Please try to specify cpu extensions " \
                      "library path in the command line parameters using " \
                      "the '-l' parameter")
                  raise NotImplementedError(
                      "Some layers are not supported on the device")
  
      def deploy_model(self, model, device, max_requests=1):
          self.check_model_support(model, device)
          plugin = self.plugins[device]
          deployed_model = plugin.load(network=model, num_requests=max_requests)
          return deployed_model
  
  
  
  class Module(object):
      def __init__(self, model):
          self.model = model
          self.device_model = None
  
          self.max_requests = 0
          self.active_requests = 0
  
          self.clear()
  
      def deploy(self, device, context, queue_size=1):
          self.context = context
          self.max_requests = queue_size
          self.device_model = context.deploy_model(
              self.model, device, self.max_requests)
          self.model = None
  
      def enqueue(self, input):
          self.clear()
  
          if self.max_requests <= self.active_requests:
              log.warning("Processing request rejected - too many requests")
              return False
  
          self.device_model.start_async(self.active_requests, input)
          self.active_requests += 1
          return True
  
      def wait(self):
          if self.active_requests <= 0:
              return
  
          self.perf_stats = [None, ] * self.active_requests
          self.outputs = [None, ] * self.active_requests
          for i in range(self.active_requests):
              self.device_model.requests[i].wait()
              self.outputs[i] = self.device_model.requests[i].outputs
              self.perf_stats[i] = self.device_model.requests[i].get_perf_counts()
  
          self.active_requests = 0
  
      def get_outputs(self):
          self.wait()
          return self.outputs
  
      def get_performance_stats(self):
          return self.perf_stats
  
      def clear(self):
          self.perf_stats = []
          self.outputs = []
  