Пример #1
0
 def test_expected_time(self):
     d = PerfectDetector(self.train_dataset, self.train_dataset, 'dog')
     img = Image(500, 375, self.train_dataset.classes, 'test')
     print d.expected_time(img)
     assert(d.expected_time(img) == 10)
     img = Image(250, 375, self.train_dataset.classes, 'test')
     assert(d.expected_time(img) == 5)
Пример #2
0
 def test_get_random_windows(self):
     image = Image(width=3, height=2, classes=[], name='test')
     window_params = WindowParams(
         min_width=2, stride=1, scales=[1, 0.5], aspect_ratios=[0.5])
     windows = image.get_random_windows(window_params, 2)
     assert(windows.shape[0] == 2)
     windows = image.get_random_windows(window_params, 3)
     assert(windows.shape[0] == 3)
Пример #3
0
    def test_get_det_gt(self):
        image = Image.load_from_json(self.classes, self.data)
        objects_table = Table(np.array([
            [0, 0, 0, 0, 0, 0, 0],
            [1, 1, 1, 1, 1, 0, 0],
            [2, 2, 2, 2, 2, 0, 0]]), self.columns)
        assert(image.get_objects() == objects_table)

        data = self.data.copy()
        data['objects'][0]['diff'] = 1
        data['objects'][1]['trun'] = 1
        image = Image.load_from_json(self.classes, data)
        objects_table = Table(np.array([
            [0, 0, 0, 0, 0, 1, 0],
            [1, 1, 1, 1, 1, 0, 1],
            [2, 2, 2, 2, 2, 0, 0]]), self.columns)
        assert(image.get_objects(
            with_diff=True, with_trun=True) == objects_table)

        objects_table = Table(np.array([
            [1, 1, 1, 1, 1, 0, 1],
            [2, 2, 2, 2, 2, 0, 0]]), self.columns)
        assert(image.get_objects(
            with_diff=False, with_trun=True) == objects_table)

        # this should be default behavior
        assert(image.get_objects() == objects_table)

        objects_table = Table(np.array([
            [2, 2, 2, 2, 2, 0, 0]]), self.columns)
        assert(image.get_objects(
            with_diff=False, with_trun=False) == objects_table)

        objects_table = Table(np.array([
            [0, 0, 0, 0, 0, 1, 0],
            [2, 2, 2, 2, 2, 0, 0]]), self.columns)
        assert(image.get_objects(
            with_diff=True, with_trun=False) == objects_table)

        # What if everything is filtered out?
        data['objects'] = data['objects'][:-1]
        objects_table = Table(np.array([
            [0, 0, 0, 0, 0, 1, 0],
            [1, 1, 1, 1, 1, 0, 1]]), self.columns)
        image = Image.load_from_json(self.classes, data)
        assert(image.get_objects(
            with_diff=True, with_trun=True) == objects_table)
        assert(
            image.get_objects(with_diff=False, with_trun=False).shape[0] == 0)
Пример #4
0
 def test_get_windows_lots(self):
     t = time.time()
     image = Image(width=640, height=480, classes=[], name='test')
     window_params = WindowParams()
     window_params.min_width = 10
     window_params.stride = 8
     window_params.aspect_ratios = [0.5, 1, 1.5]
     window_params.scales = 1. / 2 ** np.array([0, 0.5, 1, 1.5, 2])
     print(window_params)
     windows = image.get_windows(window_params)
     time_passed = time.time() - t
     print("Generating windows took %.3f seconds" % time_passed)
     print(np.shape(windows))
     print(windows[:10, :])
     rand_ind = np.random.permutation(np.shape(windows)[0])[:10]
     print(windows[rand_ind, :])
Пример #5
0
    def load_from_pascal(self, name, force=False):
        """
        Load PASCAL VOC data, loading from cache if it exists at a canonical path.
        Both the PASCAL and cache paths should be set in self.config.
        After loading, saves the images and classes to cache.

        Uses Image class to load data from the PASCAL XML files.

        Args:
            name (string): in ['train','trainval','val','test']

            force (bool): [optional] if True, do not load from cache

        Returns:
            self

        Raises:
            self
        """
        tt = TicToc().tic()
        print("Dataset: %s" % self.get_name())

        filename = self.config.get_cached_dataset_filename(self.name)
        if os.path.exists(filename) and not force:
            with open(filename) as f:
                cached = cPickle.load(f)
                self.classes = cached['classes']
                self.images = cached['images']
                print("...loaded from cache in %.2f s" % tt.qtoc())

        else:
            print("...loading from scratch")
            self.classes = self.config.pascal_classes

            with open(self.config.get_pascal_path(name)) as f:
                imgset = [line.strip() for line in f.readlines() if len(line) > 0]

            images_dir = os.path.join(self.config.pascal_dir, "JPEGImages")
            for i, img in enumerate(imgset):
                tt.running(
                    'loading', "...on image {0}/{1}".format(i, len(imgset)))

                xml_filename = os.path.join(
                    self.config.pascal_dir, 'Annotations', img + '.xml')
                self.images.append(
                    Image.load_from_pascal_xml_filename(self.classes, xml_filename, images_dir))

            print("...saving to cache file")
            data = {'classes': self.classes, 'images': self.images}
            filename = self.config.get_cached_dataset_filename(self.name)
            with open(filename, 'w') as f:
                cPickle.dump(data, f)

            print("...done in %.2f s\n" % tt.qtoc())

        return self.after_load()
