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]
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
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()]
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], allow_input_downcast=True)
def main(save_to, num_epochs): mlp = MLP([Tanh(), Softmax()], [784, 100, 10], weights_init=IsotropicGaussian(0.01), biases_init=Constant(0)) mlp.initialize() x = tensor.matrix('features') y = tensor.lmatrix('targets') #attention ---> patch_shape = (16, 16) image_shape = (784, 100) import numpy import theano.tensor as T n_spatial_dims = 2 cropper = SoftRectangularCropper(n_spatial_dims=n_spatial_dims, patch_shape=patch_shape, image_shape=image_shape, kernel=Gaussian()) batch_size = 10 scales = 1.3**numpy.arange(-7, 6) n_patches = len(scales) locations = (numpy.ones( (n_patches, batch_size, 2)) * image_shape / 2).astype(numpy.float32) scales = numpy.tile(scales[:, numpy.newaxis, numpy.newaxis], (1, batch_size, 2)).astype(numpy.float32) Tpatches = T.stack(*[ cropper.apply(x, T.constant(location), T.constant(scale))[0] for location, scale in zip(locations, scales) ]) patches = theano.function([x], Tpatches)(batch['features']) import ipdb as pdb pdb.set_trace() probs = mlp.apply(tensor.flatten(patches, outdim=2)) cost = CategoricalCrossEntropy().apply(y.flatten(), probs) error_rate = MisclassificationRate().apply(y.flatten(), probs) cg = ComputationGraph([cost]) W1, W2 = VariableFilter(roles=[WEIGHT])(cg.variables) cost = cost + .00005 * (W1**2).sum() + .00005 * (W2**2).sum() cost.name = 'final_cost' mnist_train = MNIST(("train", )) mnist_test = MNIST(("test", )) algorithm = GradientDescent(cost=cost, parameters=cg.parameters, step_rule=Scale(learning_rate=0.1)) extensions = [ Timing(), FinishAfter(after_n_epochs=num_epochs), DataStreamMonitoring([cost, error_rate], Flatten(DataStream.default_stream( mnist_test, iteration_scheme=SequentialScheme( mnist_test.num_examples, 500)), which_sources=('features', )), prefix="test"), TrainingDataMonitoring([ cost, error_rate, aggregation.mean(algorithm.total_gradient_norm) ], prefix="train", after_epoch=True), Checkpoint(save_to), Printing() ] if BLOCKS_EXTRAS_AVAILABLE: extensions.append( Plot('MNIST example', channels=[[ 'test_final_cost', 'test_misclassificationrate_apply_error_rate' ], ['train_total_gradient_norm']])) main_loop = MainLoop(algorithm, Flatten(DataStream.default_stream( mnist_train, iteration_scheme=SequentialScheme( mnist_train.num_examples, 50)), which_sources=('features', )), model=Model(cost), extensions=extensions) main_loop.run()