def bn_relu_dropout_conv(input_layer, filter_shape, strides, bn_param, keep_prob, device):
    layer = [batch_norm(input_layer, bn_param, device=device)]
    layer.append(tf.nn.relu(layer[-1]))

    if FLAGS.keep_prob!=None:
      layer.append(tf.nn.dropout(layer[-1], keep_prob[0]))

    layer.append(convolution_layer(layer[-1], shape=filter_shape, strides=strides, bias=False, layer_name='conv', device=device))
    return layer[-1]
def first_residual_block(input_layer, output_channel, bn_param, keep_prob, down_sample=False, device=None):
    input_channel = input_layer.get_shape().as_list()[-1]
    assert input_channel!=output_channel

    if down_sample: strides=[1,2,2,1]
    else: strides=[1,1,1,1]

    with tf.variable_scope('layer1_in_block'):
        conv1, relu = bn_relu_conv(input_layer, [3, 3, input_channel, output_channel], strides=strides, bn_param=bn_param, device=device)
    with tf.variable_scope('layer2_in_block'):
        conv2 = bn_relu_dropout_conv(conv1, [3, 3, output_channel, output_channel], strides=[1,1,1,1], bn_param=bn_param, keep_prob=keep_prob, device=device)

    projection = convolution_layer(relu, shape=[1,1,input_channel,output_channel], strides=strides, bias=False, layer_name='projection', device=device)

    return conv2 + projection
def inference(input_tensor_batch, bn_param, keep_prob, n, k, num_classes, device):
    layers = []
    with tf.variable_scope('group1'):
        conv0 = convolution_layer(input_tensor_batch, shape=[3, 3, 3, 16], strides=[1, 1, 1, 1], bias=False, layer_name='conv0', device=device)
        layers.append(conv0)

    for i in range(n):
        with tf.variable_scope('group2_block%d' %i):
            if i == 0 and k!=1:
                conv1 = first_residual_block(layers[-1], 16*k, bn_param, keep_prob, down_sample=False, device=device)
            else:
                conv1 = residual_block(layers[-1], 16*k, bn_param, keep_prob, device=device)
            layers.append(conv1)

    for i in range(n):
        with tf.variable_scope('group3_block%d' %i):
            if i==0:
                conv2 = first_residual_block(layers[-1], 32*k, bn_param, keep_prob, down_sample=True, device=device)
            else:
                conv2 = residual_block(layers[-1], 32*k, bn_param, keep_prob, device=device)
            layers.append(conv2)

    for i in range(n):
        with tf.variable_scope('group4_block%d' %i):
            if i==0:
                conv3 = first_residual_block(layers[-1], 64*k, bn_param, keep_prob, down_sample=True, device=device)
            else:
                conv3 = residual_block(layers[-1], 64*k, bn_param, keep_prob, device=device)
            layers.append(conv3)
        assert conv3.get_shape().as_list()[1:] == [8, 8, 64*k]

    with tf.variable_scope('fc'):
        bn_layer = batch_norm(layers[-1], bn_param, device=device)
        relu_layer = tf.nn.relu(bn_layer)
        global_pool = tf.reduce_mean(relu_layer, [1, 2])

        assert global_pool.get_shape().as_list()[-1:] == [64*k]

        shape=[global_pool.get_shape().as_list()[-1], num_classes]
        output = full_connection_layer(global_pool, shape=shape, bias=True, layer_name='output', device=device)

        layers.append(output)

    return layers[-1]
Пример #4
0
	def get_model(self):
		params = {
			'batch_size': 20,
			'learning_rate': 0.01,
			'epochs':  20,
			'num_classes':  10,
			'dropout_rate': 0.1
		}
		
		Model = model.model()
		Model.add(layers.convolution_layer(field=8, padding=0, stride=1, depth=1, filters=5))
		Model.add(layers.relu_layer())
		Model.add(layers.flatten_layer())
		Model.add(layers.dropout_layer(r = params['dropout_rate']))
		Model.add(layers.linear_layer(13*13*5, 10))
		Model.set_loss(layers.softmax_cross_entropy())
		Model.set_hyper_params(params)

		return Model
