Esempio n. 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
    def generate(self, mode="train"):
        """ 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,)
        """
        while (True):
            self.on_end_epoch()
            for index in range(self.__len__()):
                imgs, gt_confs_all, gt_locs_all = [], [], []
                for i in range(self.batch_size):
                    if (mode == 'train'):
                        sample = next(
                            tfds.as_numpy(
                                self.train_dataset.take(self.batch_size)))
                    else:
                        sample = next(
                            tfds.as_numpy(
                                self.val_dataset.take(self.batch_size)))
                    img = sample['image']
                    boxes = sample['objects']['bbox']
                    labels = sample['objects']['label']
                    # 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 + 1,
                                         dtype=tf.int64)  # 0 is background

                    # 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)
                    # print((self.input_shape, self.input_shape))
                    img = cv2.resize(img, (self.input_shape, self.input_shape))
                    # img = np.array(img.resize(
                    # (self.input_shape, self.input_shape)), dtype=np.float32)
                    img = (img / 127.0) - 1.0
                    img = tf.constant(img, dtype=tf.float32)
                    # if(len(boxes) == 0):
                    gt_confs, gt_locs = compute_target(self.default_boxes,
                                                       boxes, labels)
                    imgs.append(img)
                    gt_confs_all.append(gt_confs)
                    gt_locs_all.append(gt_locs)

                yield tf.stack(imgs), tf.stack(gt_confs_all), tf.stack(
                    gt_locs_all)
Esempio n. 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,)
        """

        # 만약 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
Esempio n. 4
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
Esempio n. 5
0
    def generate(self, split, coco, ids, num_examples, config):
        """
            num_examples : The number of examples to be used.
            It's used if you want to make model overfit a few examples
        """
        for image_id in ids:
            filename, image, original_size = self._get_image(image_id, coco)
            boxes, labels = self._get_annotation(image_id, coco)
            if len(labels) == 0:
                continue
            #assert image.shape[:2] == original_size[:2]
            height, width = original_size

            #normalize bounding box coord (range in 0~1.0)
            boxes = list(map(lambda box : (box[0]/width, box[1]/height, box[2]/width, box[3]/height), boxes))
            boxes = np.array(boxes, np.float32); labels = np.array(labels, np.float32);
            gt_confs, gt_locs = box_utils.compute_target(self.default_boxes, boxes, labels)
            image = cv2.resize(image, self.input_shape[:2], interpolation = cv2.INTER_AREA)

            if (config['inference_mode'] == 'mAP'):
                yield filename, image, labels, boxes
            else:
                yield filename, image, gt_confs, gt_locs
Esempio n. 6
0
    def __getitem__(self, index):
        """ The __getitem__ method
            so that the object can be iterable

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

        Returns:
            img: Torch tensor of shape (3, 300, 300)
            boxes: Torch tensor of shape (num_gt, 4)
            labels: Torch tensor of shape (num_gt,)
        """
        img, orig_shape = self._get_image(index)
        boxes, labels = self._get_annotation(index, orig_shape)
        boxes = torch.from_numpy(boxes)
        labels = torch.from_numpy(labels)

        if self.augmentation:
            if np.random.rand() > 0.5:
                img, boxes = self._random_flip(img, boxes)

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

        return img, gt_confs, gt_locs
Esempio n. 7
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
try:
    config = cfg['SSD300']  #[args.arch.upper()]
except AttributeError:
    pass
default_boxes = generate_default_boxes(config)

print(default_boxes)

import tensorflow_datasets as tfds

builder = tfds.builder('coco/' + '2017')
# 1. Create the tfrecord files (no-op if already exists)
builder.download_and_prepare()
# 2. Load the `tf.data.Dataset`
train_dataset = builder.as_dataset(split='train', shuffle_files=True)
val_dataset = builder.as_dataset(split='validation', shuffle_files=False)
info = builder.info
train_dataset = train_dataset

for i in range(10):
    sample = next(tfds.as_numpy(train_dataset.take(1)))
    print(sample['image/filename'])
    img = sample['image']
    boxes = sample['objects']['bbox']
    labels = sample['objects']['label']

    boxes = tf.constant(boxes, dtype=tf.float32)
    labels = tf.constant(labels, dtype=tf.int64)

    gt_confs, gt_locs = compute_target(default_boxes, boxes, labels)
Esempio n. 9
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