Пример #6
0
 def test_load_json_data(self):
     image = Image.load_from_json(self.classes, self.data)
     assert(image.width == 640 and image.height == 480)
     assert(image.classes == ['A', 'B', 'C'])
     assert(image.name == 'test_image')
     objects_table = Table(np.array([
         [0, 0, 0, 0, 0, 0, 0],
         [1, 1, 1, 1, 1, 0, 0],
         [2, 2, 2, 2, 2, 0, 0]]), self.columns)
     assert(image.objects_table == objects_table)
Пример #7
0
 def test_get_windows(self):
   image = Image(3,2,['whatever'],'test')
   window_params = WindowParams(
       min_width=2,stride=1,scales=[1,0.5],aspect_ratios=[0.5])
   windows = image.get_windows(window_params)
   correct = np.array(
       [[ 0., 0., 2., 2.],
        [ 0., 0., 3., 2.],
        [ 1., 0., 3., 2.],
        [ 2., 0., 2., 2.],
        [ 0., 1., 2., 2.],
        [ 0., 1., 3., 2.],
        [ 1., 1., 3., 2.],
        [ 2., 1., 2., 2.],
        [ 0., 0., 3., 3.],
        [ 0., 0., 4., 3.]])
   assert(windows.shape[0] > 0)
   print(windows)
   assert(np.all(correct == windows))
Пример #8
0
    def test_get_cls_counts_and_gt(self):
        data = self.data.copy()
        image = Image.load_from_json(self.classes, data)
        assert(np.all(image.get_cls_counts() == np.array([1, 1, 1])))
        assert(np.all(image.get_cls_gt() == np.array([True, True, True])))
        assert(image.contains_class('A'))
        assert(image.contains_class('B'))

        data['objects'][0]['class'] = 'B'
        image = Image.load_from_json(self.classes, data)
        # doesn't actually have to be Series, can be ndarray for comparison
        assert(np.all(image.get_cls_counts() == np.array([0, 2, 1])))
        assert(np.all(image.get_cls_gt() == np.array([False, True, True])))
        assert(not image.contains_class('A'))
        assert(image.contains_class('B'))

        data['objects'] = []
        image = Image.load_from_json(self.classes, data)
        assert(np.all(image.get_cls_counts() == np.array([0, 0, 0])))
        assert(np.all(image.get_cls_gt() == np.array([False, False, False])))
        assert(not image.contains_class('A'))
        assert(not image.contains_class('B'))
Пример #9
0
    def generate_synthetic(self):
        """
        Generate a synthetic dataset of 4 classes that follows some simple
        but strong cooccurence rules.

        Returns
        -------
        self
        """
        num_images = 1000
        choice_probs = {
            (1, 0, 0, 0): 2,
            (0, 1, 0, 0): 1,
            (0, 0, 1, 0): 1,
            (0, 0, 0, 1): 1,
            (1, 1, 0, 0): 2,
            (1, 0, 1, 0): 0,
            (1, 0, 0, 1): 0,
            (0, 1, 1, 0): 0,
            (0, 1, 0, 1): 1,
            (0, 0, 1, 1): 2,
            (1, 1, 1, 0): 0,
            (1, 1, 0, 1): 1,
            (1, 0, 1, 1): 1,
            (0, 1, 1, 1): 2}
        probs = np.array(choice_probs.values())
        cum_probs = np.cumsum(1. * probs / np.sum(probs))
        self.classes = ['A', 'B', 'C', 'D']
        for i in range(0, num_images):
            image = Image(100, 100, self.classes, str(i))
            choice = np.where(cum_probs > np.random.rand())[0][0]
            objects = []
            for cls_ind, clas in enumerate(choice_probs.keys()[choice]):
                if clas == 1:
                    objects.append(np.array([0, 0, 0, 0, cls_ind, 0, 0]))
            image.objects_table = Table(np.array(objects), Image.columns)
            self.images.append(image)

        return self.after_load()
Пример #10
0
    def load_from_json(self, filename):
        """
        Load all parameters of the dataset from a JSON file.

        Parameters
        ----------
        filename: string
            path to JSON data file

        Returns
        -------
        self
        """
        with open(filename) as f:
            json_data = json.load(f)
        self.classes = json_data['classes']
        self.images = [Image.load_from_json(
            self.classes, image) for image in json_data['images']]
        return self.after_load()
Пример #11
0
 def test_get_whole_image_bbox(self):
     image = Image(20, 10, [], 'test_image')
     assert(image.get_whole_image_bbox() == BoundingBox((0, 0, 20, 10)))
     image = Image(2, 100, [], 'test_image')
     assert(image.get_whole_image_bbox() == BoundingBox((0, 0, 2, 100)))