Exemplo n.º 1
0
def show_patches_on_frames(ims, locations_, scales_,
                           image_shape=(100, 100), patch_shape=(16, 16)):
    hyperparameters = {}
    hyperparameters["cutoff"] = 3
    hyperparameters["batched_window"] = True
    location = T.fmatrix()
    scale = T.fmatrix()
    x = T.fvector()
    cropper = LocallySoftRectangularCropper(
        patch_shape=patch_shape,
        hyperparameters=hyperparameters,
        kernel=Gaussian())
    patch = cropper.apply(
        x.reshape((1, 1,) + image_shape),
        np.array([list(image_shape)]),
        location,
        scale)
    get_patch = theano.function([x, location, scale], patch,
                                allow_input_downcast=True)
    final_shape = (image_shape[0], image_shape[0] + patch_shape[0] + 5)
    ret = np.ones((ims.shape[0], ) + final_shape + (3,), dtype=np.float32)
    for i in range(ims.shape[0]):
        im = ims[i]
        location_ = locations_[i]
        scale_ = scales_[i]
        patch_on_frame = show_patch_on_frame(im, location_, scale_)
        ret[i, :, :image_shape[1], :] = patch_on_frame
        ret[i, -patch_shape[0]:, image_shape[1] + 5:, :] = to_rgb1(
            get_patch(im, [location_], [scale_])[0, 0])
    return ret
Exemplo n.º 2
0
def apply_crop(image, image_shape, patch_shape, location, scale):
    from crop import LocallySoftRectangularCropper
    from crop import Gaussian
    hyperparameters = {}
    hyperparameters["cutoff"] = 3
    hyperparameters["batched_window"] = True
    cropper = LocallySoftRectangularCropper(
        patch_shape=patch_shape,
        hyperparameters=hyperparameters,
        kernel=Gaussian())
    patch = cropper.apply(image, image_shape, location, scale)
    return patch
Exemplo n.º 3
0
# tds, _ = get_cooking_streams(batch_size)
tds, _ = get_bmnist_streams(1)
res = tds.get_epoch_iterator(as_dict=True).next()['features']
# shape: 3 x 125 x 200
img = res[5, 0]
draw(img)
plt.savefig('img.png')

cropper = LocallySoftRectangularCropper(patch_shape=patch_shape,
                                        hyperparameters=hyperparameters,
                                        kernel=Gaussian())

patch1, matrix, dx2 = cropper.apply(
    x.reshape((
        batch_size,
        num_channel,
    ) + image_shape), np.array([list(image_shape)]), location, scale, alpha)
grads = T.grad(T.mean(patch1), x)
grad_scale = abs(T.grad(T.mean(patch1), scale))
grad_location = abs(T.grad(T.mean(patch1), location))
grad_alpha = abs(T.grad(T.mean(patch1), alpha))

f = theano.function(
    [x, location, scale, alpha],
    [patch1, grads, grad_scale + grad_location + grad_alpha, matrix, dx2],
    allow_input_downcast=True)

image = img.flatten().astype('float32')
locations = [[50, 50], [100, 70], [10, 190]]
scales = [[0.28, 0.28], [0.6, 0.6], [1, 1]]
Exemplo n.º 4
0
x = T.fvector()

patch_shape = (28, 28)
image_shape = (100, 100)
hyperparameters = {}
hyperparameters["cutoff"] = 3
hyperparameters["batched_window"] = True

cropper = LocallySoftRectangularCropper(
    patch_shape=patch_shape,
    hyperparameters=hyperparameters,
    kernel=Gaussian())

patch = cropper.apply(
    x.reshape((1, 1,) + image_shape),
    np.array([list(image_shape)]),
    location,
    scale)

f = theano.function([x, location, scale], patch, allow_input_downcast=True)

tds, vds = get_mnist_video_streams(1)
image = tds.get_epoch_iterator().next()[0][10, 0, :]

patch1 = f(image, [[60.0, 65.0]], [[1.0, 1.0]])[0, 0]
patch2 = f(image, [[60.0, 65.0]], [[2.0, 2.0]])[0, 0]
patch3 = f(image, [[50.0, 50.0]], [[0.28, 0.28]])[0, 0]