def conv_bn_relu(inputTensor, shape, bn_param, device):
  conv = convolution_layer(inputTensor, shape, strides=[1,1,1,1], bias=False, layer_name='conv', device=device)
  bn = batch_norm(conv, bn_param=bn_param, scale=False, device = device)
  return tf.nn.relu(bn)
def bn_relu_conv(input_layer, filter_shape, strides, bn_param, device):
    bn = batch_norm(input_layer, bn_param, device=device)
    relu = tf.nn.relu(bn)
    conv = convolution_layer(relu, shape=filter_shape, strides=strides, bias=False, layer_name='conv', device=device)
    return conv, relu
Пример #7
0
def run():
    model_name = 'alexnet'
    directory_caffe = './caffemodel'
    directory_theano = './theanomodel'
    url_prototxt = 'https://raw.githubusercontent.com/BVLC/caffe/master/models/bvlc_alexnet/deploy.prototxt'
    url_caffemodel = 'http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel'
    filename_prototxt = '%s/%s.prototxt' % (directory_caffe, model_name)
    filename_caffemodel = '%s/%s.caffemodel' % (directory_caffe, model_name)
    filename_theanomodel = '%s/%s.model' % (directory_theano, model_name)

    # download caffemodel
    print 'downloading caffemodel'
    if not os.path.exists(directory_caffe):
        os.mkdir(directory_caffe)
    if not os.path.exists(filename_prototxt):
        p = subprocess.Popen(('wget', url_prototxt, '-O', filename_prototxt))
        p.wait()
    if not os.path.exists(filename_caffemodel):
        p = subprocess.Popen((
            'wget',
            url_caffemodel,
            '-O',
            filename_caffemodel,
        ))
        p.wait()

    # load caffe model
    print 'loading caffe model'
    model_caffe = caffe.Net(filename_prototxt, filename_caffemodel, True)
    conv1_W = theano.shared(model_caffe.params['conv1'][0].data[:, :, ::-1, ::-1])
    conv2_W = theano.shared(model_caffe.params['conv2'][0].data[:, :, ::-1, ::-1])
    conv3_W = theano.shared(model_caffe.params['conv3'][0].data[:, :, ::-1, ::-1])
    conv4_W = theano.shared(model_caffe.params['conv4'][0].data[:, :, ::-1, ::-1])
    conv5_W = theano.shared(model_caffe.params['conv5'][0].data[:, :, ::-1, ::-1])
    conv1_b = theano.shared(model_caffe.params['conv1'][1].data.squeeze())
    conv2_b = theano.shared(model_caffe.params['conv2'][1].data.squeeze())
    conv3_b = theano.shared(model_caffe.params['conv3'][1].data.squeeze())
    conv4_b = theano.shared(model_caffe.params['conv4'][1].data.squeeze())
    conv5_b = theano.shared(model_caffe.params['conv5'][1].data.squeeze())
    fc6_W = theano.shared(model_caffe.params['fc6'][0].data.squeeze())
    fc7_W = theano.shared(model_caffe.params['fc7'][0].data.squeeze())
    fc8_W = theano.shared(model_caffe.params['fc8'][0].data.squeeze())
    fc6_b = theano.shared(model_caffe.params['fc6'][1].data.squeeze())
    fc7_b = theano.shared(model_caffe.params['fc7'][1].data.squeeze())
    fc8_b = theano.shared(model_caffe.params['fc8'][1].data.squeeze())

    # make theano model
    print 'building theano model'
    model_theano = collections.OrderedDict()
    model_theano['data'] = T.tensor4()
    model_theano['conv1'] = layers.convolution_layer(model_theano['data'], conv1_W, conv1_b, subsample=(4, 4))
    model_theano['relu1'] = layers.relu_layer(model_theano['conv1'])
    model_theano['norm1'] = layers.lrn_layer(model_theano['relu1'])
    model_theano['pool1'] = layers.pooling_layer(model_theano['norm1'])
    model_theano['conv2'] = layers.convolution_layer(model_theano['pool1'], conv2_W, conv2_b, border='same', group=2)
    model_theano['relu2'] = layers.relu_layer(model_theano['conv2'])
    model_theano['norm2'] = layers.lrn_layer(model_theano['relu2'])
    model_theano['pool2'] = layers.pooling_layer(model_theano['norm2'])
    model_theano['conv3'] = layers.convolution_layer(model_theano['pool2'], conv3_W, conv3_b, border='same')
    model_theano['relu3'] = layers.relu_layer(model_theano['conv3'])
    model_theano['conv4'] = layers.convolution_layer(model_theano['relu3'], conv4_W, conv4_b, border='same', group=2)
    model_theano['relu4'] = layers.relu_layer(model_theano['conv4'])
    model_theano['conv5'] = layers.convolution_layer(model_theano['relu4'], conv5_W, conv5_b, border='same', group=2)
    model_theano['relu5'] = layers.relu_layer(model_theano['conv5'])
    model_theano['pool5'] = layers.pooling_layer(model_theano['relu5'])
    model_theano['fc6'] = layers.inner_product_layer(model_theano['pool5'], fc6_W, fc6_b)
    model_theano['relu6'] = layers.relu_layer(model_theano['fc6'])
    model_theano['fc7'] = layers.inner_product_layer(model_theano['relu6'], fc7_W, fc7_b)
    model_theano['relu7'] = layers.relu_layer(model_theano['fc7'])
    model_theano['fc8'] = layers.inner_product_layer(model_theano['relu7'], fc8_W, fc8_b)
    model_theano['prob'] = layers.softmax_layer(model_theano['fc8'])

    # check
    print 'checking model'
    data = np.random.randn(*model_caffe.blobs['data'].data.shape)
    data = data.astype(np.float32) * 10
    model_caffe.blobs['data'].data[:] = data
    model_caffe.forward()
    theano_output = theano.function(
        [model_theano['data']],
        model_theano['prob'],
    )(data)
    error = (
        theano_output.squeeze() -
        model_caffe.blobs['prob'].data.squeeze()
    ).max()
    assert error < 1e-6

    # save
    print 'saving'
    if not os.path.exists(directory_theano):
        os.mkdir(directory_theano)
    sys.setrecursionlimit(100000)
    pickle.dump(
        model_theano, 
        open(filename_theanomodel, 'wb'),        
        protocol=pickle.HIGHEST_PROTOCOL,
    )
    print 'done'
