Esempio n. 1
0
 def __rotate(self, arr, angle=0):
     if angle == 0:
         return arr
     elif angle == 90:
         arr = arr.transpose((0, 2, 1))
         return arr
     elif angle == 180:
         arr = nd.flip(arr, axis=1)
         return arr
     elif angle == 270:
         arr = arr.transpose((0, 2, 1))
         arr = nd.flip(arr, axis=1)
         return arr
Esempio n. 2
0
def image_preprocess2(img, crop_sz, expandid, blockid, cornerid, flipid):
    nd_img = nd.array(img)
    if len(args.rgb_mean) > 0:
        rgb_mean = [float(x) for x in args.rgb_mean.split(',')]
        rgb_mean = np.array(rgb_mean, dtype=np.float32).reshape(1, 1, 3)
        rgb_mean = nd.array(rgb_mean)
        nd_img -= rgb_mean
        nd_img *= 0.0078125
    #expand = 32
    #if crop_sz<300:
    #  expand = 16
    img_sz = crop_sz + args.expand * (expandid + 1)
    #img_sz = crop_sz+int(crop_sz/7)*(expandid+1)
    nd_img = mx.image.resize_short(nd_img, img_sz)
    if flipid == 1:
        nd_img = nd.flip(nd_img, axis=1)
    img = nd_img.asnumpy()
    h = img.shape[0]
    w = img.shape[1]
    block_size = min(h, w)
    blockh = 0
    blockw = 0
    if h > w:
        if blockid == 1:
            _half = int((h - w) / 2)
            blockh = _half
        elif blockid == 2:
            blockh = h - w
    else:
        if blockid == 1:
            _half = int((w - h) / 2)
            blockw = _half
        elif blockid == 2:
            blockw = w - h
    block = img[blockh:(blockh + block_size), blockw:(blockw + block_size), :]
    if cornerid == 5:
        img = cv2.resize(block, (crop_sz, crop_sz))
    else:
        if cornerid == 0:
            cornerh = int((block_size - crop_sz) / 2)
            cornerw = int((block_size - crop_sz) / 2)
        elif cornerid == 1:
            cornerh = 0
            cornerw = 0
        elif cornerid == 2:
            cornerh = 0
            cornerw = block_size - crop_sz
        elif cornerid == 3:
            cornerh = block_size - crop_sz
            cornerw = 0
        elif cornerid == 4:
            cornerh = block_size - crop_sz
            cornerw = block_size - crop_sz
        img = block[cornerh:(cornerh + crop_sz),
                    cornerw:(cornerw + crop_sz), :]

    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2)  # change to CHW
    #print(img.shape)
    return img
Esempio n. 3
0
def image_preprocess2(img, crop_sz, blockid, cornerid, flipid):
    nd_img = nd.array(img)
    expand = 32
    #if crop_sz<300:
    #  expand = 16
    img_sz = crop_sz + expand
    nd_img = mx.image.resize_short(nd_img, img_sz)
    if flipid == 1:
        nd_img = nd.flip(nd_img, axis=1)
    img = nd_img.asnumpy()
    h = img.shape[0]
    w = img.shape[1]
    block_size = min(h, w)
    blockh = 0
    blockw = 0
    if h > w:
        if blockid == 1:
            _half = int((h - w) / 2)
            blockh = _half
        elif blockid == 2:
            blockh = h - w
    else:
        if blockid == 1:
            _half = int((w - h) / 2)
            blockw = _half
        elif blockid == 2:
            blockw = w - h
    block = img[blockh:(blockh + block_size), blockw:(blockw + block_size), :]
    if cornerid == 0:
        cornerh = int((block_size - crop_sz) / 2)
        cornerw = int((block_size - crop_sz) / 2)
    elif cornerid == 1:
        cornerh = 0
        cornerw = 0
    elif cornerid == 2:
        cornerh = 0
        cornerw = block_size - crop_sz
    elif cornerid == 3:
        cornerh = block_size - crop_sz
        cornerw = 0
    elif cornerid == 4:
        cornerh = block_size - crop_sz
        cornerw = block_size - crop_sz

    img = block[cornerh:(cornerh + crop_sz), cornerw:(cornerw + crop_sz), :]

    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2)  # change to CHW
    #print(img.shape)
    return img
Esempio n. 4
0
def image_preprocess2(img, crop_sz):
    nd_img = nd.array(img)
    img_sz = crop_sz + random.randint(8, 32)
    if args.step == 0:
        img_sz = crop_sz + 32
    if img_sz > 0:
        nd_img = mx.image.resize_short(nd_img, img_sz)
    #nd_img = mx.image.random_size_crop(nd_img, (crop_sz, crop_sz), 0.08, (3.0/4, 4.0/3))[0]
    if args.step == 0:
        nd_img = mx.image.center_crop(nd_img, (crop_sz, crop_sz))[0]
    else:
        nd_img = mx.image.random_crop(nd_img, (int(
            (img_sz + crop_sz) / 2), int((img_sz + crop_sz) / 2)))[0]
        nd_img = mx.image.center_crop(nd_img, (crop_sz, crop_sz))[0]
        if random.random() < 0.5:
            nd_img = nd.flip(nd_img, axis=1)
    img = nd_img.asnumpy()
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2)  # change to CHW
    #print(img.shape)
    return img
Esempio n. 5
0
 def forward(self, inpt):
     fwd = self._lstm_fwd(inpt)
     bwd_inpt = nd.flip(inpt, 0)
     bwd = self._lstm_bwd(bwd_inpt)
     bwd = nd.flip(bwd, 0)
     return nd.concat(fwd, bwd, dim=2)
Esempio n. 6
0
def horizon_flip(image):
    return nd.flip(image, axis=1)
Esempio n. 7
0
def random_horizon_flip(image, p=0.5):
    if random.random() <= p:
        image = nd.flip(image, axis=1)

    return image