Ejemplo n.º 1
0
def main():
    # initialization
    bot_img = cv2.imread('squirrel.jpg') / 255.0
    for i in range(256):
        for j in range(256):
            for k in range(3):
                bottom_image[i, j, k] = bot_img[i, j, k]
    target_img = cv2.imread('iclr2020.png')[:, :, 0] / 255.0
    target_img = cv2.resize(target_img, (n_grid, n_grid))
    target_img -= target_img.mean()
    cv2.imshow('target', target_img * amplify + 0.5)
    # print(target_img.min(), target_img.max())
    for i in range(n_grid):
        for j in range(n_grid):
            target[i, j] = float(target_img[i, j])

    initial[n_grid // 2, n_grid // 2] = 1
    # forward('water_renderer/initial')
    initial[n_grid // 2, n_grid // 2] = 0

    from adversarial import vgg_grad, predict

    for opt in range(10):
        with ti.Tape(loss):
            forward()

            feed_to_vgg = np.zeros((224, 224, 3), dtype=np.float32)
            # Note: do a transpose here
            for i in range(224):
                for j in range(224):
                    for k in range(3):
                        feed_to_vgg[i, j, k] = refracted_image[i + 16, j + 16,
                                                               2 - k]

            predict(feed_to_vgg)
            grad = vgg_grad(feed_to_vgg)
            for i in range(224):
                for j in range(224):
                    for k in range(3):
                        refracted_image.grad[i + 16, j + 16,
                                             k] = grad[i, j, 2 - k] * 0.001

        print('Iter', opt, ' Loss =', loss[None])

        apply_grad()

    forward('water_renderer/optimized')
Ejemplo n.º 2
0
def main():
    allocate_fields()
    # initialization
    bot_img = cv2.imread('squirrel.jpg') / 255.0
    for i in range(256):
        for j in range(256):
            for k in range(3):
                bottom_image[i, j, k] = bot_img[i, j, k]

    initial[n_grid // 2, n_grid // 2] = 1
    # forward('water_renderer/initial')
    initial[n_grid // 2, n_grid // 2] = 0

    from adversarial import vgg_grad, predict

    for opt in range(10):
        with ti.Tape(loss):
            forward()

            feed_to_vgg = np.zeros((224, 224, 3), dtype=np.float32)
            # Note: do a transpose here
            for i in range(224):
                for j in range(224):
                    for k in range(3):
                        feed_to_vgg[i, j, k] = refracted_image[i + 16, j + 16,
                                                               2 - k]

            predict(feed_to_vgg)
            grad = vgg_grad(feed_to_vgg)
            for i in range(224):
                for j in range(224):
                    for k in range(3):
                        refracted_image.grad[i + 16, j + 16,
                                             k] = grad[i, j, 2 - k] * 0.001

        print('Iter', opt)

        apply_grad()

    forward('water_renderer/optimized')