Пример #8
0
def run():
    model_name = 'alexnet'
    directory_caffe = './caffemodel'
    directory_theano = './theanomodel'
    url_prototxt = 'https://raw.githubusercontent.com/BVLC/caffe/master/models/bvlc_alexnet/deploy.prototxt'
    url_caffemodel = 'http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel'
    filename_prototxt = '%s/%s.prototxt' % (directory_caffe, model_name)
    filename_caffemodel = '%s/%s.caffemodel' % (directory_caffe, model_name)
    filename_theanomodel = '%s/%s.model' % (directory_theano, model_name)

    # download caffemodel
    print 'downloading caffemodel'
    if not os.path.exists(directory_caffe):
        os.mkdir(directory_caffe)
    if not os.path.exists(filename_prototxt):
        p = subprocess.Popen(('wget', url_prototxt, '-O', filename_prototxt))
        p.wait()
    if not os.path.exists(filename_caffemodel):
        p = subprocess.Popen((
            'wget',
            url_caffemodel,
            '-O',
            filename_caffemodel,
        ))
        p.wait()

    # load caffe model
    print 'loading caffe model'
    model_caffe = caffe.Net(filename_prototxt, filename_caffemodel, True)
    conv1_W = theano.shared(
        model_caffe.params['conv1'][0].data[:, :, ::-1, ::-1])
    conv2_W = theano.shared(
        model_caffe.params['conv2'][0].data[:, :, ::-1, ::-1])
    conv3_W = theano.shared(
        model_caffe.params['conv3'][0].data[:, :, ::-1, ::-1])
    conv4_W = theano.shared(
        model_caffe.params['conv4'][0].data[:, :, ::-1, ::-1])
    conv5_W = theano.shared(
        model_caffe.params['conv5'][0].data[:, :, ::-1, ::-1])
    conv1_b = theano.shared(model_caffe.params['conv1'][1].data.squeeze())
    conv2_b = theano.shared(model_caffe.params['conv2'][1].data.squeeze())
    conv3_b = theano.shared(model_caffe.params['conv3'][1].data.squeeze())
    conv4_b = theano.shared(model_caffe.params['conv4'][1].data.squeeze())
    conv5_b = theano.shared(model_caffe.params['conv5'][1].data.squeeze())
    fc6_W = theano.shared(model_caffe.params['fc6'][0].data.squeeze())
    fc7_W = theano.shared(model_caffe.params['fc7'][0].data.squeeze())
    fc8_W = theano.shared(model_caffe.params['fc8'][0].data.squeeze())
    fc6_b = theano.shared(model_caffe.params['fc6'][1].data.squeeze())
    fc7_b = theano.shared(model_caffe.params['fc7'][1].data.squeeze())
    fc8_b = theano.shared(model_caffe.params['fc8'][1].data.squeeze())

    # make theano model
    print 'building theano model'
    model_theano = collections.OrderedDict()
    model_theano['data'] = T.tensor4()
    model_theano['conv1'] = layers.convolution_layer(model_theano['data'],
                                                     conv1_W,
                                                     conv1_b,
                                                     subsample=(4, 4))
    model_theano['relu1'] = layers.relu_layer(model_theano['conv1'])
    model_theano['norm1'] = layers.lrn_layer(model_theano['relu1'])
    model_theano['pool1'] = layers.pooling_layer(model_theano['norm1'])
    model_theano['conv2'] = layers.convolution_layer(model_theano['pool1'],
                                                     conv2_W,
                                                     conv2_b,
                                                     border='same',
                                                     group=2)
    model_theano['relu2'] = layers.relu_layer(model_theano['conv2'])
    model_theano['norm2'] = layers.lrn_layer(model_theano['relu2'])
    model_theano['pool2'] = layers.pooling_layer(model_theano['norm2'])
    model_theano['conv3'] = layers.convolution_layer(model_theano['pool2'],
                                                     conv3_W,
                                                     conv3_b,
                                                     border='same')
    model_theano['relu3'] = layers.relu_layer(model_theano['conv3'])
    model_theano['conv4'] = layers.convolution_layer(model_theano['relu3'],
                                                     conv4_W,
                                                     conv4_b,
                                                     border='same',
                                                     group=2)
    model_theano['relu4'] = layers.relu_layer(model_theano['conv4'])
    model_theano['conv5'] = layers.convolution_layer(model_theano['relu4'],
                                                     conv5_W,
                                                     conv5_b,
                                                     border='same',
                                                     group=2)
    model_theano['relu5'] = layers.relu_layer(model_theano['conv5'])
    model_theano['pool5'] = layers.pooling_layer(model_theano['relu5'])
    model_theano['fc6'] = layers.inner_product_layer(model_theano['pool5'],
                                                     fc6_W, fc6_b)
    model_theano['relu6'] = layers.relu_layer(model_theano['fc6'])
    model_theano['fc7'] = layers.inner_product_layer(model_theano['relu6'],
                                                     fc7_W, fc7_b)
    model_theano['relu7'] = layers.relu_layer(model_theano['fc7'])
    model_theano['fc8'] = layers.inner_product_layer(model_theano['relu7'],
                                                     fc8_W, fc8_b)
    model_theano['prob'] = layers.softmax_layer(model_theano['fc8'])

    # check
    print 'checking model'
    data = np.random.randn(*model_caffe.blobs['data'].data.shape)
    data = data.astype(np.float32) * 10
    model_caffe.blobs['data'].data[:] = data
    model_caffe.forward()
    theano_output = theano.function(
        [model_theano['data']],
        model_theano['prob'],
    )(data)
    error = (theano_output.squeeze() -
             model_caffe.blobs['prob'].data.squeeze()).max()
    assert error < 1e-6

    # save
    print 'saving'
    if not os.path.exists(directory_theano):
        os.mkdir(directory_theano)
    sys.setrecursionlimit(100000)
    pickle.dump(
        model_theano,
        open(filename_theanomodel, 'wb'),
        protocol=pickle.HIGHEST_PROTOCOL,
    )
    print 'done'
