def lenet(trained=True, weights_filename=LENET_FILENAME, weights_url=LENET_URL): """Create and return instance of LeNet network. :param trained: If True, trained weights will be loaded from file. :param weights_filename: Name of a file with LeNet weights. Will be used if ``trained`` argument is set to True. :param weights_url: Url from which to download file with weights. :return: LeNet network. """ if trained: weights = load_data(get_bin_path(weights_filename), weights_url) if weights is None: raise Exception("cannot load LeNet weights") lenet = Network([ ConvolutionalLayer(image_shape=(28, 28, 1), filter_shape=(5, 5, 20)), ReLU(), MaxPool(poolsize=(2, 2)), ConvolutionalLayer(filter_shape=(5, 5, 50)), ReLU(), MaxPool(poolsize=(2, 2)), FullyConnectedLayer(n_out=500), ReLU(), FullyConnectedLayer(n_out=10), Softmax(), ]) if trained: lenet.set_params(weights) return lenet
def get_simple_network(): result = Network([ ConvolutionalLayer((5, 5, 30), image_shape=(30, 30, 1)), ReLU(), MaxPool((2, 2)), ConvolutionalLayer((5, 5, 20)), ReLU(), MaxPool((2, 2)), FullyConnectedLayer(100), ReLU(), FullyConnectedLayer(75), ReLU(), FullyConnectedLayer(50), ReLU(), FullyConnectedLayer(10) ]) return result
def test_layers_lists(self): def foo(x): return T.minimum(x, 0.) net = Network([ ConvolutionalLayer(image_shape=(42, 21, 2), filter_shape=(5, 5, 3)), ReLU(), ConvolutionalLayer(filter_shape=(5, 5, 3)), ActivationLayer(foo), FullyConnectedLayer(10), ReLU(), Softmax(), FullyConnectedLayer(3), ]) self.assertEqual(len(net.convolutional_layers), 2) self.assertEqual(len(net.weighted_layers), 4) self.assertEqual(len(net.layers), 8)
def test_relu_layer(self): in_data = np.zeros((1, 13, 5, 7), dtype=theano.config.floatX) for i in xrange(in_data.shape[1]): for j in xrange(in_data.shape[2]): for k in xrange(in_data.shape[3]): in_data[0][i][j][k] = i + j**2 - 5 * k layer = ReLU() out = eval_tensor_on_layer(layer, in_data) for i in xrange(in_data.shape[1]): for j in xrange(in_data.shape[2]): for k in xrange(in_data.shape[3]): if in_data[0][i][j][k] < 0.: self.assertEqual(out[0][i][j][k], 0.) else: self.assertEqual(out[0][i][j][k], in_data[0][i][j][k])
def alexnet(trained=True, weights_filename=ALEXNET_FILENAME, weights_url=None): if trained: weights = load_data(get_bin_path(weights_filename), weights_url) if weights is None: raise Exception("cannot load AlexNet weights") # Normalization parameters local_range = 5 alpha = 0.0001 beta = 0.75 k = 1 net = Network([ ConvolutionalLayer(image_shape=(227, 227, 3), filter_shape=(11, 11, 96), stride=(4, 4)), ReLU(), LRN(local_range=local_range, alpha=alpha, beta=beta, k=k), MaxPool(poolsize=(3, 3), stride=(2, 2)), ConvolutionalLayer(filter_shape=(5, 5, 256), padding=(2, 2), n_groups=2), ReLU(), LRN(local_range=local_range, alpha=alpha, beta=beta, k=k), MaxPool(poolsize=(3, 3), stride=(2, 2)), ConvolutionalLayer(filter_shape=(3, 3, 384), padding=(1, 1)), ReLU(), ConvolutionalLayer(filter_shape=(3, 3, 384), padding=(1, 1), n_groups=2), ReLU(), ConvolutionalLayer(filter_shape=(3, 3, 256), padding=(1, 1), n_groups=2), ReLU(), MaxPool(poolsize=(3, 3), stride=(2, 2)), FullyConnectedLayer(4096), ReLU(), Dropout(), FullyConnectedLayer(4096), ReLU(), Dropout(), FullyConnectedLayer(1000), Softmax() ]) if trained: net.set_params(weights) return net
def __init__(self, n_filters, input_layer_name=None, name='inception'): """Create inception layer. Input for the Inception layer is in the format (batch size, number of channels, image height, image width). :param int list of length 6 n_filters: Number of filters in convolutional layers. """ super(InceptionLayer, self).__init__(input_layer_name, name) layer_list1 = [ ConvolutionalLayer(filter_shape=(1, 1, n_filters[0]), name=name+'/1x1_conv1'), ReLU(), ] layer_list2 = [ ConvolutionalLayer(filter_shape=(1, 1, n_filters[1]), name=name+'/1x1_conv2'), ReLU(), ConvolutionalLayer(filter_shape=(3, 3, n_filters[2]), padding=(1, 1), name=name+'/3x3_conv'), ReLU(), ] layer_list3 = [ ConvolutionalLayer(filter_shape=(1, 1, n_filters[3]), name=name+'/1x1_conv3'), ReLU(), ConvolutionalLayer(filter_shape=(5, 5, n_filters[4]), padding=(2, 2), name=name+'/5x5_conv'), ReLU(), ] layer_list4 = [ MaxPool(poolsize=(3, 3), stride=(1, 1), padding=(1, 1)), ConvolutionalLayer(filter_shape=(1, 1, n_filters[5]), name=name+'/1x1_conv4'), ReLU(), ] self.layer_lists = [layer_list1, layer_list2, layer_list3, layer_list4] layers = np.concatenate(self.layer_lists) self.convolutional_layers = [layer for layer in layers if isinstance(layer, ConvolutionalLayer)] self.bottom_layers = [layer_list[0] for layer_list in self.layer_lists] self.top_layers = [layer_list[-1] for layer_list in self.layer_lists] self.concat = Concatenation()
def test_training_process(self): net = Network([FullyConnectedLayer(n_in=3, n_out=6), ReLU(), FullyConnectedLayer(n_out=2), Softmax()]) self.assertEqual(net.batch_size, 1) data_in = [] data_out = [] for i in xrange(2): for j in xrange(2): for k in xrange(2): data_in.append([1. * i, 1. * j, 1. * k]) data_out.append([i ^ j ^ k]) net.data_loader = DummyDataLoader(data_in, data_out) net.verbosity = 0 net.snapshot_interval = 0 # don't save snapshots config = athenet.TrainConfig() config.n_epochs = 500 config.batch_size = 1 config.learning_rate = 0.01 net.train(config) net.batch_size = 3 correct = 0. for i in xrange(2): for j in xrange(2): for k in xrange(2): raw = net.evaluate([i, j, k])[0] out = net.evaluate([i, j, k])[1] self.assertTrue(raw[out[0]] > raw[out[1]]) if out[0] == i ^ j ^ k: correct += 1. self.assertEqual(correct / len(data_in), net.test_accuracy()) # it should be automatically corrected self.assertEqual(net.batch_size, 1)
def googlenet(trained=True, weights_filename=GOOGLENET_FILENAME, weights_url=None): if trained: weights = load_data(get_bin_path(weights_filename), weights_url) if weights is None: raise Exception("cannot load GoogLeNet weights") # Normalization parameters local_range = 5 alpha = 0.0001 beta = 0.75 k = 1 net = Network([ ConvolutionalLayer(image_shape=(224, 224, 3), filter_shape=(7, 7, 64), stride=(2, 2), padding=(3, 3)), ReLU(), MaxPool(poolsize=(3, 3), stride=(2, 2), padding=(1, 1)), LRN(local_range=local_range, alpha=alpha, beta=beta, k=k), ConvolutionalLayer(filter_shape=(1, 1, 64)), ReLU(), ConvolutionalLayer(filter_shape=(3, 3, 192), padding=(1, 1)), ReLU(), LRN(local_range=local_range, alpha=alpha, beta=beta, k=k), MaxPool(poolsize=(3, 3), stride=(2, 2), padding=(1, 1)), InceptionLayer([64, 96, 128, 16, 32, 32], name='inception 3a'), InceptionLayer([128, 128, 192, 32, 96, 64], name='inception 3b'), MaxPool(poolsize=(3, 3), stride=(2, 2), padding=(1, 1)), InceptionLayer([192, 96, 208, 16, 48, 64], name='inception 4a'), InceptionLayer([160, 112, 224, 24, 64, 64], name='inception 4b'), InceptionLayer([128, 128, 256, 24, 64, 64], name='inception 4c'), InceptionLayer([112, 144, 288, 32, 64, 64], name='inception 4d'), InceptionLayer([256, 160, 320, 32, 128, 128], name='inception 4e'), MaxPool(poolsize=(3, 3), stride=(2, 2), padding=(1, 1)), InceptionLayer([256, 160, 320, 32, 128, 128], name='inception 5a'), InceptionLayer([384, 192, 384, 48, 128, 128], name='inception 5b'), AvgPool(poolsize=(7, 7), stride=(1, 1)), Dropout(0.4), FullyConnectedLayer(1000), Softmax(), ]) if trained: net.set_params(weights) return net
def custom_derivatives_normalization(data): return data / 4. def custom_activations_normalization(data): return data / (data.sum().upper + 0.5) def custom_count_function(data): return numpy.prod(data.upper) - numpy.prod(data.lower) print "creating network..." network = Network([ ConvolutionalLayer(image_shape=(28, 28, 1), filter_shape=(5, 5, 10)), ReLU(), ConvolutionalLayer(filter_shape=(3, 3, 20), n_groups=2), ReLU(), LRN(), MaxPool(poolsize=(2, 2)), InceptionLayer(n_filters=[2, 2, 2, 2, 2, 2]), FullyConnectedLayer(n_out=10), ReLU(), FullyConnectedLayer(n_out=3), Softmax(), ]) ok() print "computing derest indicators..." ind_derest = get_derest_indicators( network,