python 2.7 - Keras2.0 MissingInputError while trying to visualize the trained CNN filters -
i'm trying visualize filters of trained convolutional neural network on keras following keras blog https://blog.keras.io/how-convolutional-neural-networks-see-the-world.html.
import keras keras.layers import input, dense, dropout, flatten, activation keras.layers import conv2d, maxpooling2d keras.models import model keras import backend 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 stored in 'best_weights.hdf5' model.load_weights('best_weights.hdf5') input_tensor = model.input layer_dict = dict([(layer.name, layer) layer in model.layers]) layer_output = layer_dict['fc2'].output activation = k.mean(layer_output[:, 0]) # compute gradient of input picture wrt activation grads = k.gradients(activation, input_tensor)[0] # normalization trick: normalize gradient grads /= (k.sqrt(k.mean(k.square(grads))) + k.epsilon()) # function returns activation , grads given input picture iterate = k.function([input_tensor], [activation, grads])
however, received error:
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 graph (indices start 0), used compute if{}(keras_learning_phase, elemwise{true_div,no_inplace}.0, inplacedimshuffle{0,2,3,1}.0), not provided , not given value. use theano flag exception_verbosity='high', more information on error. backtrace when variable created: file "<stdin>", line 1, in <module> file "c:\users\mouse008\anaconda3\envs\python27\lib\site-packages\keras\__init__.py", line 3, in <module> . import activations file "c:\users\mouse008\anaconda3\envs\python27\lib\site-packages\keras\activations.py", line 3, in <module> . import backend k file "c:\users\mouse008\anaconda3\envs\python27\lib\site-packages\keras\backend\__init__.py", line 70, in <module> .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
could me? thank you.
usually need provide yet argument informs keras
if needs run function in inference
or training/learning
mode. try:
iterate = k.function([input_tensor, k.learning_phase()], [activation, grads])
and when call iterate
need provide 0
if want run function in inference
mode or 1
otherwise.
Comments
Post a Comment