def makeNeuralNet(input_var=None):
    net = {}
    net['input'] = InputLayer(shape=(None, 3, 224, 224), input_var=input_var)
    net['bnorm'] = BatchNormLayer(net['input'])
    net['conv1'] = ConvLayer(net['bnorm'],
                             num_filters=96,
                             filter_size=5,
                             stride=2)  #96*112*112
    net['norm1'] = NormLayer(
        net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)  #96*37...approx
    net['conv2'] = ConvLayer(net['pool1'],
                             num_filters=256,
                             filter_size=5,
                             pad=1)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool2'], num_units=1024)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.2)
    net['_fc7'] = DenseLayer(net['drop6'], num_units=256)
    net['_drop7'] = DropoutLayer(net['_fc7'], p=0.2)
    net['_fc8out'] = DenseLayer(net['_drop7'],
                                num_units=1,
                                nonlinearity=lasagne.nonlinearities.sigmoid)
    output_layer_driver = net['_fc8out']
    return output_layer_driver, net
    def lasagne_layers_method(self):
        '''
        INPUT: None
        OUTPUT: Dict

        Creates dictionary of vgg_cnn_s model Lasagne layer objects. Here the
        original output layer (softmax, 1000 classes) has been removed and
        the output layer returns a vector of shape (1,4096).
        '''
        # Create dictionary of VGG_CNN_S model layers
        self.lasagne_layers = {}
        self.lasagne_layers['input'] = InputLayer((None, 3, 224, 224))
        self.lasagne_layers['conv1'] = ConvLayer(self.lasagne_layers['input'],
                                                 num_filters=96,
                                                 filter_size=7,
                                                 stride=2,
                                                 flip_filters=False)
        self.lasagne_layers['norm1'] = NormLayer(self.lasagne_layers['conv1'],
                                                 alpha=0.0001)
        self.lasagne_layers['pool1'] = PoolLayer(self.lasagne_layers['norm1'],
                                                 pool_size=3,
                                                 stride=3,
                                                 ignore_border=False)
        self.lasagne_layers['conv2'] = ConvLayer(self.lasagne_layers['pool1'],
                                                 num_filters=256,
                                                 filter_size=5,
                                                 flip_filters=False)
        self.lasagne_layers['pool2'] = PoolLayer(self.lasagne_layers['conv2'],
                                                 pool_size=2,
                                                 stride=2,
                                                 ignore_border=False)
        self.lasagne_layers['conv3'] = ConvLayer(self.lasagne_layers['pool2'],
                                                 num_filters=512,
                                                 filter_size=3,
                                                 pad=1,
                                                 flip_filters=False)
        self.lasagne_layers['conv4'] = ConvLayer(self.lasagne_layers['conv3'],
                                                 num_filters=512,
                                                 filter_size=3,
                                                 pad=1,
                                                 flip_filters=False)
        self.lasagne_layers['conv5'] = ConvLayer(self.lasagne_layers['conv4'],
                                                 num_filters=512,
                                                 filter_size=3,
                                                 pad=1,
                                                 flip_filters=False)
        self.lasagne_layers['pool5'] = PoolLayer(self.lasagne_layers['conv5'],
                                                 pool_size=3,
                                                 stride=3,
                                                 ignore_border=False)
        self.lasagne_layers['fc6'] = DenseLayer(self.lasagne_layers['pool5'],
                                                num_units=4096)
        self.lasagne_layers['drop6'] = DropoutLayer(self.lasagne_layers['fc6'],
                                                    p=0.5)
        self.lasagne_layers['fc7'] = DenseLayer(self.lasagne_layers['drop6'],
                                                num_units=4096)
