Exemplo n.º 1
0
 def test_detect(self):
     modules.load_known_modules()
     detector = ImageObjectDetector.create("example_detector")
     image = Image()
     image_container = ImageContainer(image)
     detections = detector.detect(image_container)
     nose.tools.ok_(detections is not None,
                    "Unexpected empty detections" )
     nose.tools.assert_equal(len(detections), 1)
Exemplo n.º 2
0
 def test_config(self):
     modules.load_known_modules()
     detector = ImageObjectDetector.create("example_detector")
     # Verify that 6 config values are present in example_detector
     nose.tools.assert_equal(len(detector.get_configuration()), 6)
     test_cfg = _dummy_detector_cfg()
     # Verify that the detector has different configuration before setting to test
     nose.tools.assert_not_equal(detector.check_configuration(test_cfg), False)
     detector.set_configuration(test_cfg)
     # Verify that the config value is being set properly
     nose.tools.assert_equal(detector.check_configuration(test_cfg), True)
Exemplo n.º 3
0
    def test_nested_config(self):
        modules.load_known_modules()
        detector = ImageObjectDetector.create("example_detector")
        nested_cfg = config.empty_config()
        ImageObjectDetector.get_nested_algo_configuration( "detector",
                                                            nested_cfg,
                                                            detector )
        # Verify that test cfg is set to configuration inside detector
        # nested configuration uses the name of a detector as an additional configuration
        # key thus it is checked against 7 rather than 6
        #nose.tools.assert_equal(len(nested_cfg), 7)

        #test_cfg = _dummy_detector_cfg()
        #test_cfg.set_value("example_detector:type", "example_detector")
        #ImageObjectDetector.set_nested_algo_configuration( "example_detector",
        #                                    test_cfg,
        #                                    detector )
        nose.tools.assert_equal(ImageObjectDetector.check_nested_algo_configuration(
                                                            "detector",
                                                            nested_cfg), True)
Exemplo n.º 4
0
 def test_bad_detect(self):
     modules.load_known_modules()
     detector = ImageObjectDetector.create("example_detector")
     detector.detect("Image")
Exemplo n.º 5
0
 def test_bad_create(self):
     # Should fail to create an algorithm without a factory
     ImageObjectDetector.create("")
