def __init__(self,
                 input_dim,
                 dim,
                 mlp_hidden_dims,
                 batch_size,
                 image_shape,
                 patch_shape,
                 activation=None,
                 **kwargs):
        super(LSTMAttention, self).__init__(**kwargs)
        self.dim = dim
        self.image_shape = image_shape
        self.patch_shape = patch_shape
        self.batch_size = batch_size
        non_lins = [Rectifier()] * (len(mlp_hidden_dims) - 1) + [None]
        mlp_dims = [input_dim + dim] + mlp_hidden_dims
        mlp = MLP(non_lins,
                  mlp_dims,
                  weights_init=self.weights_init,
                  biases_init=self.biases_init,
                  name=self.name + '_mlp')
        hyperparameters = {}
        hyperparameters["cutoff"] = 3
        hyperparameters["batched_window"] = True
        cropper = LocallySoftRectangularCropper(
            patch_shape=patch_shape,
            hyperparameters=hyperparameters,
            kernel=Gaussian())

        if not activation:
            activation = Tanh()
        self.children = [activation, mlp, cropper]
Пример #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
Пример #3
0
 def __init__(self, configs, **kwargs):
     super(LSTMAttention, self).__init__(**kwargs)
     self.attention_mlp_hidden_dims =\
         configs['attention_mlp_hidden_dims'] + [4]
     self.conv_layers = configs['conv_layers']
     self.fc_layers = configs['fc_layers']
     self.num_layers_first_half_of_conv =\
         configs['num_layers_first_half_of_conv']
     self.lstm_dim = configs['lstm_dim']
     self.cropper_input_shape = configs['cropper_input_shape']
     self.patch_shape = configs['patch_shape']
     self.batch_size = configs['batch_size']
     self.num_channels = configs['num_channels']
     cropper = LocallySoftRectangularCropper(patch_shape=self.patch_shape,
                                             hyperparameters={
                                                 'cutoff': 3000,
                                                 'batched_window': True
                                             },
                                             kernel=Gaussian())
     self.min_scale = [
         (self.patch_shape[0] + 0.0) / self.cropper_input_shape[0],
         (self.patch_shape[1] + 0.0) / self.cropper_input_shape[1]
     ]
     self.children = [cropper, Tanh(), Rectifier(), HardTanh()]
Пример #4
0
patch_shape = (28, 28)
image_shape = (100, 100)
hyperparameters = {}
hyperparameters["cutoff"] = 3000
hyperparameters["batched_window"] = True

# 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],
Пример #5
0
from crop import LocallySoftRectangularCropper
from crop import Gaussian
import numpy as np
from datasets import get_bmnist_streams
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
import matplotlib.pyplot as plt

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])