Exemple #3
0
def build_model(input_var):
    net = {}

    net['input'] = InputLayer((None, 3, 224, 224), input_var=input_var)
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=96,
                             filter_size=7,
                             stride=2,
                             flip_filters=False)
    # caffe has alpha = alpha * pool_size
    net['norm1'] = NormLayer(net['conv1'], alpha=0.0001)
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'],
                             num_filters=256,
                             filter_size=5,
                             flip_filters=False)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv4'] = ConvLayer(net['conv3'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv5'] = ConvLayer(net['conv4'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['pool5'] = PoolLayer(net['conv5'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(net['drop7'], num_units=1000, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)

    return net
def makeNeuralNet():
    net = {}
    net['input'] = InputLayer(shape=(None, 3, 224, 224))
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=96,
                             filter_size=7,
                             stride=2)
    net['norm1'] = NormLayer(
        net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'], num_filters=256, filter_size=5)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'],
                             num_filters=512,
                             filter_size=3,
                             pad=1)
    net['conv4'] = ConvLayer(net['conv3'],
                             num_filters=512,
                             filter_size=3,
                             pad=1)
    net['conv5'] = ConvLayer(net['conv4'],
                             num_filters=512,
                             filter_size=3,
                             pad=1)
    net['pool5'] = PoolLayer(net['conv5'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    #     net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    #     net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    #     net['fc8'] = DenseLayer(net['drop7'], num_units=1000, nonlinearity=lasagne.nonlinearities.softmax)
    #     output_layer_vgg = net['fc8']
    ini = lasagne.init.HeUniform()
    net['_fc7'] = DenseLayer(net['drop6'], num_units=4096, W=ini)
    net['_drop7'] = DropoutLayer(net['_fc7'], p=0.5)
    net['_fc8out'] = DenseLayer(net['_drop7'],
                                num_units=6,
                                nonlinearity=lasagne.nonlinearities.softmax,
                                W=ini)
    output_layer_driver = net['_fc8out']
    return net['drop6'], output_layer_driver
Exemple #5
0
def build_neural_network(input_var, input_shape):
    net = {}

    net['input'] = InputLayer(input_shape, input_var)
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=96,
                             filter_size=7,
                             stride=2)
    net['norm1'] = NormLayer(net['conv1'], alpha=0.0001)
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'], num_filters=256, filter_size=5)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'],
                             num_filters=512,
                             filter_size=3,
                             pad=1)
    net['conv4'] = ConvLayer(net['conv3'],
                             num_filters=512,
                             filter_size=3,
                             pad=1)
    net['conv5'] = ConvLayer(net['conv4'],
                             num_filters=512,
                             filter_size=3,
                             pad=1)
    net['pool5'] = PoolLayer(net['conv5'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(net['drop7'], num_units=1, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], sigmoid)

    return net
Exemple #6
0
    def __init__(self, load=True):
        # Architecture
        net = {}
        net['input'] = InputLayer((None, 3, 224, 224))
        net['conv1'] = ConvLayer(net['input'],
                                 num_filters=96,
                                 filter_size=7,
                                 stride=2,
                                 flip_filters=False)
        net['norm1'] = NormLayer(
            net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
        net['pool1'] = PoolLayer(net['norm1'],
                                 pool_size=3,
                                 stride=3,
                                 ignore_border=False)
        net['conv2'] = ConvLayer(net['pool1'],
                                 num_filters=256,
                                 filter_size=5,
                                 flip_filters=False)
        net['pool2'] = PoolLayer(net['conv2'],
                                 pool_size=2,
                                 stride=2,
                                 ignore_border=False)
        net['conv3'] = ConvLayer(net['pool2'],
                                 num_filters=512,
                                 filter_size=3,
                                 pad=1,
                                 flip_filters=False)
        net['conv4'] = ConvLayer(net['conv3'],
                                 num_filters=512,
                                 filter_size=3,
                                 pad=1,
                                 flip_filters=False)
        net['conv5'] = ConvLayer(net['conv4'],
                                 num_filters=512,
                                 filter_size=3,
                                 pad=1,
                                 flip_filters=False)
        net['pool5'] = PoolLayer(net['conv5'],
                                 pool_size=3,
                                 stride=3,
                                 ignore_border=False)
        net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
        net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
        net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
        net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
        net['fc8'] = DenseLayer(net['drop7'],
                                num_units=1000,
                                nonlinearity=lasagne.nonlinearities.softmax)
        self.output_layer = net['fc8']
        self.net = net

        if load:
            self.load_weights()

        # Compile
        self.predict_fn = None
        self.predict_fns = {}
        self.train_fn = {}
        self.lr = theano.shared(np.array(1e-2, dtype=np.float32))
        self.regularizer_amount = theano.shared(
            np.array(4e-5, dtype=np.float32))
def create_pretrained_vgg_nn_data():
    '''
    Create a vgg neural net. Load pretrained weights.
    Return the dictionary of nn layers, the lasagne output_layer, the mean_image
    '''
    # define the vgg_s network
    net = {}
    net['input'] = InputLayer((None, 3, 224, 224))
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=96,
                             filter_size=7,
                             stride=2,
                             flip_filters=False)
    net['norm1'] = NormLayer(
        net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'],
                             num_filters=256,
                             filter_size=5,
                             flip_filters=False)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv4'] = ConvLayer(net['conv3'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv5'] = ConvLayer(net['conv4'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['pool5'] = PoolLayer(net['conv5'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    # these are here JUST to upload the model; then not used
    net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(net['drop7'],
                            num_units=1000,
                            nonlinearity=lasagne.nonlinearities.softmax)
    output_layer = net['fc8']

    # upload and input the pretrained weights
    model = pickle.load(open('./vgg_cnn_s.pkl'))
    MEAN_IMAGE = model['mean image']
    lasagne.layers.set_all_param_values(output_layer, model['values'])
    output_layer = net['fc7']
    net.pop('fc8')

    return net, output_layer, MEAN_IMAGE
Exemple #8
0
experimentpath = 'experiments/' + experimentname + '/'
if not os.path.exists(experimentpath):
    os.makedirs(experimentpath)

epoch = 0

#Define the network structure
print 'define network'
net = {}
net['input'] = InputLayer((None, 3, 224, 224))
net['conv1'] = ConvLayer(net['input'],
                         num_filters=96,
                         filter_size=7,
                         stride=2,
                         flip_filters=False)
net['norm1'] = NormLayer(net['conv1'],
                         alpha=0.0001)  # caffe has alpha = alpha * pool_size
net['pool1'] = PoolLayer(net['norm1'],
                         pool_size=3,
                         stride=3,
                         ignore_border=False)
net['conv2'] = ConvLayer(net['pool1'],
                         num_filters=256,
                         filter_size=5,
                         flip_filters=False)
net['pool2'] = PoolLayer(net['conv2'],
                         pool_size=2,
                         stride=2,
                         ignore_border=False)
net['conv3'] = ConvLayer(net['pool2'],
                         num_filters=512,
                         filter_size=3,
    def __init__(self):
        net = {}
        net['input'] = InputLayer((None, 3, 224, 224))
        net['conv1'] = ConvLayer(net['input'],
                                 num_filters=96,
                                 filter_size=7,
                                 stride=2,
                                 flip_filters=False)
        net['norm1'] = NormLayer(
            net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
        net['pool1'] = PoolLayer(net['norm1'],
                                 pool_size=3,
                                 stride=3,
                                 ignore_border=False)
        net['conv2'] = ConvLayer(net['pool1'],
                                 num_filters=256,
                                 filter_size=5,
                                 flip_filters=False)
        net['pool2'] = PoolLayer(net['conv2'],
                                 pool_size=2,
                                 stride=2,
                                 ignore_border=False)
        net['conv3'] = ConvLayer(net['pool2'],
                                 num_filters=512,
                                 filter_size=3,
                                 pad=1,
                                 flip_filters=False)
        net['conv4'] = ConvLayer(net['conv3'],
                                 num_filters=512,
                                 filter_size=3,
                                 pad=1,
                                 flip_filters=False)
        net['conv5'] = ConvLayer(net['conv4'],
                                 num_filters=512,
                                 filter_size=3,
                                 pad=1,
                                 flip_filters=False)
        net['pool5'] = PoolLayer(net['conv5'],
                                 pool_size=3,
                                 stride=3,
                                 ignore_border=False)
        net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
        net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
        net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
        net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
        net['fc8'] = DenseLayer(net['drop7'],
                                num_units=1000,
                                nonlinearity=lasagne.nonlinearities.softmax)

        print "loading vgg_cnn_s.pkl..."
        #model = pickle.load(urllib.urlopen('https://dl.dropboxusercontent.com/u/10578702/img/vgg_cnn_s.pkl'))
        model = pickle.load(
            open(
                '/home/tweddielin/cbir/flask-imsearch/app/static/vgg_cnn_s.pkl'
            ))
        print "vgg_cnn_s.pkl loaded!"
        self.output_layer = net['fc7']
        self.CLASSES = model['synset words']
        self.MEAN_IMAGE = model['mean image']

        lasagne.layers.set_all_param_values(self.output_layer,
                                            model['values'][0:14])