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, 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
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
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