尝试可视化训练的CNN过滤器时Keras2.0 MissingInputError

时间:2017-04-14 12:13:22

标签: python-2.7 keras conv-neural-network

我正在尝试按照Keras博客https://blog.keras.io/how-convolutional-neural-networks-see-the-world.html在Keras上对可训练的卷积神经网络的过滤器进行可视化。

import keras
from keras.layers import Input, Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
from keras.models import Model
from keras import backend as K

num_classes = 10
input_shape = (32, 32, 1) # 32x32 image, 1 channel

# model
inputs = Input(shape=input_shape)
x = Conv2D(32, (3, 3), activation='relu', name='block1_conv1')(inputs)
x = Conv2D(32, (3, 3), activation='relu', name='block1_conv2')(x)
x = Conv2D(32, (3, 3), activation='relu', name='block1_conv3')(x)
x = Conv2D(32, (3, 3), activation='relu', name='block1_conv4')(x)
x = MaxPooling2D(pool_size=(2, 2), name='block1_pool')(x)
x = Dropout(0.25)(x)
x = Conv2D(64, (3, 3), activation='relu', name='block2_conv1')(x)
x = Conv2D(64, (3, 3), activation='relu', name='block2_conv2')(x)
x = Conv2D(64, (3, 3), activation='relu', name='block2_conv3')(x)
x = Conv2D(64, (3, 3), activation='relu', name='block2_conv4')(x)
x = MaxPooling2D(pool_size=(2, 2), name='block2_pool')(x)
x = Dropout(0.25)(x)
x = Flatten(name='flatten')(x)
x = Dense(512, activation='relu', name='fc1')(x)
x = Dropout(0.5)(x)
x = Dense(num_classes, name='fc2')(x)
predictions = Activation('sigmoid')(x)
model = Model(input=inputs, output=predictions)

# weights are stored in 'best_weights.hdf5'
model.load_weights('best_weights.hdf5')

input_tensor = model.input
layer_dict = dict([(layer.name, layer) for layer in model.layers])
layer_output = layer_dict['fc2'].output

activation = K.mean(layer_output[:, 0])

# compute the gradient of the input picture wrt the activation
grads = K.gradients(activation, input_tensor)[0]

# normalization trick: we normalize the gradient
grads /= (K.sqrt(K.mean(K.square(grads))) + K.epsilon())

# this function returns the activation and grads given the input picture
iterate = K.function([input_tensor], [activation, grads])

但是,我收到错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\keras\backend\theano_backend.py", line 1132, in function
    return Function(inputs, outputs, updates=updates, **kwargs)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\keras\backend\theano_backend.py", line 1118, in __init__
    **kwargs)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\compile\function.py", line 326, in function
    output_keys=output_keys)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\compile\pfunc.py", line 486, in pfunc
    output_keys=output_keys)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\compile\function_module.py", line 1794, in orig_function
    output_keys=output_keys).create(
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\compile\function_module.py", line 1446, in __init__
    accept_inplace)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\compile\function_module.py", line 177, in std_fgraph
    update_mapping=update_mapping)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\gof\fg.py", line 180, in __init__
    self.__import_r__(output, reason="init")
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\gof\fg.py", line 351, in __import_r__
    self.__import__(variable.owner, reason=reason)
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\theano\gof\fg.py", line 397, in __import__
    raise MissingInputError(error_msg, variable=r)
theano.gof.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute if{}(keras_learning_phase, Elemwise{true_div,no_inplace}.0, InplaceDimShuffle{0,2,3,1}.0), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.
Backtrace when that variable is created:

  File "<stdin>", line 1, in <module>
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import activations
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\keras\activations.py", line 3, in <module>
    from . import backend as K
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\keras\backend\__init__.py", line 70, in <module>
    from .theano_backend import *
  File "C:\Users\mouse008\Anaconda3\envs\python27\lib\site-packages\keras\backend\theano_backend.py", line 28, in <module>
    _LEARNING_PHASE = T.scalar(dtype='uint8', name='keras_learning_phase')  # 0 = test, 1 = train

有人能帮帮我吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

通常您需要提供另一个参数,通知inference是否需要以training/learningiterate = K.function([input_tensor, K.learning_phase()], [activation, grads]) 模式运行函数。尝试:

iterate

当您致电0时,如果您希望以inference模式运行您的功能,或者1,则需要提供<?php while($a = $stmt->fetch()){ extract($a); ?> <form method="post" action=""> <input type="hidden" value="<?php echo $mbs_id; ?>" class="memid"> <select class="validity" class="upgrade-valsel"> <?php while($mv = $mval->fetch()){ extract($mv); ?> <option value="<?php echo $mv_id; ?>"><?php echo $mv_validity; if($mv_validity == 1){ echo " month"; }else{ echo " months"; } ?></option> <?php } ?> </select> <input type="submit" value="Upgrade" class="submit"> <div class="center-align" style="margin-left: -20px"><img src="images/loading.gif" width="auto" id="loading-rent" class="loading-rent" style="margin-right: 0px; height: 40px"></div> </form> <?php } ?>