将预先培养的咖啡模型加载到千层面?

时间:2016-03-23 18:09:37

标签: python machine-learning caffe lasagne

我正在尝试重现Long-term Recurrent Convolutional Networks论文。

我有一个预训练的caffe模型,我想在theano中使用它。 我有.caffemodel这个文件和prototxt。 我使用lasagne example将caffe权重加载到caffe模型。 这是code I used,但数据未加载到千层面模型。 我使用lasagne.layers.get_all_param_values(net)命令检查它,这会抛出此错误。

Traceback (most recent call last):
  File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 2411, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 1802, in run
    launch(file, globals, locals)  # execute the script
  File "/media/anilil/Data/charm/mv_clean/Vgg_las.py", line 218, in <module>
    x=lasagne.layers.get_all_param_values(net)
  File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 439, in get_all_param_values
    params = get_all_params(layer, **tags)
  File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 353, in get_all_params
    return utils.unique(params)
  File "/usr/local/lib/python2.7/dist-packages/lasagne/utils.py", line 157, in unique
    for el in l:
  File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 352, in <genexpr>
    params = chain.from_iterable(l.get_params(**tags) for l in layers)
AttributeError: 'str' object has no attribute 'get_params'

试用/测试代码: -

# -*- coding: utf-8 -*-
import os
import sys
import lasagne
from lasagne.layers import InputLayer
from lasagne.layers import DenseLayer
from lasagne.layers import NonlinearityLayer
from lasagne.nonlinearities import rectify
from lasagne.layers import DropoutLayer
from lasagne.layers import Pool2DLayer as PoolLayer
from lasagne.layers.dnn import Conv2DDNNLayer as ConvLayer
from lasagne.nonlinearities import softmax
import theano as T
from lasagne.layers import LocalResponseNormalization2DLayer as LRN
sys.path.append('/home/anilil/projects/lstm/lisa-caffe-public/python/')
import caffe
from lasagne.utils import floatX
import numpy as np

def build_model():
    net = {}
    # Input layer
    net['input'] = InputLayer((None, 3, 227, 227))
    # First Conv Layer
    net['conv1'] = ConvLayer(net['input'], num_filters=96,filter_size=7, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
    net['pool1'] = PoolLayer(net['conv1'], pool_size=3,stride=2,mode='max')
    net['norm1'] = LRN(net['pool1'],alpha=0.0001,beta=0.75,n=5)
    # 2nd Conv Layer
    net['conv2'] = ConvLayer(net['norm1'], num_filters=384,filter_size=5, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
    net['pool2'] = PoolLayer(net['conv2'], pool_size=3,stride=2,mode='max')
    net['norm2'] = LRN(net['pool2'],alpha=0.0001,beta=0.75,n=5)
    # 3rd Conv Layer
    net['conv3'] = ConvLayer(net['norm2'], num_filters=512,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
    net['conv4'] = ConvLayer(net['conv3'], num_filters=512,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
    net['conv5'] = ConvLayer(net['conv4'], num_filters=384,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
    net['pool5'] = PoolLayer(net['conv5'], pool_size=3,stride=2,mode='max')
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096,nonlinearity=rectify)
    net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=4096)
    net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8-ucf'] = DenseLayer(net['fc7_dropout'], num_units=101, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8-ucf'], softmax)

    return net

if __name__=="__main__":
    net = build_model()
    #net= load_caffe_weights(net,'/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/deploy_singleFrame.prototxt','/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/singleframe_flow/snaps/snapshots_singleFrame_flow_v2_iter_50000.caffemodel')
    caffe.set_device(0)
    caffe.set_mode_gpu()
    net_caffe = caffe.Net('/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/deploy_singleFrame.prototxt', '/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/singleframe_flow/snaps/snapshots_singleFrame_flow_v2_iter_50000.caffemodel', caffe.TEST)
    layers_caffe = dict(zip(list(net_caffe._layer_names), net_caffe.layers))

    for name, layer in net.items():
        try:
            layer.W.set_value(layers_caffe[name].blobs[0].data,borrow=True)
            layer.b.set_value(layers_caffe[name].blobs[1].data,borrow=True)
        except AttributeError:
            continue

    print ("Loaded the files without issues !!!!!!!!!!")
    x=lasagne.layers.get_all_param_values(net)
    print ("Saved Weights to the file without issues !!!!!!!!!!")

1 个答案:

答案 0 :(得分:0)

尝试:

  

X = lasagne.layers.get_all_param_values(净[ '概率'])

或以这种方式制作您的网:

def build_model():
    net = {}
    # Input layer
    net = InputLayer((None, 3, 227, 227))
    # First Conv Layer
    net = ConvLayer(net, num_filters=96,filter_size=7, pad=0,     flip_filters=False,stride=2,nonlinearity=rectify)
    net = PoolLayer(net, pool_size=3,stride=2,mode='max')
    ....
    net= NonlinearityLayer(net, softmax)
    return net