Beispiel #1
0
    def generate(self):
        """ The __getitem__ method
            so that the object can be iterable

        Args:
            index: the index to get filename from self.ids

        Returns:
            img: tensor of shape (300, 300, 3)
            boxes: tensor of shape (num_gt, 4)
            labels: tensor of shape (num_gt,)
        """
        for index in range(len(self.ids)):
            # img, orig_shape = self._get_image(index)
            img = self._get_image(index)
            w, h = img.size
            boxes, labels = self._get_annotation(index, (h, w))
            boxes = tf.constant(boxes, dtype=tf.float32)
            labels = tf.constant(labels, dtype=tf.int64)

            augmentation_method = np.random.choice(self.augmentation)
            if augmentation_method == 'patch':
                img, boxes, labels = random_patching(img, boxes, labels)
            elif augmentation_method == 'flip':
                img, boxes, labels = horizontal_flip(img, boxes, labels)

            img = np.array(img.resize((self.new_size, self.new_size)),
                           dtype=np.float32)
            img = (img / 127.0) - 1.0
            img = tf.constant(img, dtype=tf.float32)

            gt_confs, gt_locs = compute_target(self.default_boxes, boxes,
                                               labels)

            yield img, gt_confs, gt_locs
Beispiel #2
0
    def generate(self, subset=None):
        """ The __getitem__ method
            so that the object can be iterable

        Args:
            index: the index to get filename from self.ids

        Returns:
            img: tensor of shape (300, 300, 3)
            boxes: tensor of shape (num_gt, 4)
            labels: tensor of shape (num_gt,)
        """

        # 만약 Train인 경우 File적용 Test라면 Filp 적용 X
        if subset == 'train':
            indices = self.train_ids
            # 3. Random하게 Original Image를 사용할지 Flip을 실행할 Image를 사용할지 결정한다.
            augmentation_method = np.random.choice(self.augmentation)
            if augmentation_method == 'flip':
                img, boxes, labels = horizontal_flip(img, boxes, labels)

        elif subset == 'val':
            indices = self.val_ids
        else:
            indices = self.ids
        for index in range(len(indices)):
            # img, orig_shape = self._get_image(index)
            filename = indices[index]
            img = self._get_image(index)

            # 1. Input Image의 Size를 받는다.
            w, h = img.size

            # 2. get_annotation()을 통하여 Label과 Bounding Box의 Location을 입력받는다.
            boxes, labels = self._get_annotation(index, (h, w))
            boxes = tf.constant(boxes, dtype=tf.float32)
            labels = tf.constant(labels, dtype=tf.int64)

            # 4. Image의 Size를 Model Input에 맞게 (300,300)으로 바꾼뒤 0 ~ 1 사이의 값으로서 정규화를 한다.
            img = np.array(img.resize((self.new_size, self.new_size)),
                           dtype=np.float32)
            img = (img / 127.0) - 1.0
            img = tf.constant(img, dtype=tf.float32)

            # 5. Utils -> box_utils -> compute_target를 통하여 실제 Label을 Model에 맞는 Label로서 변경한다.
            gt_confs, gt_locs = compute_target(self.default_boxes, boxes,
                                               labels)

            # 6. Filename, Image, Ground Truth Label, Ground Truth Location을 반환한다
            # Generator로서 특정 Index후 다음 Index로 반환하기 위하여 Return 값을 yield로서 선언
            yield filename, img, gt_confs, gt_locs
Beispiel #3
0
    def generate(self, subset=None):
        """ The __getitem__ method
            so that the object can be iterable

        Args:
            index: the index to get filename from self.ids

        Returns:
            img: tensor of shape (300, 300, 3)
            boxes: tensor of shape (num_gt, 4)
            labels: tensor of shape (num_gt,)
        """
        if subset == 'train':
            indices = self.train_ids
        elif subset == 'val':
            indices = self.val_ids
        else:
            indices = self.ids
        for index in range(len(indices)):
            # img, orig_shape = self._get_image(index)
            filename = indices[index]
            img = self._get_image(index)
            w, h = img.size
            boxes, labels = self._get_annotation(index, (h, w))
            boxes = tf.constant(boxes, dtype=tf.float32)
            labels = tf.constant(labels, dtype=tf.int64)

            augmentation_method = np.random.choice(self.augmentation)
            if augmentation_method == 'patch':
                img, boxes, labels = random_patching(img, boxes, labels)
            elif augmentation_method == 'flip':
                img, boxes, labels = horizontal_flip(img, boxes, labels)

            img = np.array(img.resize((self.new_size, self.new_size)),
                           dtype=np.float32)
            img = np.repeat(img[:, :, np.newaxis], 3,
                            axis=2)  # transform grayscale into "forced" rgb
            img = (img / 127.0) - 1.0
            img = tf.constant(img, dtype=tf.float32)

            # print(tf.shape(img))

            gt_confs, gt_locs = compute_target(self.default_boxes, boxes,
                                               labels)

            yield filename, img, gt_confs, gt_locs
