def prepare_single(img, frame_id, scale, flip, proposal=None):
     ori_shape = img.shape
     _img, img_shape, pad_shape, scale_factor = self.img_transform(
         img, scale, flip, keep_ratio=self.resize_keep_ratio)
     _img = to_tensor(_img)
     ori_shape = img.shape
     _img_meta = dict(ori_shape=ori_shape,
                      img_shape=img_shape,
                      pad_shape=pad_shape,
                      is_first=(frame_id == 0),
                      video_id=vid,
                      frame_id=frame_id,
                      scale_factor=scale_factor,
                      flip=flip)
     if proposal is not None:
         if proposal.shape[1] == 5:
             score = proposal[:, 4, None]
             proposal = proposal[:, :4]
         else:
             score = None
         _proposal = self.bbox_transform(proposal, img_shape,
                                         scale_factor, flip)
         _proposal = np.hstack([_proposal, score
                                ]) if score is not None else _proposal
         _proposal = to_tensor(_proposal)
     else:
         _proposal = None
     return _img, _img_meta, _proposal
def _prepare_data_3d_2scales(img_np, img_np_2, img_transform, cfg, device):
    ori_shape = (img_np.shape[0], img_np.shape[1], 3)
    ori_shape_2 = (img_np_2.shape[0], img_np_2.shape[1], 3)
    total_num_slices = img_np.shape[2]
    total_num_slices_2 = img_np_2.shape[2]

    # first image
    imgs = []
    for cur_slice in range(total_num_slices):
        img = img_np[:,:,cur_slice]
        img = Image.fromarray(img).convert('RGB')
        img = np.array(img)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img, img_shape, pad_shape, scale_factor = img_transform(
            img,
            scale=cfg.data.test.img_scale,
            keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
        imgs.append(img)

    imgs = to_tensor(np.array(imgs)).to(device).unsqueeze(0)
    img_meta = [
        dict(
            ori_shape=ori_shape,
            img_shape=(*img_shape, total_num_slices),
            pad_shape=(*pad_shape, total_num_slices),
            # scale_factor=1.0 / (img_np_2.shape[0] / img_np.shape[0]), # scale up to 1.5x
            scale_factor=1.0, # scale down 1.0x
            flip=False)
    ]
    imgs = imgs.permute(0, 2, 1, 3, 4)

    # second image
    imgs_2 = []
    for cur_slice in range(total_num_slices_2):
        img = img_np_2[:,:,cur_slice]
        img = Image.fromarray(img).convert('RGB')
        img = np.array(img)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img, img_shape, pad_shape, scale_factor = img_transform(
            img,
            scale=cfg.data2_2scales.test.img_scale,
            keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
        imgs_2.append(img)

    imgs_2 = to_tensor(np.array(imgs_2)).to(device).unsqueeze(0)
    img_meta_2 = [
        dict(
            ori_shape=ori_shape_2,
            img_shape=(*img_shape, total_num_slices_2),
            pad_shape=(*pad_shape, total_num_slices_2),
            # scale_factor=scale_factor, # scale up to 1.5x
            scale_factor=1.5, # scale down 1.0x
            flip=False)
    ]
    imgs_2 = imgs_2.permute(0, 2, 1, 3, 4)

    assert imgs.shape[1] == 3 # make sure channel size is 3
    assert imgs_2.shape[1] == 3
    return dict(imgs=imgs, img_meta=[img_meta], imgs_2=imgs_2, img_meta_2=[img_meta_2])
예제 #3
0
    def parepare_img(self, img):
        if self.new_image:
            cfg = self.model.cfg
            img_transform = ImageTransform(
                size_divisor=cfg.data.test.size_divisor, **cfg.img_norm_cfg)

            device = next(self.model.parameters()).device
            img = mmcv.imread(img)

            ori_shape = img.shape
            img, img_shape, pad_shape, scale_factor = img_transform(
                img,
                scale=cfg.data.test.img_scale,
                keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
            img = to_tensor(img).to(device).unsqueeze(0)
            img_meta = [
                dict(
                    ori_shape=ori_shape,
                    img_shape=img_shape,
                    pad_shape=pad_shape,
                    scale_factor=scale_factor,
                    flip=False)
            ]
            self.img=img
            self.img_meta=img_meta
def _prepare_data_3d(img_np, img_transform, cfg, device):
    ori_shape = (img_np.shape[0], img_np.shape[1], 3)
    total_num_slices = img_np.shape[2]
    imgs = []
    for cur_slice in range(total_num_slices):
        img = img_np[:,:,cur_slice]
        img = Image.fromarray(img).convert('RGB')
        img = np.array(img)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img, img_shape, pad_shape, scale_factor = img_transform(
            img,
            scale=cfg.data.test.img_scale,
            keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
        imgs.append(img)

    imgs = to_tensor(np.array(imgs)).to(device).unsqueeze(0)
    img_meta = [
        dict(
            ori_shape=ori_shape,
            img_shape=(*img_shape, total_num_slices),
            pad_shape=(*pad_shape, total_num_slices),
            scale_factor=scale_factor,
            flip=False)
    ]
    imgs = imgs.permute(0, 2, 1, 3, 4)
    assert imgs.shape[1] == 3 # make sure channel size is 3
    return dict(imgs=imgs, img_meta=[img_meta])
예제 #5
0
def _prepare_vos_data(img, img_refer, img_transform, cfg, device):
    ori_shape = img.shape
    img, img_shape, pad_shape, scale_factor = img_transform(
        img,
        scale=cfg.data.test.img_scale,
        keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
    img = to_tensor(img).to(device).unsqueeze(0)
    img_meta = [
        dict(ori_shape=ori_shape,
             img_shape=img_shape,
             pad_shape=pad_shape,
             scale_factor=scale_factor,
             flip=False)
    ]
    img_refer = torch.unsqueeze(to_tensor(
        mmcv.imresize(np.float32(img_refer),
                      cfg.data.test.refer_scale,
                      return_scale=False)).permute(2, 0, 1),
                                dim=0).to(device)

    return dict(img=[img], img_meta=[img_meta], img_refer=[img_refer])
예제 #6
0
def _prepare_data(img, img_transform, cfg, device):
    ori_shape = img.shape
    img, img_shape, pad_shape, scale_factor = img_transform(
        img, scale=cfg.data.test.img_scale)
    img = to_tensor(img).to(device).unsqueeze(0)
    img_meta = [
        dict(ori_shape=ori_shape,
             img_shape=img_shape,
             pad_shape=pad_shape,
             scale_factor=scale_factor,
             flip=False)
    ]
    return dict(img=[img], img_meta=[img_meta])
def _prepare_single_data(img, img_transform, scale, cfg, device):
    ori_shape = img.shape
    print(ori_shape)
    img, img_shape, pad_shape, scale_factor = img_transform(
        img,
        scale=scale,
        keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
    img = to_tensor(img).to(device).unsqueeze(0)
    img_meta = dict(ori_shape=ori_shape,
                    img_shape=img_shape,
                    pad_shape=pad_shape,
                    scale_factor=scale_factor,
                    flip=False)
    return img, img_meta
예제 #8
0
def _prepare_data(img, img_transform, cfg, device):
    ori_shape = img.shape
    img, img_shape, pad_shape, scale_factor = img_transform(
        img,
        scale=cfg.data.test.img_scale,
        keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
    img = to_tensor(img).to(device).unsqueeze(0)
    img_metas = [
        dict(ori_shape=ori_shape,
             img_shape=img_shape,
             pad_shape=pad_shape,
             scale_factor=scale_factor,
             flip=False)
    ]
    return dict(img=[img], img_metas=[img_metas])
예제 #9
0
def _prepare_data(img, img_transform, cfg, device):
    ori_shape = img.shape
    # 执行img_transform实例的call方法,直接调用
    # 进行图片的归一化/翻转等增强
    img, img_shape, pad_shape, scale_factor = img_transform(
        img,
        scale=cfg.data.test.img_scale,
        keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
    img = to_tensor(img).to(device).unsqueeze(0)
    # 写入img_meta相关信息
    img_meta = [
        dict(ori_shape=ori_shape,
             img_shape=img_shape,
             pad_shape=pad_shape,
             scale_factor=scale_factor,
             flip=False)
    ]
    return dict(img=[img], img_meta=[img_meta])
예제 #10
0
def _prepare_data(img, img_transform, cfg, flip, trans_scale, device):
    if flip: img = cv2.flip(img, 1)
    ori_shape = img.shape
    height, width = img.shape[:2]
    transform_scale = trans_scale
    img, img_shape, pad_shape, scale_factor = img_transform(
        img,
        scale=transform_scale,
        keep_ratio=cfg.data.test.get('resize_keep_ratio', True))
    img = to_tensor(img).to(device).unsqueeze(0)
    img_meta = [
        dict(
            ori_shape=ori_shape,
            img_shape=img_shape,
            pad_shape=pad_shape,
            scale_factor=scale_factor,
            flip=False)
    ]
    # print(img_meta)
    return dict(img=[img], img_meta=[img_meta])