def initialize(self, **kwargs):
        if 'model' not in kwargs:
            return

        model = kwargs['model']
        model_as_file = True
        try:
            with open(model, 'r') as f:
                self.json_info = json.load(f)
        except FileNotFoundError:
            try:
                self.json_info = json.loads(model)
                model_as_file = False
            except json.decoder.JSONDecodeError:
                raise Exception("Invalid model argument")

        if 'device' in kwargs:
            device = kwargs['device']
            if device < -1:
                os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
                device = prf_utils.get_available_device()
            os.environ['CUDA_VISIBLE_DEVICES'] = str(device)
        else:
            os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

        sys.path.append(os.path.dirname(__file__))
        framework = self.json_info['Framework']
        if 'ModelConfiguration' in self.json_info:
            if isinstance(self.json_info['ModelConfiguration'], str):
                ChildModelDetector = getattr(
                    importlib.import_module('{}.{}'.format(
                        framework, self.json_info['ModelConfiguration'])),
                    'ChildObjectDetector')
            else:
                ChildModelDetector = getattr(
                    importlib.import_module('{}.{}'.format(
                        framework,
                        self.json_info['ModelConfiguration']['Name'])),
                    'ChildObjectDetector')
        else:
            raise Exception("Invalid model configuration")

        self.child_object_detector = ChildModelDetector()
        self.child_object_detector.initialize(model, model_as_file)
Ejemplo n.º 2
0
    def initialize(self, **kwargs):

        if 'model' not in kwargs:
            return

        # Read esri model definition (emd) file
        model = kwargs['model']
        model_as_file = True

        try:
            with open(model, 'r') as f:
                self.json_info = json.load(f)
        except FileNotFoundError:
            try:
                self.json_info = json.loads(model)
                model_as_file = False
            except json.decoder.JSONDecodeError:
                raise Exception("Invalid model argument")

        sys.path.append(os.path.dirname(__file__))

        framework = self.json_info['Framework']
        if 'ModelConfiguration' in self.json_info:
            modelconfig = self.json_info['ModelConfiguration']
            if isinstance(modelconfig, str):
                if modelconfig not in sys.modules:
                    ChildModelDetector = getattr(import_module(
                        '{}.{}'.format(framework, modelconfig)), 'ChildObjectDetector')
                else:
                    ChildModelDetector = getattr(reload(
                        '{}.{}'.format(framework, modelconfig)), 'ChildObjectDetector')
            else:
                modelconfig = self.json_info['ModelConfiguration']['Name']
                ChildModelDetector = getattr(importlib.import_module(
                    '{}.{}'.format(framework, modelconfig)), 'ChildObjectDetector')
        else:
            raise Exception("Invalid model configuration")

        # if 'device' in kwargs:
        #     device = kwargs['device']
        #     if device < -1:
        #         os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        #         device = prf_utils.get_available_device()
        #     os.environ['CUDA_VISIBLE_DEVICES'] = str(device)
        # else:
        #     os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

        device = None
        if 'device' in kwargs:
            device = kwargs['device']
            if device == -2:
                device = prf_utils.get_available_device()

        if device is not None:
            if device >= 0:
                os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
                os.environ['CUDA_VISIBLE_DEVICES'] = str(device)
                arcpy.env.processorType = "GPU"
                arcpy.env.gpuId = str(device)
            else:
                arcpy.env.processorType = "CPU"
        else:
            arcpy.env.processorType = "CPU"

        self.child_object_detector = ChildModelDetector()
        self.child_object_detector.initialize(model, model_as_file)