Beispiel #4
0
    def generate(self, subset=None, num=20000):
        """ The __getitem__ method
            so that the object can be iterable

        Args:
            index: the index to get filename from self.ids

        Returns:
            img: tensor of shape (300, 300, 3)
            boxes: tensor of shape (num_gt, 4)
            labels: tensor of shape (num_gt,)
        """
        if subset == 'train':
            indices = self.train_ids
        elif subset == 'val':
            indices = self.val_ids
        else:
            indices = self.ids
        indices = np.array(indices)
        counter = 0
        while (counter < num):
            counter = counter + 1
            itemp = np.random.randint(0, len(indices), size=[self.batch_size])
            # indexes = indices[itemp]
            # img, orig_shape = self._get_image(index)
            # filename = indices[index]
            imgs, gt_confs_all, gt_scores_all, gt_locs_all, o_imgs, o_boxes, o_labels = [], [],[], [], [], [], []
            for index in itemp:
                img = self._get_image(index)
                w, h = img.size
                boxes, labels = self._get_annotation(index, (h, w))
                boxes = tf.constant(boxes, dtype=tf.float32)
                labels = tf.constant(labels, dtype=tf.int64)

                augmentation_method = np.random.choice(self.augmentation)

                if augmentation_method == 'patch':
                    img, boxes, labels = random_patching(img, boxes, labels)
                    img = np.float32(img)
                elif augmentation_method == 'flip':
                    img, boxes, labels = horizontal_flip(
                        np.float32(img), boxes, labels)
                else:
                    img = np.array(img, dtype=np.float32)

                # print(img.shape, img.dtype)
                img_o = cv2.resize(img, (self.new_size, self.new_size))

                img = (img_o / 127.0) - 1.0
                img = tf.constant(img, dtype=tf.float32)

                gt_confs, gt_locs, gt_scores = compute_target(
                    self.default_boxes, boxes, labels)
                imgs.append(img)
                gt_confs_all.append(gt_confs)
                gt_locs_all.append(gt_locs)
                gt_scores_all.append(gt_scores)
                o_imgs.append(img_o)
                o_boxes.append(boxes)
                o_labels.append(labels)
                # print('1', img.dtype, gt_confs.dtype, gt_locs.dtype)
            imgs, gt_confs_all, gt_locs_all = tf.stack(imgs), tf.stack(
                gt_confs_all), tf.stack(gt_locs_all)
            # print('----', np.max(imgs.numpy()), np.max(gt_confs_all.numpy()), np.max(gt_locs_all))
            yield tf.stack(imgs), tf.stack(gt_confs_all), tf.stack(
                gt_locs_all), tf.stack(
                    gt_scores_all), o_imgs, o_boxes, o_labels
Beispiel #5
0
    def generate(self, num=20000):
        """ The __getitem__ method
            so that the object can be iterable

        Args:

        Returns:
            img: tensor of shape (300, 300, 3)
            boxes: tensor of shape (num_gt, 4)
            labels: tensor of shape (num_gt,)
        """
        counter = 0
        while (counter < num):
            counter = counter + 1
            imgs, gt_confs_all, gt_locs_all,gt_scores_all, o_imgs, o_boxes, o_labels = [], [],[], [], [], [], []
            for i in range(self.batch_size):
                img, boxes, labels, shape = None, None, None, None
                while (True):
                    self.global_index = self.global_index + 1
                    if (self.global_index == self.__len__()):
                        self.on_end_epoch()
                        self.global_index = 0
                    img, boxes, labels, shape = self.read_img(
                        self.global_index)
                    if (img is not None):
                        if (len(labels) > 0):
                            break

                # orig_shape = img.shape
                # # img, orig_shape = self._get_image(index)
                # filename = indices[index]
                # img = self._get_image(index)
                # w, h = img.size
                # boxes, labels = self._get_annotation(index, (h, w))
                boxes = tf.constant(boxes, dtype=tf.float32)
                labels = tf.constant(labels, dtype=tf.int64)

                augmentation_method = np.random.choice(self.augmentation)
                if augmentation_method == 'patch':
                    # print('patching')
                    img, boxes, labels = random_patching(img, boxes, labels)
                elif augmentation_method == 'flip':
                    # print('flipping')
                    img, boxes, labels = horizontal_flip(img, boxes, labels)
                # print((self.input_shape, self.input_shape))
                img = cv2.resize(img,
                                 (self.input_shape[0], self.input_shape[1]))
                # img = np.array(img.resize(
                # (self.input_shape, self.input_shape)), dtype=np.float32)
                o_imgs.append(img)
                img = img / 127.0 - 1
                img = tf.constant(img, dtype=tf.float32)
                # if(len(boxes) == 0):
                gt_confs, gt_locs, gt_scores = compute_target(
                    self.default_boxes, boxes, labels)
                # gt_confs, gt_locs, gt_scores = tf_ssd_bboxes_encode(labels, boxes, self.default_boxes, len(self.cat_names) + 1, None)

                imgs.append(img)
                gt_confs_all.append(gt_confs)
                gt_locs_all.append(gt_locs)
                gt_scores_all.append(gt_scores)
                o_boxes.append(boxes)
                o_labels.append(labels)

            yield tf.stack(imgs), tf.stack(gt_confs_all), tf.stack(
                gt_locs_all), tf.stack(
                    gt_scores_all), o_imgs, o_boxes, o_labels