1
Kerasのブログhttps://blog.keras.io/how-convolutional-neural-networks-see-the-world.htmlに続いて、訓練された畳み込みニューラルネットワークのフィルタをKerasで視覚化しようとしています。Keras2.0訓練されたCNNフィルタを視覚化しようとしているときにMissingInputError
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
誰も私を助けてもらえますか? ありがとうございます。
ありがとうございます。出来た! – hikaru
あなたはなぜ答えを受け入れなかったのですか? –