Пример #9
0
def run():
    model_name = "vggnet"
    directory_caffe = "./caffemodel"
    directory_theano = "./theanomodel"
    url_prototxt = "https://gist.githubusercontent.com/ksimonyan/3785162f95cd2d5fee77/raw/f02f8769e64494bcd3d7e97d5d747ac275825721/VGG_ILSVRC_19_layers_deploy.prototxt"
    url_caffemodel = "http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_19_layers.caffemodel"
    filename_prototxt = "%s/%s.prototxt" % (directory_caffe, model_name)
    filename_caffemodel = "%s/%s.caffemodel" % (directory_caffe, model_name)
    filename_theanomodel = "%s/%s.model" % (directory_theano, model_name)

    # download caffemodel
    print "downloading caffemodel"
    if not os.path.exists(directory_caffe):
        os.mkdir(directory_caffe)
    if not os.path.exists(filename_prototxt):
        p = subprocess.Popen(("wget", url_prototxt, "-O", filename_prototxt))
        p.wait()
    if not os.path.exists(filename_caffemodel):
        p = subprocess.Popen(("wget", url_caffemodel, "-O", filename_caffemodel))
        p.wait()

    # load caffe model
    model_caffe = caffe.Net(filename_prototxt, filename_caffemodel, True)
    conv1_1_W = theano.shared(model_caffe.params["conv1_1"][0].data[:, :, ::-1, ::-1])
    conv1_2_W = theano.shared(model_caffe.params["conv1_2"][0].data[:, :, ::-1, ::-1])
    conv2_1_W = theano.shared(model_caffe.params["conv2_1"][0].data[:, :, ::-1, ::-1])
    conv2_2_W = theano.shared(model_caffe.params["conv2_2"][0].data[:, :, ::-1, ::-1])
    conv3_1_W = theano.shared(model_caffe.params["conv3_1"][0].data[:, :, ::-1, ::-1])
    conv3_2_W = theano.shared(model_caffe.params["conv3_2"][0].data[:, :, ::-1, ::-1])
    conv3_3_W = theano.shared(model_caffe.params["conv3_3"][0].data[:, :, ::-1, ::-1])
    conv3_4_W = theano.shared(model_caffe.params["conv3_4"][0].data[:, :, ::-1, ::-1])
    conv4_1_W = theano.shared(model_caffe.params["conv4_1"][0].data[:, :, ::-1, ::-1])
    conv4_2_W = theano.shared(model_caffe.params["conv4_2"][0].data[:, :, ::-1, ::-1])
    conv4_3_W = theano.shared(model_caffe.params["conv4_3"][0].data[:, :, ::-1, ::-1])
    conv4_4_W = theano.shared(model_caffe.params["conv4_4"][0].data[:, :, ::-1, ::-1])
    conv5_1_W = theano.shared(model_caffe.params["conv5_1"][0].data[:, :, ::-1, ::-1])
    conv5_2_W = theano.shared(model_caffe.params["conv5_2"][0].data[:, :, ::-1, ::-1])
    conv5_3_W = theano.shared(model_caffe.params["conv5_3"][0].data[:, :, ::-1, ::-1])
    conv5_4_W = theano.shared(model_caffe.params["conv5_4"][0].data[:, :, ::-1, ::-1])
    conv1_1_b = theano.shared(model_caffe.params["conv1_1"][1].data.squeeze())
    conv1_2_b = theano.shared(model_caffe.params["conv1_2"][1].data.squeeze())
    conv2_1_b = theano.shared(model_caffe.params["conv2_1"][1].data.squeeze())
    conv2_2_b = theano.shared(model_caffe.params["conv2_2"][1].data.squeeze())
    conv3_1_b = theano.shared(model_caffe.params["conv3_1"][1].data.squeeze())
    conv3_2_b = theano.shared(model_caffe.params["conv3_2"][1].data.squeeze())
    conv3_3_b = theano.shared(model_caffe.params["conv3_3"][1].data.squeeze())
    conv3_4_b = theano.shared(model_caffe.params["conv3_4"][1].data.squeeze())
    conv4_1_b = theano.shared(model_caffe.params["conv4_1"][1].data.squeeze())
    conv4_2_b = theano.shared(model_caffe.params["conv4_2"][1].data.squeeze())
    conv4_3_b = theano.shared(model_caffe.params["conv4_3"][1].data.squeeze())
    conv4_4_b = theano.shared(model_caffe.params["conv4_4"][1].data.squeeze())
    conv5_1_b = theano.shared(model_caffe.params["conv5_1"][1].data.squeeze())
    conv5_2_b = theano.shared(model_caffe.params["conv5_2"][1].data.squeeze())
    conv5_3_b = theano.shared(model_caffe.params["conv5_3"][1].data.squeeze())
    conv5_4_b = theano.shared(model_caffe.params["conv5_4"][1].data.squeeze())
    fc6_W = theano.shared(model_caffe.params["fc6"][0].data.squeeze())
    fc7_W = theano.shared(model_caffe.params["fc7"][0].data.squeeze())
    fc8_W = theano.shared(model_caffe.params["fc8"][0].data.squeeze())
    fc6_b = theano.shared(model_caffe.params["fc6"][1].data.squeeze())
    fc7_b = theano.shared(model_caffe.params["fc7"][1].data.squeeze())
    fc8_b = theano.shared(model_caffe.params["fc8"][1].data.squeeze())

    # make theano model
    model_theano = collections.OrderedDict()
    model_theano["data"] = T.tensor4()

    model_theano["conv1_1"] = layers.convolution_layer(model_theano["data"], conv1_1_W, conv1_1_b, border="same")
    model_theano["relu1_1"] = layers.relu_layer(model_theano["conv1_1"])
    model_theano["conv1_2"] = layers.convolution_layer(model_theano["relu1_1"], conv1_2_W, conv1_2_b, border="same")
    model_theano["relu1_2"] = layers.relu_layer(model_theano["conv1_2"])
    model_theano["pool1"] = layers.pooling_layer(model_theano["relu1_2"], size=(2, 2), stride=(2, 2))

    model_theano["conv2_1"] = layers.convolution_layer(model_theano["pool1"], conv2_1_W, conv2_1_b, border="same")
    model_theano["relu2_1"] = layers.relu_layer(model_theano["conv2_1"])
    model_theano["conv2_2"] = layers.convolution_layer(model_theano["relu2_1"], conv2_2_W, conv2_2_b, border="same")
    model_theano["relu2_2"] = layers.relu_layer(model_theano["conv2_2"])
    model_theano["pool2"] = layers.pooling_layer(model_theano["relu2_2"], size=(2, 2), stride=(2, 2))

    model_theano["conv3_1"] = layers.convolution_layer(model_theano["pool2"], conv3_1_W, conv3_1_b, border="same")
    model_theano["relu3_1"] = layers.relu_layer(model_theano["conv3_1"])
    model_theano["conv3_2"] = layers.convolution_layer(model_theano["relu3_1"], conv3_2_W, conv3_2_b, border="same")
    model_theano["relu3_2"] = layers.relu_layer(model_theano["conv3_2"])
    model_theano["conv3_3"] = layers.convolution_layer(model_theano["relu3_2"], conv3_3_W, conv3_3_b, border="same")
    model_theano["relu3_3"] = layers.relu_layer(model_theano["conv3_3"])
    model_theano["conv3_4"] = layers.convolution_layer(model_theano["relu3_3"], conv3_4_W, conv3_4_b, border="same")
    model_theano["relu3_4"] = layers.relu_layer(model_theano["conv3_4"])
    model_theano["pool3"] = layers.pooling_layer(model_theano["relu3_4"], size=(2, 2), stride=(2, 2))

    model_theano["conv4_1"] = layers.convolution_layer(model_theano["pool3"], conv4_1_W, conv4_1_b, border="same")
    model_theano["relu4_1"] = layers.relu_layer(model_theano["conv4_1"])
    model_theano["conv4_2"] = layers.convolution_layer(model_theano["relu4_1"], conv4_2_W, conv4_2_b, border="same")
    model_theano["relu4_2"] = layers.relu_layer(model_theano["conv4_2"])
    model_theano["conv4_3"] = layers.convolution_layer(model_theano["relu4_2"], conv4_3_W, conv4_3_b, border="same")
    model_theano["relu4_3"] = layers.relu_layer(model_theano["conv4_3"])
    model_theano["conv4_4"] = layers.convolution_layer(model_theano["relu4_3"], conv4_4_W, conv4_4_b, border="same")
    model_theano["relu4_4"] = layers.relu_layer(model_theano["conv4_4"])
    model_theano["pool4"] = layers.pooling_layer(model_theano["relu4_4"], size=(2, 2), stride=(2, 2))

    model_theano["conv5_1"] = layers.convolution_layer(model_theano["pool4"], conv5_1_W, conv5_1_b, border="same")
    model_theano["relu5_1"] = layers.relu_layer(model_theano["conv5_1"])
    model_theano["conv5_2"] = layers.convolution_layer(model_theano["relu5_1"], conv5_2_W, conv5_2_b, border="same")
    model_theano["relu5_2"] = layers.relu_layer(model_theano["conv5_2"])
    model_theano["conv5_3"] = layers.convolution_layer(model_theano["relu5_2"], conv5_3_W, conv5_3_b, border="same")
    model_theano["relu5_3"] = layers.relu_layer(model_theano["conv5_3"])
    model_theano["conv5_4"] = layers.convolution_layer(model_theano["relu5_3"], conv5_4_W, conv5_4_b, border="same")
    model_theano["relu5_4"] = layers.relu_layer(model_theano["conv5_4"])
    model_theano["pool5"] = layers.pooling_layer(model_theano["relu5_4"], size=(2, 2), stride=(2, 2))

    model_theano["fc6"] = layers.inner_product_layer(model_theano["pool5"], fc6_W, fc6_b)
    model_theano["relu6"] = layers.relu_layer(model_theano["fc6"])
    model_theano["fc7"] = layers.inner_product_layer(model_theano["relu6"], fc7_W, fc7_b)
    model_theano["relu7"] = layers.relu_layer(model_theano["fc7"])
    model_theano["fc8"] = layers.inner_product_layer(model_theano["relu7"], fc8_W, fc8_b)
    model_theano["prob"] = layers.softmax_layer(model_theano["fc8"])

    # check
    data = np.random.randn(*model_caffe.blobs["data"].data.shape).astype(np.float32) * 10
    model_caffe.blobs["data"].data[:] = data
    model_caffe.forward()
    theano_output = theano.function([model_theano["data"]], model_theano["prob"])(data)
    error = (theano_output.squeeze() - model_caffe.blobs["prob"].data.squeeze()).max()
    assert error < 1e-6

    # save
    print "saving"
    if not os.path.exists(directory_theano):
        os.mkdir(directory_theano)
    sys.setrecursionlimit(100000)
    pickle.dump(model_theano, open(filename_theanomodel, "wb"), protocol=pickle.HIGHEST_PROTOCOL)
    print "done"