def val_dataset(record): image_id = record['ID'] gtboxes = record['gtboxes'] image_path = os.path.join(config.image_folder, image_id + '.jpg') img = cv2.imread(image_path, cv2.IMREAD_COLOR) gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) gt_boxes[:, 2:4] += gt_boxes[:, :2] return dict(data=img, boxes = gt_boxes, ID = image_id)
def load_record(self, record): if self.training: if_flap = np.random.randint(2) == 1 else: if_flap = False # image # image_path = os.path.join(self.config.image_folder, record['ID']+'.png') image_path = osp.join(self.config.image_folder, record['ID'] + '.png') image = load_img(image_path) # image_h = image.shape[0] # image_w = image.shape[1] image_h, image_w = image.shape[:2] if if_flap: image = cv2.flip(image, 1) if self.training: # ground_truth gtboxes = load_gt(record, 'gtboxes', 'fbox', self.config.class_names) keep = (gtboxes[:, 2]>=0) * (gtboxes[:, 3]>=0) gtboxes=gtboxes[keep, :] gtboxes[:, 2:4] += gtboxes[:, :2] if if_flap: gtboxes = flip_boxes(gtboxes, image_w) # im_info nr_gtboxes = gtboxes.shape[0] im_info = np.array([0, 0, 1, image_h, image_w, nr_gtboxes]) return image, gtboxes, im_info else: # image t_height, t_width, scale = target_size( image_h, image_w, self.short_size, self.max_size) # INTER_CUBIC, INTER_LINEAR, INTER_NEAREST, INTER_AREA, INTER_LANCZOS4 resized_image = cv2.resize(image, (t_width, t_height), interpolation=cv2.INTER_LINEAR) resized_image = resized_image.transpose(2, 0, 1) image = image.astype(np.float32) gtboxes = load_gt(record, 'gtboxes', 'fbox', self.config.class_names) gtboxes[:, 2:4] += gtboxes[:, :2] gtboxes = gtboxes.astype(np.float32) # im_info nr_gtboxes = gtboxes.shape[0] im_info = np.array([t_height, t_width, scale, image_h, image_w, nr_gtboxes]).astype(np.float32) return image, gtboxes, im_info, record['ID']
def val_dataset(record): image_id = record['ID'] gtboxes = record['gtboxes'] image_path = osp.join(config.image_folder, image_id + '.png') assert osp.exists(image_path) img = cv2.imread(image_path, cv2.IMREAD_COLOR) gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) gt_boxes[:, 2:4] += gt_boxes[:, :2] # batch_gtboxes = np.ones([config.max_boxes_of_image, 5]) # n = gt_boxes.shape[0] # batch_gtboxes[:n] = gt_boxes return dict(data=img, boxes = gt_boxes, ID = image_id)
def val_dataset(record): image_id = record['ID'] gtboxes = record['gtboxes'] image_path = os.path.join(config.image_folder, image_id + '.jpg') img = cv2.imread(image_path, cv2.IMREAD_COLOR) if img is None: image_path = os.path.join(config.image_folder, image_id + '.png') img = cv2.imread(image_path, cv2.IMREAD_COLOR) if img is None: raise '[!]Img not loaded correctly!' + image_path + '/jpg' gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) gt_boxes[:, 2:4] += gt_boxes[:, :2] return dict(data=img, boxes=gt_boxes, ID=image_id)
def train_dataset(seed=config.seed_dataprovider): root = config.image_folder source = config.train_source batch_per_gpu = config.batch_per_gpu short_size = config.train_image_short_size max_size = config.train_image_max_size records = misc_utils.load_json_lines(source) nr_files = len(records) print('training image number: {}'.format(nr_files)) np.random.seed(seed) np.random.shuffle(records) file_idx = 0 while file_idx < nr_files: batch_records = [] batch_images_list = [] hw_stat = np.zeros((batch_per_gpu, 2), np.int32) for i in range(batch_per_gpu): record = records[file_idx] batch_records.append(record) image_path = os.path.join(config.image_folder, record['ID'] + '.png') img = misc_utils.load_img(image_path) batch_images_list.append(img.copy()) hw_stat[i, :] = img.shape[:2] file_idx += 1 if file_idx >= nr_files: file_idx = 0 np.random.shuffle(records) batch_image_height = np.max(hw_stat[:, 0]) batch_image_width = np.max(hw_stat[:, 1]) is_batch_ok = True batch_resized_height, batch_resized_width = get_hw_by_short_size( batch_image_height, batch_image_width, short_size, max_size) batch_images = np.zeros((batch_per_gpu, 3, max_size, max_size), dtype=np.float32) batch_gts = np.zeros( (batch_per_gpu, config.max_boxes_of_image, config.nr_box_dim), dtype=np.float32) batch_info = np.zeros((batch_per_gpu, 6), dtype=np.float32) for i in range(batch_per_gpu): record = batch_records[i] img = batch_images_list[i] gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) keep = (gt_boxes[:, 2] >= 0) * (gt_boxes[:, 3] >= 0) gt_boxes = gt_boxes[keep, :] nr_gtboxes = gt_boxes.shape[0] if nr_gtboxes == 0: is_batch_ok = False break gt_boxes[:, 2:4] += gt_boxes[:, :2] padded_image = pad_image(img, batch_image_height, batch_image_width, config.image_mean) original_height, original_width, channels = padded_image.shape resized_image, scale = resize_img_by_short_and_max_size( padded_image, short_size, max_size) gt_boxes[:, 0:4] *= scale resized_gt = gt_boxes if np.random.randint(2) == 1: resized_image, resized_gt = flip_image_and_boxes( resized_image, resized_gt) resized_image = resized_image.transpose(2, 0, 1).astype(np.float32) batch_images[i, :, :int(resized_image.shape[1]), :int( resized_image.shape[2])] = resized_image batch_gts[i, :nr_gtboxes] = resized_gt batch_info[i, :] = (resized_image.shape[1], resized_image.shape[2], scale, original_height, original_width, nr_gtboxes) if not is_batch_ok: continue yield dict(data=batch_images, boxes=batch_gts, im_info=batch_info)
def eval_dataset(args): source = config.eval_source batch_per_gpu = config.train_batch_per_gpu short_size = config.train_image_short_size max_size = config.train_image_max_size #Custom records = load_coco_json_lines(source) nr_files = len(records) print('[!] Eval image number: {}'.format(nr_files)) yield nr_files file_idx = 0 while file_idx < nr_files: batch_records = [] batch_images_list = [] hw_stat = np.zeros((batch_per_gpu, 2), np.int32) done = False for i in range(batch_per_gpu): record = records[file_idx] batch_records.append(record) image_path = os.path.join(config.image_folder, record['ID'] + '.jpg') if not os.path.exists(image_path): image_path = os.path.join(config.image_folder, record['ID'] + '.png') img = misc_utils.load_img(image_path) batch_images_list.append(img.copy()) hw_stat[i, :] = img.shape[:2] if file_idx == 0: done = True file_idx += 1 if file_idx >= nr_files: file_idx = 0 batch_image_height = np.max(hw_stat[:, 0]) batch_image_width = np.max(hw_stat[:, 1]) is_batch_ok = True batch_resized_height, batch_resized_width = get_hw_by_short_size( batch_image_height, batch_image_width, short_size, max_size) batch_images = np.zeros((batch_per_gpu, 3, max_size, max_size), dtype=np.float32) batch_gts = np.zeros( (batch_per_gpu, config.max_boxes_of_image, config.nr_box_dim), dtype=np.float32) batch_info = np.zeros((batch_per_gpu, 6), dtype=np.float32) #[torch.Size([8, 256, 208, 304]), torch.Size([8, 256, 104, 152]), torch.Size([8, 256, 52, 76]), torch.Size([8, 256, 26, 38])] for i in range(batch_per_gpu): record = batch_records[i] img = batch_images_list[i] gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) keep = (gt_boxes[:, 2] >= 0) * (gt_boxes[:, 3] >= 0) gt_boxes = gt_boxes[keep, :] nr_gtboxes = gt_boxes.shape[0] if nr_gtboxes < 2: is_batch_ok = False break gt_boxes[:, 2:4] += gt_boxes[:, :2] padded_image = pad_image(img, batch_image_height, batch_image_width, config.image_mean) original_height, original_width, channels = padded_image.shape resized_image, scale = resize_img_by_short_and_max_size( padded_image, short_size, max_size) gt_boxes[:, 0:4] *= scale resized_gt = gt_boxes resized_image = resized_image.transpose(2, 0, 1).astype(np.float32) batch_images[i, :, :int(resized_image.shape[1]), :int( resized_image.shape[2])] = resized_image batch_gts[i, :nr_gtboxes] = resized_gt batch_info[i, :] = (resized_image.shape[1], resized_image.shape[2], scale, original_height, original_width, nr_gtboxes) if not is_batch_ok: continue results = dict(data=batch_images, boxes=batch_gts, im_info=batch_info, done=done) yield results
def train_dataset_debug(args, seed=config.seed_dataprovider): source = config.train_source batch_per_gpu = config.train_batch_per_gpu short_size = config.train_image_short_size max_size = config.train_image_max_size #Custom records = load_coco_json_lines(source) nr_files = len(records) print(records[0]) print('[!] Training image number: {}'.format(nr_files)) yield nr_files np.random.seed(seed) np.random.shuffle(records) file_idx = 0 while file_idx < nr_files: batch_records = [] batch_images_list = [] hw_stat = np.zeros((batch_per_gpu, 2), np.int32) done = False for i in range(batch_per_gpu): record = records[file_idx] batch_records.append(record) image_path = os.path.join(config.image_folder, record['ID'] + '.jpg') if not os.path.exists(image_path): image_path = os.path.join(config.image_folder, record['ID'] + '.png') img = misc_utils.load_img(image_path) batch_images_list.append(img.copy()) hw_stat[i, :] = img.shape[:2] if file_idx == 0: done = True file_idx += 1 if file_idx >= nr_files: file_idx = 0 np.random.shuffle(records) batch_image_height = np.max(hw_stat[:, 0]) batch_image_width = np.max(hw_stat[:, 1]) is_batch_ok = True batch_resized_height, batch_resized_width = get_hw_by_short_size( batch_image_height, batch_image_width, short_size, max_size) batch_images = np.zeros((batch_per_gpu, 3, max_size, max_size), dtype=np.float32) batch_gts = np.zeros( (batch_per_gpu, config.max_boxes_of_image, config.nr_box_dim), dtype=np.float32) batch_info = np.zeros((batch_per_gpu, 6), dtype=np.float32) for i in range(batch_per_gpu): record = batch_records[i] img = batch_images_list[i] img_ = np.uint8(img) # debugdir = '/home/felix_wu/debugarg/' # cv2.imwrite(debugdir + '/test{}.jpg'.format('A'), img_) # gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) keep = (gt_boxes[:, 2] >= 0) * (gt_boxes[:, 3] >= 0) gt_boxes = gt_boxes[keep, :] nr_gtboxes = gt_boxes.shape[0] if nr_gtboxes < 2: is_batch_ok = False break gt_boxes[:, 2:4] += gt_boxes[:, :2] padded_image = pad_image(img, batch_image_height, batch_image_width, config.image_mean) img_ = np.uint8(padded_image) # debugdir = '/home/felix_wu/debugarg/' # cv2.imwrite(debugdir + '/test{}.jpg'.format('B'), img_) # original_height, original_width, channels = padded_image.shape resized_image, scale = resize_img_by_short_and_max_size( padded_image, short_size, max_size) gt_boxes[:, 0:4] *= scale resized_gt = gt_boxes if np.random.randint(2) == 1: resized_image, resized_gt = flip_image_and_boxes( resized_image, resized_gt) resized_image = resized_image.transpose(2, 0, 1).astype(np.float32) batch_images[i, :, :int(resized_image.shape[1]), :int( resized_image.shape[2])] = resized_image batch_gts[i, :nr_gtboxes] = resized_gt batch_info[i, :] = (resized_image.shape[1], resized_image.shape[2], scale, original_height, original_width, nr_gtboxes) if not is_batch_ok: continue results = dict(data=batch_images, boxes=batch_gts, im_info=batch_info, done=done) yield results
def _train_dataset_sample(args, batch_images_list, batch_records, hw_stat, seed=config.seed_dataprovider): short_size = config.train_image_short_size max_size = config.train_image_max_size batch_per_gpu = config.train_batch_per_gpu batch_image_height = np.max(hw_stat[:, 0]) batch_image_width = np.max(hw_stat[:, 1]) is_batch_ok = True batch_resized_height, batch_resized_width = get_hw_by_short_size( batch_image_height, batch_image_width, short_size, max_size) batch_images = np.zeros((batch_per_gpu, 3, max_size, max_size), dtype=np.float32) if args.flip_aug: batch_aug_images = np.zeros((batch_per_gpu, 3, max_size, max_size), dtype=np.float32) batch_gts = np.zeros( (batch_per_gpu, config.max_boxes_of_image, config.nr_box_dim), dtype=np.float32) batch_info = np.zeros((batch_per_gpu, 6), dtype=np.float32) for i in range(batch_per_gpu): record = batch_records[i] img = batch_images_list[i] if args.flip_aug: if args.FA_heavy: aug_img = heavy_aug_seq(image=img) else: aug_img = aug_seq(image=img) gt_boxes = misc_utils.load_gt(record, 'gtboxes', 'fbox', config.class_names) keep = (gt_boxes[:, 2] >= 0) * (gt_boxes[:, 3] >= 0) gt_boxes = gt_boxes[keep, :] nr_gtboxes = gt_boxes.shape[0] if nr_gtboxes < 2: is_batch_ok = False break gt_boxes[:, 2:4] += gt_boxes[:, :2] padded_image = pad_image(img, batch_image_height, batch_image_width, config.image_mean) original_height, original_width, channels = padded_image.shape resized_image, scale = resize_img_by_short_and_max_size( padded_image, short_size, max_size) if args.flip_aug: padded_aug_img = pad_image(aug_img, batch_image_height, batch_image_width, config.image_mean) resized_aug_img, _ = resize_img_by_short_and_max_size( padded_aug_img, short_size, max_size) gt_boxes[:, 0:4] *= scale resized_gt = gt_boxes if np.random.randint(2) == 1: resized_image, resized_gt = flip_image_and_boxes( resized_image, resized_gt) if args.flip_aug: resized_aug_img, _ = flip_image_and_boxes( resized_aug_img, resized_gt.copy()) resized_image = resized_image.transpose(2, 0, 1).astype(np.float32) batch_images[i, :, :int(resized_image.shape[1]), :int( resized_image.shape[2])] = resized_image if args.flip_aug: resized_aug_img = resized_aug_img.transpose(2, 0, 1).astype(np.float32) batch_aug_images[i, :, :int(resized_image.shape[1]), :int( resized_image.shape[2])] = resized_aug_img batch_gts[i, :nr_gtboxes] = resized_gt batch_info[i, :] = (resized_image.shape[1], resized_image.shape[2], scale, original_height, original_width, nr_gtboxes) results = dict(data=batch_images, boxes=batch_gts, im_info=batch_info) if args.flip_aug: results['aug_data'] = batch_aug_images return is_batch_ok, results if is_batch_ok else None