Example #1
0
def augment(img, split):
    # resize input
    height, width = img.shape[0], img.shape[1]
    center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
    scale = max(img.shape[0], img.shape[1]) * 1.0
    if not isinstance(scale, np.ndarray) and not isinstance(scale, list):
        scale = np.array([scale, scale], dtype=np.float32)

    if split != 'train':
        center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
        scale = max(width, height) * 1.0
        scale = np.array([scale, scale])
        x = 32
        input_w, input_h = int((width / 1. + x - 1) // x * x), int((height / 1. + x - 1) // x * x)

    trans_input = data_utils.get_affine_transform(center, scale, 0, [input_w, input_h])
    inp = cv2.warpAffine(img, trans_input, (input_w, input_h), flags=cv2.INTER_LINEAR)

    # color augmentation
    orig_img = inp.copy()
    inp = (inp.astype(np.float32) / 255.)
    if split == 'train':
        data_utils.color_aug(_data_rng, inp, _eig_val, _eig_vec)

    # normalize the image
    inp = (inp - mean) / std
    inp = inp.transpose(2, 0, 1)

    output_h, output_w = input_h // tless_config.down_ratio, input_w // tless_config.down_ratio
    trans_output = data_utils.get_affine_transform(center, scale, 0, [output_w, output_h])
    inp_out_hw = (input_h, input_w, output_h, output_w)

    return orig_img, inp, trans_input, trans_output, center, scale, inp_out_hw
Example #2
0
def augment(img, split, _data_rng, _eig_val, _eig_vec, mean, std, polys=None):
    # resize input
    height, width = img.shape[0], img.shape[1]
    center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
    scale = max(img.shape[0], img.shape[1]) * 1.0
    if not isinstance(scale, np.ndarray) and not isinstance(scale, list):
        scale = np.array([scale, scale], dtype=np.float32)

    # random crop and flip augmentation
    flipped = False
    if split == 'train':
        scale = scale * np.random.uniform(0.6, 1.4)
        x, y = center
        w_border = data_utils.get_border(width / 4, scale[0]) + 1
        h_border = data_utils.get_border(height / 4, scale[0]) + 1
        center[0] = np.random.randint(low=max(x - w_border, 0),
                                      high=min(x + w_border, width - 1))
        center[1] = np.random.randint(low=max(y - h_border, 0),
                                      high=min(y + h_border, height - 1))

        # flip augmentation
        if np.random.random() < 0.5:
            flipped = True
            img = img[:, ::-1, :]
            center[0] = width - center[0] - 1

    input_h, input_w = snake_config.voc_input_h, snake_config.voc_input_w
    if split != 'train':
        center = np.array([img.shape[1] / 2., img.shape[0] / 2.],
                          dtype=np.float32)
        scale = max(width, height) * 1.0
        scale = np.array([scale, scale])
        x = 32
        input_w, input_h = 512, 512
        # input_w, input_h = (width + x - 1) // x * x, (height + x - 1) // x * x

    trans_input = data_utils.get_affine_transform(center, scale, 0,
                                                  [input_w, input_h])
    inp = cv2.warpAffine(img,
                         trans_input, (input_w, input_h),
                         flags=cv2.INTER_LINEAR)

    # color augmentation
    orig_img = inp.copy()
    inp = (inp.astype(np.float32) / 255.)
    if split == 'train':
        data_utils.color_aug(_data_rng, inp, _eig_val, _eig_vec)
        # blur_aug(inp)

    # normalize the image
    inp = (inp - mean) / std
    inp = inp.transpose(2, 0, 1)

    output_h, output_w = input_h // snake_config.down_ratio, input_w // snake_config.down_ratio
    trans_output = data_utils.get_affine_transform(center, scale, 0,
                                                   [output_w, output_h])
    inp_out_hw = (input_h, input_w, output_h, output_w)

    return orig_img, inp, trans_input, trans_output, flipped, center, scale, inp_out_hw
Example #3
0
def augment(img, split, _data_rng, _eig_val, _eig_vec, mean, std, polys):
    # resize input
    height, width = img.shape[0], img.shape[1]
    center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
    scale = snake_config.scale
    if not isinstance(scale, np.ndarray) and not isinstance(scale, list):
        scale = np.array([scale, scale], dtype=np.float32)

    # random crop and flip augmentation
    flipped = False
    if split == 'train':
        scale = scale * np.random.uniform(0.4, 1.6)
        seed = np.random.randint(0, len(polys))
        index = np.random.randint(0, len(polys[seed]))
        poly = polys[seed][index]['poly']
        center[0], center[1] = poly[np.random.randint(len(poly))]
        border = scale[0] // 2 if scale[0] < width else width - scale[0] // 2
        center[0] = np.clip(center[0], a_min=border, a_max=width - border)
        border = scale[1] // 2 if scale[1] < height else height - scale[1] // 2
        center[1] = np.clip(center[1], a_min=border, a_max=height - border)

        # flip augmentation
        if np.random.random() < 0.5:
            flipped = True
            img = img[:, ::-1, :]
            center[0] = width - center[0] - 1

    input_w, input_h = snake_config.input_w, snake_config.input_h
    if split != 'train':
        center = np.array([width // 2, height // 2])
        scale = np.array([width, height])
        # input_w, input_h = width, height
        input_w, input_h = int((width / 0.85 + 31) // 32 * 32), int(
            (height / 0.85 + 31) // 32 * 32)

    trans_input = data_utils.get_affine_transform(center, scale, 0,
                                                  [input_w, input_h])
    inp = cv2.warpAffine(img,
                         trans_input, (input_w, input_h),
                         flags=cv2.INTER_LINEAR)

    # color augmentation
    orig_img = inp.copy()
    inp = (inp.astype(np.float32) / 255.)
    if split == 'train':
        data_utils.color_aug(_data_rng, inp, _eig_val, _eig_vec)
        # data_utils.blur_aug(inp)

    # normalize the image
    inp = (inp - mean) / std
    inp = inp.transpose(2, 0, 1)

    output_h, output_w = input_h // snake_config.down_ratio, input_w // snake_config.down_ratio
    trans_output = data_utils.get_affine_transform(center, scale, 0,
                                                   [output_w, output_h])
    inp_out_hw = (input_h, input_w, output_h, output_w)

    return orig_img, inp, trans_input, trans_output, flipped, center, scale, inp_out_hw
Example #4
0
def augment(img, split):
    # resize input
    height, width = img.shape[0], img.shape[1]
    center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
    scale = max(img.shape[0], img.shape[1]) * 1.0
    if not isinstance(scale, np.ndarray) and not isinstance(scale, list):
        scale = np.array([scale, scale], dtype=np.float32)

    if split == 'train':
        scale = scale * np.random.uniform(0.6, 1.4)
        center = np.array([0, 0])

        border = scale[0] // 2 if scale[0] < width else width - scale[0] // 2
        border_r = max(width - border, border + 1)
        center[0] = np.random.randint(border, border_r)

        border = scale[1] // 2 if scale[1] < height else height - scale[1] // 2
        border_r = max(height - border, border + 1)
        center[1] = np.random.randint(border, border_r)
        input_w, input_h = input_scale

    if split != 'train':
        center = np.array([img.shape[1] / 2., img.shape[0] / 2.],
                          dtype=np.float32)
        scale = max(width, height) * 1.0
        scale = np.array([scale, scale])
        x = 32
        input_w, input_h = (width + x - 1) // x * x, (height + x - 1) // x * x

    trans_input = data_utils.get_affine_transform(center, scale, 0,
                                                  [input_w, input_h])
    inp = cv2.warpAffine(img,
                         trans_input, (input_w, input_h),
                         flags=cv2.INTER_LINEAR)

    # color augmentation
    orig_img = inp.copy()
    inp = (inp.astype(np.float32) / 255.)
    if split == 'train':
        data_utils.color_aug(_data_rng, inp, _eig_val, _eig_vec)

    # normalize the image
    inp = (inp - mean) / std
    inp = inp.transpose(2, 0, 1)

    output_h, output_w = input_h // tless_config.down_ratio, input_w // tless_config.down_ratio
    trans_output = data_utils.get_affine_transform(center, scale, 0,
                                                   [output_w, output_h])
    inp_out_hw = (input_h, input_w, output_h, output_w)

    return orig_img, inp, trans_input, trans_output, center, scale, inp_out_hw
Example #5
0
def augment(img, split, down_ratio, _data_rng, _eig_val, _eig_vec, mean, std):
    # resize input
    height, width = img.shape[0], img.shape[1]
    center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
    scale = max(img.shape[0], img.shape[1]) * 1.0

    # random crop and flip augmentation
    flipped = False
    if split == 'train':
        scale = scale * np.random.choice(np.arange(0.6, 1.4, 0.1))
        w_border = get_border(128, img.shape[1])
        h_border = get_border(128, img.shape[0])
        center[0] = np.random.randint(low=w_border,
                                      high=img.shape[1] - w_border)
        center[1] = np.random.randint(low=h_border,
                                      high=img.shape[0] - h_border)

        # flip augmentation
        if np.random.random() < 0.5:
            flipped = True
            img = img[:, ::-1, :]
            center[0] = width - center[0] - 1

    input_h, input_w = (512, 512)
    trans_input = get_affine_transform(center, scale, 0, [input_w, input_h])
    inp = cv2.warpAffine(img,
                         trans_input, (input_w, input_h),
                         flags=cv2.INTER_LINEAR)

    # color augmentation
    orig_img = inp.copy()
    inp = (inp.astype(np.float32) / 255.)
    if split == 'train':
        color_aug(_data_rng, inp, _eig_val, _eig_vec)

    # normalize the image
    inp = (inp - mean) / std
    inp = inp.transpose(2, 0, 1)

    # resize output
    output_h = input_h // down_ratio
    output_w = input_w // down_ratio
    trans_output = get_affine_transform(center, scale, 0, [output_w, output_h])

    return orig_img, inp, trans_input, trans_output, input_h, input_w, output_h, output_w, flipped
Example #6
0
def augment(img,
            split,
            down_ratio,
            _data_rng,
            _eig_val,
            _eig_vec,
            mean,
            std,
            polys,
            boxes=None,
            label=None):
    # resize input
    height, width = img.shape[0], img.shape[1]
    center = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
    scale = max(img.shape[0], img.shape[1]) * 1.0
    scale = 800
    # __import__('ipdb').set_trace()
    # random crop and flip augmentation
    flipped = False
    if cfg.small_num > 0:
        img, polys, boxes, label = small_aug(img, polys, boxes, label,
                                             cfg.small_num)
    if split == 'train':
        scale = scale * np.random.choice(np.arange(0.6, 1.4, 0.1))
        seed = np.random.randint(0, len(polys))
        index = np.random.randint(0, len(polys[seed]))
        x = polys[seed][index]['bbox'][0] + (polys[seed][index]['bbox'][2] -
                                             1) / 2
        y = polys[seed][index]['bbox'][1] + (polys[seed][index]['bbox'][3] -
                                             1) / 2
        w_border = get_border(200, scale)
        h_border = get_border(200, scale)
        if (w_border == 0) or (h_border == 0):
            center[0] = x
            center[1] = y
        else:
            center[0] = np.random.randint(low=max(x - w_border, 0),
                                          high=min(x + w_border, width - 1))
            center[1] = np.random.randint(low=max(y - h_border, 0),
                                          high=min(y + h_border, height - 1))

        # flip augmentation
        if np.random.random() < 0.5:
            flipped = True
            img = img[:, ::-1, :]
            center[0] = width - center[0] - 1

    input_h, input_w = (800, 800)
    if split == 'val':
        center = np.array([1024, 512])
        scale = [2048, 1024]
        input_h, input_w = (1024, 2048)

    # print(center,scale)
    # print(flipped)
    # center = np.array([1272., 718.])
    # scale = 358.4
    # import ipdb; ipdb.set_trace()
    # center = np.array([1583., 306.])
    # print(center)
    # scale = 358.4
    # print(center, scale)
    trans_input = get_affine_transform(center, scale, 0, [input_w, input_h])
    inp = cv2.warpAffine(img,
                         trans_input, (input_w, input_h),
                         flags=cv2.INTER_LINEAR)

    # color augmentation
    orig_img = inp.copy()
    inp = (inp.astype(np.float32) / 255.)
    if split == 'train':
        color_aug(_data_rng, inp, _eig_val, _eig_vec)
        # blur_aug(inp)

    # normalize the image
    inp = (inp - mean) / std
    inp = inp.transpose(2, 0, 1)

    # resize output
    # if split == 'train':
    output_h = input_h // down_ratio
    output_w = input_w // down_ratio
    trans_output = get_affine_transform(center, scale, 0, [output_w, output_h])

    return orig_img, inp, trans_input, trans_output, input_h, input_w, output_h, output_w, flipped, center, scale, \
           polys, boxes, label