def build_model(x=None, layer='fc8', shape=(None, 3, 227, 227), up_scale=4): net = {'data': InputLayer(shape=shape, input_var=x)} net['data_s'] = Upscale2DLayer(net['data'], up_scale) net['conv1'] = Conv2DLayer(net['data_s'], num_filters=96, filter_size=(11, 11), stride=4, nonlinearity=lasagne.nonlinearities.rectify) if layer is 'conv1': return net # pool1 net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(3, 3), stride=2) # norm1 net['norm1'] = LocalResponseNormalization2DLayer(net['pool1'], n=5, alpha=0.0001 / 5.0, beta=0.75, k=1) # conv2 # before conv2 split the data net['conv2_data1'] = SliceLayer(net['norm1'], indices=slice(0, 48), axis=1) net['conv2_data2'] = SliceLayer(net['norm1'], indices=slice(48, 96), axis=1) # now do the convolutions net['conv2_part1'] = Conv2DLayer(net['conv2_data1'], num_filters=128, filter_size=(5, 5), pad=2) net['conv2_part2'] = Conv2DLayer(net['conv2_data2'], num_filters=128, filter_size=(5, 5), pad=2) # now combine net['conv2'] = concat((net['conv2_part1'], net['conv2_part2']), axis=1) if layer is 'conv2': return net # pool2 net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(3, 3), stride=2) # norm2 net['norm2'] = LocalResponseNormalization2DLayer(net['pool2'], n=5, alpha=0.0001 / 5.0, beta=0.75, k=1) # conv3 # no group net['conv3'] = Conv2DLayer(net['norm2'], num_filters=384, filter_size=(3, 3), pad=1) if layer is 'conv3': return net # conv4 net['conv4_data1'] = SliceLayer(net['conv3'], indices=slice(0, 192), axis=1) net['conv4_data2'] = SliceLayer(net['conv3'], indices=slice(192, 384), axis=1) net['conv4_part1'] = Conv2DLayer(net['conv4_data1'], num_filters=192, filter_size=(3, 3), pad=1) net['conv4_part2'] = Conv2DLayer(net['conv4_data2'], num_filters=192, filter_size=(3, 3), pad=1) net['conv4'] = concat((net['conv4_part1'], net['conv4_part2']), axis=1) if layer is 'conv4': return net # conv5 # group 2 net['conv5_data1'] = SliceLayer(net['conv4'], indices=slice(0, 192), axis=1) net['conv5_data2'] = SliceLayer(net['conv4'], indices=slice(192, 384), axis=1) net['conv5_part1'] = Conv2DLayer(net['conv5_data1'], num_filters=128, filter_size=(3, 3), pad=1) net['conv5_part2'] = Conv2DLayer(net['conv5_data2'], num_filters=128, filter_size=(3, 3), pad=1) net['conv5'] = concat((net['conv5_part1'], net['conv5_part2']), axis=1) if layer is 'conv5': return net # pool 5 net['pool5'] = MaxPool2DLayer(net['conv5'], pool_size=(3, 3), stride=2) # fc6 net['fc6'] = DenseLayer(net['pool5'], num_units=4096, nonlinearity=lasagne.nonlinearities.rectify) if layer is 'fc6': return net # fc7 net['fc7'] = DenseLayer(net['fc6'], num_units=4096, nonlinearity=lasagne.nonlinearities.rectify) if layer is 'fc7': return net # fc8 net['fc8'] = DenseLayer(net['fc7'], num_units=1000, nonlinearity=lasagne.nonlinearities.softmax) if layer is 'fc8': # st() return net
def define_net(input_var): net = {} net['data'] = InputLayer(shape=(None, 3, IMAGE_SHAPE[0], IMAGE_SHAPE[1]), input_var=input_var) net['patch'] = sample_layer.Sample2DLayer(net['data'], 5, (227, 227), pad=False) # conv1 net['conv1'] = Conv2DLayer(net['patch'], num_filters=96, filter_size=(11, 11), stride=4, nonlinearity=lasagne.nonlinearities.rectify) # pool1 net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(3, 3), stride=2) # norm1 net['norm1'] = LocalResponseNormalization2DLayer(net['pool1'], n=5, alpha=0.0001 / 5.0, beta=0.75, k=1) # before conv2 split the data net['conv2_data1'] = SliceLayer(net['norm1'], indices=slice(0, 48), axis=1) net['conv2_data2'] = SliceLayer(net['norm1'], indices=slice(48, 96), axis=1) # now do the convolutions net['conv2_part1'] = Conv2DLayer(net['conv2_data1'], num_filters=128, filter_size=(5, 5), pad=2) net['conv2_part2'] = Conv2DLayer(net['conv2_data2'], num_filters=128, filter_size=(5, 5), pad=2) # now combine net['conv2'] = concat((net['conv2_part1'], net['conv2_part2']), axis=1) # pool2 net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(3, 3), stride=2) # norm2 net['norm2'] = LocalResponseNormalization2DLayer(net['pool2'], n=5, alpha=0.0001 / 5.0, beta=0.75, k=1) # conv3 # no group net['conv3'] = Conv2DLayer(net['norm2'], num_filters=384, filter_size=(3, 3), pad=1) # conv4 # group = 2 net['conv4_data1'] = SliceLayer(net['conv3'], indices=slice(0, 192), axis=1) net['conv4_data2'] = SliceLayer(net['conv3'], indices=slice(192, 384), axis=1) net['conv4_part1'] = Conv2DLayer(net['conv4_data1'], num_filters=192, filter_size=(3, 3), pad=1) net['conv4_part2'] = Conv2DLayer(net['conv4_data2'], num_filters=192, filter_size=(3, 3), pad=1) net['conv4'] = concat((net['conv4_part1'], net['conv4_part2']), axis=1) # conv5 # group 2 net['conv5_data1'] = SliceLayer(net['conv4'], indices=slice(0, 192), axis=1) net['conv5_data2'] = SliceLayer(net['conv4'], indices=slice(192, 384), axis=1) net['conv5_part1'] = Conv2DLayer(net['conv5_data1'], num_filters=128, filter_size=(3, 3), pad=1) net['conv5_part2'] = Conv2DLayer(net['conv5_data2'], num_filters=128, filter_size=(3, 3), pad=1) net['conv5'] = concat((net['conv5_part1'], net['conv5_part2']), axis=1) # pool 5 net['pool5'] = MaxPool2DLayer(net['conv5'], pool_size=(3, 3), stride=2) # fc6 net['fc6'] = DenseLayer(net['pool5'], num_units=4096, nonlinearity=lasagne.nonlinearities.rectify) # fc7 net['fc7'] = DenseLayer(net['fc6'], num_units=4096, nonlinearity=lasagne.nonlinearities.rectify) # fc8 net['out'] = DenseLayer(net['fc7'], num_units=1, nonlinearity=lasagne.nonlinearities.linear) # print ('Objective layer shapes:') # print (lasagne.layers.get_output_shape(net['pool5'])) # # fc6 # net['fc6'] = Conv2DLayer( # net['pool5'], num_filters=4096, filter_size=(6, 6), # nonlinearity=lasagne.nonlinearities.rectify, flip_filters=False) # print (lasagne.layers.get_output_shape(net['fc6'])) # # fc7 # net['fc7'] = Conv2DLayer( # net['fc6'], # num_filters=4096, filter_size=(1, 1), # nonlinearity=lasagne.nonlinearities.rectify) # print (lasagne.layers.get_output_shape(net['fc7'])) # # fc8 # net['out'] = Conv2DLayer( # net['fc7'], # num_filters=1, filter_size=(1, 1), # nonlinearity=lasagne.nonlinearities.linear) # print (lasagne.layers.get_output_shape(net['out'])) return net
def reference_model(): net = {} net['data'] = InputLayer(shape=(None, 3, 227, 227)) # conv1 net['conv1'] = Conv2DLayer( net['data'], num_filters=96, filter_size=(11, 11), stride = 4, nonlinearity=lasagne.nonlinearities.rectify) # pool1 net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(3, 3), stride=2) # norm1 net['norm1'] = LocalResponseNormalization2DLayer(net['pool1'], n=5, alpha=0.0001/5.0, beta = 0.75, k=1) # conv2 # The caffe reference model uses a parameter called group. # This parameter splits input to the convolutional layer. # The first half of the filters operate on the first half # of the input from the previous layer. Similarly, the # second half operate on the second half of the input. # # Lasagne does not have this group parameter, but we can # do it ourselves. # # see https://github.com/BVLC/caffe/issues/778 # also see https://code.google.com/p/cuda-convnet/wiki/LayerParams # before conv2 split the data net['conv2_data1'] = SliceLayer(net['norm1'], indices=slice(0, 48), axis=1) net['conv2_data2'] = SliceLayer(net['norm1'], indices=slice(48,96), axis=1) # now do the convolutions net['conv2_part1'] = Conv2DLayer(net['conv2_data1'], num_filters=128, filter_size=(5, 5), pad = 2) net['conv2_part2'] = Conv2DLayer(net['conv2_data2'], num_filters=128, filter_size=(5,5), pad = 2) # now combine net['conv2'] = concat((net['conv2_part1'],net['conv2_part2']),axis=1) # pool2 net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(3, 3), stride = 2) # norm2 net['norm2'] = LocalResponseNormalization2DLayer(net['pool2'], n=5, alpha=0.0001/5.0, beta = 0.75, k=1) # conv3 # no group net['conv3'] = Conv2DLayer(net['norm2'], num_filters=384, filter_size=(3, 3), pad = 1) # conv4 # group = 2 net['conv4_data1'] = SliceLayer(net['conv3'], indices=slice(0, 192), axis=1) net['conv4_data2'] = SliceLayer(net['conv3'], indices=slice(192,384), axis=1) net['conv4_part1'] = Conv2DLayer(net['conv4_data1'], num_filters=192, filter_size=(3, 3), pad = 1) net['conv4_part2'] = Conv2DLayer(net['conv4_data2'], num_filters=192, filter_size=(3,3), pad = 1) net['conv4'] = concat((net['conv4_part1'],net['conv4_part2']),axis=1) # conv5 # group 2 net['conv5_data1'] = SliceLayer(net['conv4'], indices=slice(0, 192), axis=1) net['conv5_data2'] = SliceLayer(net['conv4'], indices=slice(192,384), axis=1) net['conv5_part1'] = Conv2DLayer(net['conv5_data1'], num_filters=128, filter_size=(3, 3), pad = 1) net['conv5_part2'] = Conv2DLayer(net['conv5_data2'], num_filters=128, filter_size=(3,3), pad = 1) net['conv5'] = concat((net['conv5_part1'],net['conv5_part2']),axis=1) # pool 5 net['pool5'] = MaxPool2DLayer(net['conv5'], pool_size=(3, 3), stride = 2) # fc6 net['fc6'] = DenseLayer( net['pool5'],num_units=4096, nonlinearity=lasagne.nonlinearities.rectify) # fc7 net['fc7'] = DenseLayer( net['fc6'], num_units=4096, nonlinearity=lasagne.nonlinearities.rectify) # fc8 net['fc8'] = DenseLayer( net['fc7'], num_units=1000, nonlinearity=lasagne.nonlinearities.softmax) return net
def build_model(inp): net = {} net['input'] = InputLayer((None, 3, 224, 224), input_var=inp) net['conv1'] = Conv2DLayer(net['input'], 96, 7, stride=2, pad=(1, 1), W=lasagne.init.Uniform()) net['norm1'] = LocalResponseNormalization2DLayer(net['conv1'], alpha=0.00005) net['pool1'] = MaxPool2DLayer(net['norm1'], 3, stride=2) net['conv2'] = Conv2DLayer(net['pool1'], 256, 5, stride=2, pad=(0, 0), W=lasagne.init.Uniform(), b=lasagne.init.Constant(1.)) net['norm2'] = LocalResponseNormalization2DLayer(net['conv2'], alpha=0.00005) net['pool2'] = MaxPool2DLayer(net['norm2'], 3, stride=2) net['conv3'] = Conv2DLayer(net['pool2'], 384, 3, stride=1, pad=(1, 1), W=lasagne.init.Uniform()) net['conv4'] = Conv2DLayer(net['conv3'], 384, 3, stride=1, pad=(1, 1), W=lasagne.init.Uniform(), b=lasagne.init.Constant(1.)) net['conv5'] = Conv2DLayer(net['conv4'], 256, 3, stride=1, pad=(1, 1), W=lasagne.init.Uniform(), b=lasagne.init.Constant(1.)) net['p5_spm6'] = FlattenLayer(MaxPool2DLayer(net['conv5'], 3, stride=2)) net['p5_spm3'] = FlattenLayer(MaxPool2DLayer(net['conv5'], 5, stride=4)) net['p5_spm2'] = FlattenLayer(MaxPool2DLayer(net['conv5'], 7, stride=7)) net['p5_spm1'] = MaxPool2DLayer(net['conv5'], 13, stride=13) net['p5_spm1_f'] = FlattenLayer(net['p5_spm1']) net['conc_spm'] = ConcatLayer( [net['p5_spm6'], net['p5_spm3'], net['p5_spm2'], net['p5_spm1_f']]) net['fc6'] = DenseLayer(net['conc_spm'], num_units=4096, W=lasagne.init.Uniform(), b=lasagne.init.Constant(1.)) net['fc6_drop'] = DropoutLayer(net['fc6'], p=0.5) net['fc7'] = DenseLayer(net['fc6_drop'], num_units=4096, W=lasagne.init.Uniform(), b=lasagne.init.Constant(1.)) net['fc7_drop'] = DropoutLayer(net['fc7'], p=0.5) net['fc8'] = DenseLayer(net['fc6_drop'], num_units=1000, W=lasagne.init.Uniform(), b=lasagne.init.Constant(1.)) net['out'] = NonlinearityLayer(net['fc8'], softmax) return net
# Model implementation of Facenet # This is a ZFNet based model with 1 X 1 convolutions # Link to Paper : https://arxiv.org/pdf/1503.03832.pdf import lasagne import theano from lasagne.layers import (Conv2DLayer, MaxPool2DLayer, BatchNormLayer, DenseLayer, InputLayer, Deconv2DLayer, NonlinearityLayer, get_output, get_output_shape, batch_norm, NINLayer, GlobalPoolLayer, LocalResponseNormalization2DLayer, DropoutLayer, ReshapeLayer) def build_facenet(input) net = InputLayer((None, 220, 220, 3), input_var=input) net = Conv2DLayer(net, 64, 7, stride=2, pad=2) net = MaxPool2DLayer(net, 2) net = LocalResponseNormalization2DLayer(net) net = Conv2DLayer(net, 64, 1, stride=1) net = LocalResponseNormalization2DLayer(Conv2DLayer(net, 192, 3, stride=1, pad=1)) net = MaxPool2DLayer(net, 2) net = Conv2DLayer(net, 192, 1, stride=1) net = Conv2DLayer(net, 384, 3, stride=1, pad=1) net = MaxPool2DLayer(net, 2) net = Conv2DLayer(net, 384, 1, stride=1) net = Conv2DLayer(net, 256, 3, stride=1, pad=1) net = Conv2DLayer(net, 256, 1, stride=1) net = Conv2DLayer(net, 256, 3, stride=1, pad=1) net = Conv2DLayer(net, 256, 1, stride=1) net = Conv2DLayer(net, 256, 3, stride=1, pad=1) net = MaxPool2DLayer(net, 2) net = DropoutLayer(DenseLayer(net, 4096), p=0.2) net = DropoutLayer(DenseLayer(net, 4096), p=0.2)
return T_net T_net = build_test_model() with np.load('data/ENet_dlib_300.npz') as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] lasagne.layers.set_all_param_values(T_net['output_layer'], param_values) T_X_sym = T.tensor4() T_feat_layer = LocalResponseNormalization2DLayer( T_net['conv4_all'], alpha=0.002, k=2, beta=0.75, ) T_prediction = lasagne.layers.get_output(T_feat_layer, T_X_sym) T_pred = theano.function([T_X_sym], T_prediction) conv4shape = lasagne.layers.get_output_shape(T_net['conv4_all']) print 'conv4 shape:', conv4shape def imdata(fls, data_size=BATCH_SIZE): datablob = np.ndarray((data_size, 4, IM_SIZE, IM_SIZE)) datalb = np.zeros((data_size, 1, 1, 12)) dataps = np.zeros((data_size, 10, 4)) n = len(fls)