Lasagne / Recipes

Lasagne recipes: examples, IPython notebooks, ...
MIT License
914 stars 418 forks source link

error when set values for vgg-19 #104

Closed iElric closed 7 years ago

iElric commented 7 years ago

import numpy as np import matplotlib.pyplot as plt import lasagne from lasagne.layers import InputLayer from lasagne.layers import DenseLayer from lasagne.layers import NonlinearityLayer from lasagne.layers import DropoutLayer from lasagne.layers import Pool2DLayer as PoolLayer from lasagne.layers import Conv2DLayer as ConvLayer from lasagne.nonlinearities import softmax from lasagne.utils import floatX

def build_model(): net = {} net['input'] = InputLayer((None, 3, 224, 224)) net['conv1_1'] = ConvLayer( net['input'], 64, 3, pad=1, flip_filters=False) net['conv1_2'] = ConvLayer( net['conv1_1'], 64, 3, pad=1, flip_filters=False) net['pool1'] = PoolLayer(net['conv1_2'], 2) net['conv2_1'] = ConvLayer( net['pool1'], 128, 3, pad=1, flip_filters=False) net['conv2_2'] = ConvLayer( net['conv2_1'], 128, 3, pad=1, flip_filters=False) net['pool2'] = PoolLayer(net['conv2_2'], 2) net['conv3_1'] = ConvLayer( net['pool2'], 256, 3, pad=1, flip_filters=False) net['conv3_2'] = ConvLayer( net['conv3_1'], 256, 3, pad=1, flip_filters=False) net['conv3_3'] = ConvLayer( net['conv3_2'], 256, 3, pad=1, flip_filters=False) net['conv3_4'] = ConvLayer( net['conv3_3'], 256, 3, pad=1, flip_filters=False) net['pool3'] = PoolLayer(net['conv3_4'], 2) net['conv4_1'] = ConvLayer( net['pool3'], 512, 3, pad=1, flip_filters=False) net['conv4_2'] = ConvLayer( net['conv4_1'], 512, 3, pad=1, flip_filters=False) net['conv4_3'] = ConvLayer( net['conv4_2'], 512, 3, pad=1, flip_filters=False) net['conv4_4'] = ConvLayer( net['conv4_3'], 512, 3, pad=1, flip_filters=False) net['pool4'] = PoolLayer(net['conv4_4'], 2) net['conv5_1'] = ConvLayer( net['pool4'], 512, 3, pad=1, flip_filters=False) net['conv5_2'] = ConvLayer( net['conv5_1'], 512, 3, pad=1, flip_filters=False) net['conv5_3'] = ConvLayer( net['conv5_2'], 512, 3, pad=1, flip_filters=False) net['conv5_4'] = ConvLayer( net['conv5_3'], 512, 3, pad=1, flip_filters=False) net['pool5'] = PoolLayer(net['conv5_4'], 2) net['fc6'] = DenseLayer(net['pool5'], num_units=4096) 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'] = DenseLayer( net['fc7_dropout'], num_units=1000, nonlinearity=None) net['prob'] = NonlinearityLayer(net['fc8'], softmax) return net

output_layer = build_model()

Load the model parameter

import pickle

model = pickle.load(open('vgg19.pkl', 'rb'), encoding='latin-1') CLASSES = model['synset words'] MEAN_VALUE = model['mean value'] lasagne.layers.set_all_param_values(output_layer, model['param values'])

above is my code. I want to set parameters for vgg 19 just as i did to vgg-s. However i get this error:

wangyexindeMacBook-Pro:pythondocument wangyexin$ python3 vgg_cnn_19.py Traceback (most recent call last): File "vgg_cnn_19.py", line 80, in lasagne.layers.set_all_param_values(output_layer, model['param values']) File "/Users/wangyexin/Library/Python/3.6/lib/python/site-packages/lasagne/layers/helper.py", line 506, in set_all_param_values params = get_all_params(layer, tags) File "/Users/wangyexin/Library/Python/3.6/lib/python/site-packages/lasagne/layers/helper.py", line 371, in get_all_params return utils.unique(params) File "/Users/wangyexin/Library/Python/3.6/lib/python/site-packages/lasagne/utils.py", line 157, in unique for el in l: File "/Users/wangyexin/Library/Python/3.6/lib/python/site-packages/lasagne/layers/helper.py", line 370, in unwrap_shared=unwrap_shared, tags) for l in layers) AttributeError: 'str' object has no attribute 'get_params'

Can anybody pleas help me out? Also, since there is no mean image in vgg-19, just mean value instead, i want to know how to preprocess the image.

f0k commented 7 years ago

You set output_layer = build_model(), so your output layer is not a Lasagne layer as expected by set_all_param_values(), but a dictionary. Lasagne interprets this as a list of output layers and iterates over it, which iterates over the dictionary keys. Hence you get the error that a str object has no attribute get_params. You should pass net['prob'] to set_all_param_values() instead.

iElric commented 7 years ago

Thanks for you detailed explanation. I understand now and solved the problem.