Exemplo n.º 6
0
    def set_configuration(self, cfg_in):
        cfg = self.get_configuration()
        cfg.merge_config(cfg_in)

        # Read configs from file
        self._identifier = str(cfg.get_value("identifier"))
        self._mode = str(cfg.get_value("mode"))
        self._arch = str(cfg.get_value("arch"))
        self._seed_model = str(cfg.get_value("seed_model"))
        self._train_directory = str(cfg.get_value("train_directory"))
        self._output_directory = str(cfg.get_value("output_directory"))
        self._output_prefix = str(cfg.get_value("output_prefix"))
        self._output_plots = strtobool(cfg.get_value("output_plots"))
        self._pipeline_template = str(cfg.get_value("pipeline_template"))
        self._gpu_count = int(cfg.get_value("gpu_count"))
        self._gt_frames_only = strtobool(cfg.get_value("gt_frames_only"))
        self._augmentation = str(cfg.get_value("augmentation"))
        self._chip_width = str(cfg.get_value("chip_width"))
        self._chip_overlap = str(cfg.get_value("chip_overlap"))
        self._chip_method = str(cfg.get_value("chip_method"))
        self._max_epochs = str(cfg.get_value("max_epochs"))
        self._batch_size = str(cfg.get_value("batch_size"))
        self._learning_rate = str(cfg.get_value("learning_rate"))
        self._timeout = str(cfg.get_value("timeout"))
        self._backbone = str(cfg.get_value("backbone"))
        self._pipeline_template = str(cfg.get_value("pipeline_template"))
        self._max_scale_wrt_chip = float(cfg.get_value("max_scale_wrt_chip"))
        self._no_format = strtobool(cfg.get_value("no_format"))
        self._allow_unicode = str(cfg.get_value("allow_unicode"))
        self._aux_image_labels = str(cfg.get_value("aux_image_labels"))
        self._aux_image_extensions = str(cfg.get_value("aux_image_extensions"))
        self._area_lower_bound = float(cfg.get_value("area_lower_bound"))
        self._area_upper_bound = float(cfg.get_value("area_upper_bound"))
        self._border_exclude = float(cfg.get_value("border_exclude"))
        self._detector_model = str(cfg.get_value("detector_model"))
        self._max_neg_per_frame = float(cfg.get_value("max_neg_per_frame"))
        self._negative_category = str(cfg.get_value("negative_category"))
        self._reduce_category = str(cfg.get_value("reduce_category"))
        self._scale_type_file = str(cfg.get_value("scale_type_file"))

        # Check GPU-related variables
        gpu_memory_available = 0
        gpu_param_adj = 1

        if torch.cuda.is_available():
            if self._gpu_count < 0:
                self._gpu_count = torch.cuda.device_count()
                gpu_param_adj = self._gpu_count
            for i in range(self._gpu_count):
                single_gpu_mem = torch.cuda.get_device_properties(
                    i).total_memory
                if gpu_memory_available == 0:
                    gpu_memory_available = single_gpu_mem
                else:
                    gpu_memory_available = min(gpu_memory_available,
                                               single_gpu_mem)

        if self._mode == "detector":
            if self._batch_size == "auto":
                if len(self._aux_image_labels) > 0:
                    if gpu_memory_available >= 22e9:
                        self._batch_size = str(2 * gpu_param_adj)
                    else:
                        self._batch_size = str(1 * gpu_param_adj)
                elif gpu_memory_available > 9.5e9:
                    self._batch_size = str(4 * gpu_param_adj)
                elif gpu_memory_available >= 7.5e9:
                    self._batch_size = str(3 * gpu_param_adj)
                elif gpu_memory_available >= 4.5e9:
                    self._batch_size = str(2 * gpu_param_adj)
                else:
                    self._batch_size = str(1 * gpu_param_adj)
            if self._learning_rate == "auto":
                self._learning_rate = str(1e-3)
        elif self._mode == "frame_classifier" or self._mode == "detection_refiner":
            if self._batch_size == "auto":
                if gpu_memory_available > 9.5e9:
                    self._batch_size = str(64 * gpu_param_adj)
                elif gpu_memory_available >= 7.5e9:
                    self._batch_size = str(32 * gpu_param_adj)
                elif gpu_memory_available >= 4.5e9:
                    self._batch_size = str(16 * gpu_param_adj)
                else:
                    self._batch_size = str(8 * gpu_param_adj)
            if self._learning_rate == "auto":
                self._learning_rate = str(5e-3)
        else:
            print("Invalid mode string " + self._mode)
            return False

        # Make required directories and file streams
        if self._train_directory is not None:
            if not os.path.exists(self._train_directory):
                os.mkdir(self._train_directory)
            self._training_file = os.path.join(self._train_directory,
                                               self._tmp_training_file)
            self._validation_file = os.path.join(self._train_directory,
                                                 self._tmp_validation_file)
            self._chip_directory = os.path.join(self._train_directory,
                                                "image_chips")
        else:
            self._training_file = self._tmp_training_file
            self._validation_file = self._tmp_validation_file

        if self._output_directory is not None:
            if not os.path.exists(self._output_directory):
                os.mkdir(self._output_directory)

        from kwiver.vital.modules import load_known_modules
        load_known_modules()

        if not self._no_format:
            self._training_writer = \
              DetectedObjectSetOutput.create( "coco" )
            self._validation_writer = \
              DetectedObjectSetOutput.create( "coco" )

            writer_conf = self._training_writer.get_configuration()
            writer_conf.set_value("aux_image_labels", self._aux_image_labels)
            writer_conf.set_value("aux_image_extensions",
                                  self._aux_image_extensions)
            self._training_writer.set_configuration(writer_conf)

            writer_conf = self._validation_writer.get_configuration()
            writer_conf.set_value("aux_image_labels", self._aux_image_labels)
            writer_conf.set_value("aux_image_extensions",
                                  self._aux_image_extensions)
            self._validation_writer.set_configuration(writer_conf)

            self._training_writer.open(self._training_file)
            self._validation_writer.open(self._validation_file)

        # Set default architecture if unset
        if not self._arch:
            if self._mode == "frame_classifier" or self._mode == "detection_refiner":
                self._arch = "resnet50"
            else:
                self._arch = "cascade"

        if self._mode == "detection_refiner" and not os.path.exists(
                self._chip_directory):
            os.mkdir(self._chip_directory)

        # Load object detector if enabled
        if self._detector_model:
            self._detector = ImageObjectDetector.create("netharn")
            detector_config = self._detector.get_configuration()
            detector_config.set_value("deployed", self._detector_model)
            if not self._detector.set_configuration(detector_config):
                print("Unable to configure detector")
                return False

        # Load scale based on type file if enabled
        self._target_type_scales = dict()
        if self._scale_type_file:
            fin = open(self._scale_type_file, 'r')
            for line in fin.readlines():
                line = line.rstrip()
                parsed_line = line.split()
                if len(parsed_line) < 1:
                    continue
                target_area = float(parsed_line[-1])
                type_str = str(' '.join(parsed_line[:-1]))
                self._target_type_scales[type_str] = target_area

        # Initialize persistent variables
        self._training_data = []
        self._validation_data = []
        self._sample_count = 0
        return True