plt.imshow(
    image.reshape(image_shape),
    cmap=plt.gray(),
Exemplo n.º 5
0
tensor5 = T.TensorType(config.floatX, (False, ) * 4)
# shape: B x C x X x Y
input_ = tensor5('features')

cropper = LocallySoftRectangularCropper(patch_shape=(28, 28),
                                        hyperparameters={
                                            'cutoff': 3000,
                                            'batched_window': True
                                        },
                                        kernel=Gaussian())

down, W, dx2 = cropper.apply(
    input_, np.array([list((100, 100))]),
    T.constant(99 * [[80, 70]]).astype('float32'),
    T.constant(99 * [[0.28, 0.28]]).astype('float32'),
    T.constant(99 * [[
        0.001,
    ] * 2]).astype('float32'))

f = theano.function([input_], [down, W, dx2])

data = get_bmnist_streams(99)[0].get_epoch_iterator().next()
res = f(data[0][0])

print np.min(res[2][0], axis=0)
print np.sum(res[1][0], axis=0)
plt.imshow(res[1][0], interpolation='nearest')
plt.savefig('w.png')

import ipdb
Exemplo n.º 6
0
tensor5 = T.TensorType(config.floatX, (False,) * 4)
# shape: B x C x X x Y
input_ = tensor5('features')

cropper = LocallySoftRectangularCropper(
    patch_shape=(28, 28),
    hyperparameters={'cutoff': 3000, 'batched_window': True},
    kernel=Gaussian())

down, W, dx2 = cropper.apply(
    input_,
    np.array([list((100, 100))]),
    T.constant(
        99 *
        [[80, 70]]).astype('float32'),
    T.constant(
        99 *
        [[0.28, 0.28]]).astype('float32'),
    T.constant(
        99 *
        [[0.001, ] * 2]).astype('float32'))

f = theano.function([input_], [down, W, dx2])

data = get_bmnist_streams(99)[0].get_epoch_iterator().next()
res = f(data[0][0])

print np.min(res[2][0], axis=0)
print np.sum(res[1][0], axis=0)
plt.imshow(res[1][0], interpolation='nearest')
plt.savefig('w.png')
Exemplo n.º 7
0
batch_size = 100
num_channel = 3
patch_shape = (14, 14)
image_shape = (224, 224)
hyperparameters = {}
hyperparameters["cutoff"] = 3
hyperparameters["batched_window"] = True

cropper = LocallySoftRectangularCropper(
    patch_shape=patch_shape,
    hyperparameters=hyperparameters,
    kernel=Gaussian())

patch = cropper.apply(
    x.reshape((batch_size, num_channel,) + image_shape),
    np.array([list(image_shape)]),
    location,
    scale)

f = theano.function([x, location, scale], patch, allow_input_downcast=True)

grad_l = T.grad(T.mean(patch), location)
grad_s = T.grad(T.mean(patch), scale)
g = theano.function([x, location, scale], [grad_l, grad_s], allow_input_downcast=True)

image = np.random.randn(
    np.prod(image_shape) * batch_size * num_channel).astype('float32')
location_ = [[60.0, 65.0]] * batch_size
scale_ = [[0.0625, 0.0625]] * batch_size

g_l, g_s = g(image, location_, scale_)
Exemplo n.º 8
0
# tds, _ = get_cooking_streams(batch_size)
tds, _ = get_bmnist_streams(1)
res = tds.get_epoch_iterator(as_dict=True).next()['features']
# shape: 3 x 125 x 200
img = res[5, 0]
draw(img)
plt.savefig('img.png')

cropper = LocallySoftRectangularCropper(
    patch_shape=patch_shape,
    hyperparameters=hyperparameters,
    kernel=Gaussian())

patch1, matrix, dx2 = cropper.apply(
    x.reshape((batch_size, num_channel,) + image_shape),
    np.array([list(image_shape)]),
    location,
    scale,
    alpha)
grads = T.grad(T.mean(patch1), x)
grad_scale = abs(T.grad(T.mean(patch1), scale))
grad_location = abs(T.grad(T.mean(patch1), location))
grad_alpha = abs(T.grad(T.mean(patch1), alpha))

f = theano.function(
    [x, location, scale, alpha],
    [patch1, grads, grad_scale + grad_location + grad_alpha, matrix, dx2],
    allow_input_downcast=True)

image = img.flatten().astype('float32')
locations = [[50, 50], [100, 70], [10, 190]]
scales = [[0.28, 0.28], [0.6, 0.6], [1, 1]]
Exemplo n.º 9
0
x = T.fvector()

patch_shape = (28, 28)
image_shape = (100, 100)
hyperparameters = {}
hyperparameters["cutoff"] = 3
hyperparameters["batched_window"] = True

cropper = LocallySoftRectangularCropper(
    patch_shape=patch_shape,
    hyperparameters=hyperparameters,
    kernel=Gaussian())

patch = cropper.apply(
    x.reshape((1, 1,) + image_shape),
    np.array([list(image_shape)]),
    location,
    scale)

f = theano.function([x, location, scale], patch, allow_input_downcast=True)

tds, vds = get_mnist_video_streams(1)
image = tds.get_epoch_iterator().next()[0][2, 0, :]

import ipdb; ipdb.set_trace()

location = [[60.0, 65.0]]
scale = [[1.0, 1.0]]

patch1 = f(image, location, scale)[0, 0]